Skip to content

Commit a44efda

Browse files
authored
[shared_preferences] Add integration tests based on upstream v2.5.5 (#1047)
1 parent 5548cb5 commit a44efda

2 files changed

Lines changed: 150 additions & 6 deletions

File tree

packages/shared_preferences/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Add 12 integration test cases for the legacy-to-async migration utility.
4+
15
## 2.3.3
26

37
* Update shared_preferences to 2.5.5.

packages/shared_preferences/example/integration_test/shared_preferences_test.dart

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55
import 'package:flutter_test/flutter_test.dart';
66
import 'package:integration_test/integration_test.dart';
77
import 'package:shared_preferences/shared_preferences.dart';
8+
import 'package:shared_preferences/util/legacy_to_async_migration_util.dart';
9+
10+
const String testString = 'hello world';
11+
const bool testBool = true;
12+
const int testInt = 42;
13+
const double testDouble = 3.14159;
14+
const List<String> testList = <String>['foo', 'bar'];
15+
16+
const String stringKey = 'testString';
17+
const String boolKey = 'testBool';
18+
const String intKey = 'testInt';
19+
const String doubleKey = 'testDouble';
20+
const String listKey = 'testList';
21+
const String migrationCompletedKey = 'migrationCompleted';
822

923
void main() {
1024
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
1125

12-
const String testString = 'hello world';
13-
const bool testBool = true;
14-
const int testInt = 42;
15-
const double testDouble = 3.14159;
16-
const List<String> testList = <String>['foo', 'bar'];
17-
1826
const String testString2 = 'goodbye world';
1927
const bool testBool2 = false;
2028
const int testInt2 = 1337;
@@ -686,4 +694,136 @@ void main() {
686694
});
687695
});
688696
});
697+
698+
// Migration util tests (ported from shared_preferences_migration_util_test.dart v2.5.5).
699+
// The upstream platform-specific async option variants (Android/iOS/Linux/Windows) all
700+
// collapse to the default SharedPreferencesOptions() on Tizen, so only the
701+
// default-options group is kept.
702+
group('SharedPreferences without setting prefix', () {
703+
runAllGroups(() {});
704+
});
705+
706+
group('SharedPreferences with setPrefix', () {
707+
runAllGroups(() {
708+
SharedPreferences.setPrefix('prefix.');
709+
});
710+
});
711+
712+
group('SharedPreferences with setPrefix and allowList', () {
713+
runAllGroups(() {
714+
final Set<String> allowList = <String>{
715+
'prefix.$boolKey',
716+
'prefix.$intKey',
717+
'prefix.$doubleKey',
718+
'prefix.$listKey',
719+
};
720+
SharedPreferences.setPrefix('prefix.', allowList: allowList);
721+
}, stringValue: null);
722+
});
723+
724+
group('SharedPreferences with prefix set to empty string', () {
725+
runAllGroups(() {
726+
SharedPreferences.setPrefix('');
727+
}, keysCollide: true);
728+
});
729+
}
730+
731+
void runAllGroups(
732+
void Function() legacySharedPrefsConfig, {
733+
String? stringValue = testString,
734+
bool keysCollide = false,
735+
}) {
736+
group('default sharedPreferencesAsyncOptions', () {
737+
runTests(
738+
legacySharedPrefsConfig,
739+
stringValue: stringValue,
740+
keysAndNamesCollide: keysCollide,
741+
);
742+
});
743+
}
744+
745+
void runTests(
746+
void Function() legacySharedPrefsConfig, {
747+
String? stringValue = testString,
748+
bool keysAndNamesCollide = false,
749+
}) {
750+
const SharedPreferencesOptions sharedPreferencesAsyncOptions =
751+
SharedPreferencesOptions();
752+
753+
setUp(() async {
754+
// Configure and populate the source legacy shared preferences.
755+
SharedPreferences.resetStatic();
756+
legacySharedPrefsConfig();
757+
758+
final SharedPreferences preferences = await SharedPreferences.getInstance();
759+
await preferences.clear();
760+
await preferences.setBool(boolKey, testBool);
761+
await preferences.setInt(intKey, testInt);
762+
await preferences.setDouble(doubleKey, testDouble);
763+
await preferences.setString(stringKey, testString);
764+
await preferences.setStringList(listKey, testList);
765+
});
766+
767+
tearDown(() async {
768+
await SharedPreferencesAsync().clear();
769+
});
770+
771+
testWidgets('data is successfully transferred to new system', (
772+
WidgetTester _,
773+
) async {
774+
final SharedPreferences preferences = await SharedPreferences.getInstance();
775+
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
776+
legacySharedPreferencesInstance: preferences,
777+
sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions,
778+
migrationCompletedKey: migrationCompletedKey,
779+
);
780+
781+
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync();
782+
783+
expect(await asyncPreferences.getBool(boolKey), testBool);
784+
expect(await asyncPreferences.getInt(intKey), testInt);
785+
expect(await asyncPreferences.getDouble(doubleKey), testDouble);
786+
expect(await asyncPreferences.getString(stringKey), stringValue);
787+
expect(await asyncPreferences.getStringList(listKey), testList);
788+
});
789+
790+
testWidgets('migrationCompleted key is set', (WidgetTester _) async {
791+
final SharedPreferences preferences = await SharedPreferences.getInstance();
792+
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
793+
legacySharedPreferencesInstance: preferences,
794+
sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions,
795+
migrationCompletedKey: migrationCompletedKey,
796+
);
797+
798+
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync();
799+
800+
expect(await asyncPreferences.getBool(migrationCompletedKey), true);
801+
});
802+
803+
testWidgets(
804+
're-running migration tool does not overwrite data',
805+
(WidgetTester _) async {
806+
final SharedPreferences preferences =
807+
await SharedPreferences.getInstance();
808+
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
809+
legacySharedPreferencesInstance: preferences,
810+
sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions,
811+
migrationCompletedKey: migrationCompletedKey,
812+
);
813+
814+
final SharedPreferencesAsync asyncPreferences = SharedPreferencesAsync();
815+
await preferences.setInt(intKey, -0);
816+
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
817+
legacySharedPreferencesInstance: preferences,
818+
sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions,
819+
migrationCompletedKey: migrationCompletedKey,
820+
);
821+
expect(await asyncPreferences.getInt(intKey), testInt);
822+
},
823+
// On Tizen the legacy and async stores share a single preference
824+
// namespace, so with an empty prefix their keys collide and this
825+
// scenario cannot be distinguished (matches how upstream skips it on
826+
// other shared-namespace platforms).
827+
skip: keysAndNamesCollide,
828+
);
689829
}

0 commit comments

Comments
 (0)