Skip to content

Commit 534e88f

Browse files
committed
add qml tests
1 parent 5fc8243 commit 534e88f

6 files changed

Lines changed: 224 additions & 15 deletions

File tree

.github/workflows/tests.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ on:
66
workflow_dispatch:
77

88
jobs:
9-
Widgets:
9+
Test:
10+
name: ${{ matrix.os }} - ${{ matrix.config_name }}
1011
strategy:
12+
fail-fast: false
1113
matrix:
1214
os: [ubuntu-latest, windows-latest, macos-latest]
15+
config_name: [Widgets, QML]
16+
include:
17+
- config_name: "Widgets"
18+
cmake_flags: "-DSIMPLE_MAP_VIEW_BUILD_TESTS=ON"
19+
build_type: "Debug"
20+
21+
- config_name: "QML"
22+
cmake_flags: "-DSIMPLE_MAP_VIEW_BUILD_QML=ON -DSIMPLE_MAP_VIEW_BUILD_TESTS=ON"
23+
build_type: "Debug"
1324

1425
runs-on: ${{ matrix.os }}
1526

@@ -35,13 +46,13 @@ jobs:
3546
run: |
3647
mkdir build
3748
cd build
38-
cmake .. -DSIMPLE_MAP_VIEW_BUILD_TESTS=ON
49+
cmake .. ${{ matrix.cmake_flags }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
3950
4051
- name: Build
4152
shell: bash
4253
working-directory: build
4354
run: |
44-
cmake --build .
55+
cmake --build . --config ${{ matrix.build_type }}
4556
4657
- name: Run (Linux)
4758
if: runner.os == 'Linux'
@@ -52,4 +63,4 @@ jobs:
5263
if: runner.os != 'Linux'
5364
shell: bash
5465
working-directory: build
55-
run: ctest -C Debug --output-on-failure
66+
run: ctest -C ${{ matrix.build_type }} --output-on-failure

include/SimpleMapView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class SimpleMapView : public SimpleMapViewBase
7070
Resource
7171
};
7272

73+
#ifdef SIMPLE_MAP_VIEW_USE_QML
74+
Q_ENUM(TileServerSource);
75+
#endif
76+
7377
explicit SimpleMapView(SimpleMapViewBase* parent = nullptr);
7478
~SimpleMapView() = default;
7579

tests/CMakeLists.txt

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(SimpleMapViewTests LANGUAGES CXX)
33

4-
find_package(Qt6 REQUIRED COMPONENTS Test)
4+
if(SIMPLE_MAP_VIEW_BUILD_QML)
55

6-
add_executable(
7-
SimpleMapViewTests
8-
../Resources.qrc
9-
SimpleMapViewTest.cpp
10-
)
6+
find_package(Qt6 REQUIRED COMPONENTS QuickTest)
7+
add_executable(
8+
SimpleMapViewTests
9+
../Resources.qrc
10+
qml/SimpleMapViewTest.cpp
11+
)
12+
target_compile_definitions(
13+
SimpleMapViewTests PRIVATE
14+
QUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
15+
QML2_IMPORT_PATH="${CMAKE_BINARY_DIR}"
16+
)
17+
target_link_libraries(
18+
SimpleMapViewTests PRIVATE
19+
SimpleMapViewplugin
20+
Qt::QuickTest
21+
)
1122

12-
target_link_libraries(
13-
SimpleMapViewTests
14-
SimpleMapView
15-
Qt::Test
16-
)
23+
else()
24+
25+
find_package(Qt6 REQUIRED COMPONENTS Test)
26+
add_executable(
27+
SimpleMapViewTests
28+
../Resources.qrc
29+
widgets/SimpleMapViewTest.cpp
30+
)
31+
target_link_libraries(
32+
SimpleMapViewTests PRIVATE
33+
SimpleMapView
34+
Qt::Test
35+
)
36+
37+
endif()
1738

