Debian Codegraph @

src/tests/test_utils.h

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #pragma once
4
5 #include "common/log.h"
6
7 #include <QByteArray>
8 #include <QKeySequence>
9 #include <QString>
10 #include <QStringList>
11 #include <QTest>
12 #include <QThreadPool>
13 #include <QTimer>
14 #include <QVariantMap>
15
16 constexpr int maxReadLogSize = 1 * 1024 * 1024;
17
18 const QString sessionName = QStringLiteral("__COPYQ_TEST");
19
20 constexpr auto clipboardTabName = "CLIPBOARD";
21 constexpr auto defaultSessionColor = "#ff8800";
22 constexpr auto defaultTagColor = "#000000";
23
24 constexpr auto clipboardBrowserId = "focus:^ClipboardBrowser";
25 constexpr auto clipboardBrowserRefreshButtonId = "focus:ClipboardBrowserRefreshButton";
26 constexpr auto filterEditId = "focus:Utils::FilterLineEdit";
27 constexpr auto trayMenuId = "focus:TrayMenu";
28 constexpr auto menuId = "focus:Menu";
29 constexpr auto customMenuId = "focus:CustomMenu";
30 constexpr auto editorId = "focus::ItemEditorWidget";
31 constexpr auto tabDialogLineEditId = "focus:lineEditTabName";
32 constexpr auto commandDialogId = "focus:CommandDialog";
33 constexpr auto commandDialogSaveButtonId =
34 "focus::QPushButton'Save'<:QMessageBox'Command dialog has unsaved changes.'";
35 constexpr auto commandDialogListId = "focus:listWidgetItems";
36 constexpr auto configurationDialogId = "focus:ConfigurationManager";
37 constexpr auto shortcutButtonId = "focus::QToolButton<CommandDialog";
38 constexpr auto shortcutDialogId = "focus::QKeySequenceEdit<ShortcutDialog";
39 constexpr auto actionDialogId = "focus:ActionDialog";
40 constexpr auto aboutDialogId = "focus:AboutDialog";
41 constexpr auto logDialogId = "focus:LogDialog";
42 constexpr auto exportDialogId = "focus:ImportExportDialog:QDialog'Options for Export'";
43 constexpr auto importDialogId = "focus:ImportExportDialog:QDialog'Options for Import'";
44 constexpr auto actionHandlerDialogId = "focus:ActionHandlerDialog";
45 constexpr auto actionHandlerFilterId = "focus:filterLineEdit";
46 constexpr auto actionHandlerTableId = "focus:tableView";
47 constexpr auto clipboardDialogId = "focus:ClipboardDialog";
48 constexpr auto clipboardDialogFormatListId = "focus:listWidgetFormats";
49 constexpr auto confirmExitDialogId =
50 "focus::QPushButton'Yes'<:QMessageBox'Do you want to exit CopyQ\\\\?'";
51 constexpr auto runningCommandsExitDialogId =
52 "focus::QPushButton'Exit Anyway'<:QMessageBox'Cancel active commands and exit\\\\?'";
53 constexpr auto itemPreviewId = "focus:<dockWidgetItemPreviewContents";
54 constexpr auto passwordEntryCurrentId = "focus:QInputDialog'Current Tab Encryption Password'";
55 constexpr auto passwordEntryRetypeId = "focus:QInputDialog'Confirm Encryption Password'";
56 constexpr auto passwordEntryNewId = "focus:QInputDialog'New Tab Encryption Password'";
57 constexpr auto passwordEntryExportId = "focus:QInputDialog'Export Password'";
58 constexpr auto passwordEntryImportId = "focus:QInputDialog'Import Password'";
59 constexpr auto passwordMessageFailedId = "focus:QMessageBox'Maximum password attempts exceeded.'";
60 constexpr auto passwordMessageEmptyId = "focus:QMessageBox'New password cannot be empty.'";
61 constexpr auto passwordMessageChangeId = "focus:QMessageBox'Password has been changed successfully.'";
62 // File name line edit in Qt file dialog
63 constexpr auto fileNameEditId = "focus:fileNameEdit";
64
65 #define STR_(str) #str
66 #define STR(str) STR_(str)
67
68 #define SRC_FILE \
69 QStringLiteral(__FILE__).section(QStringLiteral("src/"), 1, -1, QString::SectionIncludeLeadingSep)
70
71 #define NO_ERRORS(ERRORS_OR_EMPTY) !m_test->writeOutErrors(ERRORS_OR_EMPTY)
72
73 /**
74 * Verify that method call (TestInterface::startServer(), TestInterface::runClient() etc.)
75 * didn't fail or print error.
76 */
77 #define TEST(ERRORS_OR_EMPTY) \
78 QVERIFY2( NO_ERRORS(ERRORS_OR_EMPTY), "Failed with errors above." )
79
80 #define LOG_ACTION(LABEL, ARGUMENTS) \
81 log(QStringLiteral("🔵 %1: %2 --- %3:%4").arg(LABEL, #ARGUMENTS, SRC_FILE, STR(__LINE__)))
82
83 #define RUN(ARGUMENTS, STDOUT_EXPECTED) \
84 do { \
85 LOG_ACTION("RUN", ARGUMENTS); \
86 TEST( m_test->runClient((Args() << ARGUMENTS), toByteArray(STDOUT_EXPECTED)) ); \
87 } while(false)
88
89 #define RUN_MULTIPLE(...) TEST(runMultiple({__VA_ARGS__}))
90
91 #define TEST_SELECTED(STDOUT_EXPECTED) \
92 RUN("testSelected", (STDOUT_EXPECTED))
93
94 #define KEYS(ARGUMENTS) \
95 do { \
96 LOG_ACTION("KEYS", ARGUMENTS); \
97 TEST( m_test->runClient((Args() << "plugins.itemtests.keys" << ARGUMENTS), QByteArray()) ); \
98 } while(false)
99
100 #define RUN_WITH_INPUT(ARGUMENTS, INPUT, STDOUT_EXPECTED) \
101 TEST( m_test->runClient((Args() << ARGUMENTS), toByteArray(STDOUT_EXPECTED), toByteArray(INPUT)) )
102
103 #define RUN_EXPECT_ERROR(ARGUMENTS, EXIT_CODE) \
104 TEST( m_test->runClientWithError((Args() << ARGUMENTS), (EXIT_CODE)) )
105
106 #define RUN_EXPECT_ERROR_WITH_STDERR(ARGUMENTS, EXIT_CODE, STDERR_CONTAINS) \
107 TEST( m_test->runClientWithError((Args() << ARGUMENTS), (EXIT_CODE), toByteArray(STDERR_CONTAINS)) )
108
109 #define WAIT_FOR_CLIPBOARD(DATA) \
110 TEST( m_test->verifyClipboard(DATA, "text/plain") )
111
112 #define WAIT_FOR_CLIPBOARD2(DATA, MIME) \
113 TEST( m_test->verifyClipboard((DATA), (MIME)) )
114
115 #define RETURN_ON_ERROR(CALLBACK, ERROR) \
116 do { \
117 const auto errors = (CALLBACK); \
118 if ( !errors.isEmpty() ) \
119 return QByteArray(ERROR) + ":\n" + QByteArray(errors); \
120 } while(false)
121
122 /// Skip rest of the tests
123 #define SKIP(MESSAGE) QSKIP(MESSAGE, SkipAll)
124
125 #define WAIT_ON_OUTPUT(ARGUMENTS, OUTPUT) \
126 TEST( m_test->waitOnOutput((Args() << ARGUMENTS), toByteArray(OUTPUT)) )
127
128 #define SKIP_ON_ENV(ENV) \
129 if ( qgetenv(ENV) == "1" ) \
130 SKIP("Unset " ENV " to run the tests")
131
132 /// Post-match wait (ms) after clipboard verification succeeds.
133 const int waitMsSetClipboard = 50;
134
135 /// Interval to wait (in ms) for pasting clipboard.
136 const int waitMsPasteClipboard = 1000;
137
138 /// Interval to wait (in ms) for client process.
139 const int waitClientRun = 10000;
140
141 const int waitClientRunMultiple = waitClientRun + 5000;
142
143 using Args = QStringList;
144
145 inline QByteArray toByteArray(const QString &text)
146 {
147 return text.toUtf8();
148 }
149
150 inline QByteArray toByteArray(const QByteArray &text)
151 {
152 return text;
153 }
154
155 inline QByteArray toByteArray(const char *text)
156 {
157 return text;
158 }
159
160 /// Naming scheme for test tabs in application.
161 inline QString testTab(int i)
162 {
163 return "Tab_&" + QString::number(i);
164 }
165
166 inline QString keyNameFor(QKeySequence::StandardKey standardKey)
167 {
168 return QKeySequence(standardKey).toString();
169 }
170
171 [[nodiscard]] inline QByteArray runMultiple(
172 const std::initializer_list<std::function<void()>> &fns)
173 {
174 QThreadPool pool;
175 pool.setMaxThreadCount(fns.size());
176 for (const auto &fn : fns)
177 pool.start(fn);
178
179 if (!pool.waitForDone(waitClientRunMultiple)) {
180 const int remaining = pool.activeThreadCount();
181 return "Failed RUN_MULTIPLE: Some calls ("
182 + QByteArray::number(remaining) + ") timed out";
183 }
184
185 if (QTest::currentTestFailed())
186 return "Failed RUN_MULTIPLE: Some calls failed";
187
188 return {};
189 }