|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 4 | +import 'package:json_cache/json_cache.dart'; |
| 5 | + |
| 6 | +/// Unit-testing purposes implementation of the [FlutterSecureStorage] class. |
| 7 | +class FlutterSecureStorageMock implements FlutterSecureStorage { |
| 8 | + /// Uses a instance of [JsonCacheFake] as the internal in-memory cache. |
| 9 | + FlutterSecureStorageMock() : this.fake(JsonCacheFake()); |
| 10 | + |
| 11 | + /// Set the fake cache instance. |
| 12 | + FlutterSecureStorageMock.fake(this._fakeCache); |
| 13 | + |
| 14 | + final JsonCache _fakeCache; |
| 15 | + |
| 16 | + /// throws [UnimplementedError]. |
| 17 | + @override |
| 18 | + AndroidOptions get aOptions => throw UnimplementedError(); |
| 19 | + |
| 20 | + /// Tells how many times [write] has been invoked. |
| 21 | + int writeInvokations = 0; |
| 22 | + |
| 23 | + /// Tells how many times [read] has been invoked. |
| 24 | + int readInvokations = 0; |
| 25 | + |
| 26 | + /// Tells how many times [delete] has been invoked. |
| 27 | + int deleteInvokations = 0; |
| 28 | + |
| 29 | + /// Tells how many times [deleteAll] has been invoked. |
| 30 | + int deleteAllInvokations = 0; |
| 31 | + @override |
| 32 | + Future<bool> containsKey({ |
| 33 | + required String key, |
| 34 | + IOSOptions? iOptions, |
| 35 | + AndroidOptions? aOptions, |
| 36 | + LinuxOptions? lOptions, |
| 37 | + WebOptions? webOptions, |
| 38 | + MacOsOptions? mOptions, |
| 39 | + WindowsOptions? wOptions, |
| 40 | + }) async { |
| 41 | + return (await _fakeCache.value(key)) != null; |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Future<void> delete({ |
| 46 | + required String key, |
| 47 | + IOSOptions? iOptions, |
| 48 | + AndroidOptions? aOptions, |
| 49 | + LinuxOptions? lOptions, |
| 50 | + WebOptions? webOptions, |
| 51 | + MacOsOptions? mOptions, |
| 52 | + WindowsOptions? wOptions, |
| 53 | + }) async { |
| 54 | + ++deleteInvokations; |
| 55 | + await _fakeCache.remove(key); |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + Future<void> deleteAll({ |
| 60 | + IOSOptions? iOptions, |
| 61 | + AndroidOptions? aOptions, |
| 62 | + LinuxOptions? lOptions, |
| 63 | + WebOptions? webOptions, |
| 64 | + MacOsOptions? mOptions, |
| 65 | + WindowsOptions? wOptions, |
| 66 | + }) async { |
| 67 | + ++deleteAllInvokations; |
| 68 | + await _fakeCache.clear(); |
| 69 | + } |
| 70 | + |
| 71 | + /// throws [UnimplementedError]. |
| 72 | + @override |
| 73 | + IOSOptions get iOptions => throw UnimplementedError(); |
| 74 | + |
| 75 | + /// throws [UnimplementedError]. |
| 76 | + @override |
| 77 | + LinuxOptions get lOptions => throw UnimplementedError(); |
| 78 | + |
| 79 | + /// throws [UnimplementedError]. |
| 80 | + @override |
| 81 | + MacOsOptions get mOptions => throw UnimplementedError(); |
| 82 | + |
| 83 | + @override |
| 84 | + Future<String?> read({ |
| 85 | + required String key, |
| 86 | + IOSOptions? iOptions, |
| 87 | + AndroidOptions? aOptions, |
| 88 | + LinuxOptions? lOptions, |
| 89 | + WebOptions? webOptions, |
| 90 | + MacOsOptions? mOptions, |
| 91 | + WindowsOptions? wOptions, |
| 92 | + }) async { |
| 93 | + ++readInvokations; |
| 94 | + final jsonObj = await _fakeCache.value(key); |
| 95 | + return jsonObj == null ? null : json.encode(jsonObj); |
| 96 | + } |
| 97 | + |
| 98 | + /// throws [UnimplementedError]. |
| 99 | + @override |
| 100 | + Future<Map<String, String>> readAll({ |
| 101 | + IOSOptions? iOptions, |
| 102 | + AndroidOptions? aOptions, |
| 103 | + LinuxOptions? lOptions, |
| 104 | + WebOptions? webOptions, |
| 105 | + MacOsOptions? mOptions, |
| 106 | + WindowsOptions? wOptions, |
| 107 | + }) { |
| 108 | + // TODO: implement readAll |
| 109 | + throw UnimplementedError(); |
| 110 | + } |
| 111 | + |
| 112 | + /// throws [UnimplementedError]. |
| 113 | + @override |
| 114 | + WindowsOptions get wOptions => throw UnimplementedError(); |
| 115 | + |
| 116 | + /// throws [UnimplementedError]. |
| 117 | + @override |
| 118 | + WebOptions get webOptions => throw UnimplementedError(); |
| 119 | + |
| 120 | + @override |
| 121 | + Future<void> write({ |
| 122 | + required String key, |
| 123 | + required String? value, |
| 124 | + IOSOptions? iOptions, |
| 125 | + AndroidOptions? aOptions, |
| 126 | + LinuxOptions? lOptions, |
| 127 | + WebOptions? webOptions, |
| 128 | + MacOsOptions? mOptions, |
| 129 | + WindowsOptions? wOptions, |
| 130 | + }) async { |
| 131 | + ++writeInvokations; |
| 132 | + if (value != null) { |
| 133 | + await _fakeCache.refresh(key, json.decode(value) as Map<String, dynamic>); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments