~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/TEST/GUI/TOPPView_test.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Chris Bielow $
 
25
// $Authors: Chris Bielow $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#include <QtGui>
 
29
#include <QtTest/QtTest>
 
30
 
 
31
#include <QTimer>
 
32
#include <QQueue>
 
33
 
 
34
#include <OpenMS/VISUAL/APPLICATIONS/TOPPViewBase.h>
 
35
#include <OpenMS/SYSTEM/File.h>
 
36
#include <OpenMS/VISUAL/EnhancedTabBar.h>
 
37
 
 
38
 
 
39
namespace OpenMS
 
40
{
 
41
 
 
42
 
 
43
/**
 
44
 
 
45
        @todo write a proper GUI base class for the scheduler below (Chris)
 
46
 
 
47
        @todo document schedule for winXP
 
48
*/
 
49
 
 
50
class TestTOPPView: public QObject
 
51
{
 
52
        Q_OBJECT
 
53
 
 
54
        /**
 
55
                @brief Store information on timed keyboard input events
 
56
                
 
57
                Store the time offset, the key sequence and the expected window title.
 
58
                
 
59
        */
 
60
        struct ScheduleInfo
 
61
        {
 
62
                ScheduleInfo():delay(0){};
 
63
                ScheduleInfo(QString p_keys, QString p_title, int p_delay)
 
64
                        :keys(p_keys),
 
65
                         title(p_title),
 
66
                         delay(p_delay)
 
67
                {}
 
68
                        
 
69
                QString keys; //< key sequence
 
70
                QString title;//< expected window title
 
71
                int delay;    //< delay in ms when event is fired off
 
72
        };
 
73
        
 
74
public slots:
 
75
        /**
 
76
                @brief Slot that tries to process the current event queue for modal dialogs until its empty.
 
77
                
 
78
                Slot that tries to process the current event queue for modal dialogs until its empty.
 
79
                The function will repeatedly invoke itself until the queue is empty, to allow other
 
80
                incoming events (e.g. loading a file) to be processed in between two scheduled dialogs.
 
81
        */
 
82
        void simulateClick_();
 
83
 
 
84
private slots:
 
85
        void testGui();
 
86
        
 
87
        private:
 
88
                /**
 
89
                        @brief Schedule a keyboard input using a QTimer signal to direct input to a modal window.
 
90
                        
 
91
                        Modal windows have their own event queue and once launched will halt the
 
92
                        execution of the test script until closed. This implies one cannot simply direct keyboard
 
93
                        input to them. To do that we pre-schedule the input in the main event loop
 
94
                        using a timer. The keyboard input sequence @p key_sequence and a subsequent 'return' key press
 
95
                        is then issued when the time out occurs, given that the current window has the correct
 
96
                        @p title! Otherwise the event is rescheduled until the title is correct.
 
97
                        The @p delay then the timer pops is relative to the last timed events successfull completion.
 
98
                */
 
99
                void scheduleModalWidget_(const QString& key_sequence, const QString& title, const int delay=100);
 
100
                
 
101
                /**
 
102
                        @brief Waits until the scheduled event queue is emtpy
 
103
                        
 
104
                        Waits until the scheduled event queue is emtpy.
 
105
                */
 
106
                void waitForModalWidget(const int max_wait, const String& line);
 
107
                
 
108
                /// event queue for modal/popup dialogs
 
109
                QQueue<ScheduleInfo> modal_key_sequence_;
 
110
};
 
111
 
 
112
 
 
113
 
 
114
void TestTOPPView::scheduleModalWidget_(const QString& key_sequence, const QString& title, const int delay)
 
115
{
 
116
        modal_key_sequence_.enqueue(ScheduleInfo(key_sequence, title, delay));
 
117
        std::cerr << "scheduled for window " << title.toStdString() << "\n";
 
118
        if (modal_key_sequence_.size()==1) // only schedule if this is the first entry
 
119
        {
 
120
                QTimer::singleShot(delay, this, SLOT(simulateClick_()));
 
121
        }
 
122
}
 
123
 
 
124
void TestTOPPView::waitForModalWidget(const int max_wait, const String& line)
 
125
{
 
126
        // test if accumulated scheduled time is less than max_wait
 
127
        int min_required_time = 0;
 
128
        foreach(ScheduleInfo i, modal_key_sequence_)
 
129
        {
 
130
                min_required_time+=i.delay;
 
131
        }
 
132
        if (min_required_time > max_wait)
 
133
        {
 
134
                QFAIL ( String("Test is bound to fail due to a time restriction in line " + line + ". Please rethink!").c_str());
 
135
        }
 
136
        
 
137
 
 
138
        QTime t;
 
139
        t.start();
 
140
        while (!modal_key_sequence_.isEmpty () && max_wait > t.elapsed())
 
141
        {
 
142
                QTest::qWait(50);
 
143
        }
 
144
        
 
145
        if (!modal_key_sequence_.isEmpty ())
 
146
        {
 
147
                QWARN ( String("Modal dialogs timed out in line " + line + ". The following tests will most likely fail.").c_str());
 
148
                modal_key_sequence_.clear();
 
149
        }
 
150
        
 
151
}
 
152
 
 
153
void TestTOPPView::simulateClick_()
 
154
{
 
155
                if (!modal_key_sequence_.isEmpty ())
 
156
                {
 
157
                        ScheduleInfo entry = modal_key_sequence_.head();
 
158
                        std::cerr << "processing entry: '" << entry.keys.toStdString() << "' with dialog title '" << entry.title.toStdString() << "'\n";
 
159
 
 
160
                        // search for a window
 
161
                        QWidget * dialog = QApplication::activeModalWidget();
 
162
                        if (!dialog) dialog = QApplication::activePopupWidget();
 
163
                        if (!dialog) dialog = QApplication::activeWindow();
 
164
                        
 
165
                        if (!dialog || (dialog->windowTitle() != entry.title))
 
166
                        {
 
167
                                std::cerr << "item not found rescheduling...\n";
 
168
                                QTimer::singleShot(100, this, SLOT(simulateClick_()));
 
169
                                return;
 
170
                        }
 
171
                        QTest::keyClicks(0,entry.keys,Qt::NoModifier,20);
 
172
                        QTest::keyClick(0,Qt::Key_Return,Qt::NoModifier,20);
 
173
                        QApplication::processEvents();
 
174
                        
 
175
                        // remove from queue
 
176
                        modal_key_sequence_.dequeue();
 
177
                }
 
178
                
 
179
                if (!modal_key_sequence_.isEmpty ())
 
180
                {       
 
181
                        std::cerr << "Q not empty... rescheduling...\n";
 
182
                        QTimer::singleShot(modal_key_sequence_.head().delay, this, SLOT(simulateClick_()));
 
183
                }
 
184
}
 
185
 
 
186
void TestTOPPView::testGui()
 
187
{
 
188
        TOPPViewBase tv;
 
189
        tv.show();
 
190
        QApplication::processEvents();
 
191
        
 
192
        QTest::qWait(1000);
 
193
 
 
194
#if 1 //def __APPLE__ // MAC OS does not support entering a filename via keyboard in the file-open menu
 
195
                tv.addDataFile(File::getOpenMSDataPath() + "/examples/peakpicker_tutorial_1.mzML", false, false);
 
196
                QCOMPARE(tv.tab_bar_->tabText(tv.tab_bar_->currentIndex()), QString("peakpicker_tutorial_1.mzML"));
 
197
#else
 
198
        scheduleModalWidget_("peakpicker_tutorial_1.mzML", "Open file(s)",1000);                                                                 // Open File dialog
 
199
        scheduleModalWidget_("", "Open data options for peakpicker_tutorial_1.mzML",1000); // layer data options dialog
 
200
        // open file dialog
 
201
        QTest::keyClicks(&tv,"f", Qt::AltModifier);
 
202
        QApplication::processEvents();
 
203
        // before we open the File-Open Dialog, we need to schedule the planned keyboard input
 
204
        // as this dialog is modal and won't return.
 
205
        // launch the modal widget
 
206
        QTest::keyClicks(0,"e");
 
207
        QApplication::processEvents();
 
208
        waitForModalWidget(15000, __LINE__);
 
209
#endif  
 
210
        
 
211
        // compare the name of the opened tab
 
212
        QCOMPARE(tv.tab_bar_->tabText(tv.tab_bar_->currentIndex()), QString("peakpicker_tutorial_1.mzML"));
 
213
 
 
214
}
 
215
 
 
216
} // Namespace OpenMS
 
217
 
 
218
using namespace OpenMS; // this is required to avoid linker errors on Windows
 
219
 
 
220
QTEST_MAIN(TestTOPPView)                 // expands to a simple main() method that runs all the test functions
 
221
#include <../source/TEST/GUI/moc_TOPPView_test.cxx> // for Qt's introspection