Debian Codegraph @

src/tests/itemsynctests.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "itemsynctests.h"
4
5 #include "common/commandstatus.h"
6 #include "common/mimetypes.h"
7 #include "common/sleeptimer.h"
8 #include "tests/test_utils.h"
9 #include "gui/fromiconid.h"
10 #include "gui/icons.h"
11
12 #include <QDir>
13 #include <QFile>
14 #include <QUrl>
15
16 #include <memory>
17
18 namespace {
19
20 using FilePtr = std::shared_ptr<QFile>;
21
22 const char sep[] = " ;; ";
23
24 const auto confirmRemoveDialogId =
25 "focus::QPushButton'Yes'<qt_msgbox_buttonbox:QDialogButtonBox<:QMessageBox"
26 "'Do you really want to remove items and associated files\\\\?'";
27
28 class TestDir final {
29 public:
30 explicit TestDir(int i, bool createPath = true)
31 : m_dir(ItemSyncTests::testDir(i))
32 {
33 clear();
34 if (createPath)
35 create();
36 }
37
38 ~TestDir()
39 {
40 clear();
41 }
42
43 void clear()
44 {
45 if (isValid())
46 m_dir.removeRecursively();
47 }
48
49 void create()
50 {
51 if ( !m_dir.mkpath(".") )
52 Q_ASSERT(false);
53 }
54
55 bool isValid() const
56 {
57 return m_dir.exists();
58 }
59
60 QStringList files() const
61 {
62 QStringList files = m_dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name);
63 files.removeOne(QStringLiteral(".copyq_lock"));
64 return files;
65 }
66
67 FilePtr file(const QString &fileName) const
68 {
69 return std::make_shared<QFile>(filePath(fileName));
70 }
71
72 QString filePath(const QString &fileName) const
73 {
74 return m_dir.absoluteFilePath(fileName);
75 }
76
77 bool remove(const QString &fileName)
78 {
79 return QFile::remove(filePath(fileName));
80 }
81
82 private:
83 Q_DISABLE_COPY(TestDir)
84 QDir m_dir;
85 };
86
87 QByteArray createFile(const TestDir &dir, const QString &fileName, const QByteArray &content)
88 {
89 FilePtr file(dir.file(fileName));
90 if ( file->exists() )
91 return "File already exists!";
92
93 if ( !file->open(QIODevice::WriteOnly) )
94 return "Cannot open file!";
95
96 if ( file->write(content) == -1 )
97 return "Cannot write to file!";
98
99 file->close();
100 return "";
101 }
102
103 } // namespace
104
105 ItemSyncTests::ItemSyncTests(const TestInterfacePtr &test, QObject *parent)
106 : QObject(parent)
107 , m_test(test)
108 {
109 setProperty("CopyQ_test_id", QStringLiteral("itemsync"));
110 m_test->setEnv("COPYQ_SYNC_UPDATE_INTERVAL_MS", "100");
111
112 QStringList tabPaths;
113 for (int i = 0; i < 10; ++i) {
114 tabPaths.append(ItemSyncTests::testTab(i));
115 tabPaths.append(ItemSyncTests::testDir(i));
116 }
117
118 QVariantList formatSettings;
119 QVariantMap format;
120
121 format["formats"] = QStringList() << "xxx";
122 format["itemMime"] = QString(COPYQ_MIME_PREFIX "test-xxx");
123 format["icon"] = QString(fromIconId(IconTrash));
124 formatSettings << format;
125
126 format["formats"] = QStringList() << "zzz" << ".yyy";
127 format["itemMime"] = QString(COPYQ_MIME_PREFIX "test-zzz");
128 format["icon"] = QString();
129 formatSettings << format;
130
131 QVariantMap settings;
132 settings["sync_tabs"] = tabPaths;
133 settings["format_settings"] = formatSettings;
134
135 setProperty("CopyQ_test_settings", settings);
136 }
137
138 QString ItemSyncTests::testTab(int i)
139 {
140 return ::testTab(i);
141 }
142
143 QString ItemSyncTests::testDir(int i)
144 {
145 return QDir::tempPath() + "/copyq_test_dirs/itemsync_" + QString::number(i);
146 }
147
148 void ItemSyncTests::initTestCase()
149 {
150 TEST(m_test->initTestCase());
151 }
152
153 void ItemSyncTests::cleanupTestCase()
154 {
155 TEST(m_test->cleanupTestCase());
156 }
157
158 void ItemSyncTests::init()
159 {
160 TEST(m_test->init());
161
162 QDir tmpDir(QDir::cleanPath(testDir(0) + "/.."));
163 if ( tmpDir.exists() )
164 QVERIFY(tmpDir.removeRecursively());
165 }
166
167 void ItemSyncTests::cleanup()
168 {
169 TEST( m_test->cleanup() );
170 }
171
172 void ItemSyncTests::createRemoveTestDir()
173 {
174 TestDir dir1(1);
175 TestDir dir2(2);
176
177 QVERIFY(dir1.isValid());
178 QCOMPARE(dir1.files().join(sep), QString());
179
180 QVERIFY(dir2.isValid());
181 QCOMPARE(dir2.files().join(sep), QString());
182
183 const QString testFileName1 = "test1.txt";
184 FilePtr f1(dir1.file(testFileName1));
185 QVERIFY(!f1->exists());
186 QVERIFY(f1->open(QIODevice::WriteOnly));
187 f1->close();
188
189 QCOMPARE(dir1.files().join(sep), testFileName1);
190
191 dir1.clear();
192 QVERIFY(!dir1.isValid());
193 QVERIFY(!f1->exists());
194 QVERIFY(dir2.isValid());
195
196 dir2.clear();
197 QVERIFY(!dir1.isValid());
198 QVERIFY(!dir2.isValid());
199
200 dir1.create();
201 QVERIFY(dir1.isValid());
202 QCOMPARE(dir2.files().join(sep), QString());
203 }
204
205 void ItemSyncTests::itemsToFiles()
206 {
207 TestDir dir1(1);
208 const QString tab1 = testTab(1);
209 RUN(Args() << "show" << tab1, "");
210
211 const Args args = Args() << "tab" << tab1;
212
213 RUN(args << "add" << "A" << "B" << "C", "");
214 RUN(args << "read" << "0" << "1" << "2", "C\nB\nA");
215 RUN(args << "size", "3\n");
216
217 const auto files = dir1.files();
218 QVERIFY2( files.size() == 3, files.join(sep).toUtf8() );
219 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
220 QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
221 QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
222 }
223
224 void ItemSyncTests::filesToItems()
225 {
226 TestDir dir1(1);
227 const QString tab1 = testTab(1);
228 RUN(Args() << "show" << tab1, "");
229
230 const Args args = Args()
231 << "tab" << tab1
232 << "read.apply(this, [...Array(size()).keys()])";
233
234 RUN(args, "");
235
236 QStringList expectedItems;
237
238 const QByteArray text1 = "TEST1";
239 TEST(createFile(dir1, "test1.txt", text1));
240 // Hidden files (starting with dot) should be ignored
241 const QByteArray hiddenFile = "HIDDEN";
242 TEST(createFile(dir1, ".hidden.txt", hiddenFile));
243 const QByteArray text2 = "TEST2";
244 TEST(createFile(dir1, "test2.txt", text2));
245 // Non-owned files are sorted by name
246 expectedItems = QStringList{text1, text2};
247 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
248
249 const QByteArray nested1_1 = "nested 1 item 1";
250 QVERIFY(QDir(dir1.filePath("nested1")).mkpath("."));
251 TEST(createFile(dir1, "nested1/test1.txt", nested1_1));
252 // More nested items come later
253 expectedItems = QStringList{text1, text2, nested1_1};
254 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
255
256 const QByteArray text3 = "TEST3";
257 TEST(createFile(dir1, "test3.txt", text3));
258 expectedItems = QStringList{text1, text2, text3, nested1_1};
259 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
260
261 const QByteArray text0 = "TEST0";
262 TEST(createFile(dir1, "test0.txt", text0));
263 expectedItems = QStringList{text0, text1, text2, text3, nested1_1};
264 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
265
266 const QByteArray nested2_1 = "nested 2 item 1";
267 QVERIFY(QDir(dir1.filePath("nested2")).mkpath("."));
268 TEST(createFile(dir1, "nested2/test1.txt", nested2_1));
269 expectedItems = QStringList{text0, text1, text2, text3, nested1_1, nested2_1};
270 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
271
272 const QByteArray nested1_0 = "nested 1 item 0";
273 TEST(createFile(dir1, "nested1/test0.txt", nested1_0));
274 expectedItems = QStringList{
275 text0, text1, text2, text3, nested1_0, nested1_1, nested2_1};
276 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
277
278 const QByteArray nested2_0 = "nested 2 item 0";
279 TEST(createFile(dir1, "nested2/test0.txt", nested2_0));
280 expectedItems = QStringList{
281 text0, text1, text2, text3, nested1_0, nested1_1, nested2_0, nested2_1};
282 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
283
284 const QByteArray nested1_1_0 = "nested 1 nested 1 item 0";
285 QVERIFY(QDir(dir1.filePath("nested1/nested1")).mkpath("."));
286 TEST(createFile(dir1, "nested1/nested1/test0.txt", nested1_1_0));
287 expectedItems = QStringList{
288 text0, text1, text2, text3, nested1_0, nested1_1, nested2_0, nested2_1, nested1_1_0};
289 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
290
291 SKIP_ON_ENV("COPYQ_TESTS_NO_SYMLINKS");
292 // Ignore symlink to already added directory
293 QFile dirLink(dir1.filePath("nested1"));
294 if (!dirLink.link(dir1.filePath("nested3")))
295 QFAIL("Failed to create directory link: " + dirLink.errorString().toUtf8());
296 // Allow other symlinks
297 QFile fileLink(dir1.filePath("test1.txt"));
298 if (!fileLink.link(dir1.filePath("test4.txt")))
299 QFAIL("Failed to create file link: " + fileLink.errorString().toUtf8());
300 expectedItems = QStringList{
301 text0, text1, text2, text3, text1, nested1_0, nested1_1, nested2_0, nested2_1, nested1_1_0};
302 WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
303 }
304
305 void ItemSyncTests::removeOwnItems()
306 {
307 TestDir dir1(1);
308 const QString tab1 = testTab(1);
309 RUN(Args() << "show" << tab1, "");
310
311 const Args args = Args() << "separator" << "," << "tab" << tab1;
312
313 RUN(args << "add" << "A" << "B" << "C" << "D", "");
314
315 const auto files = dir1.files();
316 QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
317 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
318 QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
319 QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
320 QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
321
322 // Move to test tab and select second and third item.
323 RUN("setCurrentTab" << tab1, "");
324 RUN(args << "selectItems" << "1" << "2", "true\n");
325 TEST_SELECTED(tab1.toUtf8() + " 2 1 2\n");
326
327 // Remove selected items.
328 KEYS(m_test->shortcutToRemove());
329 RUN(args << "read" << "0" << "1" << "2" << "3", "D,A,,");
330 QCOMPARE( dir1.files().join(sep),
331 files[0]
332 + sep + files[3]
333 );
334
335 // Removing own items works from script.
336 RUN(args << "remove" << "1", "");
337 RUN(args << "read" << "0" << "1" << "2" << "3", "D,,,");
338 QCOMPARE( dir1.files().join(sep), files[3] );
339 }
340
341 void ItemSyncTests::removeNotOwnedItems()
342 {
343 TestDir dir1(1);
344 const QString tab1 = testTab(1);
345 RUN(Args() << "show" << tab1, "");
346
347 const Args args = Args() << "separator" << "," << "tab" << tab1;
348
349 const QString fileA = "test1.txt";
350 const QString fileB = "test2.txt";
351 const QString fileC = "test3.txt";
352 const QString fileD = "test4.txt";
353
354 TEST(createFile(dir1, fileA, "A"));
355 WAIT_ON_OUTPUT(args << "size", "1\n");
356 TEST(createFile(dir1, fileB, "B"));
357 WAIT_ON_OUTPUT(args << "size", "2\n");
358 TEST(createFile(dir1, fileC, "C"));
359 WAIT_ON_OUTPUT(args << "size", "3\n");
360 TEST(createFile(dir1, fileD, "D"));
361 WAIT_ON_OUTPUT(args << "size", "4\n");
362
363 QCOMPARE( dir1.files().join(sep),
364 fileA
365 + sep + fileB
366 + sep + fileC
367 + sep + fileD
368 );
369
370 // Move to test tab and select second and third item.
371 RUN("setCurrentTab" << tab1, "");
372 RUN(args << "selectItems" << "1" << "2", "true\n");
373 TEST_SELECTED(tab1.toUtf8() + " 2 1 2\n");
374
375 // Accept the "Remove Items?" dialog.
376 KEYS(clipboardBrowserId << m_test->shortcutToRemove() << confirmRemoveDialogId);
377 KEYS(confirmRemoveDialogId << "ENTER" << clipboardBrowserId);
378 RUN(args << "read" << "0" << "1" << "2" << "3", "A,D,,");
379 QCOMPARE( dir1.files().join(sep),
380 fileA
381 + sep + fileD
382 );
383
384 // Removing not owned items from script doesn't work.
385 RUN_EXPECT_ERROR(args << "remove" << "1", CommandException);
386 QCOMPARE( dir1.files().join(sep),
387 fileA
388 + sep + fileD
389 );
390 }
391
392 void ItemSyncTests::removeNotOwnedItemsCancel()
393 {
394 TestDir dir1(1);
395 const QString tab1 = testTab(1);
396 RUN(Args() << "show" << tab1, "");
397
398 const Args args = Args() << "separator" << "," << "tab" << tab1;
399
400 const QString fileA = "test1.txt";
401 const QString fileB = "test2.txt";
402
403 TEST(createFile(dir1, fileA, "A"));
404 WAIT_ON_OUTPUT(args << "size", "1\n");
405 TEST(createFile(dir1, fileB, "B"));
406 WAIT_ON_OUTPUT(args << "size", "2\n");
407
408 QCOMPARE(dir1.files().join(sep), fileA + sep + fileB );
409
410 // Move to test tab and select second item.
411 RUN("setCurrentTab" << tab1, "");
412 RUN(args << "selectItems" << "1", "true\n");
413 TEST_SELECTED(tab1.toUtf8() + " 1 1\n");
414
415 // Don't accept the "Remove Items?" dialog.
416 KEYS(clipboardBrowserId << m_test->shortcutToRemove() << confirmRemoveDialogId);
417 TEST_SELECTED(tab1.toUtf8() + " 1 1\n");
418 KEYS(confirmRemoveDialogId << "ESCAPE" << clipboardBrowserId);
419 RUN(args << "read" << "0" << "1", "A,B");
420 QCOMPARE( dir1.files().join(sep), fileA + sep + fileB );
421 TEST_SELECTED(tab1.toUtf8() + " 1 1\n");
422 }
423
424 void ItemSyncTests::removeFiles()
425 {
426 TestDir dir1(1);
427 const QString tab1 = testTab(1);
428 RUN(Args() << "show" << tab1, "");
429
430 const Args args = Args() << "separator" << "," << "tab" << tab1;
431
432 RUN(args << "add" << "A" << "B" << "C" << "D", "");
433
434 const auto files = dir1.files();
435 QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
436 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
437 QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
438 QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
439 QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
440
441 FilePtr file = dir1.file(files[2]);
442 QVERIFY(file->open(QIODevice::ReadOnly));
443 QCOMPARE(file->readAll().data(), QByteArray("C").data());
444 file->remove();
445
446 WAIT_ON_OUTPUT(args << "size", "3\n");
447 RUN(args << "read" << "0" << "1" << "2", "D,B,A");
448
449 dir1.file(files[1])->remove();
450 dir1.file(files[0])->remove();
451
452 WAIT_ON_OUTPUT(args << "size", "1\n");
453 RUN(args << "read" << "0", "D");
454 }
455
456 void ItemSyncTests::modifyItems()
457 {
458 TestDir dir1(1);
459 const QString tab1 = testTab(1);
460 RUN(Args() << "show" << tab1, "");
461
462 const Args args = Args() << "separator" << "," << "tab" << tab1;
463
464 RUN(args << "add" << "A" << "B" << "C" << "D", "");
465
466 const auto files = dir1.files();
467 QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
468
469 FilePtr file = dir1.file(files[2]);
470 QVERIFY(file->open(QIODevice::ReadOnly));
471 QCOMPARE(file->readAll().data(), QByteArray("C").data());
472 file->close();
473
474 KEYS("HOME" << "DOWN" << "F2" << ":XXX" << "F2");
475 RUN(args << "size", "4\n");
476 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,XXX,B,A,");
477
478 file = dir1.file(files[2]);
479 QVERIFY(file->open(QIODevice::ReadOnly));
480 QCOMPARE(file->readAll().data(), QByteArray("XXX").data());
481 file->close();
482
483 const auto script = R"(
484 setCommands([{
485 name: 'Modify current item',
486 inMenu: true,
487 shortcuts: ['Ctrl+F1'],
488 cmd: `
489 copyq: item = selectedItemsData()[0]
490 item[mimeText] = "ZZZ"
491 setSelectedItemData(0, item)
492 `
493 }])
494 )";
495 RUN(script, "");
496 KEYS("HOME" << "DOWN" << "CTRL+F1");
497 WAIT_ON_OUTPUT(args << "read" << "1", "ZZZ");
498 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,ZZZ,B,A,");
499 RUN(args << "unload" << tab1, "");
500 RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,ZZZ,B,A,");
501
502 file = dir1.file(files[2]);
503 QVERIFY(file->open(QIODevice::ReadOnly));
504 QCOMPARE(file->readAll().data(), QByteArray("ZZZ").data());
505 file->close();
506 }
507
508 void ItemSyncTests::modifyFiles()
509 {
510 TestDir dir1(1);
511 const QString tab1 = testTab(1);
512 RUN(Args() << "show" << tab1, "");
513
514 const Args args = Args() << "separator" << "," << "tab" << tab1;
515
516 RUN(args << "add" << "A" << "B" << "C" << "D", "");
517
518 const auto files = dir1.files();
519 QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
520 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
521 QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
522 QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
523 QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
524
525 FilePtr file = dir1.file(files[2]);
526 QVERIFY(file->open(QIODevice::ReadWrite));
527 QCOMPARE(file->readAll().data(), QByteArray("C").data());
528 file->write("X");
529 file->close();
530
531 WAIT_ON_OUTPUT(args << "read" << "0" << "1" << "2" << "3", "D,CX,B,A");
532 RUN(args << "size", "4\n");
533 }
534
535 void ItemSyncTests::itemToClipboard()
536 {
537 TestDir dir1(1);
538 const QString tab1 = testTab(1);
539 RUN("show" << tab1, "");
540
541 const Args args = Args() << "tab" << tab1;
542
543 RUN(args << "add" << "TESTING2" << "TESTING1", "");
544 RUN(args << "read" << "0" << "1", "TESTING1\nTESTING2");
545
546 RUN("config"
547 << "activate_closes" << "false"
548 << "activate_focuses" << "false"
549 << "activate_pastes" << "false"
550 ,
551 "activate_closes=false\n"
552 "activate_focuses=false\n"
553 "activate_pastes=false\n"
554 );
555
556 // select second item and move to top
557 RUN("config" << "move" << "true", "true\n");
558 RUN(args << "selectItems" << "1", "true\n");
559 KEYS("ENTER");
560 RUN(args << "read" << "0" << "1", "TESTING2\nTESTING1");
561
562 WAIT_FOR_CLIPBOARD("TESTING2");
563 RUN("clipboard", "TESTING2");
564
565 // select without moving
566 RUN("config" << "move" << "0", "false\n");
567 RUN(args << "selectItems" << "1", "true\n");
568 KEYS("ENTER");
569 RUN(args << "read" << "0" << "1", "TESTING2\nTESTING1");
570
571 WAIT_FOR_CLIPBOARD("TESTING1");
572 RUN("clipboard", "TESTING1");
573 }
574
575 void ItemSyncTests::notes()
576 {
577 TestDir dir1(1);
578 const QString tab1 = testTab(1);
579 RUN(Args() << "show" << tab1, "");
580
581 const Args args = Args() << "separator" << ";" << "tab" << tab1;
582
583 RUN(args << "add" << "TEST1", "");
584
585 KEYS("CTRL+N" << ":TEST2" << "F2"
586 << "CTRL+N" << ":TEST3" << "F2");
587 RUN(args << "size", "3\n");
588 RUN(args << "read" << "0" << "1" << "2", "TEST3;TEST2;TEST1");
589
590 const auto files = dir1.files();
591 QVERIFY2( files.size() == 3, files.join(sep).toUtf8() );
592 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
593 QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
594 QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
595
596 KEYS("HOME" << "DOWN" << "SHIFT+F2" << ":NOTE1" << "F2");
597 RUN(args << "read" << mimeItemNotes << "0" << "1" << "2", ";NOTE1;");
598
599 // One new file for notes.
600 const QStringList files2 = dir1.files();
601 QCOMPARE( files2.size(), files.size() + 1 );
602 QString fileNote;
603 for (const auto &file : files2) {
604 if ( !files.contains(file) ) {
605 fileNote = file;
606 break;
607 }
608 }
609
610 // Read file with the notes.
611 FilePtr file = dir1.file(fileNote);
612 QVERIFY(file->open(QIODevice::ReadWrite));
613 QCOMPARE(file->readAll().data(), QByteArray("NOTE1").data());
614
615 // Modify notes.
616 file->write("+NOTE2");
617 file->close();
618
619 WAIT_ON_OUTPUT(args << "read" << mimeItemNotes << "0" << "1" << "2", ";NOTE1+NOTE2;");
620 RUN(args << "size", "3\n");
621
622 // Remove notes.
623 QVERIFY(file->remove());
624
625 WAIT_ON_OUTPUT(args << "read" << mimeItemNotes << "0" << "1" << "2", ";;");
626 RUN(args << "size", "3\n");
627 }
628
629 void ItemSyncTests::customFormats()
630 {
631 TestDir dir1(1);
632 const QString tab1 = testTab(1);
633 RUN(Args() << "show" << tab1, "");
634
635 const Args args = Args() << "separator" << ";" << "tab" << tab1;
636
637 createFile(dir1, "test1.xxx", "TEST1");
638
639 const auto mime1 = COPYQ_MIME_PREFIX "test-xxx";
640 const auto mime2 = COPYQ_MIME_PREFIX "test-zzz";
641
642 WAIT_ON_OUTPUT(args << "size", "1\n");
643 RUN(args << "read" << mime1 << "0", "TEST1");
644
645 createFile(dir1, "test2.yyy", "TEST2");
646
647 WAIT_ON_OUTPUT(args << "size", "2\n");
648 RUN(args << "read" << mime2 << "1", "TEST2");
649
650 createFile(dir1, "test3.zzz", "TEST3");
651
652 const auto script = QString(R"_(
653 print(' size: ')
654 print(size())
655 print(' basename: ')
656 print(read(plugins.itemsync.mimeBaseName, 0, 1, 2))
657 print(' mime1: ')
658 print(read('%1', 0, 1, 2))
659 print(' mime2: ')
660 print(read('%2', 0, 1, 2))
661 )_")
662 .arg(mime1)
663 .arg(mime2);
664
665 WAIT_ON_OUTPUT(
666 args << script,
667 " size: 3"
668 " basename: test1;test2;test3"
669 " mime1: TEST1;;"
670 " mime2: ;TEST2;TEST3"
671 );
672
673 // Remove
674 dir1.remove("test2.yyy");
675
676 WAIT_ON_OUTPUT(
677 args << script,
678 " size: 2"
679 " basename: test1;test3;"
680 " mime1: TEST1;;"
681 " mime2: ;TEST3;"
682 );
683
684 // Modify file
685 FilePtr file = dir1.file("test1.xxx");
686 QVERIFY(file->open(QIODevice::Append));
687 file->write("UPDATE");
688 file->close();
689
690 WAIT_ON_OUTPUT(
691 args << script,
692 " size: 2"
693 " basename: test1;test3;"
694 " mime1: TEST1UPDATE;;"
695 " mime2: ;TEST3;"
696 );
697
698 // Create item with custom data
699 RUN(args << "write" << mime2 << "NEW_ITEM", "");
700
701 RUN(args << "size", "3\n");
702
703 const auto files = dir1.files();
704 QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
705 const QString fileData = QString(files[0]).replace("txt", "zzz");
706 QVERIFY2( fileData.endsWith(".zzz"), fileData.toUtf8() );
707
708 // Check data
709 file = dir1.file(fileData);
710 QVERIFY(file->exists());
711 QVERIFY(file->open(QIODevice::ReadWrite));
712 QCOMPARE(file->readAll().data(), "NEW_ITEM");
713
714 // Modify data
715 file->write("+UPDATE");
716 file->close();
717
718 WAIT_ON_OUTPUT(
719 Args(args) << "read" << mime2 << "0" << "2" << mime1 << "1",
720 "NEW_ITEM+UPDATE;TEST3;TEST1UPDATE");
721 RUN(args << "size", "3\n");
722 }
723
724 void ItemSyncTests::getAbsoluteFilePath()
725 {
726 TestDir dir1(1);
727 const QString tab1 = testTab(1);
728 RUN(Args() << "show" << tab1, "");
729
730 const Args args = Args() << "separator" << ";" << "tab" << tab1;
731
732 const auto code = QString(
733 R"(
734 var path = plugins.itemsync.tabPaths["%1"]
735 var baseName = str(getItem(0)[plugins.itemsync.mimeBaseName])
736 var absoluteFilePath = Dir(path).absoluteFilePath(baseName)
737 print(absoluteFilePath)
738 )")
739 .arg(tab1);
740
741 createFile(dir1, "test1.txt", QByteArray());
742 WAIT_ON_OUTPUT(args << "size", "1\n");
743
744 RUN(args << code, dir1.filePath("test1"));
745 }
746
747 void ItemSyncTests::addItemsWhenFull()
748 {
749 TestDir dir1(1);
750 const QString tab1 = testTab(1);
751 const Args args = Args() << "separator" << ";" << "tab" << tab1;
752 RUN(args << "show" << tab1, "");
753
754 RUN("config" << "maxitems" << "2", "2\n");
755 RUN(args << "add" << "A" << "B", "");
756 RUN(args << "read" << "0" << "1" << "2", "B;A;");
757 RUN(args << "add" << "C", "");
758 RUN(args << "read" << "0" << "1" << "2", "C;B;");
759 }
760
761 void ItemSyncTests::addItemsWhenFullOmitDeletingNotOwned()
762 {
763 TestDir dir1(1);
764 const QString tab1 = testTab(1);
765 const Args args = Args() << "separator" << ";" << "tab" << tab1;
766 RUN(args << "show" << tab1, "");
767
768 RUN("config" << "maxitems" << "1", "1\n");
769
770 const QString testFileName1 = "test1.txt";
771 createFile(dir1, testFileName1, "NOT-OWNED");
772 WAIT_ON_OUTPUT(args << "size", "1\n");
773 RUN(args << "read" << "0" << "1", "NOT-OWNED;");
774
775 RUN(args << "add" << "A", "");
776 RUN(args << "read" << "0" << "1", "A;");
777 RUN(args << "remove" << "0", "");
778
779 WAIT_ON_OUTPUT(args << "size", "1\n");
780 RUN(args << "read" << "0" << "1", "NOT-OWNED;");
781
782 FilePtr f1(dir1.file(testFileName1));
783 QVERIFY(f1->exists());
784 }
785
786 void ItemSyncTests::moveOwnItemsSortsBaseNames()
787 {
788 TestDir dir1(1);
789 const QString tab1 = testTab(1);
790 RUN(Args() << "show" << tab1, "");
791
792 const Args args = Args() << "separator" << "," << "tab" << tab1;
793
794 const QString testScript = R"(
795 var baseNames = [];
796 for (var i = 0; i < 4; ++i) {
797 baseNames.push(str(read(plugins.itemsync.mimeBaseName, i)));
798 }
799 for (var i = 0; i < 3; ++i) {
800 var j = i + 1;
801 if (baseNames[i] <= baseNames[j]) {
802 print("Failed test: baseNames["+i+"] > baseNames["+j+"]\\n");
803 print(" Where baseNames["+i+"] = '" + baseNames[i] + "'\\n");
804 print(" baseNames["+j+"] = '" + baseNames[j] + "'\\n");
805 }
806 }
807 )";
808
809 RUN(args << "add" << "A" << "B" << "C" << "D", "");
810 RUN(args << "read(0,1,2,3)", "D,C,B,A");
811 RUN(args << testScript, "");
812
813 KEYS("END" << "CTRL+UP");
814 RUN(args << "read(0,1,2,3)", "D,C,A,B");
815 RUN(args << testScript, "");
816
817 KEYS("DOWN" << "CTRL+UP");
818 RUN(args << "read(0,1,2,3)", "D,C,B,A");
819 RUN(args << testScript, "");
820
821 KEYS("DOWN" << "SHIFT+UP" << "CTRL+UP");
822 RUN(args << "read(0,1,2,3)", "D,B,A,C");
823 RUN(args << testScript, "");
824
825 KEYS("CTRL+UP");
826 RUN(args << "read(0,1,2,3)", "B,A,D,C");
827 RUN(args << testScript, "");
828
829 KEYS("CTRL+DOWN");
830 RUN(args << "read(0,1,2,3)", "D,B,A,C");
831 RUN(args << testScript, "");
832
833 KEYS("END" << "CTRL+HOME");
834 RUN(args << "read(0,1,2,3)", "C,D,B,A");
835 RUN(args << testScript, "");
836
837 KEYS("END" << "UP" << "CTRL+HOME");
838 RUN(args << "read(0,1,2,3)", "B,C,D,A");
839 RUN(args << testScript, "");
840
841 KEYS("HOME" << "CTRL+END");
842 RUN(args << "read(0,1,2,3)", "C,D,A,B");
843 RUN(args << testScript, "");
844 }
845
846 void ItemSyncTests::moveOwnItemsKeepsLargeTextData()
847 {
848 TestDir dir1(1);
849 const QString tab1 = testTab(1);
850 RUN(Args() << "show" << tab1, "");
851
852 const Args args = Args() << "separator" << "," << "tab" << tab1;
853
854 const QString script = R"(
855 add('A'.repeat(4096), 'B'.repeat(3072));
856 read(0).length + ',' + read(1).length;
857 )";
858 RUN(args << script, "3072,4096\n");
859
860 // Reload so large data is represented by SyncDataFile values in memory.
861 RUN("unload" << tab1, "");
862 RUN(args << "read(0).length + ',' + read(1).length", "3072,4096\n");
863
864 RUN(args << "selectItems" << "1", "true\n");
865 KEYS("CTRL+UP");
866
867 RUN(args << "read(0).length + ',' + read(1).length", "4096,3072\n");
868 RUN(args << "getItem(0)[mimeText].length + ',' + getItem(1)[mimeText].length", "4096,3072\n");
869 }
870
871 void ItemSyncTests::avoidDuplicateItemsAddedFromClipboard()
872 {
873 TestDir dir1(1);
874 const QString tab1 = testTab(1);
875 RUN("show" << tab1, "");
876
877 const Args args = Args() << "separator" << "," << "tab" << tab1;
878
879 RUN("config" << "clipboard_tab" << tab1, tab1 + "\n");
880 WAIT_ON_OUTPUT("isClipboardMonitorRunning", "true\n");
881
882 TEST( m_test->setClipboard("one") );
883 WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,,,");
884
885 TEST( m_test->setClipboard("two") );
886 WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "two,one,,");
887
888 TEST( m_test->setClipboard("one") );
889 WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,two,,");
890 }
891
892 void ItemSyncTests::saveLargeItem()
893 {
894 const auto tab = testTab(1);
895 const auto args = Args("tab") << tab;
896
897 const auto script = R"(
898 write(0, [{
899 'text/plain': '1234567890'.repeat(10000),
900 'application/x-copyq-test-data': 'abcdefghijklmnopqrstuvwxyz'.repeat(10000),
901 }])
902 )";
903 RUN(args << script, "");
904
905 for (int i = 0; i < 2; ++i) {
906 RUN(args << "read(0).left(20)", "12345678901234567890");
907 RUN(args << "read(0).length", "100000\n");
908 RUN(args << "getItem(0)[mimeText].left(20)", "12345678901234567890");
909 RUN(args << "getItem(0)[mimeText].length", "100000\n");
910 RUN(args << "getItem(0)['application/x-copyq-test-data'].left(26)", "abcdefghijklmnopqrstuvwxyz");
911 RUN(args << "getItem(0)['application/x-copyq-test-data'].length", "260000\n");
912 RUN(args << "ItemSelection().selectAll().itemAtIndex(0)[mimeText].length", "100000\n");
913 RUN("unload" << tab, tab + "\n");
914 }
915
916 RUN("show" << tab, "");
917 KEYS(clipboardBrowserId << keyNameFor(QKeySequence::Copy));
918 WAIT_ON_OUTPUT("clipboard().left(20)", "12345678901234567890");
919 RUN("clipboard('application/x-copyq-test-data').left(26)", "abcdefghijklmnopqrstuvwxyz");
920 RUN("clipboard('application/x-copyq-test-data').length", "260000\n");
921
922 const auto tab2 = testTab(2);
923 const auto args2 = Args("tab") << tab2;
924 RUN("show" << tab2, "");
925 waitFor(waitMsPasteClipboard);
926 KEYS(clipboardBrowserId << keyNameFor(QKeySequence::Paste));
927 RUN(args2 << "read(0).left(20)", "12345678901234567890");
928 RUN(args2 << "read(0).length", "100000\n");
929 RUN(args << "getItem(0)['application/x-copyq-test-data'].left(26)", "abcdefghijklmnopqrstuvwxyz");
930 RUN(args << "getItem(0)['application/x-copyq-test-data'].length", "260000\n");
931 }
932
933 void ItemSyncTests::sortItemsSimple()
934 {
935 TestDir dir1(1);
936 const QString tab1 = testTab(1);
937 RUN(Args() << "show" << tab1, "");
938
939 const Args args = Args() << "tab" << tab1 << "separator" << ",";
940
941 RUN("config" << "maxitems" << "4", "4\n");
942 const QString initScript = R"(
943 add(1,2,0,3);
944 read(0,1,2,3);
945 )";
946 RUN(args << initScript, "3,0,2,1");
947 RUN(args << "size", "4\n");
948
949 const QString sortScript = R"(
950 let sel = ItemSelection().selectAll();
951 let items = sel.itemsFormat(mimeText);
952 serverLog("SORT BEGIN");
953 sel.sort((i, j) => items[i] < items[j]);
954 serverLog("SORT END");
955 read(0,1,2,3);
956 )";
957 RUN(args << sortScript, "0,1,2,3");
958 RUN(args << "size", "4\n");
959 }
960
961 void ItemSyncTests::sortItems()
962 {
963 TestDir dir1(1);
964 const QString tab1 = testTab(1);
965 RUN(Args() << "show" << tab1, "");
966
967 const Args args = Args() << "tab" << tab1 << "separator" << ",";
968
969 RUN("config" << "maxitems" << "4", "4\n");
970 const QString initScript = R"(
971 add(
972 {[mimeText]: 1, [mimeHtml]: "I"},
973 {[mimeText]: 2, [mimeHtml]: "II"},
974 {[mimeText]: 0, [mimeHtml]: "O"},
975 {[mimeText]: 3, [mimeHtml]: "III"},
976 );
977 read(0,1,2,3);
978 )";
979 RUN(args << initScript, "3,0,2,1");
980 RUN(args << "read(mimeHtml,0,1,2,3)", "III,O,II,I");
981 RUN(args << "size", "4\n");
982
983 const QString sortScript = R"(
984 let sel = ItemSelection().selectAll();
985 let items = sel.itemsFormat(mimeText);
986 serverLog("SORT BEGIN");
987 sel.sort((i, j) => items[i] < items[j]);
988 serverLog("SORT END");
989 read(0,1,2,3);
990 )";
991 RUN(args << sortScript, "0,1,2,3");
992 RUN(args << "read(mimeHtml,0,1,2,3)", "O,I,II,III");
993 RUN(args << "size", "4\n");
994 }
995
996 void ItemSyncTests::copyFiles()
997 {
998 TestDir dir1(1);
999 TestDir dir2(2);
1000 TestDir dir3(3);
1001 const QString tab1 = testTab(1);
1002 const QString tab2 = testTab(2);
1003 const QString tab3 = testTab(3);
1004 RUN("setCurrentTab" << tab1, "");
1005
1006 const QString getFirstItemFormats = R"(
1007 Object.keys(getItem(0))
1008 .filter(x => !x.startsWith('application/x-copyq-'))
1009 .join('\\n')
1010 )";
1011
1012 TEST(createFile(dir1, "test.txt", "TEST"));
1013 WAIT_ON_OUTPUT("tab" << tab1 << "size", "1\n");
1014 RUN("tab" << tab1 << "read(0)", "TEST");
1015 QCOMPARE(dir1.files().join(" "), "test.txt");
1016
1017 // Copying synced item from another tab, creates a new item with the same
1018 // content and file name as original - not overwriting existing files.
1019 KEYS(clipboardBrowserId << keyNameFor(QKeySequence::Copy));
1020 WAIT_FOR_CLIPBOARD("TEST");
1021 RUN("setCurrentTab" << tab2, "");
1022 KEYS(clipboardBrowserId << keyNameFor(QKeySequence::Paste));
1023 RUN("tab" << tab2 << "size", "1\n");
1024 RUN("tab" << tab2 << "read(0)", "TEST");
1025 #ifdef Q_OS_MAC
1026 RUN("tab" << tab2 << getFirstItemFormats, "public.utf16-plain-text\ntext/plain\n");
1027 QCOMPARE(dir2.files().join(" "), "test.txt test_copyq.dat");
1028 #else
1029 RUN("tab" << tab2 << getFirstItemFormats, "text/plain\n");
1030 QCOMPARE(dir2.files().join(" "), "test.txt");
1031 #endif
1032
1033 KEYS(clipboardBrowserId << keyNameFor(QKeySequence::Paste));
1034 RUN("tab" << tab2 << "size", "2\n");
1035 RUN("tab" << tab2 << "read(0)", "TEST");
1036 #ifdef Q_OS_MAC
1037 RUN("tab" << tab2 << getFirstItemFormats, "public.utf16-plain-text\ntext/plain\n");
1038 QCOMPARE(dir2.files().join(" "), "test-0001.txt test-0001_copyq.dat test.txt test_copyq.dat");
1039 #else
1040 RUN("tab" << tab2 << getFirstItemFormats, "text/plain\n");
1041 QCOMPARE(dir2.files().join(" "), "test-0001.txt test.txt");
1042 #endif
1043
1044 // Adding a local file URI, creates a new item with the file content and
1045 // the same file name as original - not overwriting existing files.
1046 const QString fileUri = QUrl::fromLocalFile(dir1.filePath("test.txt")).toString();
1047 RUN("setCurrentTab" << tab3, "");
1048 RUN("tab" << tab3 << "add({[mimeUriList]: '" + fileUri + "'})", "");
1049 RUN("tab" << tab3 << "size", "1\n");
1050 RUN("tab" << tab3 << getFirstItemFormats, "text/plain\n");
1051 QCOMPARE(dir3.files().join(" "), "test.txt");
1052
1053 RUN("tab" << tab3 << "add({[mimeUriList]: '" + fileUri + "'})", "");
1054 RUN("tab" << tab3 << "size", "2\n");
1055 RUN("tab" << tab3 << getFirstItemFormats, "text/plain\n");
1056 QCOMPARE(dir3.files().join(" "), "test-0001.txt test.txt");
1057 }
1058
1059 void ItemSyncTests::encryptionShouldNotAffectFiles()
1060 {
1061 TestDir dir1(1);
1062 const QString tab1 = testTab(1);
1063 RUN(Args() << "show" << tab1, "");
1064
1065 const Args args = Args() << "tab" << tab1;
1066 const QString inspect = R"(
1067 const sel = ItemSelection().selectAll();
1068 const items = sel.items();
1069 for (const i in items) {
1070 if (i > 0) { print(' ;; '); }
1071 print(i + ':');
1072 const item = items[i];
1073 for (const format in item) {
1074 if (!format.includes('itemsync') || format.includes('itemsync-basename')) {
1075 const fmt = format.replace('application/x-copyq-', '');
1076 print(' ' + fmt + ':' + item[format]);
1077 }
1078 }
1079 }
1080 )";
1081
1082 const QString file1 = "test1.txt";
1083 TEST(createFile(dir1, file1, "TEXT1"));
1084 QCOMPARE(dir1.files().join(sep), file1);
1085
1086 WAIT_ON_OUTPUT(args << "size", "1\n");
1087 RUN(args << inspect, "0: itemsync-basename:test1 text/plain:TEXT1");
1088
1089 const QString file2_yyy = "test2.yyy";
1090 const QString file2_txt = "test2.txt";
1091 TEST(createFile(dir1, file2_yyy, "YYY"));
1092 TEST(createFile(dir1, file2_txt, "TEXT2"));
1093
1094 WAIT_ON_OUTPUT(
1095 args << inspect, "0: itemsync-basename:test1 text/plain:TEXT1 ;; 1: itemsync-basename:test2 test-zzz:YYY text/plain:TEXT2");
1096
1097 const QStringList files({file1, file2_txt, file2_yyy});
1098 QCOMPARE(dir1.files().join(sep), files.join(sep));
1099
1100 RUN(Args() << "show" << "CLIPBOARD", "");
1101 RUN(Args() << "unload" << tab1, tab1 + "\n");
1102
1103 QCOMPARE(dir1.files().join(sep), files.join(sep));
1104
1105 RUN("config" << "encrypt_tabs" << "true", "true\n");
1106
1107 RUN(args << inspect,
1108 "0: itemsync-basename:test1 text/plain:TEXT1 ;; "
1109 "1: itemsync-basename:test2 test-zzz:YYY text/plain:TEXT2");
1110 QCOMPARE(dir1.files().join(sep), files.join(sep));
1111 }