Skip to content

Commit 95eb61e

Browse files
committed
add default shared prefs tests
1 parent f6592ee commit 95eb61e

File tree

10 files changed

+79
-24
lines changed

10 files changed

+79
-24
lines changed

packages/shared_preferences/shared_preferences/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ latest data stored on the native platform regardless of what process was used to
5050

5151
### Android platform storage
5252

53-
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs can use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) or [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) to store data.
53+
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs can use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) or [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) to store data.
54+
In most cases you should use the default option of DataStore Preferences, as it is the platform-recommended preferences storage system.
55+
However, in some cases you may need to interact with preferences that were written to SharedPreferences by code you don't control.
5456

55-
To use the `Android Shared Preferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync].
57+
To use the `Android SharedPreferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync] on Android.
5658

5759
<?code-excerpt "readme_excerpts.dart (Android_Options)"?>
5860
```dart
5961
const SharedPreferencesAsyncAndroidOptions options =
6062
SharedPreferencesAsyncAndroidOptions(
6163
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
6264
originalSharedPreferencesOptions:
63-
OriginalSharedPreferencesOptions(fileName: 'the_name_of_a_file'));
65+
AndroidSharedPreferencesStoreOptions(fileName: 'the_name_of_a_file'));
6466
```
6567

66-
The [SharedPreferences] API uses the native [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
68+
The [SharedPreferences] API uses the native [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
6769

6870
## Examples
6971
Here are small examples that show you how to use the API.

packages/shared_preferences/shared_preferences/example/lib/readme_excerpts.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ Future<void> readmeTestSnippets() async {
9898
const SharedPreferencesAsyncAndroidOptions options =
9999
SharedPreferencesAsyncAndroidOptions(
100100
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
101-
originalSharedPreferencesOptions:
102-
OriginalSharedPreferencesOptions(fileName: 'the_name_of_a_file'));
101+
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
102+
fileName: 'the_name_of_a_file'));
103103
// #enddocregion Android_Options

packages/shared_preferences/shared_preferences_android/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ should add it to your `pubspec.yaml` as usual.
1313

1414
## Options
1515

16-
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs can use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) or [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) to store data.
16+
The [SharedPreferencesAsync] and [SharedPreferencesWithCache] APIs can use [DataStore Preferences](https://developer.android.com/topic/libraries/architecture/datastore) or [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) to store data.
1717

18-
To use the `Android Shared Preferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync].
18+
To use the `Android SharedPreferences` backend, use the `SharedPreferencesAsyncAndroidOptions` when using [SharedPreferencesAsync].
1919

2020
<?code-excerpt "readme_excerpts.dart (Android_Options)"?>
2121
```dart
2222
const SharedPreferencesAsyncAndroidOptions options =
2323
SharedPreferencesAsyncAndroidOptions(
2424
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
2525
originalSharedPreferencesOptions:
26-
OriginalSharedPreferencesOptions(fileName: 'the_name_of_a_file'));
26+
AndroidSharedPreferencesStoreOptions(fileName: 'the_name_of_a_file'));
2727
```
2828

29-
The [SharedPreferences] API uses the native [Android Shared Preferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
29+
The [SharedPreferences] API uses the native [Android SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) tool to store data.
3030

3131
[1]: https://pub.dev/packages/shared_preferences
3232
[2]: https://flutter.dev/to/endorsed-federated-plugin

packages/shared_preferences/shared_preferences_android/android/src/main/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesPlugin.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.datastore.preferences.core.edit
1717
import androidx.datastore.preferences.core.longPreferencesKey
1818
import androidx.datastore.preferences.core.stringPreferencesKey
1919
import androidx.datastore.preferences.preferencesDataStore
20+
import androidx.preference.PreferenceManager
2021
import io.flutter.embedding.engine.plugins.FlutterPlugin
2122
import io.flutter.plugin.common.BinaryMessenger
2223
import java.io.ByteArrayInputStream
@@ -243,8 +244,11 @@ class SharedPreferencesBackend(
243244
}
244245

245246
private fun createSharedPreferences(options: SharedPreferencesPigeonOptions): SharedPreferences {
246-
return context.getSharedPreferences(
247-
options.fileName ?: SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
247+
return if (options.fileName == null) {
248+
PreferenceManager.getDefaultSharedPreferences(context)
249+
} else {
250+
context.getSharedPreferences(options.fileName, Context.MODE_PRIVATE)
251+
}
248252
}
249253

250254
/** Adds property to data store of type bool. */

packages/shared_preferences/shared_preferences_android/android/src/test/kotlin/io/flutter/plugins/sharedpreferences/SharedPreferencesTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package io.flutter.plugins.sharedpreferences
66

77
import android.content.Context
8+
import android.content.SharedPreferences
9+
import androidx.preference.PreferenceManager
810
import androidx.test.core.app.ApplicationProvider
911
import androidx.test.ext.junit.runners.AndroidJUnit4
1012
import io.flutter.embedding.engine.plugins.FlutterPlugin
@@ -39,10 +41,9 @@ internal class SharedPreferencesTest {
3941

4042
private val dataStoreOptions = SharedPreferencesPigeonOptions(useDataStore = true)
4143
private val sharedPreferencesOptions = SharedPreferencesPigeonOptions(useDataStore = false)
44+
private val testContext: Context = ApplicationProvider.getApplicationContext()
4245

4346
private fun pluginSetup(options: SharedPreferencesPigeonOptions): SharedPreferencesAsyncApi {
44-
val testContext: Context = ApplicationProvider.getApplicationContext()
45-
4647
val plugin = SharedPreferencesPlugin()
4748
val binaryMessenger = mockk<BinaryMessenger>()
4849
val flutterPluginBinding = mockk<FlutterPlugin.FlutterPluginBinding>()
@@ -311,4 +312,13 @@ internal class SharedPreferencesTest {
311312
Assert.assertEquals(plugin.getInt(intKey, sharedPreferencesOptions), 1L)
312313
Assert.assertEquals(plugin.getInt(intKey, optionsWithNewFile), 2L)
313314
}
315+
316+
@Test
317+
fun testSharedPreferencesDefaultFile() {
318+
val defaultPreferences: SharedPreferences =
319+
PreferenceManager.getDefaultSharedPreferences(testContext)
320+
defaultPreferences.edit().putString(stringKey, testString).commit()
321+
val plugin = pluginSetup(sharedPreferencesOptions)
322+
Assert.assertEquals(plugin.getString(stringKey, sharedPreferencesOptions), testString)
323+
}
314324
}

packages/shared_preferences/shared_preferences_android/example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ flutter {
5454

5555
dependencies {
5656
implementation "androidx.datastore:datastore-preferences:1.0.0"
57+
implementation 'androidx.preference:preference:1.2.1'
5758
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
package com.example.sharedpreferencesexample;
6+
7+
import android.content.SharedPreferences;
8+
9+
import androidx.annotation.NonNull;
10+
import androidx.preference.PreferenceManager;
11+
12+
import io.flutter.embedding.android.FlutterActivity;
13+
import io.flutter.embedding.engine.FlutterEngine;
14+
15+
public class MainActivity extends FlutterActivity {
16+
17+
@Override
18+
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
19+
super.configureFlutterEngine(flutterEngine);
20+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
21+
getApplicationContext());
22+
preferences
23+
.edit()
24+
.putString("thisStringIsWrittenInTheExampleAppKotlinCode", "testString")
25+
.commit();
26+
}
27+
}

packages/shared_preferences/shared_preferences_android/example/integration_test/shared_preferences_test.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void main() {
517517
: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
518518
originalSharedPreferencesOptions: fileName == null
519519
? null
520-
: OriginalSharedPreferencesOptions(fileName: fileName),
520+
: AndroidSharedPreferencesStoreOptions(fileName: fileName),
521521
);
522522
}
523523

@@ -782,4 +782,15 @@ void main() {
782782
await preferences.getInt(intKey, getOptions(false, fileName: 'file2')),
783783
2);
784784
});
785+
786+
testWidgets('Shared Preferences can read default sharedPreferences',
787+
(WidgetTester _) async {
788+
final SharedPreferencesAsyncPlatform preferences =
789+
await getPreferences(false);
790+
791+
expect(
792+
await preferences.getString(
793+
'thisStringIsWrittenInTheExampleAppKotlinCode', getOptions(false)),
794+
'testString');
795+
});
785796
}

packages/shared_preferences/shared_preferences_android/example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void main() {
1616
const SharedPreferencesAsyncAndroidOptions options =
1717
SharedPreferencesAsyncAndroidOptions(
1818
backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences,
19-
originalSharedPreferencesOptions:
20-
OriginalSharedPreferencesOptions(fileName: 'the_name_of_a_file'));
19+
originalSharedPreferencesOptions: AndroidSharedPreferencesStoreOptions(
20+
fileName: 'the_name_of_a_file'));
2121
// #enddocregion Android_Options
2222

2323
class MyApp extends StatelessWidget {

packages/shared_preferences/shared_preferences_android/lib/src/shared_preferences_async_android.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,25 @@ class SharedPreferencesAsyncAndroidOptions extends SharedPreferencesOptions {
257257
this.originalSharedPreferencesOptions,
258258
});
259259

260-
/// Which backend should be used with
260+
/// Which backend should be used for this method call.
261261
final SharedPreferencesAndroidBackendLibrary backend;
262262

263-
/// These options define how the `SharedPreferences` library should behave.
263+
/// These options define how the `SharedPreferences` backend should behave.
264264
///
265265
/// Any options in this field will be ignored unless the backend that is selected
266266
/// is `SharedPreferences`.
267-
final OriginalSharedPreferencesOptions? originalSharedPreferencesOptions;
267+
final AndroidSharedPreferencesStoreOptions? originalSharedPreferencesOptions;
268268
}
269269

270270
/// Options necessary for defining the use of the original `SharedPreferences`
271271
/// library.
272272
///
273273
/// These options are only ever used with the original `SharedPreferences` and
274274
/// have no purpose when using the default DataStore Preferences.
275-
class OriginalSharedPreferencesOptions {
276-
/// Constructor for OriginalSharedPreferencesOptions.
277-
const OriginalSharedPreferencesOptions({this.fileName = ''});
275+
class AndroidSharedPreferencesStoreOptions {
276+
/// Constructor for AndroidSharedPreferencesStoreOptions.
277+
const AndroidSharedPreferencesStoreOptions({this.fileName});
278278

279279
/// The name of the file in which the preferences are stored.
280-
final String fileName;
280+
final String? fileName;
281281
}

0 commit comments

Comments
 (0)