Skip to content

Commit 2f25693

Browse files
[shared_preferences] Remove use of Pigeon's Dart test generator (#10325)
Updates `shared_preferences_foundation`'s legacy implementation to use DI to inject the fake Pigeon API implementation, instead of the Dart test generator, matching the newer async code. Also updates `shared_preferences_android` to remove `dartHostTestHandler` entries that were unused. Part of flutter/flutter#159886 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent e22c523 commit 2f25693

File tree

6 files changed

+74
-621
lines changed

6 files changed

+74
-621
lines changed

packages/shared_preferences/shared_preferences_android/pigeons/messages.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'package:pigeon/pigeon.dart';
1717
copyrightHeader: 'pigeons/copyright.txt',
1818
),
1919
)
20-
@HostApi(dartHostTestHandler: 'TestSharedPreferencesApi')
20+
@HostApi()
2121
abstract class SharedPreferencesApi {
2222
/// Removes property from shared preferences data set.
2323
@TaskQueue(type: TaskQueueType.serialBackgroundThread)

packages/shared_preferences/shared_preferences_android/pigeons/messages_async.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class StringListResult {
4848
StringListLookupResultType type;
4949
}
5050

51-
@HostApi(dartHostTestHandler: 'TestSharedPreferencesAsyncApi')
51+
@HostApi()
5252
abstract class SharedPreferencesAsyncApi {
5353
/// Adds property to shared preferences data set of type bool.
5454
@TaskQueue(type: TaskQueueType.serialBackgroundThread)

packages/shared_preferences/shared_preferences_foundation/lib/src/shared_preferences_foundation.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart' show visibleForTesting;
56
import 'package:flutter/services.dart';
67
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
78
import 'package:shared_preferences_platform_interface/types.dart';
@@ -12,7 +13,11 @@ typedef _Setter = Future<void> Function(String key, Object value);
1213

1314
/// iOS and macOS implementation of shared_preferences.
1415
class SharedPreferencesFoundation extends SharedPreferencesStorePlatform {
15-
final LegacyUserDefaultsApi _api = LegacyUserDefaultsApi();
16+
/// Creates an instance of [SharedPreferencesFoundation].
17+
SharedPreferencesFoundation({@visibleForTesting LegacyUserDefaultsApi? api})
18+
: _api = api ?? LegacyUserDefaultsApi();
19+
20+
final LegacyUserDefaultsApi _api;
1621

1722
static const String _defaultPrefix = 'flutter.';
1823

packages/shared_preferences/shared_preferences_foundation/pigeons/messages.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import 'package:pigeon/pigeon.dart';
77
@ConfigurePigeon(
88
PigeonOptions(
99
dartOut: 'lib/src/messages.g.dart',
10-
dartTestOut: 'test/test_api.g.dart',
1110
swiftOut:
1211
'darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/messages.g.swift',
1312
copyrightHeader: 'pigeons/copyright_header.txt',
1413
),
1514
)
16-
@HostApi(dartHostTestHandler: 'TestUserDefaultsApi')
15+
@HostApi()
1716
abstract class LegacyUserDefaultsApi {
1817
void remove(String key);
1918
void setBool(String key, bool value);
@@ -28,7 +27,7 @@ class SharedPreferencesPigeonOptions {
2827
String? suiteName;
2928
}
3029

31-
@HostApi(dartHostTestHandler: 'TestSharedPreferencesAsyncApi')
30+
@HostApi()
3231
abstract class UserDefaultsApi {
3332
/// Adds property to shared preferences data set of type String.
3433
@SwiftFunction('set(key:value:options:)')

packages/shared_preferences/shared_preferences_foundation/test/shared_preferences_foundation_test.dart

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
import 'package:flutter/services.dart';
66
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:shared_preferences_foundation/src/messages.g.dart';
78
import 'package:shared_preferences_foundation/src/shared_preferences_foundation.dart';
89
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
910
import 'package:shared_preferences_platform_interface/types.dart';
1011

11-
import 'test_api.g.dart';
12-
13-
class _MockSharedPreferencesApi implements TestUserDefaultsApi {
12+
class _MockSharedPreferencesApi implements LegacyUserDefaultsApi {
1413
final Map<String, Object> items = <String, Object>{};
1514

1615
@override
17-
Map<String, Object> getAll(String prefix, List<String?>? allowList) {
16+
Future<Map<String, Object>> getAll(
17+
String prefix,
18+
List<String?>? allowList,
19+
) async {
1820
Set<String?>? allowSet;
1921
if (allowList != null) {
2022
allowSet = Set<String>.from(allowList);
@@ -28,27 +30,27 @@ class _MockSharedPreferencesApi implements TestUserDefaultsApi {
2830
}
2931

3032
@override
31-
void remove(String key) {
33+
Future<void> remove(String key) async {
3234
items.remove(key);
3335
}
3436

3537
@override
36-
void setBool(String key, bool value) {
38+
Future<void> setBool(String key, bool value) async {
3739
items[key] = value;
3840
}
3941

4042
@override
41-
void setDouble(String key, double value) {
43+
Future<void> setDouble(String key, double value) async {
4244
items[key] = value;
4345
}
4446

4547
@override
46-
void setValue(String key, Object value) {
48+
Future<void> setValue(String key, Object value) async {
4749
items[key] = value;
4850
}
4951

5052
@override
51-
bool clear(String prefix, List<String?>? allowList) {
53+
Future<bool> clear(String prefix, List<String?>? allowList) async {
5254
items.keys.toList().forEach((String key) {
5355
if (key.startsWith(prefix) &&
5456
(allowList == null || allowList.contains(key))) {
@@ -57,6 +59,14 @@ class _MockSharedPreferencesApi implements TestUserDefaultsApi {
5759
});
5860
return true;
5961
}
62+
63+
@override
64+
// ignore: non_constant_identifier_names
65+
BinaryMessenger? get pigeonVar_binaryMessenger => null;
66+
67+
@override
68+
// ignore: non_constant_identifier_names
69+
String get pigeonVar_messageChannelSuffix => '';
6070
}
6171

6272
void main() {
@@ -95,7 +105,6 @@ void main() {
95105

96106
setUp(() {
97107
api = _MockSharedPreferencesApi();
98-
TestUserDefaultsApi.setUp(api);
99108
});
100109

101110
test('registerWith', () async {
@@ -107,21 +116,27 @@ void main() {
107116
});
108117

109118
test('remove', () async {
110-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
119+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
120+
api: api,
121+
);
111122
api.items['flutter.hi'] = 'world';
112123
expect(await plugin.remove('flutter.hi'), isTrue);
113124
expect(api.items.containsKey('flutter.hi'), isFalse);
114125
});
115126

116127
test('clear', () async {
117-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
128+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
129+
api: api,
130+
);
118131
api.items['flutter.hi'] = 'world';
119132
expect(await plugin.clear(), isTrue);
120133
expect(api.items.containsKey('flutter.hi'), isFalse);
121134
});
122135

123136
test('clearWithPrefix', () async {
124-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
137+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
138+
api: api,
139+
);
125140
for (final String key in allTestValues.keys) {
126141
api.items[key] = allTestValues[key]!;
127142
}
@@ -136,7 +151,9 @@ void main() {
136151
});
137152

138153
test('clearWithParameters', () async {
139-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
154+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
155+
api: api,
156+
);
140157
for (final String key in allTestValues.keys) {
141158
api.items[key] = allTestValues[key]!;
142159
}
@@ -157,7 +174,9 @@ void main() {
157174
});
158175

159176
test('clearWithParameters with allow list', () async {
160-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
177+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
178+
api: api,
179+
);
161180
for (final String key in allTestValues.keys) {
162181
api.items[key] = allTestValues[key]!;
163182
}
@@ -183,7 +202,9 @@ void main() {
183202
});
184203

185204
test('getAll', () async {
186-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
205+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
206+
api: api,
207+
);
187208
for (final String key in flutterTestValues.keys) {
188209
api.items[key] = flutterTestValues[key]!;
189210
}
@@ -193,7 +214,9 @@ void main() {
193214
});
194215

195216
test('getAllWithPrefix', () async {
196-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
217+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
218+
api: api,
219+
);
197220
for (final String key in allTestValues.keys) {
198221
api.items[key] = allTestValues[key]!;
199222
}
@@ -203,7 +226,9 @@ void main() {
203226
});
204227

205228
test('getAllWithParameters', () async {
206-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
229+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
230+
api: api,
231+
);
207232
for (final String key in allTestValues.keys) {
208233
api.items[key] = allTestValues[key]!;
209234
}
@@ -215,7 +240,9 @@ void main() {
215240
});
216241

217242
test('getAllWithParameters with allow list', () async {
218-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
243+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
244+
api: api,
245+
);
219246
for (final String key in allTestValues.keys) {
220247
api.items[key] = allTestValues[key]!;
221248
}
@@ -232,7 +259,9 @@ void main() {
232259
});
233260

234261
test('setValue', () async {
235-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
262+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
263+
api: api,
264+
);
236265
expect(await plugin.setValue('Bool', 'flutter.Bool', true), isTrue);
237266
expect(api.items['flutter.Bool'], true);
238267
expect(await plugin.setValue('Double', 'flutter.Double', 1.5), isTrue);
@@ -249,14 +278,18 @@ void main() {
249278
});
250279

251280
test('setValue with unsupported type', () async {
252-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
281+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
282+
api: api,
283+
);
253284
expect(() async {
254285
await plugin.setValue('Map', 'flutter.key', <String, String>{});
255286
}, throwsA(isA<PlatformException>()));
256287
});
257288

258289
test('getAllWithNoPrefix', () async {
259-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
290+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
291+
api: api,
292+
);
260293
for (final String key in allTestValues.keys) {
261294
api.items[key] = allTestValues[key]!;
262295
}
@@ -266,7 +299,9 @@ void main() {
266299
});
267300

268301
test('clearWithNoPrefix', () async {
269-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
302+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
303+
api: api,
304+
);
270305
for (final String key in allTestValues.keys) {
271306
api.items[key] = allTestValues[key]!;
272307
}
@@ -279,7 +314,9 @@ void main() {
279314
});
280315

281316
test('getAllWithNoPrefix with param', () async {
282-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
317+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
318+
api: api,
319+
);
283320
for (final String key in allTestValues.keys) {
284321
api.items[key] = allTestValues[key]!;
285322
}
@@ -291,7 +328,9 @@ void main() {
291328
});
292329

293330
test('clearWithNoPrefix with param', () async {
294-
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
331+
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
332+
api: api,
333+
);
295334
for (final String key in allTestValues.keys) {
296335
api.items[key] = allTestValues[key]!;
297336
}

0 commit comments

Comments
 (0)