1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
class MemoryPressureObserver extends WidgetsBindingObserver {
bool sawMemoryPressure = false;
@override
void didHaveMemoryPressure() {
sawMemoryPressure = true;
}
}
class AppLifecycleStateObserver extends WidgetsBindingObserver {
AppLifecycleState lifecycleState;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
lifecycleState = state;
}
}
class PushRouteObserver extends WidgetsBindingObserver {
String pushedRoute;
@override
Future<bool> didPushRoute(String route) async {
pushedRoute = route;
return true;
}
}
void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
});
testWidgets('didHaveMemoryPressure callback', (WidgetTester tester) async {
final MemoryPressureObserver observer = new MemoryPressureObserver();
WidgetsBinding.instance.addObserver(observer);
final ByteData message = const JSONMessageCodec().encodeMessage(
<String, dynamic>{'type': 'memoryPressure'});
await BinaryMessages.handlePlatformMessage('flutter/system', message, (_) {});
expect(observer.sawMemoryPressure, true);
WidgetsBinding.instance.removeObserver(observer);
});
testWidgets('handleLifecycleStateChanged callback', (WidgetTester tester) async {
final AppLifecycleStateObserver observer = new AppLifecycleStateObserver();
WidgetsBinding.instance.addObserver(observer);
ByteData message = const StringCodec().encodeMessage('AppLifecycleState.paused');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(observer.lifecycleState, AppLifecycleState.paused);
message = const StringCodec().encodeMessage('AppLifecycleState.resumed');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(observer.lifecycleState, AppLifecycleState.resumed);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(observer.lifecycleState, AppLifecycleState.inactive);
message = const StringCodec().encodeMessage('AppLifecycleState.suspending');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(observer.lifecycleState, AppLifecycleState.suspending);
});
testWidgets('didPushRoute callback', (WidgetTester tester) async {
final PushRouteObserver observer = new PushRouteObserver();
WidgetsBinding.instance.addObserver(observer);
const String testRouteName = 'testRouteName';
final ByteData message = const JSONMethodCodec().encodeMethodCall(
const MethodCall('pushRoute', testRouteName));
await BinaryMessages.handlePlatformMessage('flutter/navigation', message, (_) {});
expect(observer.pushedRoute, testRouteName);
WidgetsBinding.instance.removeObserver(observer);
});
testWidgets('Application lifecycle affects frame scheduling', (WidgetTester tester) async {
ByteData message;
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.paused');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.resumed');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.suspending');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.paused');
await BinaryMessages.handlePlatformMessage('flutter/lifecycle', message, (_) {});
expect(tester.binding.hasScheduledFrame, isFalse);
tester.binding.scheduleFrame();
expect(tester.binding.hasScheduledFrame, isFalse);
tester.binding.scheduleForcedFrame();
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
expect(tester.binding.hasScheduledFrame, isFalse);
int frameCount = 0;
tester.binding.addPostFrameCallback((Duration duration) { frameCount += 1; });
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pump(const Duration(milliseconds: 1));
expect(tester.binding.hasScheduledFrame, isFalse);
expect(frameCount, 0);
tester.binding.scheduleWarmUpFrame(); // this actually tests flutter_test's implementation
expect(tester.binding.hasScheduledFrame, isFalse);
expect(frameCount, 1);
});
}