| 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/sleeptimer.h" |
| 8 |
|
| 9 |
void CoreTests::shortcutCommand() |
| 10 |
{ |
| 11 |
RUN("setCommands([{name: 'test', inMenu: true, shortcuts: ['Ctrl+F1'], cmd: 'copyq add OK'}])", ""); |
| 12 |
KEYS("CTRL+F1"); |
| 13 |
WAIT_ON_OUTPUT("read" << "0", "OK"); |
| 14 |
} |
| 15 |
|
| 16 |
void CoreTests::shortcutCommandOverrideEnter() |
| 17 |
{ |
| 18 |
RUN("setCommands([{name: 'test', inMenu: true, shortcuts: ['Enter'], cmd: 'copyq add OK'}])", ""); |
| 19 |
KEYS("ENTER" << "ENTER"); |
| 20 |
WAIT_ON_OUTPUT("read" << "0" << "1", "OK\nOK"); |
| 21 |
} |
| 22 |
|
| 23 |
void CoreTests::shortcutCommandMatchInput() |
| 24 |
{ |
| 25 |
// Activate only one of the two actions depending on input MIME format. |
| 26 |
const auto script = R"( |
| 27 |
function cmd(name) { |
| 28 |
var format = 'application/x-copyq-' + name |
| 29 |
return { |
| 30 |
name: name, |
| 31 |
inMenu: true, |
| 32 |
shortcuts: ['Ctrl+F1'], |
| 33 |
input: format, |
| 34 |
cmd: 'copyq add ' + name |
| 35 |
} |
| 36 |
} |
| 37 |
setCommands([ cmd('test1'), cmd('test2') ]) |
| 38 |
)"; |
| 39 |
RUN(script, ""); |
| 40 |
|
| 41 |
RUN("write" << "application/x-copyq-test1" << "", ""); |
| 42 |
KEYS("CTRL+F1"); |
| 43 |
WAIT_ON_OUTPUT("read" << "0", "test1"); |
| 44 |
RUN("tab" << QString(clipboardTabName) << "size", "2\n"); |
| 45 |
|
| 46 |
RUN("write" << "application/x-copyq-test2" << "", ""); |
| 47 |
KEYS("CTRL+F1"); |
| 48 |
WAIT_ON_OUTPUT("read" << "0", "test2"); |
| 49 |
RUN("tab" << QString(clipboardTabName) << "size", "4\n"); |
| 50 |
} |
| 51 |
|
| 52 |
void CoreTests::shortcutCommandMatchCmd() |
| 53 |
{ |
| 54 |
const auto tab = testTab(1); |
| 55 |
const Args args = Args("tab") << tab; |
| 56 |
|
| 57 |
// Activate only one of the three actions depending on exit code of command which matches input MIME format. |
| 58 |
const auto script = R"( |
| 59 |
function cmd(name) { |
| 60 |
var format = 'application/x-copyq-' + name |
| 61 |
return { |
| 62 |
name: name, |
| 63 |
inMenu: true, |
| 64 |
shortcuts: ['Ctrl+F1'], |
| 65 |
matchCmd: 'copyq: str(data("' + format + '")) || fail()', |
| 66 |
cmd: 'copyq tab )" + tab + R"( add ' + name |
| 67 |
} |
| 68 |
} |
| 69 |
setCommands([ cmd('test1'), cmd('test2') ]) |
| 70 |
)"; |
| 71 |
RUN(script, ""); |
| 72 |
|
| 73 |
RUN("show" << tab, ""); |
| 74 |
|
| 75 |
RUN(args << "write" << "application/x-copyq-test1" << "1", ""); |
| 76 |
WAIT_ON_OUTPUT(args << "plugins.itemtests.keys('Ctrl+F1'); read(0)", "test1"); |
| 77 |
|
| 78 |
RUN(args << "write" << "application/x-copyq-test2" << "2", ""); |
| 79 |
WAIT_ON_OUTPUT(args << "plugins.itemtests.keys('Ctrl+F1'); read(0)", "test2"); |
| 80 |
} |
| 81 |
|
| 82 |
void CoreTests::shortcutCommandSelectedItemData() |
| 83 |
{ |
| 84 |
const auto tab1 = testTab(1); |
| 85 |
const auto script = R"( |
| 86 |
setCommands([{ |
| 87 |
name: 'Move Second Selected Item to Other Tab', |
| 88 |
inMenu: true, |
| 89 |
shortcuts: ['Ctrl+F1'], |
| 90 |
output: 'text/plain', |
| 91 |
outputTab: ')" + tab1 + R"(', |
| 92 |
cmd: 'copyq: selectedItemData(1)["text/plain"]' |
| 93 |
}]) |
| 94 |
)"; |
| 95 |
RUN(script, ""); |
| 96 |
|
| 97 |
RUN("add" << "C" << "B" << "A", ""); |
| 98 |
RUN("selectItems" << "1" << "2", "true\n"); |
| 99 |
KEYS("CTRL+F1"); |
| 100 |
WAIT_ON_OUTPUT("tab" << tab1 << "read" << "0", "C"); |
| 101 |
} |
| 102 |
|
| 103 |
void CoreTests::shortcutCommandSetSelectedItemData() |
| 104 |
{ |
| 105 |
const auto script = R"( |
| 106 |
setCommands([{ |
| 107 |
name: 'Set Data for Second Selected Item', |
| 108 |
inMenu: true, |
| 109 |
shortcuts: ['Ctrl+F1'], |
| 110 |
cmd: 'copyq: setSelectedItemData(1, {"text/plain": "X", "DATA": "TEST"})' |
| 111 |
}]) |
| 112 |
)"; |
| 113 |
RUN(script, ""); |
| 114 |
|
| 115 |
RUN("add" << "C" << "B" << "A", ""); |
| 116 |
RUN("selectItems" << "1" << "2", "true\n"); |
| 117 |
KEYS("CTRL+F1"); |
| 118 |
WAIT_ON_OUTPUT("read" << "2", "X"); |
| 119 |
RUN("read" << "DATA" << "2", "TEST"); |
| 120 |
} |
| 121 |
|
| 122 |
void CoreTests::shortcutCommandSelectedItemsData() |
| 123 |
{ |
| 124 |
const auto tab1 = testTab(1); |
| 125 |
const auto script = R"( |
| 126 |
setCommands([{ |
| 127 |
name: 'Concatenate Selected Items to Other Tab', |
| 128 |
inMenu: true, |
| 129 |
shortcuts: ['Ctrl+F1'], |
| 130 |
output: 'text/plain', |
| 131 |
outputTab: ')" + tab1 + R"(', |
| 132 |
cmd: 'copyq: d = selectedItemsData();' |
| 133 |
+ 'for (i in d) { print(d[i][mimeText]); print(",") }' |
| 134 |
}]) |
| 135 |
)"; |
| 136 |
RUN(script, ""); |
| 137 |
|
| 138 |
RUN("add" << "C" << "B" << "A", ""); |
| 139 |
RUN("selectItems" << "1" << "2", "true\n"); |
| 140 |
KEYS("CTRL+F1"); |
| 141 |
WAIT_ON_OUTPUT("tab" << tab1 << "read" << "0", "B,C,"); |
| 142 |
} |
| 143 |
|
| 144 |
void CoreTests::shortcutCommandSetSelectedItemsData() |
| 145 |
{ |
| 146 |
const auto script = R"( |
| 147 |
setCommands([{ |
| 148 |
name: 'Set Data for Second Selected Item', |
| 149 |
inMenu: true, |
| 150 |
shortcuts: ['Ctrl+F1'], |
| 151 |
cmd: 'copyq: setSelectedItemsData([{"text/plain": "X"}, {"text/plain": "Y"}])' |
| 152 |
}]) |
| 153 |
)"; |
| 154 |
RUN(script, ""); |
| 155 |
|
| 156 |
RUN("add" << "C" << "B" << "A", ""); |
| 157 |
RUN("selectItems" << "1" << "2", "true\n"); |
| 158 |
KEYS("CTRL+F1"); |
| 159 |
WAIT_ON_OUTPUT("read" << "0" << "1" << "2", "A\nX\nY"); |
| 160 |
} |
| 161 |
|
| 162 |
void CoreTests::shortcutCommandSelectedAndCurrent() |
| 163 |
{ |
| 164 |
const auto script = R"( |
| 165 |
setCommands([{ |
| 166 |
name: 'Set Data for Second Selected Item', |
| 167 |
inMenu: true, |
| 168 |
shortcuts: ['Ctrl+F1'], |
| 169 |
output: 'text/plain', |
| 170 |
cmd: 'copyq: print(selectedItems() + "|" + currentItem() + "|" + selectedTab())' |
| 171 |
}]) |
| 172 |
)"; |
| 173 |
RUN(script, ""); |
| 174 |
|
| 175 |
const auto tab1 = testTab(1); |
| 176 |
RUN("tab" << tab1 << "add" << "C" << "B" << "A", ""); |
| 177 |
|
| 178 |
RUN("tab" << tab1 << "setCurrentTab" << tab1 << "selectItems" << "1" << "2", "true\n"); |
| 179 |
KEYS("CTRL+F1"); |
| 180 |
WAIT_ON_OUTPUT("tab" << tab1 << "read(0)", "1,2|2|" + tab1.toUtf8()); |
| 181 |
} |
| 182 |
|
| 183 |
void CoreTests::shortcutCommandMoveSelected() |
| 184 |
{ |
| 185 |
const QString script = R"( |
| 186 |
setCommands([{ |
| 187 |
name: 'Move Selected', |
| 188 |
inMenu: true, |
| 189 |
shortcuts: ['Ctrl+F1'], |
| 190 |
output: 'text/plain', |
| 191 |
cmd: 'copyq: move(%1); settings("done", 1)' |
| 192 |
}]) |
| 193 |
)"; |
| 194 |
RUN(script.arg(1), ""); |
| 195 |
|
| 196 |
const Args args = Args("tab") << testTab(1) << "separator" << ","; |
| 197 |
RUN("setCurrentTab" << testTab(1), ""); |
| 198 |
RUN(args << "add" << "4" << "3" << "2" << "1", ""); |
| 199 |
|
| 200 |
#define MOVE_SELECTED(EXPECTED_ITEMS) \ |
| 201 |
RUN("settings" << "done" << "0" << "plugins.itemtests.keys" << "CTRL+F1", ""); \ |
| 202 |
WAIT_ON_OUTPUT("settings" << "done", "1\n"); \ |
| 203 |
RUN(args << "read(0,1,2,3,4)", EXPECTED_ITEMS) |
| 204 |
|
| 205 |
RUN(args << "selectItems" << "1" << "2", "true\n"); |
| 206 |
MOVE_SELECTED("1,2,3,4,"); |
| 207 |
|
| 208 |
RUN(args << "selectItems" << "2" << "3", "true\n"); |
| 209 |
MOVE_SELECTED("1,3,4,2,"); |
| 210 |
|
| 211 |
RUN(script.arg(5), ""); |
| 212 |
MOVE_SELECTED("1,3,4,2,"); |
| 213 |
|
| 214 |
RUN(script.arg(-1), ""); |
| 215 |
MOVE_SELECTED("1,3,4,2,"); |
| 216 |
|
| 217 |
RUN(script.arg(4), ""); |
| 218 |
MOVE_SELECTED("1,2,3,4,"); |
| 219 |
|
| 220 |
RUN(script.arg(0), ""); |
| 221 |
MOVE_SELECTED("3,4,1,2,"); |
| 222 |
|
| 223 |
#undef MOVE_SELECTED |
| 224 |
} |
| 225 |
|
| 226 |
void CoreTests::automaticCommandIgnore() |
| 227 |
{ |
| 228 |
const auto script = R"( |
| 229 |
setCommands([ |
| 230 |
{ automatic: true, cmd: 'copyq ignore; copyq add OK' }, |
| 231 |
{ automatic: true, cmd: 'copyq add "SHOULD NOT BE EXECUTED"' } |
| 232 |
]) |
| 233 |
)"; |
| 234 |
RUN(script, ""); |
| 235 |
WAIT_ON_OUTPUT("commands().length", "2\n"); |
| 236 |
|
| 237 |
TEST( m_test->setClipboard("SHOULD BE IGNORED 1") ); |
| 238 |
WAIT_ON_OUTPUT("read" << "0", "OK"); |
| 239 |
RUN("separator" << "," << "read" << "0" << "1" << "2", "OK,,"); |
| 240 |
RUN("size", "1\n"); |
| 241 |
|
| 242 |
TEST( m_test->setClipboard("SHOULD BE IGNORED 2") ); |
| 243 |
WAIT_ON_OUTPUT("size", "2\n"); |
| 244 |
|
| 245 |
RUN("separator" << "," << "read" << "0" << "1" << "2", "OK,OK,"); |
| 246 |
} |
| 247 |
|
| 248 |
void CoreTests::automaticCommandRemove() |
| 249 |
{ |
| 250 |
const auto script = R"( |
| 251 |
setCommands([ |
| 252 |
{ automatic: true, remove: true, cmd: 'copyq add OK' }, |
| 253 |
{ automatic: true, cmd: 'copyq add "SHOULD NOT BE EXECUTED"' } |
| 254 |
]) |
| 255 |
)"; |
| 256 |
RUN(script, ""); |
| 257 |
WAIT_ON_OUTPUT("commands().length", "2\n"); |
| 258 |
|
| 259 |
TEST( m_test->setClipboard("SHOULD BE IGNORED 1") ); |
| 260 |
WAIT_ON_OUTPUT("read" << "0", "OK"); |
| 261 |
|
| 262 |
TEST( m_test->setClipboard("SHOULD BE IGNORED 2") ); |
| 263 |
WAIT_ON_OUTPUT("size", "2\n"); |
| 264 |
|
| 265 |
RUN("separator" << "," << "read" << "0" << "1" << "2", "OK,OK,"); |
| 266 |
} |
| 267 |
|
| 268 |
void CoreTests::automaticCommandInput() |
| 269 |
{ |
| 270 |
const auto script = R"( |
| 271 |
setCommands([ |
| 272 |
{ automatic: true, input: 'DATA', cmd: 'copyq: setData("DATA", "???")' }, |
| 273 |
{ automatic: true, input: 'text/plain', cmd: 'copyq: setData("text/plain", "OK")' }, |
| 274 |
]) |
| 275 |
)"; |
| 276 |
RUN(script, ""); |
| 277 |
WAIT_ON_OUTPUT("commands().length", "2\n"); |
| 278 |
|
| 279 |
TEST( m_test->setClipboard("SHOULD BE CHANGED") ); |
| 280 |
WAIT_ON_OUTPUT("read" << "0", "OK"); |
| 281 |
RUN("read" << "DATA" << "0", ""); |
| 282 |
} |
| 283 |
|
| 284 |
void CoreTests::automaticCommandRegExp() |
| 285 |
{ |
| 286 |
const auto script = R"( |
| 287 |
setCommands([ |
| 288 |
{ automatic: true, re: 'SHOULD BE (CHANGED)$', cmd: 'copyq: setData(mimeText, arguments[1])' }, |
| 289 |
{ automatic: true, cmd: 'copyq: setData("DATA", "DONE")' }, |
| 290 |
]) |
| 291 |
)"; |
| 292 |
RUN(script, ""); |
| 293 |
WAIT_ON_OUTPUT("commands().length", "2\n"); |
| 294 |
|
| 295 |
TEST( m_test->setClipboard("SHOULD BE CHANGED") ); |
| 296 |
WAIT_ON_OUTPUT("read" << "DATA" << "0", "DONE"); |
| 297 |
RUN("read" << "0", "CHANGED"); |
| 298 |
RUN("remove" << "0", ""); |
| 299 |
|
| 300 |
TEST( m_test->setClipboard("SHOULD NOT BE CHANGED") ); |
| 301 |
WAIT_ON_OUTPUT("read" << "DATA" << "0", "DONE"); |
| 302 |
RUN("read" << "0", "SHOULD NOT BE CHANGED"); |
| 303 |
} |
| 304 |
|
| 305 |
void CoreTests::automaticCommandSetData() |
| 306 |
{ |
| 307 |
RUN("add" << "TEST", ""); |
| 308 |
RUN("read" << "0", "TEST"); |
| 309 |
|
| 310 |
const auto script = R"( |
| 311 |
setCommands([{automatic: true, cmd: 'copyq: setData("text/plain", "OK")'}]) |
| 312 |
)"; |
| 313 |
RUN(script, ""); |
| 314 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 315 |
|
| 316 |
TEST( m_test->setClipboard("SHOULD BE CHANGED") ); |
| 317 |
WAIT_ON_OUTPUT("read" << "0", "OK"); |
| 318 |
|
| 319 |
// Ensure that previous item is not modified |
| 320 |
// See: https://github.com/hluk/CopyQ/issues/3324 |
| 321 |
RUN("read" << "1", "TEST"); |
| 322 |
} |
| 323 |
|
| 324 |
void CoreTests::automaticCommandOutputTab() |
| 325 |
{ |
| 326 |
const auto tab1 = testTab(1); |
| 327 |
const auto script = R"( |
| 328 |
var tab1 = ')" + tab1 + R"(' |
| 329 |
setCommands([{automatic: true, cmd: 'copyq: setData(mimeOutputTab, "' + tab1 + '")'}]) |
| 330 |
)"; |
| 331 |
RUN(script, ""); |
| 332 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 333 |
|
| 334 |
TEST( m_test->setClipboard("TEST") ); |
| 335 |
WAIT_ON_OUTPUT("tab" << tab1 << "read" << "0", "TEST"); |
| 336 |
RUN("tab" << QString(clipboardTabName) << "size", "0\n"); |
| 337 |
} |
| 338 |
|
| 339 |
void CoreTests::automaticCommandNoOutputTab() |
| 340 |
{ |
| 341 |
const auto script = R"( |
| 342 |
setCommands([{automatic: true, cmd: 'copyq: removeData(mimeOutputTab)'}]) |
| 343 |
)"; |
| 344 |
RUN(script, ""); |
| 345 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 346 |
|
| 347 |
TEST( m_test->setClipboard("TEST") ); |
| 348 |
WAIT_ON_OUTPUT("clipboard", "TEST"); |
| 349 |
RUN("tab" << QString(clipboardTabName) << "size", "0\n"); |
| 350 |
} |
| 351 |
|
| 352 |
void CoreTests::automaticCommandChaining() |
| 353 |
{ |
| 354 |
const auto script = R"( |
| 355 |
setCommands([ |
| 356 |
{automatic: true, cmd: 'copyq: setData(mimeText, 1)'}, |
| 357 |
{automatic: true, cmd: 'copyq: setData(mimeText, str(data(mimeText)) + 2)'}, |
| 358 |
{automatic: true, cmd: 'copyq: setData(mimeText, str(data(mimeText)) + 3)'} |
| 359 |
]) |
| 360 |
)"; |
| 361 |
RUN(script, ""); |
| 362 |
WAIT_ON_OUTPUT("commands().length", "3\n"); |
| 363 |
|
| 364 |
TEST( m_test->setClipboard("TEST") ); |
| 365 |
WAIT_ON_OUTPUT("read" << "0", "123"); |
| 366 |
} |
| 367 |
|
| 368 |
void CoreTests::automaticCommandCopyToTab() |
| 369 |
{ |
| 370 |
const auto tab1 = testTab(1); |
| 371 |
const auto script = R"( |
| 372 |
setCommands([{automatic: true, tab: ')" + tab1 + R"('}]) |
| 373 |
)"; |
| 374 |
RUN(script, ""); |
| 375 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 376 |
|
| 377 |
TEST( m_test->setClipboard("TEST") ); |
| 378 |
WAIT_ON_OUTPUT("tab" << QString(clipboardTabName) << "read" << "0", "TEST"); |
| 379 |
RUN("tab" << tab1 << "read" << "0", "TEST"); |
| 380 |
} |
| 381 |
|
| 382 |
void CoreTests::automaticCommandStoreSpecialFormat() |
| 383 |
{ |
| 384 |
const auto script = R"( |
| 385 |
setCommands([ |
| 386 |
{ automatic: true, name: 'CMD1', input: 'test-format' } |
| 387 |
]) |
| 388 |
)"; |
| 389 |
RUN(script, ""); |
| 390 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 391 |
|
| 392 |
TEST( m_test->setClipboard("DATA", "test-format") ); |
| 393 |
WAIT_ON_OUTPUT("separator" << "," << "read" << "test-format" << "0" << "1", "DATA,"); |
| 394 |
} |
| 395 |
|
| 396 |
void CoreTests::automaticCommandIgnoreSpecialFormat() |
| 397 |
{ |
| 398 |
const auto script = R"( |
| 399 |
setCommands([ |
| 400 |
{ automatic: true, name: 'CMD1', cmd: 'copyq add CMD1', input: 'test-format', remove: true }, |
| 401 |
{ automatic: true, name: 'CMD2', cmd: 'copyq add CMD2' }, |
| 402 |
{ automatic: true, name: 'CMD3', cmd: 'copyq add CMD3', input: 'test-format' } |
| 403 |
]) |
| 404 |
)"; |
| 405 |
RUN(script, ""); |
| 406 |
WAIT_ON_OUTPUT("commands().length", "3\n"); |
| 407 |
|
| 408 |
TEST( m_test->setClipboard("SHOULD BE IGNORED", "test-format") ); |
| 409 |
WAIT_ON_OUTPUT("separator" << "," << "read" << "0" << "1", "CMD1,"); |
| 410 |
|
| 411 |
TEST( m_test->setClipboard("SHOULD NOT BE IGNORED") ); |
| 412 |
WAIT_ON_OUTPUT("separator" << "," << "read" << "0" << "1" << "2" << "3", "SHOULD NOT BE IGNORED,CMD2,CMD1,"); |
| 413 |
} |
| 414 |
|
| 415 |
void CoreTests::globalCommandInMenu() |
| 416 |
{ |
| 417 |
const auto script = R"( |
| 418 |
setCommands([ |
| 419 |
{ isGlobalShortcut: true, name: 'test', cmd: 'copyq add test' }, |
| 420 |
]) |
| 421 |
)"; |
| 422 |
RUN(script, ""); |
| 423 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 424 |
RUN("menu", ""); |
| 425 |
KEYS(trayMenuId << "DOWN" << "ENTER"); |
| 426 |
KEYS(clipboardBrowserId); |
| 427 |
WAIT_ON_OUTPUT("read(0)", "test"); |
| 428 |
|
| 429 |
RUN("setCommands([])", ""); |
| 430 |
WAIT_ON_OUTPUT("commands().length", "0\n"); |
| 431 |
|
| 432 |
// Test sub-menus |
| 433 |
const auto script2 = R"( |
| 434 |
setCommands([ |
| 435 |
{ isGlobalShortcut: true, name: 'test|test1|test2', cmd: 'copyq add test2' }, |
| 436 |
]) |
| 437 |
)"; |
| 438 |
RUN(script2, ""); |
| 439 |
WAIT_ON_OUTPUT("commands().length", "1\n"); |
| 440 |
RUN("menu", ""); |
| 441 |
KEYS(trayMenuId << "DOWN" << "DOWN" << "ENTER"); |
| 442 |
waitFor(100); |
| 443 |
KEYS(trayMenuId << "ENTER"); |
| 444 |
waitFor(100); |
| 445 |
KEYS(trayMenuId << "ENTER"); |
| 446 |
KEYS(clipboardBrowserId); |
| 447 |
WAIT_ON_OUTPUT("read(0)", "test2"); |
| 448 |
} |