1
+ import 'package:flutter/services.dart' ;
1
2
import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
2
3
import 'package:flutter_secure_storage_platform_interface/flutter_secure_storage_platform_interface.dart' ;
3
4
import 'package:flutter_test/flutter_test.dart' ;
4
5
import 'package:mocktail/mocktail.dart' ;
5
- import 'package:plugin_platform_interface/plugin_platform_interface.dart' ;
6
6
7
- // ✅ Correct Mock Class Implementation
8
- class MockFlutterSecureStoragePlatform extends Mock
9
- with MockPlatformInterfaceMixin
10
- implements FlutterSecureStoragePlatform {}
7
+ import 'flutter_secure_storage_mock.dart' ;
11
8
12
9
void main () {
10
+ TestWidgetsFlutterBinding .ensureInitialized ();
11
+
13
12
late FlutterSecureStorage storage;
14
13
late MockFlutterSecureStoragePlatform mockPlatform;
15
14
15
+ const channel = MethodChannel ('plugins.it_nomads.com/flutter_secure_storage' );
16
+ final methodStorage = MethodChannelFlutterSecureStorage ();
17
+ final log = < MethodCall > [];
18
+
19
+ Future <bool ?>? handler (MethodCall methodCall) async {
20
+ log.add (methodCall);
21
+ if (methodCall.method == 'containsKey' ) {
22
+ return true ;
23
+ } else if (methodCall.method == 'isProtectedDataAvailable' ) {
24
+ return true ;
25
+ }
26
+ return null ;
27
+ }
28
+
16
29
setUp (() {
17
30
mockPlatform = MockFlutterSecureStoragePlatform ();
18
31
FlutterSecureStoragePlatform .instance = mockPlatform;
19
32
storage = const FlutterSecureStorage ();
33
+
34
+ // Ensure method channel mock is set up for the tests
35
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
36
+ .setMockMethodCallHandler (channel, handler);
37
+
38
+ log.clear (); // Clear logs before each test
39
+ });
40
+
41
+ tearDown (() {
42
+ log.clear (); // Clear logs after each test
43
+ TestDefaultBinaryMessengerBinding .instance.defaultBinaryMessenger
44
+ .setMockMethodCallHandler (channel, null ); // Remove the mock handler
45
+ });
46
+
47
+ group ('Method Channel Interaction Tests for FlutterSecureStorage' , () {
48
+ test ('read' , () async {
49
+ const key = 'test_key' ;
50
+ const options = < String , String > {};
51
+ await methodStorage.read (key: key, options: options);
52
+
53
+ expect (
54
+ log,
55
+ < Matcher > [
56
+ isMethodCall (
57
+ 'read' ,
58
+ arguments: < String , Object > {
59
+ 'key' : key,
60
+ 'options' : options,
61
+ },
62
+ ),
63
+ ],
64
+ );
65
+ });
66
+
67
+ test ('write' , () async {
68
+ const key = 'test_key' ;
69
+ const options = < String , String > {};
70
+ await methodStorage.write (key: key, value: 'test' , options: options);
71
+
72
+ expect (
73
+ log,
74
+ < Matcher > [
75
+ isMethodCall (
76
+ 'write' ,
77
+ arguments: < String , Object > {
78
+ 'key' : key,
79
+ 'value' : 'test' ,
80
+ 'options' : options,
81
+ },
82
+ ),
83
+ ],
84
+ );
85
+ });
86
+
87
+ test ('containsKey' , () async {
88
+ const key = 'test_key' ;
89
+ const options = < String , String > {};
90
+ await methodStorage.write (key: key, value: 'test' , options: options);
91
+
92
+ final result =
93
+ await methodStorage.containsKey (key: key, options: options);
94
+
95
+ expect (result, true );
96
+ });
97
+
98
+ test ('delete' , () async {
99
+ const key = 'test_key' ;
100
+ const options = < String , String > {};
101
+ await methodStorage.write (key: key, value: 'test' , options: options);
102
+ await methodStorage.delete (key: key, options: options);
103
+
104
+ expect (
105
+ log,
106
+ < Matcher > [
107
+ isMethodCall (
108
+ 'write' ,
109
+ arguments: < String , Object > {
110
+ 'key' : key,
111
+ 'value' : 'test' ,
112
+ 'options' : options,
113
+ },
114
+ ),
115
+ isMethodCall (
116
+ 'delete' ,
117
+ arguments: < String , Object > {
118
+ 'key' : key,
119
+ 'options' : options,
120
+ },
121
+ ),
122
+ ],
123
+ );
124
+ });
125
+
126
+ test ('deleteAll' , () async {
127
+ const options = < String , String > {};
128
+ await methodStorage.deleteAll (options: options);
129
+
130
+ expect (
131
+ log,
132
+ < Matcher > [
133
+ isMethodCall (
134
+ 'deleteAll' ,
135
+ arguments: < String , Object > {
136
+ 'options' : options,
137
+ },
138
+ ),
139
+ ],
140
+ );
141
+ });
142
+ });
143
+
144
+ group ('Platform-Specific Interface Tests' , () {
145
+ test ('Cannot be implemented with `implements`' , () {
146
+ expect (
147
+ () {
148
+ FlutterSecureStoragePlatform .instance =
149
+ ImplementsFlutterSecureStoragePlatform ();
150
+ },
151
+ throwsA (isInstanceOf <AssertionError >()),
152
+ );
153
+ });
154
+
155
+ test ('Can be mocked with `implements`' , () {
156
+ final mock = MockFlutterSecureStoragePlatform ();
157
+ FlutterSecureStoragePlatform .instance = mock;
158
+ });
159
+
160
+ test ('Can be extended' , () {
161
+ FlutterSecureStoragePlatform .instance =
162
+ ExtendsFlutterSecureStoragePlatform ();
163
+ });
20
164
});
21
165
22
- group ('FlutterSecureStorage Tests' , () {
166
+ group ('FlutterSecureStorage Methods Invocation Tests' , () {
23
167
const testKey = 'testKey' ;
24
168
const testValue = 'testValue' ;
25
169
@@ -118,7 +262,7 @@ void main() {
118
262
});
119
263
});
120
264
121
- group ('AndroidOptions Tests' , () {
265
+ group ('AndroidOptions Configuration Tests' , () {
122
266
test ('Default AndroidOptions should have correct default values' , () {
123
267
const options = AndroidOptions .defaultOptions;
124
268
@@ -201,7 +345,7 @@ void main() {
201
345
});
202
346
});
203
347
204
- group ('WebOptions Tests' , () {
348
+ group ('WebOptions Configuration Tests' , () {
205
349
test ('Default WebOptions should have correct default values' , () {
206
350
const options = WebOptions .defaultOptions;
207
351
@@ -261,7 +405,7 @@ void main() {
261
405
});
262
406
});
263
407
264
- group ('WindowsOptions Tests' , () {
408
+ group ('WindowsOptions Configuration Tests' , () {
265
409
test ('Default WindowsOptions should have correct default values' , () {
266
410
const options = WindowsOptions .defaultOptions;
267
411
@@ -308,7 +452,7 @@ void main() {
308
452
});
309
453
});
310
454
311
- group ('IOSOptions Tests' , () {
455
+ group ('iOSOptions Configuration Tests' , () {
312
456
test ('Default IOSOptions should have correct default values' , () {
313
457
const options = IOSOptions .defaultOptions;
314
458
@@ -368,8 +512,8 @@ void main() {
368
512
});
369
513
});
370
514
371
- group ('MacOsOptions Tests' , () {
372
- test ('Default MacOsOptions should have correct default values' , () {
515
+ group ('macOSOptions Configuration Tests' , () {
516
+ test ('Default macOSOptions should have correct default values' , () {
373
517
// Ignore for test
374
518
// ignore: use_named_constants
375
519
const options = MacOsOptions ();
@@ -382,7 +526,7 @@ void main() {
382
526
});
383
527
});
384
528
385
- test ('MacOsOptions with custom values' , () {
529
+ test ('macOSOptions with custom values' , () {
386
530
const options = MacOsOptions (
387
531
accountName: 'macAccount' ,
388
532
groupId: 'group.mac.example' ,
@@ -400,7 +544,7 @@ void main() {
400
544
});
401
545
});
402
546
403
- test ('MacOsOptions defaultOptions matches default constructor' , () {
547
+ test ('macOSOptions defaultOptions matches default constructor' , () {
404
548
const defaultOptions = MacOsOptions .defaultOptions;
405
549
// Ignore for test
406
550
// ignore: use_named_constants
0 commit comments