Skip to content

Commit 19f7ea4

Browse files
committed
[google_maps_flutter] add tests for clustermanager updates.
1 parent 0e24e2f commit 19f7ea4

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/widgets.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:google_maps_flutter/google_maps_flutter.dart';
8+
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
9+
10+
import 'fake_google_maps_flutter_platform.dart';
11+
12+
Widget _mapWithClusterManagers(Set<ClusterManager> clusterManagers) {
13+
return Directionality(
14+
textDirection: TextDirection.ltr,
15+
child: GoogleMap(
16+
initialCameraPosition: const CameraPosition(target: LatLng(10.0, 15.0)),
17+
clusterManagers: clusterManagers,
18+
),
19+
);
20+
}
21+
22+
void main() {
23+
late FakeGoogleMapsFlutterPlatform platform;
24+
25+
setUp(() {
26+
platform = FakeGoogleMapsFlutterPlatform();
27+
GoogleMapsFlutterPlatform.instance = platform;
28+
});
29+
30+
testWidgets('Initializing a cluster manager', (WidgetTester tester) async {
31+
const ClusterManager cm1 = ClusterManager(
32+
clusterManagerId: ClusterManagerId('cm_1'),
33+
);
34+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm1}));
35+
36+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
37+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.length, 1);
38+
39+
final ClusterManager initializedHeatmap =
40+
map.clusterManagerUpdates.last.clusterManagersToAdd.first;
41+
expect(initializedHeatmap, equals(cm1));
42+
expect(
43+
map.clusterManagerUpdates.last.clusterManagerIdsToRemove.isEmpty, true);
44+
expect(
45+
map.clusterManagerUpdates.last.clusterManagersToChange.isEmpty, true);
46+
});
47+
48+
testWidgets('Adding a cluster manager', (WidgetTester tester) async {
49+
const ClusterManager cm1 = ClusterManager(
50+
clusterManagerId: ClusterManagerId('cm_1'),
51+
);
52+
const ClusterManager cm2 = ClusterManager(
53+
clusterManagerId: ClusterManagerId('cm_2'),
54+
);
55+
56+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm1}));
57+
await tester
58+
.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm1, cm2}));
59+
60+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
61+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.length, 1);
62+
63+
final ClusterManager addedClusterManager =
64+
map.clusterManagerUpdates.last.clusterManagersToAdd.first;
65+
expect(addedClusterManager, equals(cm2));
66+
67+
expect(
68+
map.clusterManagerUpdates.last.clusterManagerIdsToRemove.isEmpty, true);
69+
70+
expect(
71+
map.clusterManagerUpdates.last.clusterManagersToChange.isEmpty, true);
72+
});
73+
74+
testWidgets('Removing a cluster manager', (WidgetTester tester) async {
75+
const ClusterManager cm1 = ClusterManager(
76+
clusterManagerId: ClusterManagerId('cm_1'),
77+
);
78+
79+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm1}));
80+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{}));
81+
82+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
83+
expect(map.clusterManagerUpdates.last.clusterManagerIdsToRemove.length, 1);
84+
expect(map.clusterManagerUpdates.last.clusterManagerIdsToRemove.first,
85+
equals(cm1.clusterManagerId));
86+
87+
expect(
88+
map.clusterManagerUpdates.last.clusterManagersToChange.isEmpty, true);
89+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.isEmpty, true);
90+
});
91+
92+
// This test checks that the cluster manager is not added again or changed
93+
// when the data remains the same. Since [ClusterManager] does not have any
94+
// properties to change, it should not trigger any updates. If new properties
95+
// are added to [ClusterManager] in the future, this test will need to be
96+
// updated accordingly to check that changes are triggered.
97+
testWidgets('Updating a cluster manager with same data',
98+
(WidgetTester tester) async {
99+
const ClusterManager cm1 = ClusterManager(
100+
clusterManagerId: ClusterManagerId('cm_1'),
101+
);
102+
const ClusterManager cm2 = ClusterManager(
103+
clusterManagerId: ClusterManagerId('cm_1'),
104+
);
105+
106+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm1}));
107+
await tester.pumpWidget(_mapWithClusterManagers(<ClusterManager>{cm2}));
108+
109+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
110+
111+
// As cluster manager does not have any properties to change,
112+
// it should not populate the clusterManagersToChange set.
113+
expect(
114+
map.clusterManagerUpdates.last.clusterManagersToChange.isEmpty, true);
115+
expect(
116+
map.clusterManagerUpdates.last.clusterManagerIdsToRemove.isEmpty, true);
117+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.isEmpty, true);
118+
});
119+
120+
// This test checks that the cluster manager is not added again or changed
121+
// when the data remains the same. Since [ClusterManager] does not have any
122+
// properties to change, it should not trigger any updates. If new properties
123+
// are added to [ClusterManager] in the future, this test will need to be
124+
// updated accordingly to check that changes are triggered.
125+
testWidgets('Multi update with same data', (WidgetTester tester) async {
126+
ClusterManager cm1 = const ClusterManager(
127+
clusterManagerId: ClusterManagerId('cm_1'),
128+
);
129+
ClusterManager cm2 = const ClusterManager(
130+
clusterManagerId: ClusterManagerId('cm_2'),
131+
);
132+
final Set<ClusterManager> prev = <ClusterManager>{cm1, cm2};
133+
cm1 = const ClusterManager(
134+
clusterManagerId: ClusterManagerId('cm_1'),
135+
);
136+
cm2 = const ClusterManager(
137+
clusterManagerId: ClusterManagerId('cm_2'),
138+
);
139+
final Set<ClusterManager> cur = <ClusterManager>{cm1, cm2};
140+
141+
await tester.pumpWidget(_mapWithClusterManagers(prev));
142+
await tester.pumpWidget(_mapWithClusterManagers(cur));
143+
144+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
145+
146+
// As cluster manager does not have any properties to change,
147+
// it should not populate the clusterManagersToChange set.
148+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.isEmpty, true);
149+
expect(
150+
map.clusterManagerUpdates.last.clusterManagerIdsToRemove.isEmpty, true);
151+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.isEmpty, true);
152+
});
153+
154+
// This test checks that the cluster manager is not added again or changed
155+
// when the data remains the same. Since [ClusterManager] does not have any
156+
// properties to change, it should not trigger any updates. If new properties
157+
// are added to [ClusterManager] in the future, this test will need to be
158+
// updated accordingly to check that changes are triggered.
159+
testWidgets('Partial update with same data', (WidgetTester tester) async {
160+
const ClusterManager cm1 = ClusterManager(
161+
clusterManagerId: ClusterManagerId('heatmap_1'),
162+
);
163+
const ClusterManager cm2 = ClusterManager(
164+
clusterManagerId: ClusterManagerId('heatmap_2'),
165+
);
166+
ClusterManager cm3 = const ClusterManager(
167+
clusterManagerId: ClusterManagerId('heatmap_3'),
168+
);
169+
final Set<ClusterManager> prev = <ClusterManager>{cm1, cm2, cm3};
170+
cm3 = const ClusterManager(
171+
clusterManagerId: ClusterManagerId('heatmap_3'),
172+
);
173+
final Set<ClusterManager> cur = <ClusterManager>{cm1, cm2, cm3};
174+
175+
await tester.pumpWidget(_mapWithClusterManagers(prev));
176+
await tester.pumpWidget(_mapWithClusterManagers(cur));
177+
178+
final PlatformMapStateRecorder map = platform.lastCreatedMap;
179+
180+
// As cluster manager does not have any properties to change,
181+
// it should not populate the clusterManagersToChange set.
182+
expect(
183+
map.clusterManagerUpdates.last.clusterManagersToChange.isEmpty, true);
184+
expect(
185+
map.clusterManagerUpdates.last.clusterManagerIdsToRemove.isEmpty, true);
186+
expect(map.clusterManagerUpdates.last.clusterManagersToAdd.isEmpty, true);
187+
});
188+
}

0 commit comments

Comments
 (0)