@@ -16,15 +16,36 @@ void main() {
16
16
'plugins.flutter.io/shared_preferences_android' ,
17
17
);
18
18
19
- const Map <String , Object > kTestValues = < String , Object > {
19
+ const Map <String , Object > flutterTestValues = < String , Object > {
20
20
'flutter.String' : 'hello world' ,
21
21
'flutter.Bool' : true ,
22
22
'flutter.Int' : 42 ,
23
23
'flutter.Double' : 3.14159 ,
24
24
'flutter.StringList' : < String > ['foo' , 'bar' ],
25
25
};
26
- // Create a dummy in-memory implementation to back the mocked method channel
27
- // API to simplify validation of the expected calls.
26
+
27
+ const Map <String , Object > prefixTestValues = < String , Object > {
28
+ 'prefix.String' : 'hello world' ,
29
+ 'prefix.Bool' : true ,
30
+ 'prefix.Int' : 42 ,
31
+ 'prefix.Double' : 3.14159 ,
32
+ 'prefix.StringList' : < String > ['foo' , 'bar' ],
33
+ };
34
+
35
+ const Map <String , Object > nonPrefixTestValues = < String , Object > {
36
+ 'String' : 'hello world' ,
37
+ 'Bool' : true ,
38
+ 'Int' : 42 ,
39
+ 'Double' : 3.14159 ,
40
+ 'StringList' : < String > ['foo' , 'bar' ],
41
+ };
42
+
43
+ final Map <String , Object > allTestValues = < String , Object > {};
44
+
45
+ allTestValues.addAll (flutterTestValues);
46
+ allTestValues.addAll (prefixTestValues);
47
+ allTestValues.addAll (nonPrefixTestValues);
48
+
28
49
late InMemorySharedPreferencesStore testData;
29
50
30
51
final List <MethodCall > log = < MethodCall > [];
@@ -45,6 +66,12 @@ void main() {
45
66
if (methodCall.method == 'getAll' ) {
46
67
return testData.getAll ();
47
68
}
69
+ if (methodCall.method == 'getAllWithPrefix' ) {
70
+ final Map <String , Object ?> arguments =
71
+ getArgumentDictionary (methodCall);
72
+ final String prefix = arguments['prefix' ]! as String ;
73
+ return testData.getAllWithPrefix (prefix);
74
+ }
48
75
if (methodCall.method == 'remove' ) {
49
76
final Map <String , Object ?> arguments =
50
77
getArgumentDictionary (methodCall);
@@ -54,6 +81,12 @@ void main() {
54
81
if (methodCall.method == 'clear' ) {
55
82
return testData.clear ();
56
83
}
84
+ if (methodCall.method == 'clearWithPrefix' ) {
85
+ final Map <String , Object ?> arguments =
86
+ getArgumentDictionary (methodCall);
87
+ final String prefix = arguments['prefix' ]! as String ;
88
+ return testData.clearWithPrefix (prefix);
89
+ }
57
90
final RegExp setterRegExp = RegExp (r'set(.*)' );
58
91
final Match ? match = setterRegExp.matchAsPrefix (methodCall.method);
59
92
if (match? .groupCount == 1 ) {
@@ -77,14 +110,21 @@ void main() {
77
110
78
111
test ('getAll' , () async {
79
112
store = SharedPreferencesAndroid ();
80
- testData = InMemorySharedPreferencesStore .withData (kTestValues);
81
- expect (await store.getAll (), kTestValues);
82
- expect (log.single.method, 'getAll' );
113
+ testData = InMemorySharedPreferencesStore .withData (allTestValues);
114
+ expect (await store.getAll (), flutterTestValues);
115
+ expect (log.single.method, 'getAllWithPrefix' );
116
+ });
117
+
118
+ test ('getAllWithPrefix' , () async {
119
+ store = SharedPreferencesAndroid ();
120
+ testData = InMemorySharedPreferencesStore .withData (allTestValues);
121
+ expect (await store.getAllWithPrefix ('prefix.' ), prefixTestValues);
122
+ expect (log.single.method, 'getAllWithPrefix' );
83
123
});
84
124
85
125
test ('remove' , () async {
86
126
store = SharedPreferencesAndroid ();
87
- testData = InMemorySharedPreferencesStore .withData (kTestValues );
127
+ testData = InMemorySharedPreferencesStore .withData (allTestValues );
88
128
expect (await store.remove ('flutter.String' ), true );
89
129
expect (await store.remove ('flutter.Bool' ), true );
90
130
expect (await store.remove ('flutter.Int' ), true );
@@ -102,13 +142,13 @@ void main() {
102
142
test ('setValue' , () async {
103
143
store = SharedPreferencesAndroid ();
104
144
expect (await testData.getAll (), isEmpty);
105
- for (final String key in kTestValues .keys) {
106
- final Object value = kTestValues [key]! ;
145
+ for (final String key in allTestValues .keys) {
146
+ final Object value = allTestValues [key]! ;
107
147
expect (await store.setValue (key.split ('.' ).last, key, value), true );
108
148
}
109
- expect (await testData.getAll (), kTestValues );
149
+ expect (await testData.getAll (), flutterTestValues );
110
150
111
- expect (log, hasLength (5 ));
151
+ expect (log, hasLength (15 ));
112
152
expect (log[0 ].method, 'setString' );
113
153
expect (log[1 ].method, 'setBool' );
114
154
expect (log[2 ].method, 'setInt' );
@@ -118,11 +158,36 @@ void main() {
118
158
119
159
test ('clear' , () async {
120
160
store = SharedPreferencesAndroid ();
121
- testData = InMemorySharedPreferencesStore .withData (kTestValues );
161
+ testData = InMemorySharedPreferencesStore .withData (allTestValues );
122
162
expect (await testData.getAll (), isNotEmpty);
123
163
expect (await store.clear (), true );
124
164
expect (await testData.getAll (), isEmpty);
125
- expect (log.single.method, 'clear' );
165
+ expect (log.single.method, 'clearWithPrefix' );
166
+ });
167
+
168
+ test ('clearWithPrefix' , () async {
169
+ store = SharedPreferencesAndroid ();
170
+ testData = InMemorySharedPreferencesStore .withData (allTestValues);
171
+
172
+ expect (await testData.getAllWithPrefix ('prefix.' ), isNotEmpty);
173
+ expect (await store.clearWithPrefix ('prefix.' ), true );
174
+ expect (await testData.getAllWithPrefix ('prefix.' ), isEmpty);
175
+ });
176
+
177
+ test ('getAllWithNoPrefix' , () async {
178
+ store = SharedPreferencesAndroid ();
179
+ testData = InMemorySharedPreferencesStore .withData (allTestValues);
180
+
181
+ expect (await testData.getAllWithPrefix ('' ), hasLength (15 ));
182
+ });
183
+
184
+ test ('clearWithNoPrefix' , () async {
185
+ store = SharedPreferencesAndroid ();
186
+ testData = InMemorySharedPreferencesStore .withData (allTestValues);
187
+
188
+ expect (await testData.getAllWithPrefix ('' ), isNotEmpty);
189
+ expect (await store.clearWithPrefix ('' ), true );
190
+ expect (await testData.getAllWithPrefix ('' ), isEmpty);
126
191
});
127
192
});
128
193
}
0 commit comments