Changeset View
Changeset View
Standalone View
Standalone View
src/autotests/TerminalInterfaceTest.cpp
Show First 20 Lines • Show All 93 Lines • ▼ Show 20 Line(s) | 78 | { | |||
---|---|---|---|---|---|
94 | QCOMPARE(foregroundProcessId, -1); | 94 | QCOMPARE(foregroundProcessId, -1); | ||
95 | QString foregroundProcessName = terminal->foregroundProcessName(); | 95 | QString foregroundProcessName = terminal->foregroundProcessName(); | ||
96 | QCOMPARE(foregroundProcessName, QString()); | 96 | QCOMPARE(foregroundProcessName, QString()); | ||
97 | 97 | | |||
98 | // terminalProcessId() is the user's default shell | 98 | // terminalProcessId() is the user's default shell | ||
99 | // FIXME: find a way to verify this | 99 | // FIXME: find a way to verify this | ||
100 | // int terminalProcessId = terminal->terminalProcessId(); | 100 | // int terminalProcessId = terminal->terminalProcessId(); | ||
101 | 101 | | |||
102 | // Sleep is used to allow enough time for these to work | | |||
103 | // In Qt5 we can use QSignalSpy::wait() | | |||
104 | | ||||
105 | // Let's try using QSignalSpy | 102 | // Let's try using QSignalSpy | ||
106 | // http://techbase.kde.org/Development/Tutorials/Unittests | 103 | // http://techbase.kde.org/Development/Tutorials/Unittests | ||
107 | // QSignalSpy is really a QList of QLists, so we take the first | 104 | // QSignalSpy is really a QList of QLists, so we take the first | ||
108 | // list, which corresponds to the arguments for the first signal | 105 | // list, which corresponds to the arguments for the first signal | ||
109 | // we caught. | 106 | // we caught. | ||
110 | 107 | | |||
111 | QSignalSpy stateSpy(_terminalPart, SIGNAL(currentDirectoryChanged(QString))); | 108 | QSignalSpy stateSpy(_terminalPart, SIGNAL(currentDirectoryChanged(QString))); | ||
112 | QVERIFY(stateSpy.isValid()); | 109 | QVERIFY(stateSpy.isValid()); | ||
113 | 110 | | |||
114 | // Now we check to make sure we don't have any signals already | 111 | // Now we check to make sure we don't have any signals already | ||
115 | QCOMPARE(stateSpy.count(), 0); | 112 | QCOMPARE(stateSpy.count(), 0); | ||
116 | 113 | | |||
117 | // Let's trigger some signals | 114 | // Let's trigger some signals | ||
118 | 115 | | |||
119 | // #1A - Test signal currentDirectoryChanged(QString) | 116 | // #1A - Test signal currentDirectoryChanged(QString) | ||
120 | currentDirectory = QStringLiteral("/tmp"); | 117 | currentDirectory = QStringLiteral("/tmp"); | ||
121 | terminal->sendInput(QStringLiteral("cd ") + currentDirectory + QLatin1Char('\n')); | 118 | terminal->sendInput(QStringLiteral("cd ") + currentDirectory + QLatin1Char('\n')); | ||
122 | sleep(2000); | 119 | stateSpy.wait(2000); | ||
123 | QCOMPARE(stateSpy.count(), 1); | 120 | QCOMPARE(stateSpy.count(), 1); | ||
124 | 121 | | |||
125 | // Correct result? | 122 | // Correct result? | ||
126 | QList<QVariant> firstSignalArgs = stateSpy.takeFirst(); | 123 | QList<QVariant> firstSignalArgs = stateSpy.takeFirst(); | ||
127 | 124 | | |||
128 | QString firstSignalState = firstSignalArgs.at(0).toString(); | 125 | QString firstSignalState = firstSignalArgs.at(0).toString(); | ||
129 | QCOMPARE(firstSignalState, currentDirectory); | 126 | QCOMPARE(firstSignalState, currentDirectory); | ||
130 | 127 | | |||
131 | const QString currentWorkingDirectory = terminal->currentWorkingDirectory(); | 128 | const QString currentWorkingDirectory = terminal->currentWorkingDirectory(); | ||
132 | QCOMPARE(currentWorkingDirectory, currentDirectory); | 129 | QCOMPARE(currentWorkingDirectory, currentDirectory); | ||
133 | 130 | | |||
134 | // #1B - Test signal currentDirectoryChanged(QString) | 131 | // #1B - Test signal currentDirectoryChanged(QString) | ||
135 | // Invalid directory - no signal should be emitted | 132 | // Invalid directory - no signal should be emitted | ||
136 | terminal->sendInput(QStringLiteral("cd /usrADADFASDF\n")); | 133 | terminal->sendInput(QStringLiteral("cd /usrADADFASDF\n")); | ||
137 | sleep(2000); | 134 | stateSpy.wait(2000); | ||
138 | QCOMPARE(stateSpy.count(), 0); | 135 | QCOMPARE(stateSpy.count(), 0); | ||
139 | 136 | | |||
140 | // Should be no change since the above cd didn't work | 137 | // Should be no change since the above cd didn't work | ||
141 | const QString currentWorkingDirectory2 = terminal->currentWorkingDirectory(); | 138 | const QString currentWorkingDirectory2 = terminal->currentWorkingDirectory(); | ||
142 | QCOMPARE(currentWorkingDirectory2, currentDirectory); | 139 | QCOMPARE(currentWorkingDirectory2, currentDirectory); | ||
143 | 140 | | |||
144 | // Test starting a new program | 141 | // Test starting a new program | ||
145 | QString command = QStringLiteral("top"); | 142 | QString command = QStringLiteral("top"); | ||
146 | terminal->sendInput(command + QLatin1Char('\n')); | 143 | terminal->sendInput(command + QLatin1Char('\n')); | ||
147 | sleep(2000); | 144 | stateSpy.wait(2000); | ||
148 | // FIXME: find a good way to validate process id of 'top' | 145 | // FIXME: find a good way to validate process id of 'top' | ||
149 | foregroundProcessId = terminal->foregroundProcessId(); | 146 | foregroundProcessId = terminal->foregroundProcessId(); | ||
150 | QVERIFY(foregroundProcessId != -1); | 147 | QVERIFY(foregroundProcessId != -1); | ||
151 | foregroundProcessName = terminal->foregroundProcessName(); | 148 | foregroundProcessName = terminal->foregroundProcessName(); | ||
152 | QCOMPARE(foregroundProcessName, command); | 149 | QCOMPARE(foregroundProcessName, command); | ||
153 | 150 | | |||
154 | terminal->sendInput(QStringLiteral("q")); | 151 | terminal->sendInput(QStringLiteral("q")); | ||
155 | sleep(2000); | 152 | stateSpy.wait(2000); | ||
156 | 153 | | |||
157 | // Nothing running in foreground | 154 | // Nothing running in foreground | ||
158 | foregroundProcessId = terminal->foregroundProcessId(); | 155 | foregroundProcessId = terminal->foregroundProcessId(); | ||
159 | QCOMPARE(foregroundProcessId, -1); | 156 | QCOMPARE(foregroundProcessId, -1); | ||
160 | foregroundProcessName = terminal->foregroundProcessName(); | 157 | foregroundProcessName = terminal->foregroundProcessName(); | ||
161 | QCOMPARE(foregroundProcessName, QString()); | 158 | QCOMPARE(foregroundProcessName, QString()); | ||
162 | 159 | | |||
163 | // Test destroyed() | 160 | // Test destroyed() | ||
164 | QSignalSpy destroyedSpy(_terminalPart, SIGNAL(destroyed())); | 161 | QSignalSpy destroyedSpy(_terminalPart, SIGNAL(destroyed())); | ||
165 | QVERIFY(destroyedSpy.isValid()); | 162 | QVERIFY(destroyedSpy.isValid()); | ||
166 | 163 | | |||
167 | // Now we check to make sure we don't have any signals already | 164 | // Now we check to make sure we don't have any signals already | ||
168 | QCOMPARE(destroyedSpy.count(), 0); | 165 | QCOMPARE(destroyedSpy.count(), 0); | ||
169 | 166 | | |||
170 | delete _terminalPart; | 167 | delete _terminalPart; | ||
171 | QCOMPARE(destroyedSpy.count(), 1); | 168 | QCOMPARE(destroyedSpy.count(), 1); | ||
172 | } | 169 | } | ||
173 | 170 | | |||
174 | void TerminalInterfaceTest::sleep(int msecs) | | |||
175 | { | | |||
176 | QEventLoop loop; | | |||
177 | QTimer::singleShot(msecs, &loop, SLOT(quit())); | | |||
178 | loop.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers); | | |||
179 | } | | |||
180 | | ||||
181 | KParts::Part *TerminalInterfaceTest::createPart() | 171 | KParts::Part *TerminalInterfaceTest::createPart() | ||
182 | { | 172 | { | ||
183 | KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("konsolepart")); | 173 | KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("konsolepart")); | ||
184 | if (!service) { // not found | 174 | if (!service) { // not found | ||
185 | return nullptr; | 175 | return nullptr; | ||
186 | } | 176 | } | ||
187 | KPluginFactory *factory = KPluginLoader(service->library()).factory(); | 177 | KPluginFactory *factory = KPluginLoader(service->library()).factory(); | ||
188 | if (factory == nullptr) { // not found | 178 | if (factory == nullptr) { // not found | ||
Show All 9 Lines |