Skip to content

Commit

Permalink
Tests: Fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Apr 14, 2024
1 parent e51ff08 commit 71a563c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 36 deletions.
6 changes: 6 additions & 0 deletions docs/scripting-api.rst
Expand Up @@ -498,12 +498,18 @@ unlike in GUI, where row numbers start from 1 by default.

Opens external editor if set, otherwise opens internal editor.

If row is -1 (or other negative number) edits clipboard instead
and creates new item.

.. js:function:: editItem(row, [mimeType, [data]])

Edits specific format for the item.

Opens external editor if set, otherwise opens internal editor.

If row is -1 (or other negative number) edits clipboard instead
and creates new item.

.. js:function:: read([mimeType])

Same as :js:func:`clipboard`.
Expand Down
65 changes: 33 additions & 32 deletions src/scriptable/scriptable.cpp
Expand Up @@ -1137,16 +1137,19 @@ void Scriptable::edit()
{
m_skipArguments = -1;

QJSValue value;
QByteArray content;
int row = -1;
int editRow = -1;
bool changeClipboard = true;

const int len = argumentCount();
for ( int i = 0; i < len; ++i ) {
value = argument(i);
const QJSValue value = argument(i);
if (i > 0)
content.append(m_inputSeparator.toUtf8());
if ( toInt(value, &row) ) {
editRow = (i == 0) ? row : -1;
changeClipboard = i == 0 && row < 0;
const QByteArray bytes = row >= 0 ? m_proxy->browserItemData(m_tabName, row, mimeText)
: getClipboardData(mimeText);
content.append(bytes);
Expand All @@ -1155,46 +1158,29 @@ void Scriptable::edit()
}
}

bool changeClipboard = row < 0;

if ( !m_proxy->browserOpenEditor(m_tabName, row, mimeText, content, changeClipboard) ) {
m_proxy->showBrowser(m_tabName);
if (len == 1 && row >= 0) {
m_proxy->browserSetCurrent(m_tabName, row);
m_proxy->browserEditRow(m_tabName, row, mimeText);
} else {
m_proxy->browserEditNew(m_tabName, mimeText, content, changeClipboard);
}
}
editContent(editRow, mimeText, content, changeClipboard);
}

QJSValue Scriptable::editItem()
{
m_skipArguments = 3;

int row;
if ( !toInt(argument(0), &row) ) {
int editRow;
if ( !toInt(argument(0), &editRow) )
return throwError(argumentError());
}

const auto format = arg(1, mimeText);
const bool changeClipboard = row < 0;
const QByteArray content = argumentCount() > 2
? makeByteArray( argument(2) )
: ( row >= 0
? m_proxy->browserItemData(m_tabName, row, format)
: getClipboardData(format));

if ( !m_proxy->browserOpenEditor(m_tabName, row, format, content, changeClipboard) ) {
m_proxy->showBrowser(m_tabName);
if (row >= 0) {
m_proxy->browserSetCurrent(m_tabName, row);
m_proxy->browserEditRow(m_tabName, row, format);
} else {
m_proxy->browserEditNew(m_tabName, format, content, changeClipboard);
}
}
const bool changeClipboard = editRow < 0;

QByteArray content;
if ( argumentCount() > 2 )
content = makeByteArray(argument(2));
else if (editRow >= 0)
content = m_proxy->browserItemData(m_tabName, editRow, format);
else
content = getClipboardData(format);

editContent(editRow, format, content, changeClipboard);
return {};
}

Expand Down Expand Up @@ -3076,6 +3062,21 @@ void Scriptable::nextToClipboard(int where)
#endif
}

void Scriptable::editContent(
int editRow, const QString &format, const QByteArray &content, bool changeClipboard)
{
if ( m_proxy->browserOpenEditor(m_tabName, editRow, format, content, changeClipboard) )
return;

m_proxy->showBrowser(m_tabName);
if (editRow >= 0) {
m_proxy->browserSetCurrent(m_tabName, editRow);
m_proxy->browserEditRow(m_tabName, editRow, format);
} else {
m_proxy->browserEditNew(m_tabName, format, content, changeClipboard);
}
}

