-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathtst_mainwindow.cpp
More file actions
268 lines (231 loc) · 9.52 KB
/
tst_mainwindow.cpp
File metadata and controls
268 lines (231 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* @brief Widget tests for MainWindow.
*
* These tests exercise the public surface of MainWindow in isolation.
* Each test gets a fresh window instance; a single QTemporaryDir serves as
* the pass store for the whole run.
*
* Coverage deferred here (requires live GPG / pass / git operations):
* - addPassword / addFolder / renameFolder / renamePassword — invoke Pass
* - on_treeView_clicked / doubleClicked — need a populated store model
* - onGrepFinished — depends on a running grep
* - generateKeyPair — spawns gpg key generation
*/
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QScopedPointer>
#include <QStatusBar>
#include <QTemporaryDir>
#include <QTextBrowser>
#include <QTextEdit>
#include <QTreeView>
#include <QtTest>
#include "../../../src/mainwindow.h"
#include "../../../src/qtpasssettings.h"
#include "../../../src/util.h"
class tst_mainwindow : public QObject {
Q_OBJECT
QTemporaryDir m_storeDir;
QScopedPointer<MainWindow> m_window;
QString m_gpgPath;
QString m_savedPassStore;
bool m_savedUsePass;
bool m_savedShowProcessOutput;
QString m_savedGpgExecutable;
private Q_SLOTS:
void initTestCase();
void init();
void cleanup();
void cleanupTestCase();
void constructionDoesNotCrash();
void getKeygenDialogInitiallyNull();
void cleanKeygenDialogWithNullIsHarmless();
void setUiElementsEnabledDisablesTreeView();
void setUiElementsEnabledEnablesTreeView();
void flashTextSetsContent();
void flashTextErrorDoesNotCrash();
void flashTextHtmlRenderedInBrowser();
void showStatusMessageAppearsInStatusBar();
void deselectDoesNotCrash();
void onProcessOutputAppendsToPanel();
void onProcessOutputSkippedWhenPanelHidden();
};
void tst_mainwindow::initTestCase() {
QVERIFY2(m_storeDir.isValid(), "temp store dir must be created");
// Minimal valid pass store: just a .gpg-id file
QFile gpgId(QDir::cleanPath(
QDir(m_storeDir.path()).filePath(QStringLiteral(".gpg-id"))));
QVERIFY2(gpgId.open(QIODevice::WriteOnly), ".gpg-id must be writable");
gpgId.write("0000000000000000\n");
gpgId.close();
// Save original settings before modifying them
m_savedPassStore = QtPassSettings::getPassStore();
m_savedUsePass = QtPassSettings::isUsePass();
m_savedShowProcessOutput = QtPassSettings::isShowProcessOutput();
m_savedGpgExecutable = QtPassSettings::getGpgExecutable();
// Point QtPassSettings at the temp store and use gpg (not pass) mode so
// configIsValid() only requires the .gpg-id file + a gpg binary.
QtPassSettings::setPassStore(QDir::cleanPath(m_storeDir.path()));
QtPassSettings::setUsePass(false);
// Verify gpg is reachable. We also pre-set the executable path so that
// QtPass::init() → initExecutables() finds it even when only "gpg" (not
// "gpg2") exists in PATH (e.g. Ubuntu CI where gpg2 is not a separate
// binary). Without this, initExecutables() leaves the path empty,
// configIsValid() returns false, and config() opens a blocking modal
// dialog that times out the test after 300 s.
m_gpgPath = Util::findBinaryInPath(QStringLiteral("gpg2"));
if (m_gpgPath.isEmpty())
m_gpgPath = Util::findBinaryInPath(QStringLiteral("gpg"));
if (m_gpgPath.isEmpty())
QSKIP("gpg not available — skipping MainWindow construction tests");
QtPassSettings::setGpgExecutable(m_gpgPath);
}
void tst_mainwindow::init() {
// Re-apply store settings in case a previous test modified them.
QtPassSettings::setPassStore(QDir::cleanPath(m_storeDir.path()));
QtPassSettings::setUsePass(false);
QtPassSettings::setShowProcessOutput(true);
// Re-apply gpg path: initExecutables() inside the constructor overwrites
// the setting to findBinaryInPath("gpg2"), which is empty on systems where
// only "gpg" exists. Setting it here ensures configIsValid() sees a valid
// executable and does not fall back to the blocking config() dialog.
QtPassSettings::setGpgExecutable(m_gpgPath);
m_window.reset(new MainWindow);
}
void tst_mainwindow::cleanup() { m_window.reset(); }
void tst_mainwindow::cleanupTestCase() {
// Restore all saved settings so the user's live config is not left pointing
// at our temp store (which will be deleted when m_storeDir goes out of
// scope).
QtPassSettings::setPassStore(m_savedPassStore);
QtPassSettings::setUsePass(m_savedUsePass);
QtPassSettings::setShowProcessOutput(m_savedShowProcessOutput);
QtPassSettings::setGpgExecutable(m_savedGpgExecutable);
}
// ---------------------------------------------------------------------------
/**
* @brief Constructor completes without crashing when a valid store exists.
*/
void tst_mainwindow::constructionDoesNotCrash() {
// init() already constructed the window; reaching this line is the test.
QVERIFY2(m_window != nullptr, "MainWindow must have been constructed");
}
/**
* @brief getKeygenDialog() returns nullptr before any keygen is started.
*/
void tst_mainwindow::getKeygenDialogInitiallyNull() {
QCOMPARE(m_window->getKeygenDialog(), nullptr);
}
/**
* @brief cleanKeygenDialog() is a no-op (and harmless) when no dialog exists.
*/
void tst_mainwindow::cleanKeygenDialogWithNullIsHarmless() {
QCOMPARE(m_window->getKeygenDialog(), nullptr);
m_window->cleanKeygenDialog();
QCOMPARE(m_window->getKeygenDialog(), nullptr);
}
/**
* @brief setUiElementsEnabled(false) disables the tree view and search field.
*/
void tst_mainwindow::setUiElementsEnabledDisablesTreeView() {
auto *treeView = m_window->findChild<QTreeView *>(QStringLiteral("treeView"));
QVERIFY2(treeView != nullptr, "treeView widget must exist");
m_window->setUiElementsEnabled(false);
QVERIFY2(!treeView->isEnabled(), "treeView must be disabled");
}
/**
* @brief setUiElementsEnabled(true) re-enables the tree view.
*/
void tst_mainwindow::setUiElementsEnabledEnablesTreeView() {
auto *treeView = m_window->findChild<QTreeView *>(QStringLiteral("treeView"));
QVERIFY2(treeView != nullptr, "treeView widget must exist");
m_window->setUiElementsEnabled(false);
m_window->setUiElementsEnabled(true);
QVERIFY2(treeView->isEnabled(), "treeView must be re-enabled");
}
/**
* @brief flashText() sets the text browser content.
*/
void tst_mainwindow::flashTextSetsContent() {
auto *browser =
m_window->findChild<QTextBrowser *>(QStringLiteral("textBrowser"));
QVERIFY2(browser != nullptr, "textBrowser must exist");
m_window->flashText(QStringLiteral("hello mainwindow test"), false);
QVERIFY2(
browser->toPlainText().contains(QStringLiteral("hello mainwindow test")),
"textBrowser must contain the flashed text");
}
/**
* @brief flashText() with isError=true does not crash.
*/
void tst_mainwindow::flashTextErrorDoesNotCrash() {
auto *browser =
m_window->findChild<QTextBrowser *>(QStringLiteral("textBrowser"));
QVERIFY2(browser != nullptr, "textBrowser must exist");
m_window->flashText(QStringLiteral("error text"), true);
QVERIFY2(browser->toPlainText().contains(QStringLiteral("error text")),
"textBrowser must contain the error text");
}
/**
* @brief flashText() with isHtml=true renders HTML into the browser.
*/
void tst_mainwindow::flashTextHtmlRenderedInBrowser() {
auto *browser =
m_window->findChild<QTextBrowser *>(QStringLiteral("textBrowser"));
QVERIFY2(browser != nullptr, "textBrowser must exist");
m_window->flashText(QStringLiteral("<b>bold</b>"), false, true);
// Qt's rich text engine converts <b> to font-weight CSS internally, so
// toHtml() never emits literal <b> tags. Verify the text content appears
// and that the HTML was rendered (not escaped as <b>).
QVERIFY2(browser->toHtml().contains(QStringLiteral("bold")),
"flashText with isHtml=true must set content in textBrowser");
QVERIFY2(
!browser->toHtml().contains(QStringLiteral("<b>bold</b>")),
"flashText with isHtml=true must not escape HTML tags");
}
/**
* @brief showStatusMessage() writes to the status bar.
*/
void tst_mainwindow::showStatusMessageAppearsInStatusBar() {
m_window->showStatusMessage(QStringLiteral("status test"), 60000);
QCOMPARE(m_window->statusBar()->currentMessage(),
QStringLiteral("status test"));
}
/**
* @brief deselect() does not crash when nothing is selected.
*/
void tst_mainwindow::deselectDoesNotCrash() {
m_window->deselect();
// No assertion needed — reaching this line means no crash / assert fired.
}
/**
* @brief onProcessOutput() appends text to the process output panel when the
* panel is visible.
*/
void tst_mainwindow::onProcessOutputAppendsToPanel() {
auto *outputEdit =
m_window->findChild<QTextEdit *>(QStringLiteral("processOutputEdit"));
QVERIFY2(outputEdit != nullptr, "processOutputEdit must exist");
m_window->onProcessOutput(QStringLiteral("process output line"), false);
QVERIFY2(
outputEdit->toPlainText().contains(QStringLiteral("process output line")),
"processOutputEdit must contain the appended line");
}
/**
* @brief onProcessOutput() does nothing when the panel is hidden
* (isShowProcessOutput == false).
*/
void tst_mainwindow::onProcessOutputSkippedWhenPanelHidden() {
QtPassSettings::setShowProcessOutput(false);
auto *outputEdit =
m_window->findChild<QTextEdit *>(QStringLiteral("processOutputEdit"));
QVERIFY2(outputEdit != nullptr, "processOutputEdit must exist");
const QString before = outputEdit->toPlainText();
m_window->onProcessOutput(QStringLiteral("should not appear"), false);
QCOMPARE(outputEdit->toPlainText(), before);
}
QTEST_MAIN(tst_mainwindow)
#include "tst_mainwindow.moc"