-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathintegration_test.ts
More file actions
2022 lines (1810 loc) · 56.1 KB
/
Copy pathintegration_test.ts
File metadata and controls
2022 lines (1810 loc) · 56.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
AudioGuidance,
CameraPerspective,
TravelMode,
NavigationSessionStatus,
RouteStatus,
type ArrivalEvent,
type ContinueToNextDestinationResponse,
type LatLng,
type Location,
type MapViewController,
type NavigationController,
type NavigationViewController,
type TimeAndDistance,
type TurnByTurnEvent,
} from '@googlemaps/react-native-navigation-sdk';
import { Platform } from 'react-native';
import { delay, roundDown } from './utils';
import { NIGHT_MODE_STYLE } from '../../styles/mapStyles';
interface TestTools {
navigationController: NavigationController;
mapViewController: MapViewController | null;
navigationViewController: NavigationViewController | null;
setOnNavigationReady: (listener: (() => void) | null | undefined) => void;
setOnArrival: (
listener: ((arrivalEvent: ArrivalEvent) => void) | null | undefined
) => void;
setOnRemainingTimeOrDistanceChanged: (
listener: ((timeAndDistance: TimeAndDistance) => void) | null | undefined
) => void;
setOnRouteChanged: (listener: (() => void) | null | undefined) => void;
setOnLocationChanged: (
listener: ((location: Location) => void) | null | undefined
) => void;
setOnTurnByTurn: (
listener: ((turnByTurnEvents: TurnByTurnEvent[]) => void) | null | undefined
) => void;
passTest: () => void;
failTest: (message: string) => void;
setDetoxStep: (stepNumber: number) => void;
expectFalseError: (expectation: string) => void;
expectTrueError: (expectation: string) => void;
// UI settings setters for props-based testing
setCompassEnabled: (enabled: boolean | undefined) => void;
setRotateGesturesEnabled: (enabled: boolean | undefined) => void;
setScrollGesturesEnabled: (enabled: boolean | undefined) => void;
setScrollGesturesDuringRotateOrZoomEnabled: (
enabled: boolean | undefined
) => void;
setTiltGesturesEnabled: (enabled: boolean | undefined) => void;
setZoomGesturesEnabled: (enabled: boolean | undefined) => void;
setZoomControlsEnabled: (enabled: boolean | undefined) => void;
setMapToolbarEnabled: (enabled: boolean | undefined) => void;
setMapStyle: (style: string | undefined) => void;
setMinZoomLevel: (level: number | undefined) => void;
setMaxZoomLevel: (level: number | undefined) => void;
}
const NAVIGATOR_NOT_READY_ERROR_CODE = 'NO_NAVIGATOR_ERROR_CODE';
const NO_DESTINATIONS_ERROR_CODE = 'NO_DESTINATIONS';
export const NO_ERRORS_DETECTED_LABEL = 'No errors detected';
type NativeModuleError = {
code?: string;
};
const extractNativeErrorCode = (error: unknown): string | undefined => {
if (typeof error === 'object' && error !== null) {
const nativeError = error as NativeModuleError;
if (typeof nativeError.code === 'string') {
return nativeError.code;
}
}
return undefined;
};
const isNavigatorUnavailableError = (code?: string): boolean =>
code === NAVIGATOR_NOT_READY_ERROR_CODE;
/**
* Helper function to reset and show the ToS dialog.
* This should be called at the start of tests that require ToS acceptance.
* The dialog will block until the user (or Detox) accepts it.
*
* @param navigationController - The navigation controller
* @param failTest - Function to call if acceptance fails
* @returns true if ToS was accepted, false otherwise
*/
const acceptToS = async (
navigationController: NavigationController,
failTest: (message: string) => void
): Promise<boolean> => {
// Reset ToS acceptance state to ensure dialog is shown
await navigationController.resetTermsAccepted();
// Show the ToS dialog - Detox will tap the accept button
const accepted = await navigationController.showTermsAndConditionsDialog();
if (!accepted) {
failTest('Terms and Conditions were not accepted');
return false;
}
return true;
};
/**
* Helper function to initialize navigation after ToS is accepted.
*
* @param navigationController - The navigation controller
* @param failTest - Function to call if initialization fails
* @returns true if initialization succeeded, false otherwise
*/
const initializeNavigation = async (
navigationController: NavigationController,
failTest: (message: string) => void
): Promise<boolean> => {
const status = await navigationController.init();
if (status !== NavigationSessionStatus.OK) {
failTest(`Navigation initialization failed with status: ${status}`);
return false;
}
return true;
};
const DEFAULT_TEST_WAYPOINT = {
title: 'Grace Cathedral',
position: {
lat: 37.791957,
lng: -122.412529,
},
};
const DEFAULT_POLL_RETRY_COUNT = 10;
const DEFAULT_POLL_RETRY_DELAY_MS = 250;
const waitForCondition = async <T>(
callFn: () => Promise<T>,
predicate: (value: T) => boolean,
attempts = DEFAULT_POLL_RETRY_COUNT,
delayMs = DEFAULT_POLL_RETRY_DELAY_MS
): Promise<T | null> => {
for (let attempt = 0; attempt < attempts; attempt++) {
const result = await callFn();
if (predicate(result)) {
return result;
}
await delay(delayMs);
}
return null;
};
const waitForTimeAndDistance = async (
navigationController: NavigationController,
attempts = DEFAULT_POLL_RETRY_COUNT,
delayMs = DEFAULT_POLL_RETRY_DELAY_MS
): Promise<TimeAndDistance | null> =>
waitForCondition<TimeAndDistance | null>(
() => navigationController.getCurrentTimeAndDistance(),
result => result !== null,
attempts,
delayMs
);
const disableVoiceGuidanceForTests = (
navigationController: NavigationController
) => {
navigationController.setAudioGuidanceType(AudioGuidance.SILENT);
};
const LOCATION_THRESHOLD_METERS = 100;
const LOCATION_WAIT_TIMEOUT_MS = 15000;
const distanceBetween = (a: LatLng, b: LatLng): number => {
const toRad = (deg: number) => (deg * Math.PI) / 180;
const R = 6371000;
const dLat = toRad(b.lat - a.lat);
const dLng = toRad(b.lng - a.lng);
const sinDLat = Math.sin(dLat / 2);
const sinDLng = Math.sin(dLng / 2);
const h =
sinDLat * sinDLat +
Math.cos(toRad(a.lat)) * Math.cos(toRad(b.lat)) * sinDLng * sinDLng;
return R * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
};
const simulateAndWaitForLocation = (
navigationController: NavigationController,
setOnLocationChanged: (
listener: ((location: Location) => void) | null | undefined
) => void,
target: LatLng,
thresholdMeters = LOCATION_THRESHOLD_METERS,
timeoutMs = LOCATION_WAIT_TIMEOUT_MS
): Promise<Location | null> => {
return new Promise(resolve => {
const timer = setTimeout(() => {
setOnLocationChanged(null);
resolve(null);
}, timeoutMs);
setOnLocationChanged((location: Location) => {
if (distanceBetween(location, target) <= thresholdMeters) {
clearTimeout(timer);
setOnLocationChanged(null);
resolve(location);
}
});
// Ensure the road-snapped location provider is active so that
// onLocationChanged events are emitted for simulated locations.
navigationController.startUpdatingLocation();
navigationController.simulator.simulateLocation(target);
});
};
export const testNavigationSessionInitialization = async (
testTools: TestTools
) => {
const {
navigationController,
setOnNavigationReady,
passTest,
failTest,
expectTrueError,
} = testTools;
// Accept ToS first
if (!(await acceptToS(navigationController, failTest))) {
return;
}
const checkDefaults = async () => {
// After successful init, terms should be accepted
if (!(await navigationController.areTermsAccepted())) {
return expectTrueError('navigationController.areTermsAccepted()');
}
passTest();
};
setOnNavigationReady(() => {
disableVoiceGuidanceForTests(navigationController);
checkDefaults();
});
// Now initialize navigation
await initializeNavigation(navigationController, failTest);
};
export const testMapInitialization = async (testTools: TestTools) => {
const {
mapViewController,
passTest,
failTest,
expectFalseError,
setCompassEnabled,
setRotateGesturesEnabled,
setScrollGesturesEnabled,
setScrollGesturesDuringRotateOrZoomEnabled,
setTiltGesturesEnabled,
setZoomGesturesEnabled,
setZoomControlsEnabled,
setMapToolbarEnabled,
} = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Disable all UI settings via props
setCompassEnabled(false);
setRotateGesturesEnabled(false);
setScrollGesturesEnabled(false);
setScrollGesturesDuringRotateOrZoomEnabled(false);
setTiltGesturesEnabled(false);
setZoomGesturesEnabled(false);
if (Platform.OS === 'android') {
setZoomControlsEnabled(false);
setMapToolbarEnabled(false);
}
const uiSettingsAfterDisable = await waitForCondition(
() => mapViewController.getUiSettings(),
settings =>
!settings.isZoomGesturesEnabled &&
(Platform.OS !== 'android' || !settings.isMapToolbarEnabled)
);
if (!uiSettingsAfterDisable) {
return expectFalseError(
'mapViewController UI settings did not disable as expected'
);
}
if (uiSettingsAfterDisable.isCompassEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isCompassEnabled'
);
}
if (uiSettingsAfterDisable.isRotateGesturesEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isRotateGesturesEnabled'
);
}
if (uiSettingsAfterDisable.isScrollGesturesEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isScrollGesturesEnabled'
);
}
if (uiSettingsAfterDisable.isScrollGesturesEnabledDuringRotateOrZoom) {
return expectFalseError(
'mapViewController.getUiSettings()).isScrollGesturesEnabledDuringRotateOrZoom'
);
}
if (uiSettingsAfterDisable.isTiltGesturesEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isTiltGesturesEnabled'
);
}
if (uiSettingsAfterDisable.isZoomGesturesEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isZoomGesturesEnabled'
);
}
if (Platform.OS === 'android') {
if (uiSettingsAfterDisable.isZoomControlsEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isZoomControlsEnabled'
);
}
if (uiSettingsAfterDisable.isMapToolbarEnabled) {
return expectFalseError(
'mapViewController.getUiSettings()).isMapToolbarEnabled'
);
}
}
// Enable all UI settings via props
setCompassEnabled(true);
setRotateGesturesEnabled(true);
setScrollGesturesEnabled(true);
setScrollGesturesDuringRotateOrZoomEnabled(true);
setTiltGesturesEnabled(true);
setZoomGesturesEnabled(true);
if (Platform.OS === 'android') {
setZoomControlsEnabled(true);
setMapToolbarEnabled(true);
}
const uiSettingsAfterEnable = await waitForCondition(
() => mapViewController.getUiSettings(),
settings =>
settings.isZoomGesturesEnabled &&
(Platform.OS !== 'android' || settings.isMapToolbarEnabled)
);
if (!uiSettingsAfterEnable) {
return expectFalseError(
'mapViewController UI settings did not enable as expected'
);
}
if (!uiSettingsAfterEnable.isCompassEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isCompassEnabled'
);
}
if (!uiSettingsAfterEnable.isRotateGesturesEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isRotateGesturesEnabled'
);
}
if (!uiSettingsAfterEnable.isScrollGesturesEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isScrollGesturesEnabled'
);
}
if (!uiSettingsAfterEnable.isScrollGesturesEnabledDuringRotateOrZoom) {
return expectFalseError(
'!mapViewController.getUiSettings()).isScrollGesturesEnabledDuringRotateOrZoom'
);
}
if (!uiSettingsAfterEnable.isTiltGesturesEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isTiltGesturesEnabled'
);
}
if (!uiSettingsAfterEnable.isZoomGesturesEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isZoomGesturesEnabled'
);
}
if (Platform.OS === 'android') {
if (!uiSettingsAfterEnable.isZoomControlsEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isZoomControlsEnabled'
);
}
if (!uiSettingsAfterEnable.isMapToolbarEnabled) {
return expectFalseError(
'!mapViewController.getUiSettings()).isMapToolbarEnabled'
);
}
}
passTest();
};
export const testNavigationToSingleDestination = async (
testTools: TestTools
) => {
const {
navigationController,
setOnNavigationReady,
setOnArrival,
setOnLocationChanged,
passTest,
failTest,
} = testTools;
// Accept ToS first
if (!(await acceptToS(navigationController, failTest))) {
return;
}
const startLocation: LatLng = { lat: 37.4195823, lng: -122.0799018 };
setOnNavigationReady(async () => {
disableVoiceGuidanceForTests(navigationController);
const located = await simulateAndWaitForLocation(
navigationController,
setOnLocationChanged,
startLocation
);
if (!located) {
return failTest(
'Timed out waiting for simulated location to be confirmed'
);
}
await navigationController.setDestinations(
[
{
position: {
lat: 37.418761,
lng: -122.080484,
},
},
],
{
routingOptions: {
travelMode: TravelMode.DRIVING,
avoidFerries: true,
avoidTolls: false,
},
}
);
await navigationController.startGuidance();
const routeSegments = await waitForCondition(
() => navigationController.getRouteSegments(),
segments => segments.length > 0
);
if (!routeSegments) {
return failTest(
'Timed out waiting for route segments before starting simulation'
);
}
await navigationController.simulator.simulateLocationsAlongExistingRoute({
speedMultiplier: Platform.OS === 'ios' ? 5 : 10,
});
});
setOnArrival(async () => {
navigationController.cleanup();
return passTest();
});
await initializeNavigation(navigationController, failTest);
};
export const testNavigationToMultipleDestination = async (
testTools: TestTools
) => {
const {
navigationController,
setOnNavigationReady,
setOnArrival,
setOnLocationChanged,
passTest,
failTest,
} = testTools;
// Accept ToS first
if (!(await acceptToS(navigationController, failTest))) {
return;
}
const startLocation: LatLng = {
lat: 37.79136614772824,
lng: -122.41565900473043,
};
let onArrivalCount = 0;
setOnNavigationReady(async () => {
disableVoiceGuidanceForTests(navigationController);
const located = await simulateAndWaitForLocation(
navigationController,
setOnLocationChanged,
startLocation
);
if (!located) {
return failTest(
'Timed out waiting for simulated location to be confirmed'
);
}
await navigationController.setDestinations(
[
{
position: {
lat: 37.7917,
lng: -122.4142,
},
},
{
position: {
lat: 37.79196,
lng: -122.41253,
},
},
],
{
routingOptions: {
travelMode: TravelMode.DRIVING,
avoidFerries: true,
avoidTolls: false,
},
}
);
await navigationController.startGuidance();
const routeSegments = await waitForCondition(
() => navigationController.getRouteSegments(),
segments => segments.length > 0
);
if (!routeSegments) {
return failTest(
'Timed out waiting for route segments before starting simulation'
);
}
await navigationController.simulator.simulateLocationsAlongExistingRoute({
speedMultiplier: 5,
});
});
setOnArrival(async () => {
onArrivalCount += 1;
if (onArrivalCount > 1) {
navigationController.cleanup();
return passTest();
}
const response: ContinueToNextDestinationResponse =
await navigationController.continueToNextDestination();
if (!response.waypoint) {
return failTest(
'continueToNextDestination returned null waypoint when next destination exists'
);
}
if (
Platform.OS === 'ios' &&
!Object.values(RouteStatus).includes(response.routeStatus as RouteStatus)
) {
return failTest(
`continueToNextDestination returned unexpected routeStatus on iOS: ${response.routeStatus}`
);
}
await navigationController.startGuidance();
const nextRouteSegments = await waitForCondition(
() => navigationController.getRouteSegments(),
segments => segments.length > 0
);
if (!nextRouteSegments) {
return failTest(
'Timed out waiting for route segments after continueToNextDestination'
);
}
await navigationController.simulator.simulateLocationsAlongExistingRoute({
speedMultiplier: 5,
});
});
await initializeNavigation(navigationController, failTest);
};
export const testRouteSegments = async (testTools: TestTools) => {
const {
navigationController,
setOnNavigationReady,
setOnArrival,
setOnLocationChanged,
passTest,
failTest,
expectFalseError,
} = testTools;
// Accept ToS first
if (!(await acceptToS(navigationController, failTest))) {
return;
}
const startLocation: LatLng = {
lat: 37.79136614772824,
lng: -122.41565900473043,
};
let beginTraveledPath;
setOnNavigationReady(async () => {
disableVoiceGuidanceForTests(navigationController);
const located = await simulateAndWaitForLocation(
navigationController,
setOnLocationChanged,
startLocation
);
if (!located) {
return failTest(
'Timed out waiting for simulated location to be confirmed'
);
}
await navigationController.setDestination({
title: 'Grace Cathedral',
position: {
lat: 37.791957,
lng: -122.412529,
},
});
await navigationController.startGuidance();
const beginRouteSegments = await waitForCondition(
() => navigationController.getRouteSegments(),
segments => segments.length > 0
);
if (!beginRouteSegments) {
expectFalseError('beginRouteSegments.length === 0');
return;
}
const beginCurrentRouteSegment = await waitForCondition(
() => navigationController.getCurrentRouteSegment(),
segment => segment !== null
);
if (!beginCurrentRouteSegment) {
return expectFalseError('!beginCurrentRouteSegment');
}
beginTraveledPath = await navigationController.getTraveledPath();
await navigationController.simulator.simulateLocationsAlongExistingRoute({
speedMultiplier: 5,
});
});
setOnArrival(async () => {
const endTraveledPath = await navigationController.getTraveledPath();
if (endTraveledPath.length <= beginTraveledPath.length) {
return expectFalseError(
'endTraveledPath.length <= beginTraveledPath.length'
);
}
navigationController.cleanup();
passTest();
});
await initializeNavigation(navigationController, failTest);
};
export const testGetCurrentTimeAndDistance = async (testTools: TestTools) => {
const {
navigationController,
setOnNavigationReady,
setOnArrival,
setOnLocationChanged,
passTest,
failTest,
expectFalseError,
} = testTools;
// Accept ToS first
if (!(await acceptToS(navigationController, failTest))) {
return;
}
const startLocation: LatLng = {
lat: 37.79136614772824,
lng: -122.41565900473043,
};
let beginTimeAndDistance: TimeAndDistance | null = null;
setOnNavigationReady(async () => {
disableVoiceGuidanceForTests(navigationController);
const located = await simulateAndWaitForLocation(
navigationController,
setOnLocationChanged,
startLocation
);
if (!located) {
return failTest(
'Timed out waiting for simulated location to be confirmed'
);
}
await navigationController.setDestination({
title: 'Grace Cathedral',
position: {
lat: 37.791957,
lng: -122.412529,
},
});
await navigationController.startGuidance();
beginTimeAndDistance = await waitForTimeAndDistance(navigationController);
if (!beginTimeAndDistance) {
return failTest(
'initialTimeAndDistance is null (navigationController.getCurrentTimeAndDistance())'
);
}
if (beginTimeAndDistance.seconds <= 0) {
return expectFalseError('beginTimeAndDistance.seconds <= 0');
}
if (beginTimeAndDistance.meters <= 0) {
return expectFalseError('beginTimeAndDistance.meters <= 0');
}
await navigationController.simulator.simulateLocationsAlongExistingRoute({
speedMultiplier: 5,
});
});
setOnArrival(async () => {
const endTimeAndDistance =
await waitForTimeAndDistance(navigationController);
if (!endTimeAndDistance) {
return expectFalseError(
'endTimeAndDistance is null (navigationController.getCurrentTimeAndDistance())'
);
}
if (!beginTimeAndDistance) {
return expectFalseError('beginTimeAndDistance is null');
}
if (endTimeAndDistance.meters >= beginTimeAndDistance.meters) {
return expectFalseError(
'endTimeAndDistance.meters >= beginTimeAndDistance.meters'
);
}
if (endTimeAndDistance.seconds >= beginTimeAndDistance.seconds) {
return expectFalseError(
'endTimeAndDistance.seconds >= beginTimeAndDistance.seconds'
);
}
navigationController.cleanup();
passTest();
});
await initializeNavigation(navigationController, failTest);
};
export const testMoveCamera = async (testTools: TestTools) => {
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Move camera to Hong Kong
mapViewController.moveCamera({
target: {
lat: 22.2987849,
lng: 114.1719271,
},
});
const hongKongPosition = await waitForCondition(
() => mapViewController.getCameraPosition(),
position =>
roundDown(position.target.lat) === 22 &&
roundDown(position.target.lng) === 114
);
if (!hongKongPosition) {
expectFalseError(
'roundDown(hongKongPosition.target.lat) !== 22 || roundDown(hongKongPosition.target.lng) !== 114'
);
}
// Move camera to Tokyo
mapViewController.moveCamera({
target: {
lat: 35.6805707,
lng: 139.7658596,
},
});
const tokyoPosition = await waitForCondition(
() => mapViewController.getCameraPosition(),
position =>
roundDown(position.target.lat) === 35 &&
roundDown(position.target.lng) === 139
);
if (!tokyoPosition) {
expectFalseError(
'roundDown(tokyoPosition.target.lat) !== 35 || roundDown(tokyoPosition.target.lng) !== 139'
);
}
passTest();
};
export const testAnimateCamera = async (testTools: TestTools) => {
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Animate camera to Hong Kong
await mapViewController.animateCamera({
target: {
lat: 22.2987849,
lng: 114.1719271,
},
});
const hongKongPosition = await waitForCondition(
() => mapViewController.getCameraPosition(),
position =>
roundDown(position.target.lat) === 22 &&
roundDown(position.target.lng) === 114
);
if (!hongKongPosition) {
expectFalseError(
'roundDown(hongKongPosition.target.lat) !== 22 || roundDown(hongKongPosition.target.lng) !== 114'
);
}
// Animate camera to Tokyo
await mapViewController.animateCamera({
target: {
lat: 35.6805707,
lng: 139.7658596,
},
});
const tokyoPosition = await waitForCondition(
() => mapViewController.getCameraPosition(),
position =>
roundDown(position.target.lat) === 35 &&
roundDown(position.target.lng) === 139
);
if (!tokyoPosition) {
expectFalseError(
'roundDown(tokyoPosition.target.lat) !== 35 || roundDown(tokyoPosition.target.lng) !== 139'
);
}
passTest();
};
export const testTiltZoomBearingCamera = async (testTools: TestTools) => {
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Move camera to Hong Kong and set bearing, tilt and zoom.
mapViewController.moveCamera({
target: {
lat: 22.2987849,
lng: 114.1719271,
},
bearing: 270,
tilt: 20,
zoom: 6,
});
const hongKongPosition = await waitForCondition(
() => mapViewController.getCameraPosition(),
position =>
position.bearing === 270 && position.tilt === 20 && position.zoom === 6
);
if (!hongKongPosition) {
expectFalseError(
'hongKongPosition.bearing !== 270 || hongKongPosition.tilt !== 20 || hongKongPosition.zoom !== 6'
);
}
passTest();
};
export const testMapMarkers = async (testTools: TestTools) => {
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Test adding a marker
const marker = await mapViewController.addMarker({
position: { lat: 37.7749, lng: -122.4194 },
title: 'San Francisco',
snippet: 'Test marker snippet',
alpha: 0.8,
rotation: 45,
});
if (!marker.id) {
return expectFalseError('marker.id should exist');
}
if (marker.position.lat !== 37.7749 || marker.position.lng !== -122.4194) {
return expectFalseError('marker.position should match input');
}
if (marker.title !== 'San Francisco') {
return expectFalseError('marker.title should be "San Francisco"');
}
// Test getMarkers returns the marker
let markers = await mapViewController.getMarkers();
if (markers.length !== 1) {
return expectFalseError('getMarkers should return 1 marker');
}
if (markers[0]!.id !== marker.id) {
return expectFalseError('getMarkers should return marker with correct id');
}
// Test updating marker with same ID (should update, not create new)
const updatedMarker = await mapViewController.addMarker({
id: marker.id,
position: { lat: 37.7849, lng: -122.4294 },
title: 'Updated San Francisco',
snippet: 'Updated snippet',
alpha: 1.0,
rotation: 90,
});
if (updatedMarker.id !== marker.id) {
return expectFalseError('updatedMarker.id should match original marker.id');
}
if (updatedMarker.title !== 'Updated San Francisco') {
return expectFalseError(
'updatedMarker.title should be "Updated San Francisco"'
);
}
// Verify only one marker exists (update, not add)
markers = await mapViewController.getMarkers();
if (markers.length !== 1) {
return expectFalseError(
'getMarkers should still return 1 marker after update'
);
}
// Test removing marker
await mapViewController.removeMarker(marker.id);
// Verify marker was removed
markers = await mapViewController.getMarkers();
if (markers.length !== 0) {
return expectFalseError('getMarkers should return 0 markers after removal');
}
// Test adding marker with custom image
const markerWithIcon = await mapViewController.addMarker({
position: { lat: 37.7849, lng: -122.4094 },
title: 'Marker with Icon',
imgPath: 'circle.png',
});
if (!markerWithIcon.id) {
return expectFalseError('markerWithIcon.id should exist');
}
await mapViewController.removeMarker(markerWithIcon.id);
passTest();
};
export const testMapCircles = async (testTools: TestTools) => {
const { mapViewController, passTest, failTest, expectFalseError } = testTools;
if (!mapViewController) {
return failTest('mapViewController was expected to exist');
}
// Test adding a circle
const circle = await mapViewController.addCircle({
center: { lat: 37.7749, lng: -122.4194 },
radius: 1000,
strokeWidth: 2,
strokeColor: '#FF0000',
fillColor: '#00FF0080',
clickable: true,
});
if (!circle.id) {
return expectFalseError('circle.id should exist');