Debian Codegraph @

src/tests/itempinnedtests.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "itempinnedtests.h"
4
5 #include "tests/test_utils.h"
6
7 #include "common/commandstatus.h"
8
9 #include <QRegularExpression>
10
11 ItemPinnedTests::ItemPinnedTests(const TestInterfacePtr &test, QObject *parent)
12 : QObject(parent)
13 , m_test(test)
14 {
15 setProperty("CopyQ_test_id", QStringLiteral("itempinned"));
16 }
17
18 void ItemPinnedTests::initTestCase()
19 {
20 TEST(m_test->initTestCase());
21 }
22
23 void ItemPinnedTests::cleanupTestCase()
24 {
25 TEST(m_test->cleanupTestCase());
26 }
27
28 void ItemPinnedTests::init()
29 {
30 TEST(m_test->init());
31 }
32
33 void ItemPinnedTests::cleanup()
34 {
35 TEST( m_test->cleanup() );
36 }
37
38 void ItemPinnedTests::isPinned()
39 {
40 RUN("add" << "b" << "a", "");
41 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
42 RUN("-e" << "plugins.itempinned.isPinned(1)", "false\n");
43 }
44
45 void ItemPinnedTests::pin()
46 {
47 RUN("add" << "b" << "a", "");
48 RUN("-e" << "plugins.itempinned.pin(1)", "");
49 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
50 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
51
52 RUN("-e" << "plugins.itempinned.pin(0)", "");
53 RUN("-e" << "plugins.itempinned.isPinned(0)", "true\n");
54 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
55 }
56
57 void ItemPinnedTests::pinMultiple()
58 {
59 RUN("add" << "d" << "c" << "b" << "a", "");
60 RUN("-e" << "plugins.itempinned.pin(1, 2)", "");
61 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
62 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
63 RUN("-e" << "plugins.itempinned.isPinned(2)", "true\n");
64 RUN("-e" << "plugins.itempinned.isPinned(3)", "false\n");
65
66 RUN("-e" << "plugins.itempinned.pin(1, 2 ,3)", "");
67 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
68 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
69 RUN("-e" << "plugins.itempinned.isPinned(2)", "true\n");
70 RUN("-e" << "plugins.itempinned.isPinned(3)", "true\n");
71 }
72
73 void ItemPinnedTests::unpin()
74 {
75 RUN("add" << "b" << "a", "");
76 RUN("-e" << "plugins.itempinned.pin(0, 1)", "");
77 RUN("-e" << "plugins.itempinned.isPinned(0)", "true\n");
78 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
79
80 RUN("-e" << "plugins.itempinned.unpin(0)", "");
81 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
82 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
83
84 RUN("-e" << "plugins.itempinned.unpin(1)", "");
85 RUN("-e" << "plugins.itempinned.isPinned(0)", "false\n");
86 RUN("-e" << "plugins.itempinned.isPinned(1)", "false\n");
87 }
88
89 void ItemPinnedTests::unpinMultiple()
90 {
91 RUN("add" << "d" << "c" << "b" << "a", "");
92 RUN("-e" << "plugins.itempinned.pin(0, 1, 2, 3)", "");
93 RUN("-e" << "plugins.itempinned.isPinned(0)", "true\n");
94 RUN("-e" << "plugins.itempinned.isPinned(1)", "true\n");
95 RUN("-e" << "plugins.itempinned.isPinned(2)", "true\n");
96 RUN("-e" << "plugins.itempinned.isPinned(3)", "true\n");
97
98 RUN("-e" << "plugins.itempinned.unpin(1, 2)", "");
99 RUN("-e" << "plugins.itempinned.isPinned(0)", "true\n");
100 RUN("-e" << "plugins.itempinned.isPinned(1)", "false\n");
101 RUN("-e" << "plugins.itempinned.isPinned(2)", "false\n");
102 RUN("-e" << "plugins.itempinned.isPinned(3)", "true\n");
103
104 RUN("-e" << "plugins.itempinned.unpin(2, 3)", "");
105 RUN("-e" << "plugins.itempinned.isPinned(0)", "true\n");
106 RUN("-e" << "plugins.itempinned.isPinned(1)", "false\n");
107 RUN("-e" << "plugins.itempinned.isPinned(2)", "false\n");
108 RUN("-e" << "plugins.itempinned.isPinned(3)", "false\n");
109 }
110
111 void ItemPinnedTests::removePinnedThrows()
112 {
113 RUN("add" << "b" << "a", "");
114 RUN("-e" << "plugins.itempinned.pin(0, 1)", "");
115
116 RUN_EXPECT_ERROR("remove" << "0" << "1", CommandException);
117 RUN("separator" << " " << "read" << "0" << "1", "a b");
118 }
119
120 void ItemPinnedTests::pinToRow()
121 {
122 const auto read = Args() << "separator" << " " << "read";
123
124 RUN("add" << "a", "");
125 RUN("-e" << "plugins.itempinned.pin(0)", "");
126 RUN("add" << "b", "");
127 RUN(read << "0" << "1", "a b");
128
129 RUN("add" << "c", "");
130 RUN(read << "0" << "1" << "2", "a c b");
131
132 RUN("-e" << "plugins.itempinned.pin(1)", "");
133 RUN("add" << "d", "");
134 RUN(read << "0" << "1" << "2" << "3", "a c d b");
135
136 RUN("-e" << "plugins.itempinned.pin(2)", "");
137 RUN("-e" << "plugins.itempinned.unpin(1); remove(1)", "");
138 RUN(read << "0" << "1" << "2", "a b d");
139 }
140
141 void ItemPinnedTests::fullTab()
142 {
143 RUN("config" << "maxitems" << "3", "3\n");
144 RUN("add" << "c" << "b" << "a", "");
145 RUN("-e" << "plugins.itempinned.pin(0,1,2)", "");
146
147 // Tab is full and no items can be removed.
148 m_test->ignoreErrors(
149 QRegularExpression(R"(Cannot add new items\. Tab "CLIPBOARD" reached the maximum number of items\.)") );
150 RUN_EXPECT_ERROR("add" << "X", CommandException);
151 RUN_EXPECT_ERROR("write" << "1" << "text/plain" << "X", CommandException);
152 RUN("separator" << " " << "read" << "0" << "1" << "2", "a b c");
153 RUN("size", "3\n");
154 }
155
156 void ItemPinnedTests::keepPinnedIfMaxItemsChanges()
157 {
158 const auto read = Args() << "separator" << " " << "read";
159
160 RUN("config" << "maxitems" << "3", "3\n");
161 RUN("add" << "pin2" << "no-pin" << "pin1", "");
162 RUN("-e" << "plugins.itempinned.pin(0,2)", "");
163
164 RUN("config" << "maxitems" << "2", "2\n");
165 RUN(read << "0" << "1" << "2", "pin1 pin2 ");
166
167 RUN("config" << "maxitems" << "1", "1\n");
168 RUN(read << "0" << "1" << "2", "pin1 pin2 ");
169 }
170
171 void ItemPinnedTests::noDuplicateWhenAllPinned()
172 {
173 const auto read = Args() << "separator" << " " << "read";
174
175 RUN("config" << "move" << "true", "true\n");
176
177 // Add items and pin them all.
178 RUN("add" << "c" << "b" << "a", "");
179 RUN("-e" << "plugins.itempinned.pin(0,1,2)", "");
180 RUN(read << "0" << "1" << "2", "a b c");
181 RUN("size", "3\n");
182
183 // Activate second pinned item by pressing ENTER.
184 // This sets the clipboard but must not move or duplicate the item.
185 RUN("show", "");
186 KEYS(clipboardBrowserId << "DOWN" << "ENTER");
187 WAIT_FOR_CLIPBOARD("b");
188
189 // Wait for provideClipboard to take clipboard ownership before
190 // setting clipboard externally — otherwise provideClipboard may
191 // overwrite the external clipboard data.
192 WAIT_ON_OUTPUT("str(clipboard(mimeOwner)).includes('provideClipboard')", "true\n");
193
194 // Use a sentinel clipboard change to ensure all prior
195 // async clipboard processing (provideClipboard exit) completed.
196 TEST(m_test->setClipboard("d"));
197 WAIT_FOR_CLIPBOARD("d");
198 WAIT_ON_OUTPUT("read" << "0" << "1" << "2" << "3", "a\nb\nc\nd");
199
200 // Pinned items must stay in place; only the sentinel "d" was added.
201 RUN("size", "4\n");
202 RUN(read << "0" << "1" << "2" << "3", "a b c d");
203
204 // Set clipboard to same text as a pinned item externally.
205 // This triggers addUnique() which must detect the duplicate
206 // even though the pinned item's data contains mimePinned
207 // but the clipboard data does not.
208 TEST(m_test->setClipboard("a"));
209 WAIT_FOR_CLIPBOARD("a");
210
211 // Another sentinel to confirm the above was fully processed.
212 TEST(m_test->setClipboard("e"));
213 WAIT_FOR_CLIPBOARD("e");
214 WAIT_ON_OUTPUT("read" << "0" << "1" << "2" << "3" << "4", "a\nb\nc\ne\nd");
215
216 // Verify: 5 items total (a, b, c pinned + e, d new), no duplicates.
217 // "e" appears before "d" because new unpinned items are inserted
218 // at the top and shifted past pinned items.
219 RUN("size", "5\n");
220 RUN(read << "0" << "1" << "2" << "3" << "4", "a b c e d");
221 }