Skip to content

Commit 12f8356

Browse files
committed
Tests: Fix flaky test
1 parent a2e4476 commit 12f8356

File tree

5 files changed

+99
-36
lines changed

5 files changed

+99
-36
lines changed

docs/scripting-api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,18 @@ unlike in GUI, where row numbers start from 1 by default.
498498

499499
Opens external editor if set, otherwise opens internal editor.
500500

501+
If row is -1 (or other negative number) edits clipboard instead
502+
and creates new item.
503+
501504
.. js:function:: editItem(row, [mimeType, [data]])
502505

503506
Edits specific format for the item.
504507

505508
Opens external editor if set, otherwise opens internal editor.
506509

510+
If row is -1 (or other negative number) edits clipboard instead
511+
and creates new item.
512+
507513
.. js:function:: read([mimeType])
508514

509515
Same as :js:func:`clipboard`.

src/scriptable/scriptable.cpp

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,16 +1137,19 @@ void Scriptable::edit()
11371137
{
11381138
m_skipArguments = -1;
11391139

1140-
QJSValue value;
11411140
QByteArray content;
11421141
int row = -1;
1142+
int editRow = -1;
1143+
bool changeClipboard = true;
11431144

11441145
const int len = argumentCount();
11451146
for ( int i = 0; i < len; ++i ) {
1146-
value = argument(i);
1147+
const QJSValue value = argument(i);
11471148
if (i > 0)
11481149
content.append(m_inputSeparator.toUtf8());
11491150
if ( toInt(value, &row) ) {
1151+
editRow = (i == 0) ? row : -1;
1152+
changeClipboard = i == 0 && row < 0;
11501153
const QByteArray bytes = row >= 0 ? m_proxy->browserItemData(m_tabName, row, mimeText)
11511154
: getClipboardData(mimeText);
11521155
content.append(bytes);
@@ -1155,46 +1158,29 @@ void Scriptable::edit()
11551158
}
11561159
}
11571160

1158-
bool changeClipboard = row < 0;
1159-
1160-
if ( !m_proxy->browserOpenEditor(m_tabName, row, mimeText, content, changeClipboard) ) {
1161-
m_proxy->showBrowser(m_tabName);
1162-
if (len == 1 && row >= 0) {
1163-
m_proxy->browserSetCurrent(m_tabName, row);
1164-
m_proxy->browserEditRow(m_tabName, row, mimeText);
1165-
} else {
1166-
m_proxy->browserEditNew(m_tabName, mimeText, content, changeClipboard);
1167-
}
1168-
}
1161+
editContent(editRow, mimeText, content, changeClipboard);
11691162
}
11701163

11711164
QJSValue Scriptable::editItem()
11721165
{
11731166
m_skipArguments = 3;
11741167

1175-
int row;
1176-
if ( !toInt(argument(0), &row) ) {
1168+
int editRow;
1169+
if ( !toInt(argument(0), &editRow) )
11771170
return throwError(argumentError());
1178-
}
11791171

11801172
const auto format = arg(1, mimeText);
1181-
const bool changeClipboard = row < 0;
1182-
const QByteArray content = argumentCount() > 2
1183-
? makeByteArray( argument(2) )
1184-
: ( row >= 0
1185-
? m_proxy->browserItemData(m_tabName, row, format)
1186-
: getClipboardData(format));
1187-
1188-
if ( !m_proxy->browserOpenEditor(m_tabName, row, format, content, changeClipboard) ) {
1189-
m_proxy->showBrowser(m_tabName);
1190-
if (row >= 0) {
1191-
m_proxy->browserSetCurrent(m_tabName, row);
1192-
m_proxy->browserEditRow(m_tabName, row, format);
1193-
} else {
1194-
m_proxy->browserEditNew(m_tabName, format, content, changeClipboard);
1195-
}
1196-
}
1173+
const bool changeClipboard = editRow < 0;
1174+
1175+
QByteArray content;
1176+
if ( argumentCount() > 2 )
1177+
content = makeByteArray(argument(2));
1178+
else if (editRow >= 0)
1179+
content = m_proxy->browserItemData(m_tabName, editRow, format);
1180+
else
1181+
content = getClipboardData(format);
11971182

1183+
editContent(editRow, format, content, changeClipboard);
11981184
return {};
11991185
}
12001186

@@ -3076,6 +3062,21 @@ void Scriptable::nextToClipboard(int where)
30763062
#endif
30773063
}
30783064

