diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 375dff55b6..9c0ef5facc 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -728,6 +728,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatfor FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.m FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h index 38b1e49fe7..f69cbd2ea2 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h @@ -39,31 +39,6 @@ FLUTTER_EXPORT - (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions; -/** - * Calls all plugins registered for `UIApplicationDelegate` callbacks. - */ -- (void)applicationDidBecomeActive:(UIApplication*)application; - -/** - * Calls all plugins registered for `UIApplicationDelegate` callbacks. - */ -- (void)applicationWillResignActive:(UIApplication*)application; - -/** - * Calls all plugins registered for `UIApplicationDelegate` callbacks. - */ -- (void)applicationDidEnterBackground:(UIApplication*)application; - -/** - * Calls all plugins registered for `UIApplicationDelegate` callbacks. - */ -- (void)applicationWillEnterForeground:(UIApplication*)application; - -/** - * Calls all plugins registered for `UIApplicationDelegate` callbacks. - */ -- (void)applicationWillTerminate:(UIApplication*)application; - /** * Called if this plugin has been registered for `UIApplicationDelegate` callbacks. */ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index b918437cf6..70f856ba77 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -57,26 +57,6 @@ static NSString* kBackgroundFetchCapatibility = @"fetch"; } } -- (void)applicationDidEnterBackground:(UIApplication*)application { - [_lifeCycleDelegate applicationDidEnterBackground:application]; -} - -- (void)applicationWillEnterForeground:(UIApplication*)application { - [_lifeCycleDelegate applicationWillEnterForeground:application]; -} - -- (void)applicationWillResignActive:(UIApplication*)application { - [_lifeCycleDelegate applicationWillResignActive:application]; -} - -- (void)applicationDidBecomeActive:(UIApplication*)application { - [_lifeCycleDelegate applicationDidBecomeActive:application]; -} - -- (void)applicationWillTerminate:(UIApplication*)application { - [_lifeCycleDelegate applicationWillTerminate:application]; -} - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" - (void)application:(UIApplication*)application diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm index d8c147f990..b0ab5b5a84 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm @@ -15,23 +15,56 @@ static const SEL selectorsHandledByPlugins[] = { @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:), @selector(application:performFetchWithCompletionHandler:)}; +@interface FlutterPluginAppLifeCycleDelegate () +- (void)handleDidEnterBackground:(NSNotification*)notification; +- (void)handleWillEnterForeground:(NSNotification*)notification; +- (void)handleWillResignActive:(NSNotification*)notification; +- (void)handleDidBecomeActive:(NSNotification*)notification; +- (void)handleWillTerminate:(NSNotification*)notification; +@end + @implementation FlutterPluginAppLifeCycleDelegate { + NSMutableArray* _notificationUnsubscribers; UIBackgroundTaskIdentifier _debugBackgroundTask; // Weak references to registered plugins. NSPointerArray* _delegates; } +- (void)addObserverFor:(NSString*)name selector:(SEL)selector { + [[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil]; + __block NSObject* blockSelf = self; + dispatch_block_t unsubscribe = ^{ + [[NSNotificationCenter defaultCenter] removeObserver:blockSelf name:name object:nil]; + }; + [_notificationUnsubscribers addObject:[[unsubscribe copy] autorelease]]; +} + - (instancetype)init { if (self = [super init]) { + _notificationUnsubscribers = [[NSMutableArray alloc] init]; std::string cachePath = fml::paths::JoinPaths({getenv("HOME"), kCallbackCacheSubDir}); [FlutterCallbackCache setCachePath:[NSString stringWithUTF8String:cachePath.c_str()]]; + [self addObserverFor:UIApplicationDidEnterBackgroundNotification + selector:@selector(handleDidEnterBackground:)]; + [self addObserverFor:UIApplicationWillEnterForegroundNotification + selector:@selector(handleWillEnterForeground:)]; + [self addObserverFor:UIApplicationWillResignActiveNotification + selector:@selector(handleWillResignActive:)]; + [self addObserverFor:UIApplicationDidBecomeActiveNotification + selector:@selector(handleDidBecomeActive:)]; + [self addObserverFor:UIApplicationWillTerminateNotification + selector:@selector(handleWillTerminate:)]; _delegates = [[NSPointerArray weakObjectsPointerArray] retain]; } return self; } - (void)dealloc { + for (dispatch_block_t unsubscribe in _notificationUnsubscribers) { + unsubscribe(); + } + [_notificationUnsubscribers release]; [_delegates release]; [super dealloc]; } @@ -99,7 +132,8 @@ static BOOL isPowerOfTwo(NSUInteger x) { return YES; } -- (void)applicationDidEnterBackground:(UIApplication*)application { +- (void)handleDidEnterBackground:(NSNotification*)notification { + UIApplication* application = [UIApplication sharedApplication]; #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG // The following keeps the Flutter session alive when the device screen locks // in debug mode. It allows continued use of features like hot reload and @@ -121,13 +155,14 @@ static BOOL isPowerOfTwo(NSUInteger x) { if (!delegate) { continue; } - if ([delegate respondsToSelector:_cmd]) { + if ([delegate respondsToSelector:@selector(applicationDidEnterBackground:)]) { [delegate applicationDidEnterBackground:application]; } } } -- (void)applicationWillEnterForeground:(UIApplication*)application { +- (void)handleWillEnterForeground:(NSNotification*)notification { + UIApplication* application = [UIApplication sharedApplication]; #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG [application endBackgroundTask:_debugBackgroundTask]; #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG @@ -135,40 +170,43 @@ static BOOL isPowerOfTwo(NSUInteger x) { if (!delegate) { continue; } - if ([delegate respondsToSelector:_cmd]) { + if ([delegate respondsToSelector:@selector(applicationWillEnterForeground:)]) { [delegate applicationWillEnterForeground:application]; } } } -- (void)applicationWillResignActive:(UIApplication*)application { +- (void)handleWillResignActive:(NSNotification*)notification { + UIApplication* application = [UIApplication sharedApplication]; for (NSObject* delegate in _delegates) { if (!delegate) { continue; } - if ([delegate respondsToSelector:_cmd]) { + if ([delegate respondsToSelector:@selector(applicationWillResignActive:)]) { [delegate applicationWillResignActive:application]; } } } -- (void)applicationDidBecomeActive:(UIApplication*)application { +- (void)handleDidBecomeActive:(NSNotification*)notification { + UIApplication* application = [UIApplication sharedApplication]; for (NSObject* delegate in _delegates) { if (!delegate) { continue; } - if ([delegate respondsToSelector:_cmd]) { + if ([delegate respondsToSelector:@selector(applicationDidBecomeActive:)]) { [delegate applicationDidBecomeActive:application]; } } } -- (void)applicationWillTerminate:(UIApplication*)application { +- (void)handleWillTerminate:(NSNotification*)notification { + UIApplication* application = [UIApplication sharedApplication]; for (NSObject* delegate in _delegates) { if (!delegate) { continue; } - if ([delegate respondsToSelector:_cmd]) { + if ([delegate respondsToSelector:@selector(applicationWillTerminate:)]) { [delegate applicationWillTerminate:application]; } } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.m b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.m new file mode 100644 index 0000000000..3a202d22da --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.m @@ -0,0 +1,77 @@ +// Copyright 2013 The Flutter 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 +#import +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h" + +#ifndef __has_feature +#define __has_feature(x) 0 /* for non-clang compilers */ +#endif + +#if !__has_feature(objc_arc) +#error ARC must be enabled! +#endif + +@interface FlutterPluginAppLifeCycleDelegateTest : XCTestCase + +@end + +@implementation FlutterPluginAppLifeCycleDelegateTest + +- (void)testCreate { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + XCTAssertNotNil(delegate); +} + +- (void)testDidEnterBackground { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationDidEnterBackgroundNotification + object:nil]; + OCMVerify([plugin applicationDidEnterBackground:[UIApplication sharedApplication]]); +} + +- (void)testWillEnterForeground { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationWillEnterForegroundNotification + object:nil]; + OCMVerify([plugin applicationWillEnterForeground:[UIApplication sharedApplication]]); +} + +- (void)testWillResignActive { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationWillResignActiveNotification + object:nil]; + OCMVerify([plugin applicationWillResignActive:[UIApplication sharedApplication]]); +} + +- (void)testDidBecomeActive { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationDidBecomeActiveNotification + object:nil]; + OCMVerify([plugin applicationDidBecomeActive:[UIApplication sharedApplication]]); +} + +- (void)testWillTerminate { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillTerminateNotification + object:nil]; + OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]); +} + +@end diff --git a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj index ddcb900774..ab7ad7ca36 100644 --- a/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj +++ b/engine/src/flutter/testing/ios/IosUnitTests/IosUnitTests.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 0D17A5C022D78FCD0057279F /* FlutterViewControllerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D17A5BF22D78FCD0057279F /* FlutterViewControllerTest.m */; }; + 0D4C3FB022DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D4C3FAF22DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; 0D52D3BD22C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0D52D3B622C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; 0D6AB6B622BB05E100EEE540 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D6AB6B522BB05E100EEE540 /* AppDelegate.m */; }; 0D6AB6B922BB05E100EEE540 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D6AB6B822BB05E100EEE540 /* ViewController.m */; }; @@ -74,6 +75,7 @@ /* Begin PBXFileReference section */ 0D17A5BF22D78FCD0057279F /* FlutterViewControllerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlutterViewControllerTest.m; sourceTree = ""; }; + 0D4C3FAF22DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FlutterPluginAppLifeCycleDelegateTest.m; sourceTree = ""; }; 0D52D3B622C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FlutterBinaryMessengerRelayTest.mm; sourceTree = ""; }; 0D6AB6B122BB05E100EEE540 /* IosUnitTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IosUnitTests.app; sourceTree = BUILT_PRODUCTS_DIR; }; 0D6AB6B422BB05E100EEE540 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -163,6 +165,7 @@ 0D52D3B622C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm */, 0D6AB6E722BB40CF00EEE540 /* FlutterEngineTest.mm */, 0D17A5BF22D78FCD0057279F /* FlutterViewControllerTest.m */, + 0D4C3FAF22DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m */, ); name = Source; path = ../../../../shell/platform/darwin/ios/framework/Source; @@ -374,6 +377,7 @@ 0D6AB6EB22BB40E700EEE540 /* FlutterEngineTest.mm in Sources */, 0D17A5C022D78FCD0057279F /* FlutterViewControllerTest.m in Sources */, 0D52D3BD22C566D50011DEBD /* FlutterBinaryMessengerRelayTest.mm in Sources */, + 0D4C3FB022DF9F5300A67C70 /* FlutterPluginAppLifeCycleDelegateTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };