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