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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "itemsynctests.h"
#include "common/commandstatus.h"
#include "common/mimetypes.h"
#include "common/sleeptimer.h"
#include "tests/test_utils.h"
#include <QDir>
#include <QFile>
#include <QUrl>
#include <memory>
namespace {
using FilePtr = std::shared_ptr<QFile>;
const char sep[] = " ;; ";
const auto confirmRemoveDialogId =
"focus::QPushButton'Yes'<qt_msgbox_buttonbox:QDialogButtonBox<:QMessageBox"
"'Do you really want to remove items and associated files\\\\?'";
class TestDir final {
public:
explicit TestDir(int i, bool createPath = true)
: m_dir(ItemSyncTests::testDir(i))
{
clear();
if (createPath)
create();
}
~TestDir()
{
clear();
}
void clear()
{
if (isValid())
m_dir.removeRecursively();
}
void create()
{
if ( !m_dir.mkpath(".") )
Q_ASSERT(false);
}
bool isValid() const
{
return m_dir.exists();
}
QStringList files() const
{
QStringList files = m_dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name);
files.removeOne(QStringLiteral(".copyq_lock"));
return files;
}
FilePtr file(const QString &fileName) const
{
return std::make_shared<QFile>(filePath(fileName));
}
QString filePath(const QString &fileName) const
{
return m_dir.absoluteFilePath(fileName);
}
bool remove(const QString &fileName)
{
return QFile::remove(filePath(fileName));
}
private:
Q_DISABLE_COPY(TestDir)
QDir m_dir;
};
QByteArray createFile(const TestDir &dir, const QString &fileName, const QByteArray &content)
{
FilePtr file(dir.file(fileName));
if ( file->exists() )
return "File already exists!";
if ( !file->open(QIODevice::WriteOnly) )
return "Cannot open file!";
if ( file->write(content) == -1 )
return "Cannot write to file!";
file->close();
return "";
}
} // namespace
ItemSyncTests::ItemSyncTests(const TestInterfacePtr &test, QObject *parent)
: QObject(parent)
, m_test(test)
{
m_test->setEnv("COPYQ_SYNC_UPDATE_INTERVAL_MS", "100");
}
QString ItemSyncTests::testTab(int i)
{
return ::testTab(i);
}
QString ItemSyncTests::testDir(int i)
{
return QDir::tempPath() + "/copyq_test_dirs/itemsync_" + QString::number(i);
}
void ItemSyncTests::initTestCase()
{
TEST(m_test->initTestCase());
}
void ItemSyncTests::cleanupTestCase()
{
TEST(m_test->cleanupTestCase());
}
void ItemSyncTests::init()
{
TEST(m_test->init());
QDir tmpDir(QDir::cleanPath(testDir(0) + "/.."));
if ( tmpDir.exists() )
QVERIFY(tmpDir.removeRecursively());
}
void ItemSyncTests::cleanup()
{
TEST( m_test->cleanup() );
}
void ItemSyncTests::createRemoveTestDir()
{
TestDir dir1(1);
TestDir dir2(2);
QVERIFY(dir1.isValid());
QCOMPARE(dir1.files().join(sep), QString());
QVERIFY(dir2.isValid());
QCOMPARE(dir2.files().join(sep), QString());
const QString testFileName1 = "test1.txt";
FilePtr f1(dir1.file(testFileName1));
QVERIFY(!f1->exists());
QVERIFY(f1->open(QIODevice::WriteOnly));
f1->close();
QCOMPARE(dir1.files().join(sep), testFileName1);
dir1.clear();
QVERIFY(!dir1.isValid());
QVERIFY(!f1->exists());
QVERIFY(dir2.isValid());
dir2.clear();
QVERIFY(!dir1.isValid());
QVERIFY(!dir2.isValid());
dir1.create();
QVERIFY(dir1.isValid());
QCOMPARE(dir2.files().join(sep), QString());
}
void ItemSyncTests::itemsToFiles()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "tab" << tab1;
RUN(args << "add" << "A" << "B" << "C", "");
RUN(args << "read" << "0" << "1" << "2", "C\nB\nA");
RUN(args << "size", "3\n");
const auto files = dir1.files();
QVERIFY2( files.size() == 3, files.join(sep).toUtf8() );
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
}
void ItemSyncTests::filesToItems()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args()
<< "tab" << tab1
<< "read.apply(this, [...Array(size()).keys()])";
RUN(args, "");
QStringList expectedItems;
const QByteArray text1 = "TEST1";
TEST(createFile(dir1, "test1.txt", text1));
// Hidden files (starting with dot) should be ignored
const QByteArray hiddenFile = "HIDDEN";
TEST(createFile(dir1, ".hidden.txt", hiddenFile));
const QByteArray text2 = "TEST2";
TEST(createFile(dir1, "test2.txt", text2));
// Non-owned files are sorted by name
expectedItems = QStringList{text1, text2};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray nested1_1 = "nested 1 item 1";
QVERIFY(QDir(dir1.filePath("nested1")).mkpath("."));
TEST(createFile(dir1, "nested1/test1.txt", nested1_1));
// More nested items come later
expectedItems = QStringList{text1, text2, nested1_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray text3 = "TEST3";
TEST(createFile(dir1, "test3.txt", text3));
expectedItems = QStringList{text1, text2, text3, nested1_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray text0 = "TEST0";
TEST(createFile(dir1, "test0.txt", text0));
expectedItems = QStringList{text0, text1, text2, text3, nested1_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray nested2_1 = "nested 2 item 1";
QVERIFY(QDir(dir1.filePath("nested2")).mkpath("."));
TEST(createFile(dir1, "nested2/test1.txt", nested2_1));
expectedItems = QStringList{text0, text1, text2, text3, nested1_1, nested2_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray nested1_0 = "nested 1 item 0";
TEST(createFile(dir1, "nested1/test0.txt", nested1_0));
expectedItems = QStringList{
text0, text1, text2, text3, nested1_0, nested1_1, nested2_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray nested2_0 = "nested 2 item 0";
TEST(createFile(dir1, "nested2/test0.txt", nested2_0));
expectedItems = QStringList{
text0, text1, text2, text3, nested1_0, nested1_1, nested2_0, nested2_1};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
const QByteArray nested1_1_0 = "nested 1 nested 1 item 0";
QVERIFY(QDir(dir1.filePath("nested1/nested1")).mkpath("."));
TEST(createFile(dir1, "nested1/nested1/test0.txt", nested1_1_0));
expectedItems = QStringList{
text0, text1, text2, text3, nested1_0, nested1_1, nested2_0, nested2_1, nested1_1_0};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
SKIP_ON_ENV("COPYQ_TESTS_NO_SYMLINKS");
// Ignore symlink to already added directory
QFile dirLink(dir1.filePath("nested1"));
if (!dirLink.link(dir1.filePath("nested3")))
QFAIL("Failed to create directory link: " + dirLink.errorString().toUtf8());
// Allow other symlinks
QFile fileLink(dir1.filePath("test1.txt"));
if (!fileLink.link(dir1.filePath("test4.txt")))
QFAIL("Failed to create file link: " + fileLink.errorString().toUtf8());
expectedItems = QStringList{
text0, text1, text2, text3, text1, nested1_0, nested1_1, nested2_0, nested2_1, nested1_1_0};
WAIT_ON_OUTPUT(args, expectedItems.join('\n'));
}
void ItemSyncTests::removeOwnItems()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
RUN(args << "add" << "A" << "B" << "C" << "D", "");
const auto files = dir1.files();
QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
// Move to test tab and select second and third item.
RUN("setCurrentTab" << tab1, "");
RUN(args << "selectItems" << "1" << "2", "true\n");
RUN(args << "testSelected", tab1.toUtf8() + " 2 1 2\n");
// Remove selected items.
RUN(args << "keys" << m_test->shortcutToRemove(), "");
RUN(args << "read" << "0" << "1" << "2" << "3", "D,A,,");
QCOMPARE( dir1.files().join(sep),
files[0]
+ sep + files[3]
);
// Removing own items works from script.
RUN(args << "remove" << "1", "");
RUN(args << "read" << "0" << "1" << "2" << "3", "D,,,");
QCOMPARE( dir1.files().join(sep), files[3] );
}
void ItemSyncTests::removeNotOwnedItems()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
const QString fileA = "test1.txt";
const QString fileB = "test2.txt";
const QString fileC = "test3.txt";
const QString fileD = "test4.txt";
TEST(createFile(dir1, fileA, "A"));
WAIT_ON_OUTPUT(args << "size", "1\n");
TEST(createFile(dir1, fileB, "B"));
WAIT_ON_OUTPUT(args << "size", "2\n");
TEST(createFile(dir1, fileC, "C"));
WAIT_ON_OUTPUT(args << "size", "3\n");
TEST(createFile(dir1, fileD, "D"));
WAIT_ON_OUTPUT(args << "size", "4\n");
QCOMPARE( dir1.files().join(sep),
fileA
+ sep + fileB
+ sep + fileC
+ sep + fileD
);
// Move to test tab and select second and third item.
RUN("setCurrentTab" << tab1, "");
RUN(args << "selectItems" << "1" << "2", "true\n");
RUN(args << "testSelected", tab1.toUtf8() + " 2 1 2\n");
// Accept the "Remove Items?" dialog.
RUN(args << "keys" << clipboardBrowserId << m_test->shortcutToRemove() << confirmRemoveDialogId, "");
RUN(args << "keys" << confirmRemoveDialogId << "ENTER" << clipboardBrowserId, "");
RUN(args << "read" << "0" << "1" << "2" << "3", "A,D,,");
QCOMPARE( dir1.files().join(sep),
fileA
+ sep + fileD
);
// Removing not owned items from script doesn't work.
RUN_EXPECT_ERROR(args << "remove" << "1", CommandException);
QCOMPARE( dir1.files().join(sep),
fileA
+ sep + fileD
);
}
void ItemSyncTests::removeNotOwnedItemsCancel()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
const QString fileA = "test1.txt";
const QString fileB = "test2.txt";
TEST(createFile(dir1, fileA, "A"));
WAIT_ON_OUTPUT(args << "size", "1\n");
TEST(createFile(dir1, fileB, "B"));
WAIT_ON_OUTPUT(args << "size", "2\n");
QCOMPARE(dir1.files().join(sep), fileA + sep + fileB );
// Move to test tab and select second item.
RUN("setCurrentTab" << tab1, "");
RUN(args << "selectItems" << "1", "true\n");
RUN(args << "testSelected", tab1.toUtf8() + " 1 1\n");
// Don't accept the "Remove Items?" dialog.
RUN(args << "keys" << clipboardBrowserId << m_test->shortcutToRemove() << confirmRemoveDialogId, "");
RUN(args << "testSelected", tab1.toUtf8() + " 1 1\n");
RUN(args << "keys" << confirmRemoveDialogId << "ESCAPE" << clipboardBrowserId, "");
RUN(args << "read" << "0" << "1", "A,B");
QCOMPARE( dir1.files().join(sep), fileA + sep + fileB );
RUN(args << "testSelected", tab1.toUtf8() + " 1 1\n");
}
void ItemSyncTests::removeFiles()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
RUN(args << "add" << "A" << "B" << "C" << "D", "");
const auto files = dir1.files();
QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
FilePtr file = dir1.file(files[2]);
QVERIFY(file->open(QIODevice::ReadOnly));
QCOMPARE(file->readAll().data(), QByteArray("C").data());
file->remove();
WAIT_ON_OUTPUT(args << "size", "3\n");
RUN(args << "read" << "0" << "1" << "2", "D,B,A");
dir1.file(files[1])->remove();
dir1.file(files[0])->remove();
WAIT_ON_OUTPUT(args << "size", "1\n");
RUN(args << "read" << "0", "D");
}
void ItemSyncTests::modifyItems()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
RUN(args << "add" << "A" << "B" << "C" << "D", "");
const auto files = dir1.files();
QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
FilePtr file = dir1.file(files[2]);
QVERIFY(file->open(QIODevice::ReadOnly));
QCOMPARE(file->readAll().data(), QByteArray("C").data());
file->close();
RUN(args << "keys" << "HOME" << "DOWN" << "F2" << ":XXX" << "F2", "");
RUN(args << "size", "4\n");
RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,XXX,B,A,");
file = dir1.file(files[2]);
QVERIFY(file->open(QIODevice::ReadOnly));
QCOMPARE(file->readAll().data(), QByteArray("XXX").data());
file->close();
const auto script = R"(
setCommands([{
name: 'Modify current item',
inMenu: true,
shortcuts: ['Ctrl+F1'],
cmd: `
copyq: item = selectedItemsData()[0]
item[mimeText] = "ZZZ"
setSelectedItemData(0, item)
`
}])
)";
RUN(script, "");
RUN(args << "keys" << "HOME" << "DOWN" << "CTRL+F1", "");
WAIT_ON_OUTPUT(args << "read" << "1", "ZZZ");
RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,ZZZ,B,A,");
RUN(args << "unload" << tab1, "");
RUN(args << "read" << "0" << "1" << "2" << "3" << "4", "D,ZZZ,B,A,");
file = dir1.file(files[2]);
QVERIFY(file->open(QIODevice::ReadOnly));
QCOMPARE(file->readAll().data(), QByteArray("ZZZ").data());
file->close();
}
void ItemSyncTests::modifyFiles()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
RUN(args << "add" << "A" << "B" << "C" << "D", "");
const auto files = dir1.files();
QVERIFY2( files.size() == 4, files.join(sep).toUtf8() );
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
QVERIFY2( files[3].startsWith("copyq_"), files[3].toUtf8() );
FilePtr file = dir1.file(files[2]);
QVERIFY(file->open(QIODevice::ReadWrite));
QCOMPARE(file->readAll().data(), QByteArray("C").data());
file->write("X");
file->close();
WAIT_ON_OUTPUT(args << "read" << "0" << "1" << "2" << "3", "D,CX,B,A");
RUN(args << "size", "4\n");
}
void ItemSyncTests::itemToClipboard()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN("show" << tab1, "");
const Args args = Args() << "tab" << tab1;
RUN(args << "add" << "TESTING2" << "TESTING1", "");
RUN(args << "read" << "0" << "1", "TESTING1\nTESTING2");
RUN("config"
<< "activate_closes" << "false"
<< "activate_focuses" << "false"
<< "activate_pastes" << "false"
,
"activate_closes=false\n"
"activate_focuses=false\n"
"activate_pastes=false\n"
);
// select second item and move to top
RUN("config" << "move" << "true", "true\n");
RUN(args << "selectItems" << "1", "true\n");
RUN(args << "keys" << "ENTER", "");
RUN(args << "read" << "0" << "1", "TESTING2\nTESTING1");
WAIT_FOR_CLIPBOARD("TESTING2");
RUN("clipboard", "TESTING2");
// select without moving
RUN("config" << "move" << "0", "false\n");
RUN(args << "selectItems" << "1", "true\n");
RUN(args << "keys" << "ENTER", "");
RUN(args << "read" << "0" << "1", "TESTING2\nTESTING1");
WAIT_FOR_CLIPBOARD("TESTING1");
RUN("clipboard", "TESTING1");
}
void ItemSyncTests::notes()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << ";" << "tab" << tab1;
RUN(args << "add" << "TEST1", "");
RUN(args << "keys"
<< "CTRL+N" << ":TEST2" << "F2"
<< "CTRL+N" << ":TEST3" << "F2", "");
RUN(args << "size", "3\n");
RUN(args << "read" << "0" << "1" << "2", "TEST3;TEST2;TEST1");
const auto files = dir1.files();
QVERIFY2( files.size() == 3, files.join(sep).toUtf8() );
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
QVERIFY2( files[1].startsWith("copyq_"), files[1].toUtf8() );
QVERIFY2( files[2].startsWith("copyq_"), files[2].toUtf8() );
RUN(args << "keys" << "HOME" << "DOWN" << "SHIFT+F2" << ":NOTE1" << "F2", "");
RUN(args << "read" << mimeItemNotes << "0" << "1" << "2", ";NOTE1;");
// One new file for notes.
const QStringList files2 = dir1.files();
QCOMPARE( files2.size(), files.size() + 1 );
QString fileNote;
for (const auto &file : files2) {
if ( !files.contains(file) ) {
fileNote = file;
break;
}
}
// Read file with the notes.
FilePtr file = dir1.file(fileNote);
QVERIFY(file->open(QIODevice::ReadWrite));
QCOMPARE(file->readAll().data(), QByteArray("NOTE1").data());
// Modify notes.
file->write("+NOTE2");
file->close();
WAIT_ON_OUTPUT(args << "read" << mimeItemNotes << "0" << "1" << "2", ";NOTE1+NOTE2;");
RUN(args << "size", "3\n");
// Remove notes.
QVERIFY(file->remove());
WAIT_ON_OUTPUT(args << "read" << mimeItemNotes << "0" << "1" << "2", ";;");
RUN(args << "size", "3\n");
}
void ItemSyncTests::customFormats()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << ";" << "tab" << tab1;
createFile(dir1, "test1.xxx", "TEST1");
const auto mime1 = COPYQ_MIME_PREFIX "test-xxx";
const auto mime2 = COPYQ_MIME_PREFIX "test-zzz";
WAIT_ON_OUTPUT(args << "size", "1\n");
RUN(args << "read" << mime1 << "0", "TEST1");
createFile(dir1, "test2.yyy", "TEST2");
WAIT_ON_OUTPUT(args << "size", "2\n");
RUN(args << "read" << mime2 << "1", "TEST2");
createFile(dir1, "test3.zzz", "TEST3");
const auto script = QString(R"_(
print(' size: ')
print(size())
print(' basename: ')
print(read(plugins.itemsync.mimeBaseName, 0, 1, 2))
print(' mime1: ')
print(read('%1', 0, 1, 2))
print(' mime2: ')
print(read('%2', 0, 1, 2))
)_")
.arg(mime1)
.arg(mime2);
WAIT_ON_OUTPUT(
args << script,
" size: 3"
" basename: test1;test2;test3"
" mime1: TEST1;;"
" mime2: ;TEST2;TEST3"
);
// Remove
dir1.remove("test2.yyy");
WAIT_ON_OUTPUT(
args << script,
" size: 2"
" basename: test1;test3;"
" mime1: TEST1;;"
" mime2: ;TEST3;"
);
// Modify file
FilePtr file = dir1.file("test1.xxx");
QVERIFY(file->open(QIODevice::Append));
file->write("UPDATE");
file->close();
WAIT_ON_OUTPUT(
args << script,
" size: 2"
" basename: test1;test3;"
" mime1: TEST1UPDATE;;"
" mime2: ;TEST3;"
);
// Create item with custom data
RUN(args << "write" << mime2 << "NEW_ITEM", "");
RUN(args << "size", "3\n");
const auto files = dir1.files();
QVERIFY2( files[0].startsWith("copyq_"), files[0].toUtf8() );
const QString fileData = QString(files[0]).replace("txt", "zzz");
QVERIFY2( fileData.endsWith(".zzz"), fileData.toUtf8() );
// Check data
file = dir1.file(fileData);
QVERIFY(file->exists());
QVERIFY(file->open(QIODevice::ReadWrite));
QCOMPARE(file->readAll().data(), "NEW_ITEM");
// Modify data
file->write("+UPDATE");
file->close();
WAIT_ON_OUTPUT(
Args(args) << "read" << mime2 << "0" << "2" << mime1 << "1",
"NEW_ITEM+UPDATE;TEST3;TEST1UPDATE");
RUN(args << "size", "3\n");
}
void ItemSyncTests::getAbsoluteFilePath()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << ";" << "tab" << tab1;
const auto code = QString(
R"(
var path = plugins.itemsync.tabPaths["%1"]
var baseName = str(getItem(0)[plugins.itemsync.mimeBaseName])
var absoluteFilePath = Dir(path).absoluteFilePath(baseName)
print(absoluteFilePath)
)")
.arg(tab1);
createFile(dir1, "test1.txt", QByteArray());
WAIT_ON_OUTPUT(args << "size", "1\n");
RUN(args << code, dir1.filePath("test1"));
}
void ItemSyncTests::addItemsWhenFull()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
const Args args = Args() << "separator" << ";" << "tab" << tab1;
RUN(args << "show" << tab1, "");
RUN("config" << "maxitems" << "2", "2\n");
RUN(args << "add" << "A" << "B", "");
RUN(args << "read" << "0" << "1" << "2", "B;A;");
RUN(args << "add" << "C", "");
RUN(args << "read" << "0" << "1" << "2", "C;B;");
}
void ItemSyncTests::addItemsWhenFullOmitDeletingNotOwned()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
const Args args = Args() << "separator" << ";" << "tab" << tab1;
RUN(args << "show" << tab1, "");
RUN("config" << "maxitems" << "1", "1\n");
const QString testFileName1 = "test1.txt";
createFile(dir1, testFileName1, "NOT-OWNED");
WAIT_ON_OUTPUT(args << "size", "1\n");
RUN(args << "read" << "0" << "1", "NOT-OWNED;");
RUN(args << "add" << "A", "");
RUN(args << "read" << "0" << "1", "A;");
RUN(args << "remove" << "0", "");
WAIT_ON_OUTPUT(args << "size", "1\n");
RUN(args << "read" << "0" << "1", "NOT-OWNED;");
FilePtr f1(dir1.file(testFileName1));
QVERIFY(f1->exists());
}
void ItemSyncTests::moveOwnItemsSortsBaseNames()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
const QString testScript = R"(
var baseNames = [];
for (var i = 0; i < 4; ++i) {
baseNames.push(str(read(plugins.itemsync.mimeBaseName, i)));
}
for (var i = 0; i < 3; ++i) {
var j = i + 1;
if (baseNames[i] <= baseNames[j]) {
print("Failed test: baseNames["+i+"] > baseNames["+j+"]\\n");
print(" Where baseNames["+i+"] = '" + baseNames[i] + "'\\n");
print(" baseNames["+j+"] = '" + baseNames[j] + "'\\n");
}
}
)";
RUN(args << "add" << "A" << "B" << "C" << "D", "");
RUN(args << "read(0,1,2,3)", "D,C,B,A");
RUN(args << testScript, "");
RUN(args << "keys" << "END" << "CTRL+UP", "");
RUN(args << "read(0,1,2,3)", "D,C,A,B");
RUN(args << testScript, "");
RUN(args << "keys" << "DOWN" << "CTRL+UP", "");
RUN(args << "read(0,1,2,3)", "D,C,B,A");
RUN(args << testScript, "");
RUN(args << "keys" << "DOWN" << "SHIFT+UP" << "CTRL+UP", "");
RUN(args << "read(0,1,2,3)", "D,B,A,C");
RUN(args << testScript, "");
RUN(args << "keys" << "CTRL+UP", "");
RUN(args << "read(0,1,2,3)", "B,A,D,C");
RUN(args << testScript, "");
RUN(args << "keys" << "CTRL+DOWN", "");
RUN(args << "read(0,1,2,3)", "D,B,A,C");
RUN(args << testScript, "");
RUN(args << "keys" << "END" << "CTRL+HOME", "");
RUN(args << "read(0,1,2,3)", "C,D,B,A");
RUN(args << testScript, "");
RUN(args << "keys" << "END" << "UP" << "CTRL+HOME", "");
RUN(args << "read(0,1,2,3)", "B,C,D,A");
RUN(args << testScript, "");
RUN(args << "keys" << "HOME" << "CTRL+END", "");
RUN(args << "read(0,1,2,3)", "C,D,A,B");
RUN(args << testScript, "");
}
void ItemSyncTests::avoidDuplicateItemsAddedFromClipboard()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN("show" << tab1, "");
const Args args = Args() << "separator" << "," << "tab" << tab1;
RUN("config" << "clipboard_tab" << tab1, tab1 + "\n");
WAIT_ON_OUTPUT("isClipboardMonitorRunning", "true\n");
TEST( m_test->setClipboard("one") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,,,");
TEST( m_test->setClipboard("two") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "two,one,,");
TEST( m_test->setClipboard("one") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,two,,");
}
void ItemSyncTests::saveLargeItem()
{
const auto tab = testTab(1);
const auto args = Args("tab") << tab;
const auto script = R"(
write(0, [{
'text/plain': '1234567890'.repeat(10000),
'application/x-copyq-test-data': 'abcdefghijklmnopqrstuvwxyz'.repeat(10000),
}])
)";
RUN(args << script, "");
for (int i = 0; i < 2; ++i) {
RUN(args << "read(0).left(20)", "12345678901234567890");
RUN(args << "read(0).length", "100000\n");
RUN(args << "getItem(0)[mimeText].left(20)", "12345678901234567890");
RUN(args << "getItem(0)[mimeText].length", "100000\n");
RUN(args << "getItem(0)['application/x-copyq-test-data'].left(26)", "abcdefghijklmnopqrstuvwxyz");
RUN(args << "getItem(0)['application/x-copyq-test-data'].length", "260000\n");
RUN(args << "ItemSelection().selectAll().itemAtIndex(0)[mimeText].length", "100000\n");
RUN("unload" << tab, tab + "\n");
}
RUN("show" << tab, "");
RUN("keys" << clipboardBrowserId << keyNameFor(QKeySequence::Copy), "");
WAIT_ON_OUTPUT("clipboard().left(20)", "12345678901234567890");
RUN("clipboard('application/x-copyq-test-data').left(26)", "abcdefghijklmnopqrstuvwxyz");
RUN("clipboard('application/x-copyq-test-data').length", "260000\n");
const auto tab2 = testTab(2);
const auto args2 = Args("tab") << tab2;
RUN("show" << tab2, "");
waitFor(waitMsPasteClipboard);
RUN("keys" << clipboardBrowserId << keyNameFor(QKeySequence::Paste), "");
RUN(args2 << "read(0).left(20)", "12345678901234567890");
RUN(args2 << "read(0).length", "100000\n");
RUN(args << "getItem(0)['application/x-copyq-test-data'].left(26)", "abcdefghijklmnopqrstuvwxyz");
RUN(args << "getItem(0)['application/x-copyq-test-data'].length", "260000\n");
}
void ItemSyncTests::sortItemsSimple()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "tab" << tab1 << "separator" << ",";
RUN("config" << "maxitems" << "4", "4\n");
const QString initScript = R"(
add(1,2,0,3);
read(0,1,2,3);
)";
RUN(args << initScript, "3,0,2,1");
RUN(args << "size", "4\n");
const QString sortScript = R"(
let sel = ItemSelection().selectAll();
let items = sel.itemsFormat(mimeText);
serverLog("SORT BEGIN");
sel.sort((i, j) => items[i] < items[j]);
serverLog("SORT END");
read(0,1,2,3);
)";
RUN(args << sortScript, "0,1,2,3");
RUN(args << "size", "4\n");
}
void ItemSyncTests::sortItems()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN(Args() << "show" << tab1, "");
const Args args = Args() << "tab" << tab1 << "separator" << ",";
RUN("config" << "maxitems" << "4", "4\n");
const QString initScript = R"(
add(
{[mimeText]: 1, [mimeHtml]: "I"},
{[mimeText]: 2, [mimeHtml]: "II"},
{[mimeText]: 0, [mimeHtml]: "O"},
{[mimeText]: 3, [mimeHtml]: "III"},
);
read(0,1,2,3);
)";
RUN(args << initScript, "3,0,2,1");
RUN(args << "read(mimeHtml,0,1,2,3)", "III,O,II,I");
RUN(args << "size", "4\n");
const QString sortScript = R"(
let sel = ItemSelection().selectAll();
let items = sel.itemsFormat(mimeText);
serverLog("SORT BEGIN");
sel.sort((i, j) => items[i] < items[j]);
serverLog("SORT END");
read(0,1,2,3);
)";
RUN(args << sortScript, "0,1,2,3");
RUN(args << "read(mimeHtml,0,1,2,3)", "O,I,II,III");
RUN(args << "size", "4\n");
}
void ItemSyncTests::copyFiles()
{
TestDir dir1(1);
TestDir dir2(2);
TestDir dir3(3);
const QString tab1 = testTab(1);
const QString tab2 = testTab(2);
const QString tab3 = testTab(3);
RUN("setCurrentTab" << tab1, "");
const QString getFirstItemFormats = R"(
Object.keys(getItem(0))
.filter(x => !x.startsWith('application/x-copyq-'))
.join('\\n')
)";
TEST(createFile(dir1, "test.txt", "TEST"));
WAIT_ON_OUTPUT("tab" << tab1 << "size", "1\n");
RUN("tab" << tab1 << "read(0)", "TEST");
QCOMPARE(dir1.files().join(" "), "test.txt");
// Copying synced item from another tab, creates a new item with the same
// content and file name as original - not overwriting existing files.
RUN("keys" << clipboardBrowserId << keyNameFor(QKeySequence::Copy), "");
WAIT_FOR_CLIPBOARD("TEST");
RUN("setCurrentTab" << tab2, "");
RUN("keys" << clipboardBrowserId << keyNameFor(QKeySequence::Paste), "");
RUN("tab" << tab2 << "size", "1\n");
RUN("tab" << tab2 << "read(0)", "TEST");
#ifdef Q_OS_MAC
RUN("tab" << tab2 << getFirstItemFormats, "public.utf16-plain-text\ntext/plain\n");
QCOMPARE(dir2.files().join(" "), "test.txt test_copyq.dat");
#else
RUN("tab" << tab2 << getFirstItemFormats, "text/plain\n");
QCOMPARE(dir2.files().join(" "), "test.txt");
#endif
RUN("keys" << clipboardBrowserId << keyNameFor(QKeySequence::Paste), "");
RUN("tab" << tab2 << "size", "2\n");
RUN("tab" << tab2 << "read(0)", "TEST");
#ifdef Q_OS_MAC
RUN("tab" << tab2 << getFirstItemFormats, "public.utf16-plain-text\ntext/plain\n");
QCOMPARE(dir2.files().join(" "), "test-0001.txt test-0001_copyq.dat test.txt test_copyq.dat");
#else
RUN("tab" << tab2 << getFirstItemFormats, "text/plain\n");
QCOMPARE(dir2.files().join(" "), "test-0001.txt test.txt");
#endif
// Adding a local file URI, creates a new item with the file content and
// the same file name as original - not overwriting existing files.
const QString fileUri = QUrl::fromLocalFile(dir1.filePath("test.txt")).toString();
RUN("setCurrentTab" << tab3, "");
RUN("tab" << tab3 << "add({[mimeUriList]: '" + fileUri + "'})", "");
RUN("tab" << tab3 << "size", "1\n");
RUN("tab" << tab3 << getFirstItemFormats, "text/plain\n");
QCOMPARE(dir3.files().join(" "), "test.txt");
RUN("tab" << tab3 << "add({[mimeUriList]: '" + fileUri + "'})", "");
RUN("tab" << tab3 << "size", "2\n");
RUN("tab" << tab3 << getFirstItemFormats, "text/plain\n");
QCOMPARE(dir3.files().join(" "), "test-0001.txt test.txt");
}
|