Skip to content

Commit

Permalink
Create a new GTest for Mouse Click Position
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Feustel committed Mar 19, 2024
1 parent 14c4b73 commit de9f36c
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(Basic)
add_subdirectory(GTest)
add_subdirectory(GTestMouseClickPosition)
add_subdirectory(ListGridView)
add_subdirectory(RemoteCtrl)
add_subdirectory(RepeaterLoader)
14 changes: 14 additions & 0 deletions examples/GTestMouseClickPosition/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SPIX_QT_MAJOR "6" CACHE STRING "Major Qt version to build Spix against")

find_package(Qt${SPIX_QT_MAJOR} COMPONENTS Core Quick REQUIRED)
find_package(GTest REQUIRED)

add_executable(SpixGTestMouseClickPosition "main.cpp" "qml.qrc")
target_compile_definitions(SpixGTestMouseClickPosition PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(SpixGTestMouseClickPosition PRIVATE Qt${SPIX_QT_MAJOR}::Core Qt${SPIX_QT_MAJOR}::Quick GTest::GTest Spix)
43 changes: 43 additions & 0 deletions examples/GTestMouseClickPosition/ResultsView.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import QtQuick 2.11
import QtQuick.Controls 2.4

Rectangle {
id: resultsView
color: "#222222"

function appendText(text) {
resultsArea.append(text);
}

Text {
id: resultsTitle
text: "Result: "
color: "white"
font.bold: true

anchors {
top: resultsView.top
left: resultsView.left
right: resultsView.right

topMargin: 5
leftMargin: 2
bottomMargin: 2
}
}
TextEdit {
id: resultsArea
objectName: "results"
color: "white"
anchors {
top: resultsTitle.bottom
left: resultsView.left
right: resultsView.right
bottom: resultsView.bottom

leftMargin: 2
rightMargin: 2
bottomMargin: 2
}
}
}
158 changes: 158 additions & 0 deletions examples/GTestMouseClickPosition/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/***
* Copyright (C) Falko Axmann. All rights reserved.
* Licensed under the MIT license.
* See LICENSE.txt file in the project root for full license information.
****/

/**
* This is a very basic example to demonstrate how to run your UI tests
* using GTest. It can be useful when you have to make sure that your
* UI tests work well in an existing, GTest based environment.
*
* Keep in mind that GTest is not designed for UI testing and that the
* order of the test execution is not guaranteed. Thus, you should only
* have one test per executable.
*/

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <Spix/Events/Identifiers.h>
#include <Spix/QtQmlBot.h>

#include <atomic>
#include <gtest/gtest.h>

class SpixGTest;
static SpixGTest* srv;

class SpixGTest : public spix::TestServer {
public:
SpixGTest(int argc, char* argv[])
{
m_argc = argc;
m_argv = argv;
}

int testResult() { return m_result.load(); }

protected:
int m_argc;
char** m_argv;
std::atomic<int> m_result {0};

void executeTest() override
{
srv = this;
::testing::InitGoogleTest(&m_argc, m_argv);
auto testResult = RUN_ALL_TESTS();
m_result.store(testResult);
}
};

TEST(GTestExample, ProportionAllButtons)
{
const auto upperLeft = spix::Point(0.25, 0.25);
const auto upperRigth = spix::Point(0.75, 0.25);
const auto lowerLeft = spix::Point(0.25, 0.75);
const auto lowerRigth = spix::Point(0.75, 0.75);

srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), upperLeft);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), upperRigth);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), lowerLeft);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), lowerRigth);
srv->wait(std::chrono::milliseconds(500));
auto result = srv->getStringProperty("mainWindow/results", "text");

auto expected_result = R"RSLT(Button 1 clicked
Button 2 clicked
Button 3 clicked
Button 4 clicked)RSLT";

EXPECT_EQ(result, expected_result);
}


TEST(GTestExample, ToMuchProportion)
{
// This Test click 200% under the Button 1 => under the Button 1 is Button 3 which will trigger.
const auto upperLeft = spix::Point(0.25, 2);

srv->setStringProperty("mainWindow/results", "text", "");
srv->mouseClick(spix::ItemPath("mainWindow/Button_1"), upperLeft);
srv->wait(std::chrono::milliseconds(500));

auto result = srv->getStringProperty("mainWindow/results", "text");

auto expected_result = R"RSLT(Button 3 clicked)RSLT";

EXPECT_EQ(result, expected_result);
}


TEST(GTestExample, OffsetAllButtons)
{
const auto upperLeft = spix::Point(100, 100);
const auto upperRigth = spix::Point(400, 100);
const auto lowerLeft = spix::Point(100, 200);
const auto lowerRigth = spix::Point(400, 200);

srv->setStringProperty("mainWindow/results", "text", "");
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), spix::Point(0,0), upperLeft);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), spix::Point(0,0), upperRigth);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), spix::Point(0,0), lowerLeft);
srv->wait(std::chrono::milliseconds(500));
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), spix::Point(0,0), lowerRigth);
srv->wait(std::chrono::milliseconds(500));

auto result = srv->getStringProperty("mainWindow/results", "text");

auto expected_result = R"RSLT(Button 1 clicked
Button 2 clicked
Button 3 clicked
Button 4 clicked)RSLT";

EXPECT_EQ(result, expected_result);
}

TEST(GTestExample, OffsetOnPositionWithNoButton)
{
const auto centre = spix::Point(320, 120);


srv->setStringProperty("mainWindow/results", "text", "No Click");
srv->mouseClick(spix::ItemPath("mainWindow/Grid_1"), spix::Point(0,0), centre);
srv->wait(std::chrono::milliseconds(500));

auto result = srv->getStringProperty("mainWindow/results", "text");

auto expected_result = R"RSLT(No Click)RSLT";

EXPECT_EQ(result, expected_result);

srv->quit();
}



int main(int argc, char* argv[])
{
// Init Qt Qml Application
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

// Instantiate and run tests
SpixGTest tests(argc, argv);
auto bot = new spix::QtQmlBot();
bot->runTestServer(tests);

app.exec();
return tests.testResult();
}
99 changes: 99 additions & 0 deletions examples/GTestMouseClickPosition/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.11

Window {
objectName: "mainWindow"
visible: true
width: 640
height: 480
title: qsTr("Spix Example")
color: "#111111"


GridLayout {
objectName: "Grid_1"
rowSpacing: 20
columnSpacing: 20
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: parent.verticalCenter
}
columns: 2

Button {
objectName: "Button_1"
text: "Press Me"
Layout.fillHeight: true
Layout.fillWidth: true
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
resultsView.appendText("Button 1 clicked")
}
}
}
Button {
objectName: "Button_2"
text: "Press Me"
Layout.fillHeight: true
Layout.fillWidth: true
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
resultsView.appendText("Button 2 clicked")
}
}
}
Button {
objectName: "Button_3"
text: "Press Me"
Layout.fillHeight: true
Layout.fillWidth: true
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
resultsView.appendText("Button 3 clicked")
}
}
}
Button {
objectName: "Button_4"
text: "Press Me"
Layout.fillHeight: true
Layout.fillWidth: true
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
resultsView.appendText("Button 4 clicked")
}
}
}
}


ResultsView {
id: resultsView
anchors {
top: parent.verticalCenter
left: parent.left
right: parent.right
bottom: parent.bottom
}
}
}
6 changes: 6 additions & 0 deletions examples/GTestMouseClickPosition/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>ResultsView.qml</file>
</qresource>
</RCC>

0 comments on commit de9f36c

Please sign in to comment.