From cdcc85d370054b5cc331b98950b935f206a6975a Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Wed, 22 Feb 2017 20:48:38 +0100
Subject: [PATCH] Tests: Faster key clicks

Use zero intervals between shortcuts and key clicks.

Environment variables can be used to set the intervals
(cannot be too big for test otherwise client process will be killed).

Example:

    COPYQ_TESTS_KEYS_WAIT=2000 COPYQ_TESTS_KEY_DELAY=50 \
        copyq tests copyItems
---
 src/gui/mainwindow.cpp             | 18 +++++++++++++-----
 src/gui/mainwindow.h               |  4 ++--
 src/scriptable/scriptable.cpp      | 18 ++++++++++++++++--
 src/scriptable/scriptableproxy.cpp |  6 +++---
 src/scriptable/scriptableproxy.h   |  2 +-
 src/tests/tests.cpp                |  3 ++-
 6 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp
index 1a426a356..c26752b2d 100644
--- a/src/gui/mainwindow.cpp
+++ b/src/gui/mainwindow.cpp
@@ -1032,7 +1032,7 @@ void MainWindow::invoke(Callable *callable)
 }
 
 #ifdef HAS_TESTS
-void MainWindow::keyClicks(const QString &keys)
+void MainWindow::keyClicks(const QString &keys, int delay)
 {
     QWidget *widget;
 
@@ -1048,9 +1048,11 @@ void MainWindow::keyClicks(const QString &keys)
         }
     }
 
+    const auto className = widget->metaObject()->className();
+
     auto widgetName = QString("%1:%2")
             .arg(widget->objectName())
-            .arg(widget->metaObject()->className());
+            .arg(className);
 
     if (widget != widget->window()) {
         widgetName.append(
@@ -1059,12 +1061,17 @@ void MainWindow::keyClicks(const QString &keys)
                     .arg(widget->window()->metaObject()->className()) );
     }
 
+    // There could be some animation/transition effect on check boxes
+    // so wait for checkbox to be set.
+    if ( className == QString("QCheckBox") )
+        waitFor(100);
+
     COPYQ_LOG( QString("Sending keys \"%1\" to %2.")
                .arg(keys)
                .arg(widgetName) );
 
     if ( keys.startsWith(":") ) {
-        QTest::keyClicks(widget, keys.mid(1), Qt::NoModifier, 50);
+        QTest::keyClicks(widget, keys.mid(1), Qt::NoModifier, delay);
 
         // Increment key clicks sequence number after typing all the text.
         ++m_receivedKeyClicks;
@@ -1094,11 +1101,12 @@ void MainWindow::keyClicks(const QString &keys)
                .arg(widgetName) );
 }
 
-uint MainWindow::sendKeyClicks(const QString &keys)
+uint MainWindow::sendKeyClicks(const QString &keys, int delay)
 {
     // Don't stop when modal window is open.
     QMetaObject::invokeMethod( this, "keyClicks", Qt::QueuedConnection,
-                               Q_ARG(const QString &, keys)
+                               Q_ARG(const QString &, keys),
+                               Q_ARG(int, delay)
                                );
 
     return ++m_sentKeyClicks;
diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h
index 6cbad647e..d01525908 100644
--- a/src/gui/mainwindow.h
+++ b/src/gui/mainwindow.h
@@ -437,13 +437,13 @@ public slots:
      *
      * Increments key clicks sequence number returned by lastReceivedKeyClicks().
      */
-    void keyClicks(const QString &keys);
+    void keyClicks(const QString &keys, int delay);
 
     /**
      * Send key clicks to focused widget.
      * @return Key clicks sequence number.
      */
-    uint sendKeyClicks(const QString &keys);
+    uint sendKeyClicks(const QString &keys, int delay);
 
     /**
      * @return Last key clicks sequence number received by widgets.
diff --git a/src/scriptable/scriptable.cpp b/src/scriptable/scriptable.cpp
index 7ae230d3f..84a98c242 100644
--- a/src/scriptable/scriptable.cpp
+++ b/src/scriptable/scriptable.cpp
@@ -1058,11 +1058,25 @@ void Scriptable::fail()
 void Scriptable::keys()
 {
 #ifdef HAS_TESTS
+    bool ok;
+
+    // Wait interval after shortcut pressed or text typed.
+    const auto waitValue = qgetenv("COPYQ_TESTS_KEYS_WAIT");
+    int wait = waitValue.toInt(&ok);
+    if (!ok)
+        wait = 0;
+
+    // Delay while typing.
+    const auto delayValue = qgetenv("COPYQ_TESTS_KEY_DELAY");
+    int delay = delayValue.toInt(&ok);
+    if (!ok)
+        delay = 0;
+
     for (int i = 0; i < argumentCount(); ++i) {
         const QString keys = toString(argument(i));
 
-        waitFor(500);
-        m_proxy->sendKeys(keys);
+        waitFor(wait);
+        m_proxy->sendKeys(keys, delay);
 
         // Make sure all keys are send (shortcuts are postponed because they can be blocked by modal windows).
         while ( !m_proxy->keysSent() ) {
diff --git a/src/scriptable/scriptableproxy.cpp b/src/scriptable/scriptableproxy.cpp
index 9995aa5c7..b1d044670 100644
--- a/src/scriptable/scriptableproxy.cpp
+++ b/src/scriptable/scriptableproxy.cpp
@@ -853,10 +853,10 @@ QList<int> ScriptableProxy::selectedItems()
 }
 
 #ifdef HAS_TESTS
-void ScriptableProxy::sendKeys(const QString &keys)
+void ScriptableProxy::sendKeys(const QString &keys, int delay)
 {
-    INVOKE2(sendKeys(keys));
-    m_sentKeyClicks = m_wnd->sendKeyClicks(keys);
+    INVOKE2(sendKeys(keys, delay));
+    m_sentKeyClicks = m_wnd->sendKeyClicks(keys, delay);
 }
 
 bool ScriptableProxy::keysSent()
diff --git a/src/scriptable/scriptableproxy.h b/src/scriptable/scriptableproxy.h
index 9afdc7a21..798dac224 100644
--- a/src/scriptable/scriptableproxy.h
+++ b/src/scriptable/scriptableproxy.h
@@ -151,7 +151,7 @@ public:
     QList<int> selectedItems();
 
 #ifdef HAS_TESTS
-    void sendKeys(const QString &keys);
+    void sendKeys(const QString &keys, int delay);
     bool keysSent();
     QString testSelected();
 #endif // HAS_TESTS
diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp
index fed4ce569..add3907b8 100644
--- a/src/tests/tests.cpp
+++ b/src/tests/tests.cpp
@@ -1473,7 +1473,8 @@ void Tests::openAndSavePreferences()
 
     // Focus and set wrap text option.
     // This behavior could differ on some systems and in other languages.
-    RUN("keys" << "ALT+1" << "ENTER", "");
+    RUN("keys" << "ALT+1", "");
+    RUN("keys" << "ENTER", "");
     RUN(args, "true\n");
 
     RUN(args << "false", "");
-- 
GitLab