QJSValue Scriptable::screenshot(bool select)
{
m_skipArguments = 2;
Expand Down
1 change: 1 addition & 0 deletions src/scriptable/scriptable.h
Expand Up @@ -428,6 +428,7 @@ public slots:
QJSValue copy(ClipboardMode mode);
QJSValue changeItem(bool create);
void nextToClipboard(int where);
void editContent(int editRow, const QString &format, const QByteArray &content, bool changeClipboard);
QJSValue screenshot(bool select);
QByteArray serialize(const QJSValue &value);
QJSValue eval(const QString &script);
Expand Down
62 changes: 58 additions & 4 deletions src/tests/tests.cpp
Expand Up @@ -1592,15 +1592,67 @@ void Tests::commandEdit()

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

// Edit new item.
// Edit clipboard and new item.
TEST( m_test->setClipboard("TEST") );
RUN("edit" << "-1", "");
RUN("keys" << "END" << ":LINE 1" << "F2", "");
RUN("read" << "0", "TESTLINE 1");
WAIT_FOR_CLIPBOARD("TESTLINE 1");

// Edit existing item.
RUN("edit" << "0", "");
RUN("keys" << "END" << "ENTER" << ":LINE 2" << "F2", "");
RUN("read" << "0", "TESTLINE 1\nLINE 2");
WAIT_FOR_CLIPBOARD("TESTLINE 1");

// Edit clipboard (ignore existing data) and new item.
RUN("edit", "");
RUN("keys" << ":LINE 1" << "F2", "");
RUN("keys" << "END" << ":LINE 1" << "F2", "");
RUN("read" << "0", "LINE 1");
WAIT_FOR_CLIPBOARD("LINE 1");
}

void Tests::commandEditItem()
{
SKIP_ON_ENV("COPYQ_TESTS_SKIP_COMMAND_EDIT");

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

// Edit clipboard and new item.
TEST( m_test->setClipboard("TEST", mimeHtml) );
RUN("editItem" << "-1" << mimeHtml, "");
RUN("keys" << "END" << ":LINE 1" << "F2", "");
#ifdef Q_OS_WIN
# define FRAG_START "<!--StartFragment-->"
# define FRAG_END "<!--EndFragment-->"
const auto expected = QByteArrayLiteral(FRAG_START "TEST" FRAG_END "LINE 1");
#else
const auto expected = QByteArrayLiteral("TESTLINE 1");
#endif
RUN("read" << mimeHtml << "0", expected);
RUN("read" << "0", "");
WAIT_FOR_CLIPBOARD2(expected, mimeHtml);
WAIT_FOR_CLIPBOARD("");

// Edit existing item.
RUN("edit" << "0", "");
RUN("editItem" << "0" << mimeHtml, "");
RUN("keys" << "END" << "ENTER" << ":LINE 2" << "F2", "");
RUN("read" << "0", "LINE 1\nLINE 2");
RUN("read" << mimeHtml << "0", expected + "\nLINE 2");
RUN("read" << "0", "");
WAIT_FOR_CLIPBOARD2(expected, mimeHtml);
WAIT_FOR_CLIPBOARD("");

// Edit clipboard (ignore existing data) and new item.
RUN("editItem" << "-1" << mimeHtml << "TEST", "");
RUN("keys" << "END" << ":LINE 1" << "F2", "");
RUN("read" << mimeHtml << "0", "TESTLINE 1");
RUN("read" << "0", "");
#ifdef Q_OS_WIN
WAIT_FOR_CLIPBOARD2(FRAG_START "TESTLINE 1" FRAG_END, mimeHtml);
#else
WAIT_FOR_CLIPBOARD2("TESTLINE 1", mimeHtml);
#endif
WAIT_FOR_CLIPBOARD("");
}

void Tests::commandGetSetCurrentPath()
Expand Down Expand Up @@ -3190,6 +3242,8 @@ void Tests::openAndSavePreferences()
// Focus and set wrap text option.
// This behavior could differ on some systems and in other languages.
RUN("keys" << configurationDialogId << "ALT+1", "");
// Wait for any checkbox animation or delay
waitFor(1000);
RUN("keys" << configurationDialogId << "ENTER" << clipboardBrowserId, "");
WAIT_ON_OUTPUT("config" << "check_clipboard", "true\n");
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/tests.h
Expand Up @@ -94,6 +94,7 @@ private slots:
void commandHasClipboardFormat();

void commandEdit();
void commandEditItem();

void commandGetSetCurrentPath();

Expand Down

0 comments on commit 71a563c

Please sign in to comment.