Debian Codegraph @

src/tests/tests_script_commands.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "test_utils.h"
4 #include "tests.h"
5
6 #include "common/commandstatus.h"
7 #include "common/mimetypes.h"
8 #include "common/sleeptimer.h"
9
10 #include <QRegularExpression>
11
12 void CoreTests::scriptCommandLoaded()
13 {
14 const auto script = R"(
15 setCommands([{
16 isScript: true,
17 cmd: 'add("LOADED")'
18 }])
19 )";
20 RUN(script, "");
21 RUN("read(0)", "LOADED");
22 }
23
24 void CoreTests::scriptCommandAddFunction()
25 {
26 const auto script = R"(
27 setCommands([{
28 isScript: true,
29 cmd: 'global.test = function() { return "TEST"; }'
30 }])
31 )";
32 RUN(script, "");
33 RUN("test", "TEST\n");
34 }
35
36 void CoreTests::scriptCommandOverrideFunction()
37 {
38 const auto script = R"(
39 setCommands([{
40 isScript: true,
41 cmd: 'popup = function(msg) { return msg; }'
42 }])
43 )";
44 RUN(script, "");
45 RUN("popup" << "test" << "xxx", "test");
46 }
47
48 void CoreTests::scriptCommandEnhanceFunction()
49 {
50 const auto script = R"(
51 setCommands([
52 {
53 isScript: true,
54 cmd: 'var popup_ = popup; global.popup = function(msg) { popup_(msg); return msg + 1; }'
55 },
56 {
57 isScript: true,
58 cmd: 'var popup_ = popup; global.popup = function(msg) { return popup_(msg) + msg + 2; }'
59 },
60 ])
61 )";
62 RUN(script, "");
63 RUN("popup" << "test", "test1test2\n");
64 }
65
66 void CoreTests::scriptCommandEndingWithComment()
67 {
68 /*
69 With Qml scripts in Qt 5, it's not possible to execute script in new context,
70 only in the global one.
71
72 Workaround is to wrap the script properly in a new function:
73
74 function() {
75 %1
76 }()
77
78 (Unfortunately, it's still possible to escape the new context with a script injection.)
79 */
80
81 const auto script = R"(
82 setCommands([
83 {
84 isScript: true,
85 cmd: 'global.popup = function(msg) { return msg + 1; } // TEST'
86 },
87 ])
88 )";
89 RUN(script, "");
90 RUN("popup" << "test", "test1\n");
91 }
92
93 void CoreTests::scriptCommandWithError()
94 {
95 const auto script = R"(
96 setCommands([
97 {
98 isScript: true,
99 name: 'bad_script',
100 cmd: 'if (env("COPYQ_TEST_THROW") == "1") throw Error("BAD SCRIPT")'
101 },
102 ])
103 )";
104 RUN(script, "");
105 m_test->setEnv("COPYQ_TEST_THROW", "1");
106 RUN_EXPECT_ERROR_WITH_STDERR(
107 "", CommandError,
108 "ScriptError: BAD SCRIPT\n"
109 "\n"
110 "--- backtrace ---\n"
111 );
112 RUN_EXPECT_ERROR_WITH_STDERR(
113 "", CommandError,
114 "At line 1 in:\n"
115 "source@<bad_script>\n"
116 "--- end backtrace ---\n"
117 );
118 }
119
120 void CoreTests::scriptPaste()
121 {
122 const auto script = R"(
123 setCommands([
124 {
125 isScript: true,
126 cmd: 'global.paste = function() { add("PASTE") }'
127 },
128 ])
129 )";
130 RUN(script, "");
131 RUN("add(1)", "");
132 KEYS(clipboardBrowserId << "ENTER");
133 WAIT_ON_OUTPUT("read(0)", "PASTE");
134 }
135
136 void CoreTests::scriptOnTabSelected()
137 {
138 const auto script = R"(
139 setCommands([
140 {
141 isScript: true,
142 cmd: 'global.onTabSelected = function() { add(selectedTab()) }'
143 },
144 ])
145 )";
146 RUN(script, "");
147
148 const auto tab1 = testTab(1);
149 const auto tab2 = testTab(2);
150 RUN("show" << tab1, "");
151 WAIT_ON_OUTPUT("tab" << tab1 << "read(0)", tab1);
152 RUN("show" << tab2, "");
153 WAIT_ON_OUTPUT("tab" << tab2 << "read(0)", tab2);
154 }
155
156 void CoreTests::scriptOnItemsRemoved()
157 {
158 const auto script = R"(
159 setCommands([
160 {
161 isScript: true,
162 cmd: `
163 global.onItemsRemoved = function() {
164 items = ItemSelection().current().items();
165 tab(tab()[0]);
166 add("R0:" + str(items[0][mimeText]));
167 add("R1:" + str(items[1][mimeText]));
168 }
169 `
170 },
171 ])
172 )";
173 RUN(script, "");
174 const auto tab1 = testTab(1);
175 RUN("tab" << tab1 << "add(3,2,1,0)", "");
176 RUN("tab" << tab1 << "remove(1,2)", "");
177 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2,)", "R1:2,R0:1,");
178
179 // Cancel item removal
180 const auto script2 = R"(
181 setCommands([
182 {
183 isScript: true,
184 cmd: "global.onItemsRemoved = global.fail",
185 },
186 ])
187 )";
188 RUN(script2, "");
189 const auto tab2 = testTab(2);
190 RUN("tab" << tab2 << "add(3,2,1,0)", "");
191 RUN("tab" << tab2 << "remove(1,2)", "");
192 RUN("tab" << tab2 << "separator" << "," << "read(0,1,2,3,4)", "0,1,2,3,");
193
194 // Avoid crash if the tab itself is removed while removing items
195 const auto script3 = R"(
196 setCommands([
197 {
198 isScript: true,
199 cmd: `
200 global.onItemsRemoved = function() {
201 removeTab(selectedTab())
202 }
203 `
204 },
205 ])
206 )";
207 RUN(script3, "");
208 const auto tab3 = testTab(3);
209 RUN("tab" << tab3 << "add(3,2,1,0)", "");
210 RUN("tab" << tab3 << "remove(1,2)", "");
211 RUN("tab" << tab3 << "separator" << "," << "read(0,1,2,3,4)", ",,,,");
212 }
213
214 void CoreTests::scriptOnItemsAdded()
215 {
216 const auto script = R"(
217 setCommands([
218 {
219 isScript: true,
220 cmd: `
221 global.onItemsAdded = function() {
222 sel = ItemSelection().current();
223 items = sel.items();
224 for (i = 0; i < items.length; ++i)
225 items[i][mimeText] = "A:" + str(items[i][mimeText])
226 sel.setItems(items);
227 }
228 `
229 },
230 ])
231 )";
232 RUN(script, "");
233 const auto tab1 = testTab(1);
234 RUN("tab" << tab1 << "add(1,0)", "");
235 WAIT_ON_OUTPUT("tab" << tab1 << "separator" << "," << "read(0,1,2)", "A:0,A:1,");
236 }
237
238 void CoreTests::scriptOnItemsChanged()
239 {
240 const auto script = R"(
241 setCommands([
242 {
243 isScript: true,
244 cmd: `
245 global.onItemsChanged = function() {
246 if (selectedTab() == tab()[0]) abort();
247 items = ItemSelection().current().items();
248 tab(tab()[0]);
249 add("C:" + str(items[0][mimeText]));
250 }
251 `
252 },
253 ])
254 )";
255 RUN(script, "");
256 const auto tab1 = testTab(1);
257 RUN("tab" << tab1 << "add(0)", "");
258 RUN("tab" << tab1 << "change(0, mimeText, 'A')", "");
259 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2)", "C:A,,");
260 RUN("tab" << tab1 << "change(0, mimeText, 'B')", "");
261 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2)", "C:B,C:A,");
262 }
263
264 void CoreTests::scriptOnItemsLoaded()
265 {
266 const auto script = R"(
267 setCommands([
268 {
269 isScript: true,
270 cmd: `
271 global.onItemsLoaded = function() {
272 if (selectedTab() == tab()[0]) abort();
273 tab(tab()[0]);
274 add(selectedTab());
275 }
276 `
277 },
278 ])
279 )";
280 RUN(script, "");
281
282 const auto tab1 = testTab(1);
283 RUN("show" << tab1, "");
284 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2)", tab1 + ",,");
285
286 const auto tab2 = testTab(2);
287 RUN("show" << tab2, "");
288 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2)", tab2 + "," + tab1 + ",");
289 }
290
291 void CoreTests::scriptEventMaxRecursion()
292 {
293 const auto script = R"(
294 setCommands([
295 {
296 isScript: true,
297 cmd: `global.onItemsRemoved = function() {
298 const toRemove = str(selectedItemData(0)[mimeText]);
299 const newItem = (toRemove == "X") ? "A" : ("WRONG:" + toRemove);
300 add(newItem);
301 remove(size()-1);
302 }`
303 },
304 ])
305 )";
306 RUN(script, "");
307 m_test->ignoreErrors(QRegularExpression("Event handler maximum recursion reached"));
308 RUN("add('X'); remove(0)", "");
309 WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2,3,4,5,6,7,8,9,10)", "A,A,A,A,A,A,A,A,A,A,");
310 waitFor(100);
311 RUN("separator" << "," << "read(0,1,2,3,4,5,6,7,8,9,10)", "A,A,A,A,A,A,A,A,A,A,");
312 }
313
314 void CoreTests::scriptSlowCollectOverrides()
315 {
316 const auto script = R"(
317 setCommands([
318 {
319 isScript: true,
320 cmd: 'global.onTabSelected = function() { add(selectedTab()) }'
321 },
322 {
323 isScript: true,
324 cmd: `
325 var collectOverrides_ = global.collectOverrides;
326 global.collectOverrides = function() { sleep(1000); collectOverrides_() }
327 `
328 },
329 ])
330 )";
331 RUN(script, "");
332
333 const auto tab1 = testTab(1);
334 RUN("show" << tab1, "");
335 WAIT_ON_OUTPUT("tab" << tab1 << "read(0)", tab1);
336 }
337
338 void CoreTests::displayCommand()
339 {
340 const auto testMime = COPYQ_MIME_PREFIX "test";
341 const auto script = QString(R"(
342 setCommands([{
343 display: true,
344 input: '%1',
345 cmd: 'copyq:'
346 + 'text = str(data(mimeText));'
347 + 'currentTab = str(data(mimeCurrentTab));'
348 + 'add(currentTab + "/" + text);'
349 }])
350 )").arg(testMime);
351
352 RUN(script, "");
353
354 RUN("write" << "0" << testMime << "" << mimeText << "a", "");
355 WAIT_ON_OUTPUT(
356 "read(0,1,2)",
357 QString::fromLatin1("%1/a\na\n")
358 .arg(clipboardTabName)
359 .toUtf8() );
360
361 RUN("write" << "0" << testMime << "" << mimeText << "b", "");
362 WAIT_ON_OUTPUT(
363 "read(0,1,2,3,4)",
364 QString::fromLatin1("%1/b\nb\n%1/a\na\n")
365 .arg(clipboardTabName)
366 .toUtf8() );
367 }
368
369 void CoreTests::displayCommandForMenu()
370 {
371 const auto tab = testTab(1);
372 const auto args = Args("tab") << tab << "separator" << ",";
373 const auto script = QString(R"(
374 setCommands([{
375 display: true,
376 cmd: 'copyq:'
377 + 'currentTab = str(data(mimeCurrentTab));'
378 + 'inMenu = str(data(mimeDisplayItemInMenu));'
379 + 'if (inMenu != "1" || currentTab != "%1") abort();'
380 + 'text = str(data(mimeText));'
381 + 'setData(mimeText, "display:" + text);'
382 + 'setData(mimeIcon, String.fromCharCode(0xF328));'
383 + 'setData("application/x-copyq-item-tag", "TAG");'
384 + 'tab(tab()[0]);'
385 + 'old = str(read(0));'
386 + 'add(old + "|" + text);'
387 }])
388 )").arg(tab);
389
390 RUN("config" << "tray_tab" << tab, tab + "\n");
391 RUN("config" << "tray_tab_is_current" << "false", "false\n");
392 RUN(script, "");
393
394 RUN(args << "add(1,2,3,4,5)", "");
395 RUN("menu", "");
396 WAIT_ON_OUTPUT("read(0)", "|5|4|3|2|1");
397 }