22library package_info_plus_web_test;
33
44import 'dart:convert' ;
5+ import 'dart:ui_web' as ui_web;
56
7+ import 'package:clock/clock.dart' ;
68import 'package:flutter_test/flutter_test.dart' ;
79import 'package:http/http.dart' as http;
810import 'package:integration_test/integration_test.dart' ;
@@ -13,7 +15,7 @@ import 'package:package_info_plus/src/package_info_plus_web.dart';
1315import 'package_info_plus_test.dart' as common_tests;
1416import 'package_info_plus_web_test.mocks.dart' ;
1517
16- @GenerateMocks ([http.Client ])
18+ @GenerateMocks ([http.Client , ui_web. AssetManager ])
1719void main () {
1820 common_tests.main ();
1921
@@ -29,17 +31,28 @@ void main() {
2931 'build_signature' : '' ,
3032 };
3133
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+
3244 late PackageInfoPlusWebPlugin plugin;
3345 late MockClient client;
34-
35- setUp (() {
36- client = MockClient ();
37- plugin = PackageInfoPlusWebPlugin (client);
38- });
46+ late MockAssetManager assetManagerMock;
3947
4048 group (
4149 'Package Info Web' ,
4250 () {
51+ setUp (() {
52+ client = MockClient ();
53+ plugin = PackageInfoPlusWebPlugin (client);
54+ });
55+
4356 testWidgets (
4457 'Get correct values when response status is 200' ,
4558 (tester) async {
@@ -78,6 +91,35 @@ void main() {
7891 },
7992 );
8093
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+
81123 testWidgets (
82124 'Get correct versionJsonUrl for http and https' ,
83125 (tester) async {
@@ -177,4 +219,89 @@ void main() {
177219 });
178220 },
179221 );
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+ });
180307}
0 commit comments