|
| 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 'dart:io'; |
| 6 | + |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:integration_test/integration_test.dart'; |
| 9 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 10 | +import 'package:shared_preferences/util/legacy_to_async_migration_util.dart'; |
| 11 | +import 'package:shared_preferences_android/shared_preferences_android.dart'; |
| 12 | +import 'package:shared_preferences_foundation/shared_preferences_foundation.dart'; |
| 13 | +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; |
| 14 | +import 'package:shared_preferences_platform_interface/types.dart'; |
| 15 | +import 'package:shared_preferences_windows/shared_preferences_windows.dart'; |
| 16 | + |
| 17 | +const String stringKey = 'testString'; |
| 18 | +const String boolKey = 'testBool'; |
| 19 | +const String intKey = 'testInt'; |
| 20 | +const String doubleKey = 'testDouble'; |
| 21 | +const String listKey = 'testList'; |
| 22 | + |
| 23 | +const String testString = 'hello world'; |
| 24 | +const bool testBool = true; |
| 25 | +const int testInt = 42; |
| 26 | +const double testDouble = 3.14159; |
| 27 | +const List<String> testList = <String>['foo', 'bar']; |
| 28 | + |
| 29 | +const String migrationCompletedKey = 'migrationCompleted'; |
| 30 | + |
| 31 | +void main() { |
| 32 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 33 | + |
| 34 | + group('SharedPreferences without setting prefix', () { |
| 35 | + runAllGroups(() {}); |
| 36 | + }); |
| 37 | + |
| 38 | + group('SharedPreferences with setPrefix', () { |
| 39 | + runAllGroups(() { |
| 40 | + SharedPreferences.setPrefix('prefix.'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + group('SharedPreferences with setPrefix and allowList', () { |
| 45 | + runAllGroups( |
| 46 | + () { |
| 47 | + final Set<String> allowList = <String>{ |
| 48 | + 'prefix.$boolKey', |
| 49 | + 'prefix.$intKey', |
| 50 | + 'prefix.$doubleKey', |
| 51 | + 'prefix.$listKey' |
| 52 | + }; |
| 53 | + SharedPreferences.setPrefix('prefix.', allowList: allowList); |
| 54 | + }, |
| 55 | + stringValue: null, |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + group('SharedPreferences with prefix set to empty string', () { |
| 60 | + runAllGroups( |
| 61 | + () { |
| 62 | + SharedPreferences.setPrefix(''); |
| 63 | + }, |
| 64 | + keysCollide: true, |
| 65 | + ); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +void runAllGroups(void Function() legacySharedPrefsConfig, |
| 70 | + {String? stringValue = testString, bool keysCollide = false}) { |
| 71 | + group('default sharedPreferencesAsyncOptions', () { |
| 72 | + const SharedPreferencesOptions sharedPreferencesAsyncOptions = |
| 73 | + SharedPreferencesOptions(); |
| 74 | + |
| 75 | + runTests( |
| 76 | + sharedPreferencesAsyncOptions, |
| 77 | + legacySharedPrefsConfig, |
| 78 | + stringValue: stringValue, |
| 79 | + keysAndNamesCollide: keysCollide, |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + group('file name (or equivalent) sharedPreferencesAsyncOptions', () { |
| 84 | + final SharedPreferencesOptions sharedPreferencesAsyncOptions; |
| 85 | + if (Platform.isAndroid) { |
| 86 | + sharedPreferencesAsyncOptions = |
| 87 | + const SharedPreferencesAsyncAndroidOptions( |
| 88 | + backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences, |
| 89 | + originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions( |
| 90 | + fileName: 'fileName', |
| 91 | + ), |
| 92 | + ); |
| 93 | + } else if (Platform.isIOS || Platform.isMacOS) { |
| 94 | + sharedPreferencesAsyncOptions = |
| 95 | + SharedPreferencesAsyncFoundationOptions(suiteName: 'group.fileName'); |
| 96 | + } else if (Platform.isLinux) { |
| 97 | + sharedPreferencesAsyncOptions = const SharedPreferencesLinuxOptions( |
| 98 | + fileName: 'fileName', |
| 99 | + ); |
| 100 | + } else if (Platform.isWindows) { |
| 101 | + sharedPreferencesAsyncOptions = |
| 102 | + const SharedPreferencesWindowsOptions(fileName: 'fileName'); |
| 103 | + } else { |
| 104 | + sharedPreferencesAsyncOptions = const SharedPreferencesOptions(); |
| 105 | + } |
| 106 | + |
| 107 | + runTests( |
| 108 | + sharedPreferencesAsyncOptions, |
| 109 | + legacySharedPrefsConfig, |
| 110 | + stringValue: stringValue, |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + if (Platform.isAndroid) { |
| 115 | + group('Android default sharedPreferences', () { |
| 116 | + const SharedPreferencesOptions sharedPreferencesAsyncOptions = |
| 117 | + SharedPreferencesAsyncAndroidOptions( |
| 118 | + backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences, |
| 119 | + originalSharedPreferencesOptions: |
| 120 | + AndroidSharedPreferencesStoreOptions(), |
| 121 | + ); |
| 122 | + |
| 123 | + runTests( |
| 124 | + sharedPreferencesAsyncOptions, |
| 125 | + legacySharedPrefsConfig, |
| 126 | + stringValue: stringValue, |
| 127 | + ); |
| 128 | + }); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void runTests(SharedPreferencesOptions sharedPreferencesAsyncOptions, |
| 133 | + void Function() legacySharedPrefsConfig, |
| 134 | + {String? stringValue = testString, bool keysAndNamesCollide = false}) { |
| 135 | + setUp(() async { |
| 136 | + // Configure and populate the source legacy shared preferences. |
| 137 | + SharedPreferences.resetStatic(); |
| 138 | + legacySharedPrefsConfig(); |
| 139 | + |
| 140 | + final SharedPreferences preferences = await SharedPreferences.getInstance(); |
| 141 | + await preferences.clear(); |
| 142 | + await preferences.setBool(boolKey, testBool); |
| 143 | + await preferences.setInt(intKey, testInt); |
| 144 | + await preferences.setDouble(doubleKey, testDouble); |
| 145 | + await preferences.setString(stringKey, testString); |
| 146 | + await preferences.setStringList(listKey, testList); |
| 147 | + }); |
| 148 | + |
| 149 | + tearDown(() async { |
| 150 | + await SharedPreferencesAsync(options: sharedPreferencesAsyncOptions) |
| 151 | + .clear(); |
| 152 | + }); |
| 153 | + |
| 154 | + testWidgets('data is successfully transferred to new system', (_) async { |
| 155 | + final SharedPreferences preferences = await SharedPreferences.getInstance(); |
| 156 | + await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary( |
| 157 | + legacySharedPreferencesInstance: preferences, |
| 158 | + sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions, |
| 159 | + migrationCompletedKey: migrationCompletedKey, |
| 160 | + ); |
| 161 | + |
| 162 | + final SharedPreferencesAsync asyncPreferences = |
| 163 | + SharedPreferencesAsync(options: sharedPreferencesAsyncOptions); |
| 164 | + |
| 165 | + expect(await asyncPreferences.getBool(boolKey), testBool); |
| 166 | + expect(await asyncPreferences.getInt(intKey), testInt); |
| 167 | + expect(await asyncPreferences.getDouble(doubleKey), testDouble); |
| 168 | + expect(await asyncPreferences.getString(stringKey), stringValue); |
| 169 | + expect(await asyncPreferences.getStringList(listKey), testList); |
| 170 | + }); |
| 171 | + |
| 172 | + testWidgets('migrationCompleted key is set', (_) async { |
| 173 | + final SharedPreferences preferences = await SharedPreferences.getInstance(); |
| 174 | + await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary( |
| 175 | + legacySharedPreferencesInstance: preferences, |
| 176 | + sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions, |
| 177 | + migrationCompletedKey: migrationCompletedKey, |
| 178 | + ); |
| 179 | + |
| 180 | + final SharedPreferencesAsync asyncPreferences = |
| 181 | + SharedPreferencesAsync(options: sharedPreferencesAsyncOptions); |
| 182 | + |
| 183 | + expect(await asyncPreferences.getBool(migrationCompletedKey), true); |
| 184 | + }); |
| 185 | + |
| 186 | + testWidgets( |
| 187 | + 're-running migration tool does not overwrite data', |
| 188 | + (_) async { |
| 189 | + final SharedPreferences preferences = |
| 190 | + await SharedPreferences.getInstance(); |
| 191 | + await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary( |
| 192 | + legacySharedPreferencesInstance: preferences, |
| 193 | + sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions, |
| 194 | + migrationCompletedKey: migrationCompletedKey, |
| 195 | + ); |
| 196 | + |
| 197 | + final SharedPreferencesAsync asyncPreferences = |
| 198 | + SharedPreferencesAsync(options: sharedPreferencesAsyncOptions); |
| 199 | + await preferences.setInt(intKey, -0); |
| 200 | + await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary( |
| 201 | + legacySharedPreferencesInstance: preferences, |
| 202 | + sharedPreferencesAsyncOptions: sharedPreferencesAsyncOptions, |
| 203 | + migrationCompletedKey: migrationCompletedKey, |
| 204 | + ); |
| 205 | + expect(await asyncPreferences.getInt(intKey), testInt); |
| 206 | + }, |
| 207 | + // Skips platforms that would be adding the preferences to the same file. |
| 208 | + skip: keysAndNamesCollide && |
| 209 | + (Platform.isWindows || |
| 210 | + Platform.isLinux || |
| 211 | + Platform.isMacOS || |
| 212 | + Platform.isIOS), |
| 213 | + ); |
| 214 | +} |
0 commit comments