22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ import 'dart:async' ;
6+
7+ import 'package:flutter/services.dart' ;
58import 'package:mockito/mockito.dart' ;
69import 'package:test/test.dart' ;
710
@@ -14,10 +17,23 @@ void main() {
1417 group ('FirebaseAnalyticsObserver' , () {
1518 FirebaseAnalytics analytics;
1619 FirebaseAnalyticsObserver observer;
20+ final List <String > printLog = < String > [];
21+
22+ void overridePrint (void Function () func) {
23+ final ZoneSpecification spec =
24+ ZoneSpecification (print: (_, __, ___, String msg) {
25+ // Add to log instead of printing to stdout
26+ printLog.add (msg);
27+ });
28+ return Zone .current.fork (specification: spec).run (func);
29+ }
1730
1831 setUp (() {
32+ printLog.clear ();
1933 analytics = MockFirebaseAnalytics ();
2034 observer = FirebaseAnalyticsObserver (analytics: analytics);
35+ when (analytics.setCurrentScreen (screenName: anyNamed ('screenName' )))
36+ .thenAnswer ((Invocation invocation) => Future <void >.value ());
2137 });
2238
2339 test ('setCurrentScreen on route pop' , () {
@@ -53,6 +69,66 @@ void main() {
5369
5470 verify (analytics.setCurrentScreen (screenName: 'foo' )).called (1 );
5571 });
72+
73+ test ('handles only ${PlatformException }s' , () async {
74+ observer = FirebaseAnalyticsObserver (
75+ analytics: analytics,
76+ nameExtractor: (RouteSettings settings) => 'foo' ,
77+ );
78+
79+ final PageRoute <dynamic > route = MockPageRoute ();
80+ final PageRoute <dynamic > previousRoute = MockPageRoute ();
81+
82+ // Throws non-PlatformExceptions
83+ when (analytics.setCurrentScreen (screenName: anyNamed ('screenName' )))
84+ .thenThrow (ArgumentError ());
85+
86+ expect (() => observer.didPush (route, previousRoute), throwsArgumentError);
87+
88+ // Print PlatformExceptions
89+ Future <void > throwPlatformException () async =>
90+ throw PlatformException (code: 'a' );
91+
92+ when (analytics.setCurrentScreen (screenName: anyNamed ('screenName' )))
93+ .thenAnswer ((Invocation invocation) => throwPlatformException ());
94+
95+ overridePrint (() => observer.didPush (route, previousRoute));
96+
97+ await pumpEventQueue ();
98+ expect (
99+ printLog,
100+ < String > ['$FirebaseAnalyticsObserver : ${PlatformException (code : 'a' )}' ],
101+ );
102+ });
103+
104+ test ('runs onError' , () async {
105+ PlatformException passedException;
106+
107+ final void Function (PlatformException error) handleError =
108+ (PlatformException error) {
109+ passedException = error;
110+ };
111+
112+ observer = FirebaseAnalyticsObserver (
113+ analytics: analytics,
114+ nameExtractor: (RouteSettings settings) => 'foo' ,
115+ onError: handleError,
116+ );
117+
118+ final PageRoute <dynamic > route = MockPageRoute ();
119+ final PageRoute <dynamic > previousRoute = MockPageRoute ();
120+
121+ final PlatformException thrownException = PlatformException (code: 'b' );
122+ Future <void > throwPlatformException () async => throw thrownException;
123+
124+ when (analytics.setCurrentScreen (screenName: anyNamed ('screenName' )))
125+ .thenAnswer ((Invocation invocation) => throwPlatformException ());
126+
127+ observer.didPush (route, previousRoute);
128+
129+ await pumpEventQueue ();
130+ expect (passedException, thrownException);
131+ });
56132 });
57133}
58134
0 commit comments