|
5 | 5 | import 'package:flutter_test/flutter_test.dart'; |
6 | 6 | import 'package:integration_test/integration_test.dart'; |
7 | 7 | 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'; |
8 | 22 |
|
9 | 23 | void main() { |
10 | 24 | IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
11 | 25 |
|
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 | | - |
18 | 26 | const String testString2 = 'goodbye world'; |
19 | 27 | const bool testBool2 = false; |
20 | 28 | const int testInt2 = 1337; |
@@ -686,4 +694,136 @@ void main() { |
686 | 694 | }); |
687 | 695 | }); |
688 | 696 | }); |
| 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 | + ); |
689 | 829 | } |
0 commit comments