Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'package:pigeon/pigeon.dart';
copyrightHeader: 'pigeons/copyright.txt',
),
)
@HostApi(dartHostTestHandler: 'TestSharedPreferencesApi')
@HostApi()
abstract class SharedPreferencesApi {
/// Removes property from shared preferences data set.
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class StringListResult {
StringListLookupResultType type;
}

@HostApi(dartHostTestHandler: 'TestSharedPreferencesAsyncApi')
@HostApi()
abstract class SharedPreferencesAsyncApi {
/// Adds property to shared preferences data set of type bool.
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

/// iOS and macOS implementation of shared_preferences.
class SharedPreferencesFoundation extends SharedPreferencesStorePlatform {
final LegacyUserDefaultsApi _api = LegacyUserDefaultsApi();
/// Creates an instance of [SharedPreferencesFoundation].
SharedPreferencesFoundation({@visibleForTesting LegacyUserDefaultsApi? api})
: _api = api ?? LegacyUserDefaultsApi();

final LegacyUserDefaultsApi _api;

static const String _defaultPrefix = 'flutter.';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import 'package:pigeon/pigeon.dart';
@ConfigurePigeon(
PigeonOptions(
dartOut: 'lib/src/messages.g.dart',
dartTestOut: 'test/test_api.g.dart',
swiftOut:
'darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/messages.g.swift',
copyrightHeader: 'pigeons/copyright_header.txt',
),
)
@HostApi(dartHostTestHandler: 'TestUserDefaultsApi')
@HostApi()
abstract class LegacyUserDefaultsApi {
void remove(String key);
void setBool(String key, bool value);
Expand All @@ -28,7 +27,7 @@ class SharedPreferencesPigeonOptions {
String? suiteName;
}

@HostApi(dartHostTestHandler: 'TestSharedPreferencesAsyncApi')
@HostApi()
abstract class UserDefaultsApi {
/// Adds property to shared preferences data set of type String.
@SwiftFunction('set(key:value:options:)')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences_foundation/src/messages.g.dart';
import 'package:shared_preferences_foundation/src/shared_preferences_foundation.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_platform_interface/types.dart';

import 'test_api.g.dart';

class _MockSharedPreferencesApi implements TestUserDefaultsApi {
class _MockSharedPreferencesApi implements LegacyUserDefaultsApi {
final Map<String, Object> items = <String, Object>{};

@override
Map<String, Object> getAll(String prefix, List<String?>? allowList) {
Future<Map<String, Object>> getAll(
String prefix,
List<String?>? allowList,
) async {
Set<String?>? allowSet;
if (allowList != null) {
allowSet = Set<String>.from(allowList);
Expand All @@ -28,27 +30,27 @@ class _MockSharedPreferencesApi implements TestUserDefaultsApi {
}

@override
void remove(String key) {
Future<void> remove(String key) async {
items.remove(key);
}

@override
void setBool(String key, bool value) {
Future<void> setBool(String key, bool value) async {
items[key] = value;
}

@override
void setDouble(String key, double value) {
Future<void> setDouble(String key, double value) async {
items[key] = value;
}

@override
void setValue(String key, Object value) {
Future<void> setValue(String key, Object value) async {
items[key] = value;
}

@override
bool clear(String prefix, List<String?>? allowList) {
Future<bool> clear(String prefix, List<String?>? allowList) async {
items.keys.toList().forEach((String key) {
if (key.startsWith(prefix) &&
(allowList == null || allowList.contains(key))) {
Expand All @@ -57,6 +59,14 @@ class _MockSharedPreferencesApi implements TestUserDefaultsApi {
});
return true;
}

@override
// ignore: non_constant_identifier_names
BinaryMessenger? get pigeonVar_binaryMessenger => null;

@override
// ignore: non_constant_identifier_names
String get pigeonVar_messageChannelSuffix => '';
}

void main() {
Expand Down Expand Up @@ -95,7 +105,6 @@ void main() {

setUp(() {
api = _MockSharedPreferencesApi();
TestUserDefaultsApi.setUp(api);
});

test('registerWith', () async {
Expand All @@ -107,21 +116,27 @@ void main() {
});

test('remove', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
api.items['flutter.hi'] = 'world';
expect(await plugin.remove('flutter.hi'), isTrue);
expect(api.items.containsKey('flutter.hi'), isFalse);
});

test('clear', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
api.items['flutter.hi'] = 'world';
expect(await plugin.clear(), isTrue);
expect(api.items.containsKey('flutter.hi'), isFalse);
});

test('clearWithPrefix', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -136,7 +151,9 @@ void main() {
});

test('clearWithParameters', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -157,7 +174,9 @@ void main() {
});

test('clearWithParameters with allow list', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -183,7 +202,9 @@ void main() {
});

test('getAll', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in flutterTestValues.keys) {
api.items[key] = flutterTestValues[key]!;
}
Expand All @@ -193,7 +214,9 @@ void main() {
});

test('getAllWithPrefix', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -203,7 +226,9 @@ void main() {
});

test('getAllWithParameters', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -215,7 +240,9 @@ void main() {
});

test('getAllWithParameters with allow list', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -232,7 +259,9 @@ void main() {
});

test('setValue', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
expect(await plugin.setValue('Bool', 'flutter.Bool', true), isTrue);
expect(api.items['flutter.Bool'], true);
expect(await plugin.setValue('Double', 'flutter.Double', 1.5), isTrue);
Expand All @@ -249,14 +278,18 @@ void main() {
});

test('setValue with unsupported type', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
expect(() async {
await plugin.setValue('Map', 'flutter.key', <String, String>{});
}, throwsA(isA<PlatformException>()));
});

test('getAllWithNoPrefix', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -266,7 +299,9 @@ void main() {
});

test('clearWithNoPrefix', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -279,7 +314,9 @@ void main() {
});

test('getAllWithNoPrefix with param', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand All @@ -291,7 +328,9 @@ void main() {
});

test('clearWithNoPrefix with param', () async {
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation();
final SharedPreferencesFoundation plugin = SharedPreferencesFoundation(
api: api,
);
for (final String key in allTestValues.keys) {
api.items[key] = allTestValues[key]!;
}
Expand Down
Loading