1
1
import 'package:flutter/foundation.dart' ;
2
+ import 'package:url_launcher/url_launcher.dart' as url_launcher;
2
3
import 'package:zulip/model/binding.dart' ;
3
4
import 'package:zulip/model/store.dart' ;
4
5
5
6
import 'test_store.dart' ;
6
7
7
8
/// A concrete binding for use in the `flutter test` environment.
8
9
///
9
- /// Tests that will mount a [GlobalStoreWidget] should initialize this binding
10
+ /// Tests that will mount a [GlobalStoreWidget] , or invoke a Flutter plugin,
11
+ /// should initialize this binding
10
12
/// by calling [ensureInitialized] at the start of the `main` method.
11
13
///
12
14
/// Individual test functions that mount a [GlobalStoreWidget] may then use
@@ -41,37 +43,41 @@ class TestZulipBinding extends ZulipBinding {
41
43
_instance = this ;
42
44
}
43
45
44
- /// The current global store offered to a [GlobalStoreWidget] .
45
- ///
46
- /// The store is created lazily when accessing this getter, or when mounting
47
- /// a [GlobalStoreWidget] . The same store will continue to be provided until
48
- /// a call to [reset] .
49
- ///
50
- /// Tests that access this getter, or that mount a [GlobalStoreWidget] ,
51
- /// should clean up by calling [reset] .
52
- TestGlobalStore get globalStore => _globalStore ?? = TestGlobalStore (accounts: []);
53
- TestGlobalStore ? _globalStore;
54
-
55
- bool _debugAlreadyLoaded = false ;
56
-
57
46
/// Reset all test data to a clean state.
58
47
///
59
- /// Tests that mount a [GlobalStoreWidget] , or that access [globalStore] ,
48
+ /// Tests that mount a [GlobalStoreWidget] , or invoke a Flutter plugin,
49
+ /// or access [globalStore] or other methods on this class,
60
50
/// should clean up by calling this method. Typically this is done using
61
51
/// [addTearDown] , like `addTearDown(TestZulipBinding.instance.reset);` .
62
52
void reset () {
63
53
_globalStore? .dispose ();
64
54
_globalStore = null ;
65
55
assert (() {
66
- _debugAlreadyLoaded = false ;
56
+ _debugAlreadyLoadedStore = false ;
67
57
return true ;
68
58
}());
59
+
60
+ launchUrlResult = true ;
61
+ _launchUrlCalls = null ;
69
62
}
70
63
64
+ /// The current global store offered to a [GlobalStoreWidget] .
65
+ ///
66
+ /// The store is created lazily when accessing this getter, or when mounting
67
+ /// a [GlobalStoreWidget] . The same store will continue to be provided until
68
+ /// a call to [reset] .
69
+ ///
70
+ /// Tests that access this getter, or that mount a [GlobalStoreWidget] ,
71
+ /// should clean up by calling [reset] .
72
+ TestGlobalStore get globalStore => _globalStore ?? = TestGlobalStore (accounts: []);
73
+ TestGlobalStore ? _globalStore;
74
+
75
+ bool _debugAlreadyLoadedStore = false ;
76
+
71
77
@override
72
78
Future <GlobalStore > loadGlobalStore () {
73
79
assert (() {
74
- if (_debugAlreadyLoaded ) {
80
+ if (_debugAlreadyLoadedStore ) {
75
81
throw FlutterError .fromParts ([
76
82
ErrorSummary ('The same test global store was loaded twice.' ),
77
83
ErrorDescription (
@@ -87,9 +93,37 @@ class TestZulipBinding extends ZulipBinding {
87
93
),
88
94
]);
89
95
}
90
- _debugAlreadyLoaded = true ;
96
+ _debugAlreadyLoadedStore = true ;
91
97
return true ;
92
98
}());
93
99
return Future .value (globalStore);
94
100
}
101
+
102
+ /// The value that `ZulipBinding.instance.launchUrl()` should return.
103
+ ///
104
+ /// See also [takeLaunchUrlCalls] .
105
+ bool launchUrlResult = true ;
106
+
107
+ /// Consume the log of calls made to `ZulipBinding.instance.launchUrl()` .
108
+ ///
109
+ /// This returns a list of the arguments to all calls made
110
+ /// to `ZulipBinding.instance.launchUrl()` since the last call to
111
+ /// either this method or [reset] .
112
+ ///
113
+ /// See also [launchUrlResult] .
114
+ List <({Uri url, url_launcher.LaunchMode mode})> takeLaunchUrlCalls () {
115
+ final result = _launchUrlCalls;
116
+ _launchUrlCalls = null ;
117
+ return result ?? [];
118
+ }
119
+ List <({Uri url, url_launcher.LaunchMode mode})>? _launchUrlCalls;
120
+
121
+ @override
122
+ Future <bool > launchUrl (
123
+ Uri url, {
124
+ url_launcher.LaunchMode mode = url_launcher.LaunchMode .platformDefault,
125
+ }) async {
126
+ (_launchUrlCalls ?? = []).add ((url: url, mode: mode));
127
+ return launchUrlResult;
128
+ }
95
129
}
0 commit comments