1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 // Qt includes
22 #include <QAction>
23 #include <QApplication>
24 #include <QDebug>
25 #include <QMenu>
26 #include <QMenuBar>
27 #include <QStandardItemModel>
28 #include <QSignalSpy>
29 #include <QTimer>
30 #include <QTreeView>
31 
32 // CTK includes
33 #include <ctkCallback.h>
34 #include <ctkConfig.h>
35 #include "ctkEventTranslatorPlayerWidget.h"
36 #include "ctkSetName.h"
37 
38 // QtTesting includes
39 #include "pqTestUtility.h"
40 
41 // STD includes
42 #include <cstdlib>
43 #include <iostream>
44 
45 namespace
46 {
47 QSignalSpy* Spy1;
48 QSignalSpy* Spy2;
49 QSignalSpy* Spy3;
50 QSignalSpy* Spy4;
51 
52 QSignalSpy* Spy5;
53 QSignalSpy* Spy6;
54 QSignalSpy* Spy7;
55 QSignalSpy* Spy8;
56 QSignalSpy* Spy9;
57 QSignalSpy* Spy10;
58 
59 //-----------------------------------------------------------------------------
checkFinalWidgetState(void * data)60 void checkFinalWidgetState(void* data)
61   {
62   QMenuBar* widget = reinterpret_cast<QMenuBar*>(data);
63 
64   Q_UNUSED(widget);
65   CTKCOMPARE(Spy1->count(), 1);
66   CTKCOMPARE(Spy2->count(), 1);
67   CTKCOMPARE(Spy3->count(), 1);
68   CTKCOMPARE(Spy4->count(), 1);
69   CTKCOMPARE(Spy5->count(), 1);
70   CTKCOMPARE(Spy6->count(), 1);
71   CTKCOMPARE(Spy7->count(), 1);
72   CTKCOMPARE(Spy8->count(), 1);
73   CTKCOMPARE(Spy9->count(), 1);
74   CTKCOMPARE(Spy10->count(), 1);
75   }
76 }
77 
78 //-----------------------------------------------------------------------------
ctkMenuEventTranslatorPlayerTest1(int argc,char * argv[])79 int ctkMenuEventTranslatorPlayerTest1(int argc, char * argv [] )
80 {
81   QApplication app(argc, argv);
82 
83   QString xmlDirectory = CTK_SOURCE_DIR "/Libs/Widgets/Testing/Cpp/";
84 
85   // ------------------------
86   ctkEventTranslatorPlayerWidget etpWidget;
87   pqTestUtility* testUtility = new pqTestUtility(&etpWidget);
88   etpWidget.setTestUtility(testUtility);
89 
90   // Test case 1
91   QMenuBar menuBar;
92   QMenu menu("MainMenu");
93   menu.setObjectName("MainMenu");
94   QMenu subMenuFile("subMenuFile");
95   QMenu* menuOpen = new QMenu("Open...");
96   menuOpen->setObjectName("subMenuOpen");
97   QAction* volume = new QAction("&Volume...", menuOpen);
98   QAction* data = new QAction("&TData...", menuOpen);
99   QAction* files = new QAction("&Files...", 0);
100   menuOpen->addAction(volume);
101   menuOpen->addAction(data);
102   menuOpen->addAction(files);
103 
104   QAction* save = new QAction("&Save...", &subMenuFile);
105   QAction* add = new QAction("&Add...", &subMenuFile);
106   subMenuFile.setObjectName("subMenu1");
107   subMenuFile.addMenu(menuOpen);
108   subMenuFile.addAction(save);
109   subMenuFile.addAction(add);
110 
111   QMenu subMenuTxxx("subMenuTxxx");
112   subMenuTxxx.setObjectName("subMenu2");
113   QAction* toto = new QAction("&Toto...", &subMenuTxxx);
114   QAction* titi = new QAction("&Titi...", &subMenuTxxx);
115   QAction* tutu = new QAction("&Tutu...", &subMenuTxxx);
116   subMenuTxxx.addAction(toto);
117   subMenuTxxx.addAction(titi);
118   subMenuTxxx.addAction(tutu);
119 
120   menu.addMenu(&subMenuFile);
121   menu.addMenu(&subMenuTxxx);
122 
123   QAction* foo = new QAction("&Foo...", &menu);
124   QAction* exit = new QAction("&Exit...", &menu);
125   menu.addAction(foo);
126   menu.addAction(exit);
127 
128   menuBar.addMenu(&menu);
129 
130   // Create QSignal Spy
131   QSignalSpy spy1(toto, SIGNAL(triggered()));
132   QSignalSpy spy2(titi, SIGNAL(triggered()));
133   QSignalSpy spy3(tutu, SIGNAL(triggered()));
134   QSignalSpy spy4(volume, SIGNAL(triggered()));
135   QSignalSpy spy5(data, SIGNAL(triggered()));
136   QSignalSpy spy6(files, SIGNAL(triggered()));
137   QSignalSpy spy7(save, SIGNAL(triggered()));
138   QSignalSpy spy8(add, SIGNAL(triggered()));
139   QSignalSpy spy9(foo, SIGNAL(triggered()));
140   QSignalSpy spy10(exit, SIGNAL(triggered()));
141 
142   Spy1 = &spy1;
143   Spy2 = &spy2;
144   Spy3 = &spy3;
145   Spy4 = &spy4;
146   Spy5 = &spy5;
147   Spy6 = &spy6;
148   Spy7 = &spy7;
149   Spy8 = &spy8;
150   Spy9 = &spy9;
151   Spy10 = &spy10;
152 
153   etpWidget.addTestCase(&menuBar,
154                         xmlDirectory + "ctkMenuEventTranslatorPlayerTest1.xml",
155                         &checkFinalWidgetState);
156 
157   // ------------------------
158   if (argc < 2 || QString(argv[1]) != "-I")
159     {
160     QTimer::singleShot(0, &etpWidget, SLOT(play()));
161     }
162 
163   etpWidget.show();
164   return app.exec();
165 }
166 
167