Debian Codegraph @

src/tests/tests_items.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "test_utils.h"
4 #include "tests.h"
5 #include "tests_common.h"
6
7 #include "common/mimetypes.h"
8 #include "common/commandstatus.h"
9
10 #include <QKeySequence>
11 #include <QRegularExpression>
12
13 void CoreTests::configMaxitems()
14 {
15 RUN("config" << "maxitems" << "3", "3\n");
16 RUN("add" << "A" << "B" << "C", "");
17 RUN("add" << "D", "");
18 RUN("separator" << " " << "read" << "0" << "1" << "2", "D C B");
19 RUN("size", "3\n");
20
21 RUN("add" << "E" << "F", "");
22 RUN("separator" << " " << "read" << "0" << "1" << "2", "F E D");
23 RUN("size", "3\n");
24
25 RUN("config" << "maxitems" << "2", "2\n");
26 RUN("separator" << " " << "read" << "0" << "1", "F E");
27 RUN("size", "2\n");
28
29 // Adding too many items fails.
30 m_test->ignoreErrors(
31 QRegularExpression(R"(Cannot add new items\. Tab "CLIPBOARD" reached the maximum number of items\.)") );
32 RUN_EXPECT_ERROR("add" << "1" << "2" << "3", CommandException);
33 RUN("separator" << " " << "read" << "0" << "1", "F E");
34 RUN("size", "2\n");
35
36 // Single item in tabs.
37 RUN("config" << "maxitems" << "1", "1\n");
38 RUN("separator" << " " << "read" << "0", "F");
39 RUN("size", "1\n");
40
41 RUN("add" << "G", "");
42 RUN("separator" << " " << "read" << "0", "G");
43 RUN("size", "1\n");
44
45 RUN("write" << "1" << "text/plain" << "H", "");
46 RUN("separator" << " " << "read" << "0", "H");
47 RUN("size", "1\n");
48
49 // No items in tabs.
50 RUN("config" << "maxitems" << "0", "0\n");
51 RUN("size", "0\n");
52
53 RUN_EXPECT_ERROR("add" << "1", CommandException);
54 RUN_EXPECT_ERROR("write" << "1", CommandException);
55 RUN("size", "0\n");
56
57 // Invalid value.
58 RUN("config" << "maxitems" << "-99", "0\n");
59 RUN("size", "0\n");
60 }
61
62 void CoreTests::keysAndFocusing()
63 {
64 RUN("disable", "");
65 KEYS(clipboardBrowserId << "CTRL+T");
66 WAIT_ON_OUTPUT("currentWindowTitle", appWindowTitle("New Tab"));
67
68 KEYS(tabDialogLineEditId << "ESC");
69 WAIT_ON_OUTPUT("currentWindowTitle", appWindowTitle("*Clipboard Storing Disabled*"));
70
71 RUN("enable", "");
72 }
73
74 void CoreTests::selectItems()
75 {
76 const auto tab = QString(clipboardTabName);
77 RUN("add" << "C" << "B" << "A", "");
78
79 KEYS("RIGHT" << "SHIFT+DOWN" << "SHIFT+DOWN");
80 TEST_SELECTED(tab + " 2 0 1 2\n");
81
82 KEYS("SHIFT+UP");
83 TEST_SELECTED(tab + " 1 0 1\n");
84
85 KEYS("END");
86 TEST_SELECTED(tab + " 2 2\n");
87
88 KEYS("SHIFT+UP");
89 TEST_SELECTED(tab + " 1 2 1\n");
90
91 KEYS("CTRL+A");
92 TEST_SELECTED(tab + " 1 0 1 2\n");
93
94 // CTRL+SPACE toggles current item selection
95 RUN("add" << "D", "");
96 KEYS("PGUP" << "CTRL+SHIFT+DOWN" << "CTRL+SHIFT+DOWN");
97 TEST_SELECTED(tab + " 2 0\n");
98 KEYS("CTRL+SPACE");
99 TEST_SELECTED(tab + " 2 0 2\n");
100 KEYS("SHIFT+DOWN");
101 TEST_SELECTED(tab + " 3 0 2 3\n");
102 KEYS("CTRL+SPACE");
103 TEST_SELECTED(tab + " 3 0 2\n");
104 }
105
106 void CoreTests::moveItems()
107 {
108 const auto tab = QString(clipboardTabName);
109 const auto args = Args() << "separator" << " ";
110 RUN(args << "add" << "C" << "B" << "A", "");
111
112 // move item one down
113 KEYS("RIGHT" << "CTRL+DOWN");
114 RUN(args << "read" << "0" << "1" << "2", "B A C");
115 TEST_SELECTED(tab + " 1 1\n");
116
117 // move items to top
118 KEYS("SHIFT+DOWN" << "CTRL+HOME");
119 RUN(args << "read" << "0" << "1" << "2", "A C B");
120 TEST_SELECTED(tab + " 1 0 1\n");
121 }
122
123 void CoreTests::deleteItems()
124 {
125 const auto tab = QString(clipboardTabName);
126 const auto args = Args() << "separator" << ",";
127 RUN(args << "add" << "C" << "B" << "A", "");
128
129 // delete first item
130 KEYS("RIGHT" << m_test->shortcutToRemove());
131 RUN(args << "read" << "0" << "1" << "2", "B,C,");
132 TEST_SELECTED(tab + " 0 0\n");
133
134 // select all and delete
135 KEYS("CTRL+A" << m_test->shortcutToRemove());
136 RUN(args << "size", "0\n");
137 }
138
139 void CoreTests::searchItems()
140 {
141 RUN("add" << "a" << "b" << "c", "");
142 KEYS(":b" << "TAB");
143 TEST_SELECTED(QString(clipboardTabName) + " 1 1\n");
144 }
145
146 void CoreTests::searchItemsAndSelect()
147 {
148 RUN("add" << "xx1" << "a" << "xx2" << "c" << "xx3" << "d", "");
149 KEYS(":xx" << filterEditId);
150 TEST_SELECTED(QString(clipboardTabName) + " 1 1\n");
151
152 KEYS(filterEditId << "DOWN" << filterEditId);
153 TEST_SELECTED(QString(clipboardTabName) + " 3 3\n");
154
155 KEYS(filterEditId << "DOWN" << filterEditId);
156 TEST_SELECTED(QString(clipboardTabName) + " 5 5\n");
157
158 KEYS(filterEditId << "TAB" << clipboardBrowserId);
159 }
160
161 void CoreTests::searchItemsAndCopy()
162 {
163 RUN("add" << "TEST_ITEM", "");
164 KEYS(":test" << "CTRL+C" << filterEditId);
165 WAIT_FOR_CLIPBOARD("TEST_ITEM");
166 }
167
168 void CoreTests::searchRowNumber()
169 {
170 RUN("add" << "d2" << "c" << "b2" << "a", "");
171
172 KEYS(":2");
173 KEYS(filterEditId << "TAB" << clipboardBrowserId);
174 TEST_SELECTED(QString(clipboardTabName) + " 1 1\n");
175 KEYS("CTRL+A");
176 TEST_SELECTED(QString(clipboardTabName) + " 1 1 3\n");
177
178 KEYS(":0");
179 KEYS(filterEditId << "TAB" << clipboardBrowserId << "CTRL+A");
180 TEST_SELECTED(QString(clipboardTabName) + " _\n");
181
182 RUN("config" << "row_index_from_one" << "false", "false\n");
183 KEYS(":2");
184 KEYS(filterEditId << "TAB" << clipboardBrowserId);
185 TEST_SELECTED(QString(clipboardTabName) + " 2 2\n");
186 KEYS("CTRL+A");
187 TEST_SELECTED(QString(clipboardTabName) + " 2 1 2 3\n");
188
189 KEYS(":0");
190 KEYS(filterEditId << "TAB" << clipboardBrowserId << "CTRL+A");
191 TEST_SELECTED(QString(clipboardTabName) + " 0 0\n");
192
193 KEYS(":5");
194 KEYS(filterEditId << "TAB" << clipboardBrowserId << "CTRL+A");
195 TEST_SELECTED(QString(clipboardTabName) + " _\n");
196
197 RUN("filter" << "-1", "");
198 RUN("filter", "-1\n");
199 KEYS(clipboardBrowserId << "CTRL+A");
200 TEST_SELECTED(QString(clipboardTabName) + " _\n");
201 }
202
203 void CoreTests::searchAccented()
204 {
205 RUN("add" << "a" << "väčšina" << "a", "");
206 RUN("filter" << "vacsina", "");
207 WAIT_ON_OUTPUT("testSelected", QByteArray(clipboardTabName) + " 1 1\n");
208 }
209
210 void CoreTests::searchManyItems()
211 {
212 RUN("config"
213 << "maxitems" << "100000"
214 << "row_index_from_one" << "false",
215 "maxitems=100000\n"
216 "row_index_from_one=false\n"
217 );
218 RUN("add.apply(this, [...Array(100000).keys()].reverse())", "");
219
220 RUN("filter" << "90001", "");
221 TEST_SELECTED(QString(clipboardTabName) + " 90001 90001\n");
222
223 RUN("filter" << "9 8 7 6 5", "");
224 WAIT_ON_OUTPUT("testSelected", QString(clipboardTabName) + " 56789 56789\n");
225
226 RUN("config" << "filter_regular_expression" << "true", "true\n");
227 RUN("filter" << ".*99999", "");
228 WAIT_ON_OUTPUT("testSelected", QString(clipboardTabName) + " 99999 99999\n");
229
230 KEYS(":9" << ":0002" << filterEditId);
231 RUN("filter", "90002\n");
232 KEYS(filterEditId << "TAB" << clipboardBrowserId);
233 TEST_SELECTED(QString(clipboardTabName) + " 90002 90002\n");
234
235 KEYS(":9" << ":999" << filterEditId);
236 KEYS(filterEditId << "TAB" << clipboardBrowserId);
237 WAIT_ON_OUTPUT(
238 QStringLiteral(
239 "plugins.itemtests.keys('%1', 'CTRL+A', '%1');"
240 "testSelected()")
241 .arg(clipboardBrowserId),
242 QString(clipboardTabName) + " 9999"
243 " 9999 19999 29999 39999 49999 59999 69999 79999 89999"
244 " 99990 99991 99992 99993 99994 99995 99996 99997 99998 99999\n"
245 );
246 }
247
248 void CoreTests::copyItems()
249 {
250 const auto tab = QString(clipboardTabName);
251 RUN("add" << "C" << "B" << "A", "");
252
253 // Select and copy all items.
254 KEYS("CTRL+A" << keyNameFor(QKeySequence::Copy));
255
256 // This seems to be required on Windows.
257 WAIT_ON_OUTPUT("clipboard", "A\nB\nC");
258
259 // Paste all items.
260 KEYS(keyNameFor(QKeySequence::Paste));
261 RUN("separator" << " " << "read" << "0" << "1" << "2" << "3" << "4" << "5", "A B C A B C");
262 RUN("size", "6\n");
263 }
264
265 void CoreTests::copyItemsNullCharacter()
266 {
267 // Items with embedded null characters should have nulls stripped
268 // when concatenated for the clipboard (GitHub issue #3515).
269 const auto tab = QString(clipboardTabName);
270 RUN("write(0,"
271 "{'text/plain': 'A' + String.fromCharCode(0) + 'B'},"
272 "{'text/plain': 'CD'})", "");
273
274 // Select and copy all items.
275 KEYS("CTRL+A" << keyNameFor(QKeySequence::Copy));
276
277 // Clipboard should contain both items without null characters.
278 WAIT_ON_OUTPUT("clipboard", "CD\nAB");
279 }
280
281
282 void CoreTests::selectAndCopyOrder()
283 {
284 const auto tab = testTab(1);
285 const Args args = Args("tab") << tab << "separator" << " ";
286 RUN(args << "add" << "D" << "C" << "B" << "A", "");
287 RUN("setCurrentTab" << tab, "");
288
289 KEYS("END" << "SHIFT+UP" << "SHIFT+UP" << "SHIFT+UP");
290 TEST_SELECTED(tab + " 0 3 2 1 0\n");
291
292 KEYS(keyNameFor(QKeySequence::Copy));
293 WAIT_ON_OUTPUT("clipboard", "D\nC\nB\nA");
294 }
295
296 void CoreTests::sortAndReverse()
297 {
298 const auto tab = testTab(1);
299 const Args args = Args("tab") << tab << "separator" << " ";
300 RUN(args << "add" << "D" << "A" << "C" << "B", "");
301 RUN("setCurrentTab" << tab, "");
302
303 KEYS("CTRL+A");
304 TEST_SELECTED(tab + " 0 0 1 2 3\n");
305
306 KEYS("CTRL+SHIFT+S");
307 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "A B C D ");
308 TEST_SELECTED(tab + " 1 0 1 2 3\n");
309 KEYS(keyNameFor(QKeySequence::Copy));
310 WAIT_ON_OUTPUT("clipboard", "A\nB\nC\nD");
311
312 KEYS("CTRL+SHIFT+R");
313 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D C B A ");
314 TEST_SELECTED(tab + " 2 0 1 2 3\n");
315 KEYS(keyNameFor(QKeySequence::Copy));
316 WAIT_ON_OUTPUT("clipboard", "D\nC\nB\nA");
317
318 KEYS("CTRL+SHIFT+R");
319 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "A B C D ");
320 TEST_SELECTED(tab + " 1 0 1 2 3\n");
321 }
322
323 void CoreTests::editItems()
324 {
325 RUN("config" << "edit_ctrl_return" << "true", "true\n");
326
327 RUN("add" << "Line 4" << "Line 1", "");
328
329 KEYS(clipboardBrowserId << "F2"
330 << editorId << "END" << "ENTER" << ":Line 2" << "F2");
331 RUN("read" << "0", "Line 1\nLine 2");
332
333 KEYS(clipboardBrowserId << "DOWN" << "F2"
334 << editorId << "HOME" << ":Line 3" << "ENTER" << "F2");
335 RUN("read" << "1", "Line 3\nLine 4");
336 RUN("read" << "0", "Line 1\nLine 2");
337
338 // Edit multiple items
339 KEYS(clipboardBrowserId << "SHIFT+UP" << "F2"
340 << editorId << "END" << "ENTER" << ":Line 5" << "F2");
341 RUN("read" << "0", "Line 3\nLine 4\nLine 1\nLine 2\nLine 5");
342 RUN("read" << "1", "Line 1\nLine 2");
343 RUN("read" << "2", "Line 3\nLine 4");
344 }
345
346 void CoreTests::createNewItem()
347 {
348 RUN("config" << "edit_ctrl_return" << "true", "true\n");
349
350 KEYS("CTRL+N" << editorId << ":Line 1" << "ENTER" << ":Line 2" << "F2");
351 RUN("read" << "0", "Line 1\nLine 2");
352
353 KEYS("CTRL+N" << editorId << ":Line 3" << "ENTER" << ":Line 4" << "F2");
354 RUN("read" << "0", "Line 3\nLine 4");
355 }
356
357 void CoreTests::editNotes()
358 {
359 const auto script = R"(
360 add(
361 {
362 [mimeText]: 'B',
363 // https://stackoverflow.com/a/13139830
364 [mimeIcon]: frombase64("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"),
365 },
366 {
367 [mimeText]: 'A',
368 [mimeIcon]: String.fromCharCode(0xf188),
369 }
370 )
371 )";
372 RUN(script, "");
373
374 RUN("config" << "editor" << "", "\n");
375 KEYS("SHIFT+F2" << ":A Note" << "F2");
376 RUN("read" << mimeText << "0" << mimeItemNotes << "0" << "F2", "A\nA Note");
377 RUN("read" << mimeText << "1" << mimeItemNotes << "1" << "F2", "B\n");
378
379 KEYS("DOWN");
380
381 KEYS("SHIFT+F2" << ":B Note" << "F2");
382 RUN("read" << mimeText << "1" << mimeItemNotes << "1" << "F2", "B\nB Note");
383 RUN("read" << mimeText << "0" << mimeItemNotes << "0" << "F2", "A\nA Note");
384 }
385
386 void CoreTests::editHtml()
387 {
388 // Create new item with bold text
389 KEYS("CTRL+N" << editorId << ":BOLD"
390 << QKeySequence(QKeySequence::SelectAll).toString()
391 << QKeySequence(QKeySequence::Bold).toString()
392 << "F2" << clipboardBrowserId);
393
394 const auto readHtmlItem = [this](){
395 QByteArray out;
396 run(Args() << "read(mimeHtml, 0)", &out);
397 return QString::fromUtf8(out);
398 };
399
400 QRegularExpression re("font-weight: *[67]00[^>]*>BOLD</[^>]*>");
401 QString html = readHtmlItem();
402 QVERIFY2( html.contains(re), html.toUtf8() );
403
404 // Append normal text
405 KEYS("F2" << editorId << "END"
406 << QKeySequence(QKeySequence::Bold).toString()
407 << ":,NORMAL" << "F2" << clipboardBrowserId);
408 html = readHtmlItem();
409 QVERIFY2( html.contains(re), html.toUtf8() );
410 re.setPattern( re.pattern() + ",NORMAL" );
411 QVERIFY2( html.contains(re), html.toUtf8() );
412
413 // Append italic text
414 KEYS("F2" << editorId << "END"
415 << QKeySequence(QKeySequence::Italic).toString()
416 << ":,ITALIC" << "F2" << clipboardBrowserId);
417 html = readHtmlItem();
418 QVERIFY2( html.contains(re), html.toUtf8() );
419 re.setPattern( re.pattern() + "<[^>]*font-style: *italic[^>]*>,ITALIC</[^>]*>" );
420 QVERIFY2( html.contains(re), html.toUtf8() );
421
422 // Append underline text
423 KEYS("F2" << editorId << "END"
424 << QKeySequence(QKeySequence::Underline).toString()
425 << ":,ULINE" << "F2" << clipboardBrowserId);
426 html = readHtmlItem();
427 QVERIFY2( html.contains(re), html.toUtf8() );
428 re.setPattern( re.pattern() + "<[^>]*text-decoration: *underline[^>]*>,ULINE</[^>]*>" );
429 QVERIFY2( html.contains(re), html.toUtf8() );
430
431 // Append strike-through text
432 KEYS("F2" << editorId << "END"
433 << "mouse|CLICK|text=Strikethrough"
434 << ":,XXX" << "F2" << clipboardBrowserId);
435 html = readHtmlItem();
436 QVERIFY2( html.contains(re), html.toUtf8() );
437 re.setPattern( re.pattern() + "<[^>]*text-decoration: *underline line-through[^>]*>,XXX</[^>]*>" );
438 QVERIFY2( html.contains(re), html.toUtf8() );
439
440 // Erase style
441 KEYS("F2" << editorId << "END"
442 << QKeySequence(QKeySequence::SelectAll).toString()
443 << "mouse|CLICK|text=Erase Style"
444 << "F2" << clipboardBrowserId);
445 RUN("read(0)", "BOLD,NORMAL,ITALIC,ULINE,XXX");
446 }