Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add QtTest example #100

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.idea
build
dep-libs
CMakeLists.txt.user
8 changes: 4 additions & 4 deletions examples/Basic/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Window {
}

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

onClicked:
onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 1 right clicked")
Expand All @@ -37,13 +37,13 @@ Window {
}
}
Button {
objectName: "Button_2"
text: "Or Click Me"
MouseArea {
objectName: "Button_2"
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 2 right clicked")
Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(GTest)
add_subdirectory(ListGridView)
add_subdirectory(RemoteCtrl)
add_subdirectory(RepeaterLoader)
add_subdirectory(QtTest)
50 changes: 25 additions & 25 deletions examples/GTest/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ Window {
}

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

onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 1 right clicked")
else
resultsView.appendText("Button 1 clicked")
}
}
MouseArea {
objectName: "Button_1"
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 1 right clicked")
else
resultsView.appendText("Button 1 clicked")
}
}
}
Button {
objectName: "Button_2"
text: "Or Click Me"
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons
MouseArea {
objectName: "Button_2"
anchors.fill: parent
acceptedButtons: Qt.AllButtons

onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 2 right clicked")
else
resultsView.appendText("Button 2 clicked")
}
}
onClicked:
{
if(mouse.button & Qt.RightButton)
resultsView.appendText("Button 2 right clicked")
else
resultsView.appendText("Button 2 clicked")
}
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions examples/QtTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.16)

project(QtTestExample VERSION 1.0 LANGUAGES CXX)

enable_testing()

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(AnyRPC REQUIRED)
find_package(Qt${SPIX_QT_MAJOR} REQUIRED COMPONENTS Test Quick)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(${AnyRPC_INCLUDE_DIRS})

add_executable(QtTestExample tst_test.cpp)
add_test(NAME QtTestExample COMMAND QtTestExample)

target_link_libraries(QtTestExample PRIVATE
Qt${SPIX_QT_MAJOR}::Test
Spix

PRIVATE
AnyRPC::anyrpc
)

145 changes: 145 additions & 0 deletions examples/QtTest/tst_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <QtTest>

#include "anyrpc/anyrpc.h"

#include <memory>

using namespace anyrpc;

/**
* @brief Example using QtTest for running test cases against a remote server
* It also contains some handy helper functions to call remote methods.
*
* Run e.g. the RemoteCtrl application as the application under test
*/
class QtTestExample : public QObject {
Q_OBJECT

std::shared_ptr<XmlHttpClient> client;

public:
QtTestExample();
~QtTestExample();

private slots:
void initTestCase();
void cleanupTestCase();
void test_case1();

void waitFor(int time)
{
Value paramsWait;
Value resultWait;
paramsWait[0] = time;
client->Call("wait", paramsWait, resultWait);
}

void mouseClick(std::string path)
{
Value paramsMouse;
Value resultMouse;
paramsMouse[0] = path;
client->Call("mouseClick", paramsMouse, resultMouse);
waitFor(500);
}

void screenshot(std::string path, std::string filepath, bool wait = true, bool compare = true)
{
Value params;
Value result;
params[0] = path;
params[1] = filepath;
client->Call("takeScreenshot", params, result);
}

void setProperty(std::string path, std::string field, std::string value)
{
Value params;
Value result;
params[0] = path;
params[1] = field;
params[2] = value;
client->Call("setStringProperty", params, result);
}

QString getProperty(std::string path, std::string field)
{
Value params;
Value result;
params[0] = path;
params[1] = field;
client->Call("getStringProperty", params, result);

return QString::fromStdString(result.GetString());
}

void invokeMethod(std::string path, std::string field, const std::vector<std::string> functionParameter)
{
Value params;
Value result;

Value parameters;
parameters.SetArray(functionParameter.size());
for (int i = 0; i < functionParameter.size(); ++i) {
parameters[i] = functionParameter.at(i);
}

params[0] = path;
params[1] = field;
params[2] = parameters;
client->Call("invokeMethod", params, result);
}
};

QtTestExample::QtTestExample()
{
}

QtTestExample::~QtTestExample()
{
}

void QtTestExample::initTestCase()
{
client = std::make_shared<anyrpc::XmlHttpClient>();
client->SetServer("127.0.0.1", 9000);
client->SetTimeout(10000);
}

void QtTestExample::cleanupTestCase()
{
}

void QtTestExample::test_case1()
{
setProperty("mainWindow/results", "text", "");

mouseClick("mainWindow/Button_1");
waitFor(500);
mouseClick("mainWindow/Button_2");
waitFor(500);

// in the "old" variant, this call also triggers mouseArea_2
// this is somehow correct, because the mouse area is the top component,
// but sometimes we do not want to trigger the top most component in test cases
mouseClick("mainWindow/Button_3");
waitFor(500);
mouseClick("mainWindow/Button_4");
waitFor(500);

mouseClick("mainWindow/mouseArea_1");
waitFor(500);
mouseClick("mainWindow/mouseArea_2");
waitFor(500);

auto resultText = getProperty("mainWindow/results", "text");
std::cout << resultText.toStdString() << std::endl;

QCOMPARE(resultText,
"Button 1 clicked\nButton 2 clicked\nButton 3 clicked\nButton 4 clicked\nMouseArea 1 clicked\nMouseArea 2 "
"clicked");
}

QTEST_APPLESS_MAIN(QtTestExample)

#include "tst_test.moc"
2 changes: 0 additions & 2 deletions examples/RemoteCtrl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <Spix/AnyRpcServer.h>
#include <Spix/QtQmlBot.h>

#include <iostream>

int main(int argc, char* argv[])
{
// Init Qt Qml Application
Expand Down
77 changes: 53 additions & 24 deletions examples/RemoteCtrl/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,63 @@ Window {
title: qsTr("Spix Example")
color: "#111111"

RowLayout {
ColumnLayout {
id: columnLayout

anchors {
top: parent.top
left: parent.left
topMargin: 5
leftMargin: 5
}
RowLayout {
id: rowLayout

Button {
objectName: "Button_1"
text: "Press Me"
onClicked: resultsView.appendText("Button 1 clicked")
}
Button {
objectName: "Button_2"
text: "Or Click Me"
onClicked: resultsView.appendText("Button 2 clicked")
Layout.preferredHeight: 200

Button {
objectName: "Button_1"
text: "Press Me"
onClicked: resultsView.appendText("Button 1 clicked")
}
Button {
objectName: "Button_2"
text: "Or Click Me"
onClicked: resultsView.appendText("Button 2 clicked")
}

Rectangle {
id: rectangle
color: "lightgray"

Layout.preferredWidth: 200
Layout.preferredHeight: 100

MouseArea {
id: mouseArea_1
anchors.fill: parent

onClicked: resultsView.appendText("MouseArea 1 clicked")
}

RowLayout {
Button {
objectName: "Button_3"
text: "Press Me"
onClicked: resultsView.appendText("Button 3 clicked")
}
Button {
objectName: "Button_4"
text: "Or Click Me"
onClicked: resultsView.appendText("Button 4 clicked")
}
}

MouseArea {
id: mouseArea_2
anchors.fill: parent

onClicked: resultsView.appendText("MouseArea 2 clicked")
}
}
}
}

ResultsView {
id: resultsView
anchors {
top: parent.verticalCenter
left: parent.left
right: parent.right
bottom: parent.bottom
ResultsView {
id: resultsView
}
}
}
Loading