2
2
library package_info_plus_web_test;
3
3
4
4
import 'dart:convert' ;
5
+ import 'dart:ui_web' as ui_web;
5
6
7
+ import 'package:clock/clock.dart' ;
6
8
import 'package:flutter_test/flutter_test.dart' ;
7
9
import 'package:http/http.dart' as http;
8
10
import 'package:integration_test/integration_test.dart' ;
@@ -13,7 +15,7 @@ import 'package:package_info_plus/src/package_info_plus_web.dart';
13
15
import 'package_info_plus_test.dart' as common_tests;
14
16
import 'package_info_plus_web_test.mocks.dart' ;
15
17
16
- @GenerateMocks ([http.Client ])
18
+ @GenerateMocks ([http.Client , ui_web. AssetManager ])
17
19
void main () {
18
20
common_tests.main ();
19
21
@@ -29,17 +31,28 @@ void main() {
29
31
'build_signature' : '' ,
30
32
};
31
33
34
+ // ignore: constant_identifier_names
35
+ const VERSION_2_JSON = {
36
+ 'app_name' : 'package_info_example' ,
37
+ 'build_number' : '2' ,
38
+ 'package_name' : 'io.flutter.plugins.packageinfoexample' ,
39
+ 'version' : '2.0' ,
40
+ 'installerStore' : null ,
41
+ 'build_signature' : '' ,
42
+ };
43
+
32
44
late PackageInfoPlusWebPlugin plugin;
33
45
late MockClient client;
34
-
35
- setUp (() {
36
- client = MockClient ();
37
- plugin = PackageInfoPlusWebPlugin (client);
38
- });
46
+ late MockAssetManager assetManagerMock;
39
47
40
48
group (
41
49
'Package Info Web' ,
42
50
() {
51
+ setUp (() {
52
+ client = MockClient ();
53
+ plugin = PackageInfoPlusWebPlugin (client);
54
+ });
55
+
43
56
testWidgets (
44
57
'Get correct values when response status is 200' ,
45
58
(tester) async {
@@ -78,6 +91,35 @@ void main() {
78
91
},
79
92
);
80
93
94
+ testWidgets (
95
+ 'Get correct values when using a custom base URL' ,
96
+ (tester) async {
97
+ const String baseUrl = 'https://www.example.com/' ;
98
+ final DateTime now = DateTime .now ();
99
+ final Clock fakeClock = Clock (() => now);
100
+
101
+ await withClock (fakeClock, () async {
102
+ final int cache = now.millisecondsSinceEpoch;
103
+
104
+ when (client.get (
105
+ Uri .parse ('${baseUrl }version.json?cachebuster=$cache ' ),
106
+ )).thenAnswer (
107
+ (_) => Future .value (
108
+ http.Response (jsonEncode (VERSION_JSON ), 200 ),
109
+ ),
110
+ );
111
+
112
+ final versionMap = await plugin.getAll (baseUrl: baseUrl);
113
+
114
+ expect (versionMap.appName, VERSION_JSON ['app_name' ]);
115
+ expect (versionMap.version, VERSION_JSON ['version' ]);
116
+ expect (versionMap.buildNumber, VERSION_JSON ['build_number' ]);
117
+ expect (versionMap.packageName, VERSION_JSON ['package_name' ]);
118
+ expect (versionMap.buildSignature, VERSION_JSON ['build_signature' ]);
119
+ });
120
+ },
121
+ );
122
+
81
123
testWidgets (
82
124
'Get correct versionJsonUrl for http and https' ,
83
125
(tester) async {
@@ -177,4 +219,89 @@ void main() {
177
219
});
178
220
},
179
221
);
222
+
223
+ group ('Package Info Web (using MockAssetManager)' , () {
224
+ setUp (() {
225
+ client = MockClient ();
226
+ assetManagerMock = MockAssetManager ();
227
+ plugin = PackageInfoPlusWebPlugin (client, assetManagerMock);
228
+ });
229
+
230
+ testWidgets (
231
+ 'Get correct values when using the AssetManager baseUrl' ,
232
+ (tester) async {
233
+ const String baseUrl = 'https://an.example.com/using-asset-manager/' ;
234
+ const String assetsDir = 'assets' ;
235
+ final DateTime now = DateTime .now ();
236
+ final Clock fakeClock = Clock (() => now);
237
+
238
+ when (assetManagerMock.assetsDir).thenReturn (assetsDir);
239
+ when (assetManagerMock.getAssetUrl ('' ))
240
+ .thenReturn ('$baseUrl $assetsDir /' );
241
+
242
+ await withClock (fakeClock, () async {
243
+ final int cache = now.millisecondsSinceEpoch;
244
+
245
+ when (client.get (
246
+ Uri .parse ('${baseUrl }version.json?cachebuster=$cache ' ),
247
+ )).thenAnswer (
248
+ (_) => Future .value (
249
+ http.Response (jsonEncode (VERSION_JSON ), 200 ),
250
+ ),
251
+ );
252
+
253
+ final versionMap = await plugin.getAll ();
254
+
255
+ expect (versionMap.appName, VERSION_JSON ['app_name' ]);
256
+ expect (versionMap.version, VERSION_JSON ['version' ]);
257
+ expect (versionMap.buildNumber, VERSION_JSON ['build_number' ]);
258
+ expect (versionMap.packageName, VERSION_JSON ['package_name' ]);
259
+ expect (versionMap.buildSignature, VERSION_JSON ['build_signature' ]);
260
+ });
261
+ },
262
+ );
263
+
264
+ testWidgets (
265
+ 'Has preference for the custom base URL over the other 2 locations' ,
266
+ (tester) async {
267
+ const String customBaseUrl = 'https://www.example.com/with-path/' ;
268
+ const String managerBaseUrl = 'https://www.asset-manager.com/path/' ;
269
+ const String assetsDir = 'assets' ;
270
+ final DateTime now = DateTime .now ();
271
+ final Clock fakeClock = Clock (() => now);
272
+
273
+ when (assetManagerMock.assetsDir).thenReturn (assetsDir);
274
+ when (assetManagerMock.getAssetUrl ('' ))
275
+ .thenReturn ('$managerBaseUrl $assetsDir /' );
276
+
277
+ await withClock (fakeClock, () async {
278
+ final int cache = now.millisecondsSinceEpoch;
279
+
280
+ when (client.get (
281
+ Uri .parse ('${customBaseUrl }version.json?cachebuster=$cache ' ),
282
+ )).thenAnswer (
283
+ (_) => Future .value (
284
+ http.Response (jsonEncode (VERSION_JSON ), 200 ),
285
+ ),
286
+ );
287
+
288
+ when (client.get (
289
+ Uri .parse ('${managerBaseUrl }version.json?cachebuster=$cache ' ),
290
+ )).thenAnswer (
291
+ (_) => Future .value (
292
+ http.Response (jsonEncode (VERSION_2_JSON ), 200 ),
293
+ ),
294
+ );
295
+
296
+ final versionMap = await plugin.getAll (baseUrl: customBaseUrl);
297
+
298
+ expect (versionMap.appName, VERSION_JSON ['app_name' ]);
299
+ expect (versionMap.version, VERSION_JSON ['version' ]);
300
+ expect (versionMap.buildNumber, VERSION_JSON ['build_number' ]);
301
+ expect (versionMap.packageName, VERSION_JSON ['package_name' ]);
302
+ expect (versionMap.buildSignature, VERSION_JSON ['build_signature' ]);
303
+ });
304
+ },
305
+ );
306
+ });
180
307
}
0 commit comments