Flutter Engine
The Flutter Engine
FlutterAppLifecycleDelegateTest.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppLifecycleDelegate.h"
6
7#import "flutter/testing/testing.h"
8#include "third_party/googletest/googletest/include/gtest/gtest.h"
9
11@property(nonatomic, readwrite, nullable) NSNotification* lastNotification;
12@end
13
15
16- (void)setNotification:(NSNotification*)notification {
17 self.lastNotification = notification;
18}
19
20- (void)handleWillFinishLaunching:(NSNotification*)notification {
21 [self setNotification:notification];
22}
23
24- (void)handleDidFinishLaunching:(NSNotification*)notification {
25 [self setNotification:notification];
26}
27
28- (void)handleWillBecomeActive:(NSNotification*)notification {
29 [self setNotification:notification];
30}
31
32- (void)handleDidBecomeActive:(NSNotification*)notification {
33 [self setNotification:notification];
34}
35
36- (void)handleWillResignActive:(NSNotification*)notification {
37 [self setNotification:notification];
38}
39
40- (void)handleDidResignActive:(NSNotification*)notification {
41 [self setNotification:notification];
42}
43
44- (void)handleWillHide:(NSNotification*)notification {
45 [self setNotification:notification];
46}
47
48- (void)handleDidHide:(NSNotification*)notification {
49 [self setNotification:notification];
50}
51
52- (void)handleWillUnhide:(NSNotification*)notification {
53 [self setNotification:notification];
54}
55
56- (void)handleDidUnhide:(NSNotification*)notification {
57 [self setNotification:notification];
58}
59
60- (void)handleDidChangeScreenParameters:(NSNotification*)notification {
61 [self setNotification:notification];
62}
63
64- (void)handleDidChangeOcclusionState:(NSNotification*)notification API_AVAILABLE(macos(10.9)) {
65 [self setNotification:notification];
66}
67
68- (void)handleWillTerminate:(NSNotification*)notification {
69 [self setNotification:notification];
70}
71
72@end
73
74namespace flutter::testing {
75
76TEST(FlutterAppLifecycleDelegateTest, RespondsToWillFinishLaunching) {
79 [registrar addDelegate:delegate];
80
81 NSNotification* willFinishLaunching =
82 [NSNotification notificationWithName:NSApplicationWillFinishLaunchingNotification object:nil];
83 [registrar handleWillFinishLaunching:willFinishLaunching];
84 EXPECT_EQ([delegate lastNotification], willFinishLaunching);
85}
86
87TEST(FlutterAppLifecycleDelegateTest, RespondsToDidFinishLaunching) {
90 [registrar addDelegate:delegate];
91
92 NSNotification* didFinishLaunching =
93 [NSNotification notificationWithName:NSApplicationDidFinishLaunchingNotification object:nil];
94 [registrar handleDidFinishLaunching:didFinishLaunching];
95 EXPECT_EQ([delegate lastNotification], didFinishLaunching);
96}
97
98TEST(FlutterAppLifecycleDelegateTest, RespondsToWillBecomeActive) {
101 [registrar addDelegate:delegate];
102
103 NSNotification* willBecomeActive =
104 [NSNotification notificationWithName:NSApplicationWillBecomeActiveNotification object:nil];
105 [registrar handleWillBecomeActive:willBecomeActive];
106 EXPECT_EQ([delegate lastNotification], willBecomeActive);
107}
108
109TEST(FlutterAppLifecycleDelegateTest, RespondsToDidBecomeActive) {
112 [registrar addDelegate:delegate];
113
114 NSNotification* didBecomeActive =
115 [NSNotification notificationWithName:NSApplicationDidBecomeActiveNotification object:nil];
116 [registrar handleDidBecomeActive:didBecomeActive];
117 EXPECT_EQ([delegate lastNotification], didBecomeActive);
118}
119
120TEST(FlutterAppLifecycleDelegateTest, RespondsToWillResignActive) {
123 [registrar addDelegate:delegate];
124
125 NSNotification* willResignActive =
126 [NSNotification notificationWithName:NSApplicationWillResignActiveNotification object:nil];
127 [registrar handleWillResignActive:willResignActive];
128 EXPECT_EQ([delegate lastNotification], willResignActive);
129}
130
131TEST(FlutterAppLifecycleDelegateTest, RespondsToDidResignActive) {
134 [registrar addDelegate:delegate];
135
136 NSNotification* didResignActive =
137 [NSNotification notificationWithName:NSApplicationDidResignActiveNotification object:nil];
138 [registrar handleDidResignActive:didResignActive];
139 EXPECT_EQ([delegate lastNotification], didResignActive);
140}
141
142TEST(FlutterAppLifecycleDelegateTest, RespondsToWillTerminate) {
145 [registrar addDelegate:delegate];
146
147 NSNotification* applicationWillTerminate =
148 [NSNotification notificationWithName:NSApplicationWillTerminateNotification object:nil];
149 [registrar handleWillTerminate:applicationWillTerminate];
150 EXPECT_EQ([delegate lastNotification], applicationWillTerminate);
151}
152
153TEST(FlutterAppLifecycleDelegateTest, RespondsToWillHide) {
156 [registrar addDelegate:delegate];
157
158 NSNotification* willHide = [NSNotification notificationWithName:NSApplicationWillHideNotification
159 object:nil];
160 [registrar handleWillHide:willHide];
161 EXPECT_EQ([delegate lastNotification], willHide);
162}
163
164TEST(FlutterAppLifecycleDelegateTest, RespondsToWillUnhide) {
167 [registrar addDelegate:delegate];
168
169 NSNotification* willUnhide =
170 [NSNotification notificationWithName:NSApplicationWillUnhideNotification object:nil];
171 [registrar handleWillUnhide:willUnhide];
172 EXPECT_EQ([delegate lastNotification], willUnhide);
173}
174
175TEST(FlutterAppLifecycleDelegateTest, RespondsToDidHide) {
178 [registrar addDelegate:delegate];
179
180 NSNotification* didHide = [NSNotification notificationWithName:NSApplicationDidHideNotification
181 object:nil];
182 [registrar handleDidHide:didHide];
183 EXPECT_EQ([delegate lastNotification], didHide);
184}
185
186TEST(FlutterAppLifecycleDelegateTest, RespondsToDidUnhide) {
189 [registrar addDelegate:delegate];
190
191 NSNotification* didUnhide =
192 [NSNotification notificationWithName:NSApplicationDidUnhideNotification object:nil];
193 [registrar handleDidUnhide:didUnhide];
194 EXPECT_EQ([delegate lastNotification], didUnhide);
195}
196
197TEST(FlutterAppLifecycleDelegateTest, RespondsToDidChangeScreenParameters) {
200 [registrar addDelegate:delegate];
201
202 NSNotification* didChangeScreenParameters =
203 [NSNotification notificationWithName:NSApplicationDidChangeScreenParametersNotification
204 object:nil];
205 [registrar handleDidChangeScreenParameters:didChangeScreenParameters];
206 EXPECT_EQ([delegate lastNotification], didChangeScreenParameters);
207}
208
209TEST(FlutterAppLifecycleDelegateTest, RespondsToDidChangeOcclusionState) {
212 [registrar addDelegate:delegate];
213
214 NSNotification* didChangeOcclusionState =
215 [NSNotification notificationWithName:NSApplicationDidChangeOcclusionStateNotification
216 object:nil];
217 if ([registrar respondsToSelector:@selector(handleDidChangeOcclusionState:)]) {
218 [registrar handleDidChangeOcclusionState:didChangeOcclusionState];
219 EXPECT_EQ([delegate lastNotification], didChangeOcclusionState);
220 }
221}
222
223TEST(FlutterAppLifecycleDelegateTest, ReleasesDelegateOnDealloc) {
224 __weak FlutterAppLifecycleRegistrar* weakRegistrar;
225 __weak TestFlutterAppLifecycleDelegate* weakDelegate;
226 @autoreleasepool {
228 weakRegistrar = registrar;
230 weakDelegate = delegate;
231 [registrar addDelegate:delegate];
232 }
233 EXPECT_EQ(weakRegistrar, nil);
234 EXPECT_EQ(weakDelegate, nil);
235}
236
237} // namespace flutter::testing
void addDelegate:(NSObject< FlutterAppLifecycleDelegate > *delegate)
void setNotification:(NSNotification *notification)
TEST(FlutterAppLifecycleDelegateTest, ReleasesDelegateOnDealloc)
API_AVAILABLE(ios(14.0), macos(11.0)) static NSString *MTLCommandEncoderErrorStateToString(MTLCommandEncoderErrorState state)
void handleWillUnhide:(NSNotification *notification)
void handleWillTerminate:(NSNotification *notification)
void handleDidUnhide:(NSNotification *notification)
void handleDidBecomeActive:(NSNotification *notification)
void handleDidHide:(NSNotification *notification)
void handleDidResignActive:(NSNotification *notification)
void handleWillResignActive:(NSNotification *notification)
void handleDidChangeScreenParameters:(NSNotification *notification)
void handleWillBecomeActive:(NSNotification *notification)
void handleWillHide:(NSNotification *notification)
void handleDidFinishLaunching:(NSNotification *notification)
void handleDidChangeOcclusionState:(NSNotification *notification)
void handleWillFinishLaunching:(NSNotification *notification)