3065+
void Scriptable::editContent(
3066+
int editRow, const QString &format, const QByteArray &content, bool changeClipboard)
3067+
{
3068+
if ( m_proxy->browserOpenEditor(m_tabName, editRow, format, content, changeClipboard) )
3069+
return;
3070+
3071+
m_proxy->showBrowser(m_tabName);
3072+
if (editRow >= 0) {
3073+
m_proxy->browserSetCurrent(m_tabName, editRow);
3074+
m_proxy->browserEditRow(m_tabName, editRow, format);
3075+
} else {
3076+
m_proxy->browserEditNew(m_tabName, format, content, changeClipboard);
3077+
}
3078+
}
3079+
30793080
QJSValue Scriptable::screenshot(bool select)
30803081
{
30813082
m_skipArguments = 2;

src/scriptable/scriptable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ public slots:
428428
QJSValue copy(ClipboardMode mode);
429429
QJSValue changeItem(bool create);
430430
void nextToClipboard(int where);
431+
void editContent(int editRow, const QString &format, const QByteArray &content, bool changeClipboard);
431432
QJSValue screenshot(bool select);
432433
QByteArray serialize(const QJSValue &value);
433434
QJSValue eval(const QString &script);

src/tests/tests.cpp

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,15 +1592,67 @@ void Tests::commandEdit()
15921592

15931593
RUN("config" << "editor" << "", "\n");
15941594

1595-
// Edit new item.
1595+
// Edit clipboard and new item.
1596+
TEST( m_test->setClipboard("TEST") );
1597+
RUN("edit" << "-1", "");
1598+
RUN("keys" << "END" << ":LINE 1" << "F2", "");
1599+
RUN("read" << "0", "TESTLINE 1");
1600+
WAIT_FOR_CLIPBOARD("TESTLINE 1");
1601+
1602+
// Edit existing item.
1603+
RUN("edit" << "0", "");
1604+
RUN("keys" << "END" << "ENTER" << ":LINE 2" << "F2", "");
1605+
RUN("read" << "0", "TESTLINE 1\nLINE 2");
1606+
WAIT_FOR_CLIPBOARD("TESTLINE 1");
1607+
1608+
// Edit clipboard (ignore existing data) and new item.
15961609
RUN("edit", "");
1597-
RUN("keys" << ":LINE 1" << "F2", "");
1610+
RUN("keys" << "END" << ":LINE 1" << "F2", "");
15981611
RUN("read" << "0", "LINE 1");
1612+
WAIT_FOR_CLIPBOARD("LINE 1");
1613+
}
1614+
1615+
void Tests::commandEditItem()
1616+
{
1617+
SKIP_ON_ENV("COPYQ_TESTS_SKIP_COMMAND_EDIT");
1618+
1619+
RUN("config" << "editor" << "", "\n");
1620+
1621+
// Edit clipboard and new item.
1622+
TEST( m_test->setClipboard("TEST", mimeHtml) );
1623+
RUN("editItem" << "-1" << mimeHtml, "");
1624+
RUN("keys" << "END" << ":LINE 1" << "F2", "");
1625+
#ifdef Q_OS_WIN
1626+
# define FRAG_START "<!--StartFragment-->"
1627+
# define FRAG_END "<!--EndFragment-->"
1628+
const auto expected = QByteArrayLiteral(FRAG_START "TEST" FRAG_END "LINE 1");
1629+
#else
1630+
const auto expected = QByteArrayLiteral("TESTLINE 1");
1631+
#endif
1632+
RUN("read" << mimeHtml << "0", expected);
1633+
RUN("read" << "0", "");
1634+
WAIT_FOR_CLIPBOARD2(expected, mimeHtml);
1635+
WAIT_FOR_CLIPBOARD("");
15991636

16001637
// Edit existing item.
1601-
RUN("edit" << "0", "");
1638+
RUN("editItem" << "0" << mimeHtml, "");
16021639
RUN("keys" << "END" << "ENTER" << ":LINE 2" << "F2", "");
1603-
RUN("read" << "0", "LINE 1\nLINE 2");
1640+
RUN("read" << mimeHtml << "0", expected + "\nLINE 2");
1641+
RUN("read" << "0", "");
1642+
WAIT_FOR_CLIPBOARD2(expected, mimeHtml);
1643+
WAIT_FOR_CLIPBOARD("");
1644+
1645+
// Edit clipboard (ignore existing data) and new item.
1646+
RUN("editItem" << "-1" << mimeHtml << "TEST", "");
1647+
RUN("keys" << "END" << ":LINE 1" << "F2", "");
1648+
RUN("read" << mimeHtml << "0", "TESTLINE 1");
1649+
RUN("read" << "0", "");
1650+
#ifdef Q_OS_WIN
1651+
WAIT_FOR_CLIPBOARD2(FRAG_START "TESTLINE 1" FRAG_END, mimeHtml);
1652+
#else
1653+
WAIT_FOR_CLIPBOARD2("TESTLINE 1", mimeHtml);
1654+
#endif
1655+
WAIT_FOR_CLIPBOARD("");
16041656
}
16051657

16061658
void Tests::commandGetSetCurrentPath()
@@ -3190,6 +3242,8 @@ void Tests::openAndSavePreferences()
31903242
// Focus and set wrap text option.
31913243
// This behavior could differ on some systems and in other languages.
31923244
RUN("keys" << configurationDialogId << "ALT+1", "");
3245+
// Wait for any checkbox animation or delay
3246+
waitFor(1000);
31933247
RUN("keys" << configurationDialogId << "ENTER" << clipboardBrowserId, "");
31943248
WAIT_ON_OUTPUT("config" << "check_clipboard", "true\n");
31953249
}

src/tests/tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private slots:
9494
void commandHasClipboardFormat();
9595

9696
void commandEdit();
97+
void commandEditItem();
9798

9899
void commandGetSetCurrentPath();
99100

0 commit comments

Comments
 (0)