1839
include(CTest)
1940
add_test(NAME SimpleMapViewTests COMMAND SimpleMapViewTests)

tests/qml/SimpleMapViewTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <QtQuickTest>
2+
3+
QUICK_TEST_MAIN(SimpleMapViewTest)

tests/qml/tst_SimpleMapView.qml

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import QtQuick
2+
import QtTest
3+
import com.github.ozguronsoy.SimpleMapView
4+
5+
TestCase {
6+
name: "SimpleMapViewTests"
7+
width: 1024
8+
height: 768
9+
visible: true
10+
when: windowShown
11+
12+
SimpleMapView {
13+
id: map
14+
anchors.fill: parent
15+
focus: true
16+
}
17+
18+
SignalSpy { id: zoomSpy; target: map; signalName: "zoomLevelChanged" }
19+
SignalSpy { id: centerSpy; target: map; signalName: "centerChanged" }
20+
SignalSpy { id: tileServerSpy; target: map; signalName: "tileServerChanged" }
21+
22+
function test_instantiation() {
23+
verify(map !== null, "SimpleMapView widget failed to instantiate.")
24+
25+
compare(map.tileServer, TileServers.OSM, "Default `tileServer` should be OSM.")
26+
compare(map.tileServerSource(), SimpleMapView.Remote, "Default source should be Remote.")
27+
28+
verify(!map.isZoomLocked(), "Default `isZoomLocked` should be false")
29+
verify(!map.isGeolocationLocked(), "Default `isGeolocationLocked` should be false")
30+
verify(!map.isMouseWheelZoomDisabled(), "Default `isMouseWheelZoomDisabled` should be false")
31+
verify(!map.isMouseMoveMapDisabled(), "Default `isMouseMoveMapDisabled` should be false")
32+
}
33+
34+
function test_zoomLevel() {
35+
var minZoom = 5
36+
var maxZoom = 15
37+
var wheelTestLevel = 10
38+
39+
zoomSpy.clear()
40+
41+
map.zoomLevel = 10
42+
compare(map.zoomLevel, 10, "Failed to set zoom level.")
43+
compare(zoomSpy.count, 1)
44+
45+
map.minZoomLevel = minZoom
46+
map.maxZoomLevel = maxZoom
47+
48+
map.zoomLevel = minZoom - 1
49+
compare(map.zoomLevel, minZoom, "Failed to clamp to minimum.")
50+
51+
map.zoomLevel = maxZoom + 1
52+
compare(map.zoomLevel, maxZoom, "Failed to clamp to maximum.")
53+
54+
map.zoomLevel = wheelTestLevel
55+
56+
mouseWheel(map, map.width/2, map.height/2, -120, -120)
57+
verify(map.zoomLevel < wheelTestLevel, "Failed to zoom-out using mouse wheel.")
58+
59+
var preZoom = map.zoomLevel
60+
mouseWheel(map, map.width/2, map.height/2, 120, 120)
61+
verify(map.zoomLevel > preZoom, "Failed to zoom-in using mouse wheel.")
62+
63+
map.zoomLevel = 5
64+
zoomSpy.clear()
65+
66+
map.lockZoom = true
67+
map.zoomLevel = 12
68+
compare(map.zoomLevel, 5, "Failed to lock zoom.")
69+
compare(zoomSpy.count, 0)
70+
71+
map.lockZoom = false
72+
map.zoomLevel = 12
73+
compare(map.zoomLevel, 12, "Failed to unlock zoom.")
74+
compare(zoomSpy.count, 1)
75+
76+
map.disableMouseWheelZoom = true
77+
verify(map.isMouseWheelZoomDisabled(), "Failed to disable mouse wheel zoom.")
78+
79+
preZoom = map.zoomLevel
80+
mouseWheel(map, map.width/2, map.height/2, 120, 120)
81+
compare(map.zoomLevel, preZoom, "Mouse wheel should be ignored when disabled.")
82+
83+
map.disableMouseWheelZoom = false
84+
verify(!map.isMouseWheelZoomDisabled(), "Failed to enable mouse wheel zoom.")
85+
}
86+
87+
function test_positioning() {
88+
var expectedLat = 39.749656
89+
var expectedLon = 30.476754
90+
91+
centerSpy.clear()
92+
93+
map.latitude = expectedLat
94+
compare(map.latitude, expectedLat, "Failed to set latitude.")
95+
compare(centerSpy.count, 1)
96+
97+
map.longitude = expectedLon
98+
compare(map.longitude, expectedLon, "Failed to set longitude.")
99+
compare(centerSpy.count, 2)
100+
101+
map.setCenter(10, 20)
102+
compare(map.latitude, 10, "Failed to set center lat.")
103+
compare(map.longitude, 20, "Failed to set center lon.")
104+
105+
var startX = map.width / 2
106+
var startY = map.height / 2
107+
var initialLon = map.longitude
108+
var initialLat = map.latitude
109+
110+
mousePress(map, startX, startY, Qt.LeftButton)
111+
mouseMove(map, startX - 100, startY, 0, Qt.LeftButton)
112+
mouseRelease(map, startX - 100, startY, Qt.LeftButton)
113+
114+
verify(map.longitude > initialLon, "Failed to move map (Longitude) via mouse.")
115+
verify(map.latitude === initialLat, "Latitude should remain constant on horizontal drag.")
116+
117+
map.longitude = initialLon
118+
map.latitude = initialLat
119+
120+
mousePress(map, startX, startY, Qt.LeftButton)
121+
mouseMove(map, startX, startY + 100, 0, Qt.LeftButton)
122+
mouseRelease(map, startX, startY + 100, Qt.LeftButton)
123+
124+
verify(map.longitude === initialLon, "Failed to move map (Longitude) via mouse.")
125+
verify(map.latitude > initialLat, "Latitude should remain constant on horizontal drag.")
126+
127+
map.latitude = 10.0
128+
centerSpy.clear()
129+
130+
map.lockGeolocation = true
131+
map.latitude = 20.0
132+
compare(map.latitude, 10.0, "Failed to lock geolocation.")
133+
compare(centerSpy.count, 0)
134+
135+
map.lockGeolocation = false
136+
map.latitude = 20.0
137+
compare(map.latitude, 20.0, "Failed to unlock geolocation.")
138+
139+
map.disableMouseMoveMap = true
140+
verify(map.isMouseMoveMapDisabled(), "Failed to flag mouse move disabled.")
141+
142+
var frozenCenter = map.center
143+
mousePress(map, startX, startY, Qt.LeftButton)
144+
mouseMove(map, startX - 100, startY + 100, 0, Qt.LeftButton)
145+
mouseRelease(map, startX - 100, startY + 100, Qt.LeftButton)
146+
compare(map.center, frozenCenter, "Map should not move when mouse move is disabled.")
147+
148+
map.disableMouseMoveMap = false
149+
verify(!map.isMouseMoveMapDisabled(), "Failed to flag mouse move enabled.")
150+
}
151+
152+
function test_tileServers() {
153+
var invalidUrl = "https://asfdfdsa"
154+
155+
tileServerSpy.clear()
156+
157+
map.setTileServer(invalidUrl)
158+
wait(5000)
159+
160+
compare(map.tileServer, TileServers.OSM, "Should default to OSM on invalid server.")
161+
162+
map.setTileServer(TileServers.GOOGLE_MAP)
163+
compare(map.tileServer, TileServers.GOOGLE_MAP, "Failed to change tile server.")
164+
}
165+
166+
function test_markers() {
167+
var marker = map.addMarker(map.latitude, map.longitude)
168+
verify(marker !== null, "Failed to add marker.")
169+
}
170+
}

0 commit comments

Comments
 (0)