Skip to content

Commit d103252

Browse files
authored
chore: improve patrol tests (#197)
* test: instead of for loop, use variants for patrol tests * test: improve network error test * chore: meta package version dependency for testing
1 parent 67770c7 commit d103252

File tree

5 files changed

+1012
-1005
lines changed

5 files changed

+1012
-1005
lines changed

example/integration_test/shared.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import 'dart:async';
1616
import 'dart:io';
17+
import 'package:meta/meta.dart';
1718

1819
// This is a basic Flutter integration test.
1920
//
@@ -36,17 +37,23 @@ export 'package:patrol/patrol.dart';
3637
// Type used for iterating over different maps to be tested.
3738
enum TestMapType {
3839
/// Regular google map view.
39-
map,
40+
mapView,
4041

41-
/// Navigation map.
42-
navigation,
42+
/// Navigation map view.
43+
navigationView,
4344
}
4445

46+
// Map types to be tested
4547
const List<TestMapType> testMapTypes = <TestMapType>[
46-
TestMapType.map,
47-
TestMapType.navigation
48+
TestMapType.mapView,
49+
TestMapType.navigationView
4850
];
4951

52+
// Get a variants for the test map types.
53+
ValueVariant<TestMapType> getMapTypeVariants() {
54+
return ValueVariant<TestMapType>(testMapTypes.toSet());
55+
}
56+
5057
/// Location coordinates for starting position simulation in Finland - Näkkäläntie.
5158
const double startLocationLat = 68.593793;
5259
const double startLocationLng = 23.510763;
@@ -59,12 +66,14 @@ const NativeAutomatorConfig _nativeAutomatorConfig = NativeAutomatorConfig(
5966
);
6067

6168
/// Create a wrapper [patrol] for [patrolTest] with custom options.
69+
@isTest
6270
void patrol(
6371
String description,
6472
Future<void> Function(PatrolIntegrationTester) callback, {
6573
bool skip = false,
6674
int timeoutSeconds = testTimeoutSeconds,
6775
NativeAutomatorConfig? nativeAutomatorConfig,
76+
TestVariant<Object?> variant = const DefaultTestVariant(),
6877
}) {
6978
/// The patrolTest skip functionality does not work as expected and can
7079
/// hang the test execution.
@@ -74,10 +83,12 @@ void patrol(
7483
debugPrint('Skipping test: $description');
7584
return;
7685
}
86+
7787
patrolTest(
7888
description,
7989
callback,
8090
skip: skip,
91+
variant: variant,
8192
timeout: Timeout(Duration(seconds: timeoutSeconds)),
8293
nativeAutomatorConfig: nativeAutomatorConfig ?? _nativeAutomatorConfig,
8394
);
@@ -265,7 +276,7 @@ Future<GoogleMapViewController> getMapViewControllerForTestMapType(
265276

266277
switch (testMapType) {
267278
/// Set up map.
268-
case TestMapType.map:
279+
case TestMapType.mapView:
269280
viewController = await startMapView(
270281
$,
271282
onMarkerClicked: onMarkerClicked,
@@ -286,7 +297,7 @@ Future<GoogleMapViewController> getMapViewControllerForTestMapType(
286297
break;
287298

288299
/// Set up navigation map.
289-
case TestMapType.navigation:
300+
case TestMapType.navigationView:
290301
viewController = await startNavigationWithoutDestination($,
291302
initializeNavigation: initializeNavigation,
292303
simulateLocation: simulateLocation,

example/integration_test/t03_navigation_test.dart

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -598,23 +598,30 @@ void main() {
598598
displayOptions: NavigationDisplayOptions(showDestinationMarkers: false),
599599
);
600600

601-
/// Cut network connection.
602-
await $.native.disableCellular();
603-
await $.native.disableWifi();
604-
605-
/// Test that the error is received.
606-
final NavigationRouteStatus routeStatus =
607-
await GoogleMapsNavigator.setDestinations(destinations);
608-
expect(routeStatus, equals(NavigationRouteStatus.networkError));
609-
610-
final NavigationRouteStatus routeStatusSim = await GoogleMapsNavigator
611-
.simulator
612-
.simulateLocationsAlongNewRoute(waypoint);
613-
expect(routeStatusSim, equals(NavigationRouteStatus.networkError));
614-
615-
/// Re-enable network connection.
616-
await $.native.enableCellular();
617-
await $.native.enableWifi();
601+
try {
602+
/// Cut network connection.
603+
await $.native.disableCellular();
604+
await $.native.disableWifi();
605+
606+
// Wait a while to ensure network is down.
607+
await $.pumpAndSettle(duration: const Duration(seconds: 1));
608+
609+
/// Test that the error is received.
610+
final NavigationRouteStatus routeStatus =
611+
await GoogleMapsNavigator.setDestinations(destinations);
612+
expect(routeStatus, equals(NavigationRouteStatus.networkError),
613+
reason: 'setDestinations did not return networkError');
614+
615+
final NavigationRouteStatus routeStatusSim = await GoogleMapsNavigator
616+
.simulator
617+
.simulateLocationsAlongNewRoute(waypoint);
618+
expect(routeStatusSim, equals(NavigationRouteStatus.networkError),
619+
reason: 'simulateLocationsAlongNewRoute did not return networkError');
620+
} finally {
621+
/// Re-enable network connection.
622+
await $.native.enableCellular();
623+
await $.native.enableWifi();
624+
}
618625
}, skip: Platform.isIOS && !isPhysicalDevice);
619626

620627
patrol('Test route not found errors', (PatrolIntegrationTester $) async {

example/integration_test/t06_map_test.dart

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import 'package:flutter/material.dart';
2626
import 'shared.dart';
2727

2828
void main() {
29-
for (final TestMapType testMapType in testMapTypes) {
30-
patrol('Test map types (${testMapType.name})',
31-
(PatrolIntegrationTester $) async {
29+
final mapTypeVariants = getMapTypeVariants();
30+
patrol(
31+
'Test map types',
32+
(PatrolIntegrationTester $) async {
3233
/// Get viewController for the test type (navigation map or regular map).
3334
GoogleMapViewController viewController =
34-
await getMapViewControllerForTestMapType($, testMapType: testMapType);
35+
await getMapViewControllerForTestMapType($,
36+
testMapType: mapTypeVariants.currentValue!);
3537

3638
// Test default type.
3739
expect(await viewController.getMapType(), MapType.normal);
@@ -47,17 +49,18 @@ void main() {
4749
await viewController.setMapType(mapType: type);
4850
expect(await viewController.getMapType(), type);
4951
}
50-
});
51-
}
52+
},
53+
variant: mapTypeVariants,
54+
);
5255

53-
for (final TestMapType testMapType in testMapTypes) {
54-
patrol('Test platform view creation params (${testMapType.name})',
55-
(PatrolIntegrationTester $) async {
56+
patrol(
57+
'Test platform view creation params',
58+
(PatrolIntegrationTester $) async {
5659
final Completer<GoogleMapViewController> controllerCompleter =
5760
Completer<GoogleMapViewController>();
5861

59-
switch (testMapType) {
60-
case TestMapType.map:
62+
switch (mapTypeVariants.currentValue!) {
63+
case TestMapType.mapView:
6164
final Key key = GlobalKey();
6265
await pumpMapView(
6366
$,
@@ -78,7 +81,7 @@ void main() {
7881
),
7982
);
8083
break;
81-
case TestMapType.navigation:
84+
case TestMapType.navigationView:
8285
final Key key = GlobalKey();
8386
await pumpNavigationView(
8487
$,
@@ -119,12 +122,13 @@ void main() {
119122
expect(await controller.settings.isZoomControlsEnabled(), false);
120123
expect(await controller.settings.isMapToolbarEnabled(), false);
121124
}
122-
});
123-
}
125+
},
126+
variant: mapTypeVariants,
127+
);
124128

125-
for (final TestMapType testMapType in testMapTypes) {
126-
patrol('Test map UI settings (${testMapType.name})',
127-
(PatrolIntegrationTester $) async {
129+
patrol(
130+
'Test map UI settings',
131+
(PatrolIntegrationTester $) async {
128132
/// The events are not tested because there's currently no reliable way to trigger them.
129133
void onMyLocationButtonClicked(MyLocationButtonClickedEvent event) {
130134
debugPrint('My location button clicked event: currently $event');
@@ -149,7 +153,7 @@ void main() {
149153
/// Get viewController for the test type (navigation map or regular map).
150154
await getMapViewControllerForTestMapType(
151155
$,
152-
testMapType: testMapType,
156+
testMapType: mapTypeVariants.currentValue!,
153157
initializeNavigation: false,
154158
onMapClicked: expectAsync1((LatLng msg) {
155159
expectSync(msg, isA<LatLng>());
@@ -247,15 +251,17 @@ void main() {
247251
expect(e, const TypeMatcher<UnsupportedError>());
248252
}
249253
}
250-
});
251-
}
254+
},
255+
variant: mapTypeVariants,
256+
);
252257

253-
for (final TestMapType testMapType in testMapTypes) {
254-
patrol('Test map style (${testMapType.name})',
255-
(PatrolIntegrationTester $) async {
258+
patrol(
259+
'Test map style',
260+
(PatrolIntegrationTester $) async {
256261
/// Get viewController for the test type (navigation map or regular map).
257262
GoogleMapViewController viewController =
258-
await getMapViewControllerForTestMapType($, testMapType: testMapType);
263+
await getMapViewControllerForTestMapType($,
264+
testMapType: mapTypeVariants.currentValue!);
259265

260266
// Test that valid json doens't throw exception.
261267
await viewController.setMapStyle(
@@ -271,21 +277,22 @@ void main() {
271277
} on MapStyleException catch (e) {
272278
expect(e, isNotNull);
273279
}
274-
});
275-
}
280+
},
281+
variant: mapTypeVariants,
282+
);
276283

277-
for (final TestMapType testMapType in testMapTypes) {
278-
patrol('Test min max zoom level (${testMapType.name})',
279-
(PatrolIntegrationTester $) async {
284+
patrol(
285+
'Test min max zoom level',
286+
(PatrolIntegrationTester $) async {
280287
/// For some reason the functionality works on Android example app, but it doesn't work
281288
/// during the testing. Will skip Android testing for now.
282289
final Completer<GoogleMapViewController> viewControllerCompleter =
283290
Completer<GoogleMapViewController>();
284291

285292
await checkLocationDialogAcceptance($);
286293

287-
switch (testMapType) {
288-
case TestMapType.map:
294+
switch (mapTypeVariants.currentValue!) {
295+
case TestMapType.mapView:
289296

290297
/// Display map view.
291298
final Key key = GlobalKey();
@@ -299,7 +306,7 @@ void main() {
299306
),
300307
);
301308
break;
302-
case TestMapType.navigation:
309+
case TestMapType.navigationView:
303310

304311
/// Display navigation view.
305312
final Key key = GlobalKey();
@@ -363,6 +370,7 @@ void main() {
363370
// Expect the same values. The actual zoom level will be limited by the map.
364371
expect(newMinZoomPreference, 0.0);
365372
expect(newMaxZoomPreference, 50.0);
366-
});
367-
}
373+
},
374+
variant: mapTypeVariants,
375+
);
368376
}

0 commit comments

Comments
 (0)