Home > database >  How to unit test Double Tap in TapHandler?
How to unit test Double Tap in TapHandler?

Time:11-20

I am trying to unit test a QtQuick TapHandler:

TapHandlerUnderTest

import QtQuick
import QtQuick.Controls


TapHandler {
    required property ApplicationWindow appWindow

    onDoubleTapped: {
        console.warn('double tapped')
        appWindow.toggleMaximized()
    }

    onSingleTapped: {
        console.warn('single tapped')
    }

}

Test Case

import QtQuick
import QtQuick.Controls
import QtTest


Item {
    id: testHelper
    width: 400
    height: 400

    property bool toggleMaximizedCalled: false

    property var appWindow: ApplicationWindow {
        function toggleMaximized() {
            testHelper.toggleMaximizedCalled = true
        }
    }

    TapHandlerUnderTest {
        id: objectUnderTest

        appWindow: testHelper.appWindow
    }

    TestCase {
        name: "TapHandlerUnderTest"
        when: windowShown

        function cleanup() {
            testHelper.toggleMaximizedCalled = false
        }

        function test_tap_data() {
            return [
                { taps: 2, called: true, tag: '2x' },
            ]
        }

        function test_tap(data) {
            mouseDoubleClickSequence(testHelper)
            compare(testHelper.toggleMaximizedCalled, data.called)
        }

    }

}

What did I try?

  1. I tried using a mouseDoubleClickSequence. The test is failing with the following log:
QWARN  : qmltestrunner::TapHandlerUnderTest::test_tap(2x) qml: single tapped
QWARN  : qmltestrunner::TapHandlerUnderTest::test_tap(2x) qml: single tapped
FAIL!  : qmltestrunner::TapHandlerUnderTest::test_tap(2x) Compared values are not the same
   Actual   (): false
   Expected (): true
  1. I tried using two mouseClick functions instead of the mouseDoubleClickSequence. I got exactly the same result as in (1).
  2. I tried using the touchEvent. That leads to no output - it does not even recognize a single tap this way.

Is this a bug or how can we test a double tap (double click) here?

I am setting export QT_QPA_PLATFORM=xcb for my test cases but I tested it with different platform settings. It did not change anything.

Best regards,
Elias

CodePudding user response:

Converting my comment to an answer as requested.

A simple solution (workaround?) would be to refactor a method, say actionDoubleTapped(). e.g.

Page {
    TabHandler {
        onDoubleTapped: actionDoubleTapped()
    }

    function actionDoubleTapped() {
        //...
    }
}

Then unit test merely invokes the actionDoubleTapped method directly.

  • Related