From 5abad59767671b84951b9a51eb746bcec12a1d80 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 8 Jul 2020 04:44:03 -0700 Subject: [PATCH] Call Dart_NotifyLowMemory more on iOS (flutter/engine#19289) --- .../ci/licenses_golden/licenses_flutter | 1 + .../shell/platform/darwin/ios/BUILD.gn | 1 + .../ios/framework/Source/FlutterEngine.mm | 22 ++++- .../Source/FlutterEnginePlatformViewTest.mm | 96 +++++++++++++++++++ .../framework/Source/FlutterEngine_Internal.h | 2 + .../framework/Source/FlutterViewController.mm | 1 + .../Source/FlutterViewControllerTest.mm | 18 ++++ 7 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 60751e774d..1e22176412 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -898,6 +898,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbac FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterHeadlessDartRunner.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index e8d1c67bee..dcfce07913 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -167,6 +167,7 @@ source_set("ios_test_flutter_mrc") { "-mios-simulator-version-min=$ios_testing_deployment_target", ] sources = [ + "framework/Source/FlutterEnginePlatformViewTest.mm", "framework/Source/accessibility_bridge_test.mm", ] deps = [ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index a6ec2a6482..48b8558588 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -117,6 +117,11 @@ static constexpr int kNumProfilerSamplesPerSec = 5; name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; + [center addObserver:self + selector:@selector(applicationDidEnterBackground:) + name:UIApplicationDidEnterBackgroundNotification + object:nil]; + [center addObserver:self selector:@selector(applicationBecameActive:) name:UIApplicationDidBecomeActiveNotification @@ -232,6 +237,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5; }]; } else { self.flutterViewControllerWillDeallocObserver = nil; + [self notifyLowMemory]; } } @@ -558,6 +564,13 @@ static constexpr int kNumProfilerSamplesPerSec = 5; return [self runWithEntrypoint:entrypoint libraryURI:nil]; } +- (void)notifyLowMemory { + if (_shell) { + _shell->NotifyLowMemoryWarning(); + } + [_systemChannel sendMessage:@{@"type" : @"memoryPressure"}]; +} + #pragma mark - Text input delegate - (void)updateEditingClient:(int)client withState:(NSDictionary*)state { @@ -748,11 +761,12 @@ static constexpr int kNumProfilerSamplesPerSec = 5; [self setIsGpuDisabled:YES]; } +- (void)applicationDidEnterBackground:(NSNotification*)notification { + [self notifyLowMemory]; +} + - (void)onMemoryWarning:(NSNotification*)notification { - if (_shell) { - _shell->NotifyLowMemoryWarning(); - } - [_systemChannel sendMessage:@{@"type" : @"memoryPressure"}]; + [self notifyLowMemory]; } - (void)setIsGpuDisabled:(BOOL)value { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm new file mode 100644 index 0000000000..1e40459131 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm @@ -0,0 +1,96 @@ +// 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. + +#define FML_USED_ON_EMBEDDER + +#import +#import + +#import "flutter/fml/message_loop.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h" +#import "flutter/shell/platform/darwin/ios/platform_view_ios.h" + +FLUTTER_ASSERT_NOT_ARC + +namespace flutter { +namespace { + +class MockDelegate : public PlatformView::Delegate { + void OnPlatformViewCreated(std::unique_ptr surface) override {} + void OnPlatformViewDestroyed() override {} + void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} + void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} + void OnPlatformViewDispatchPlatformMessage(fml::RefPtr message) override {} + void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr packet) override { + } + void OnPlatformViewDispatchSemanticsAction(int32_t id, + SemanticsAction action, + std::vector args) override {} + void OnPlatformViewSetSemanticsEnabled(bool enabled) override {} + void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {} + void OnPlatformViewRegisterTexture(std::shared_ptr texture) override {} + void OnPlatformViewUnregisterTexture(int64_t texture_id) override {} + void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {} + + std::unique_ptr> ComputePlatformViewResolvedLocale( + const std::vector& supported_locale_data) override { + std::unique_ptr> out = std::make_unique>(); + return out; + } +}; + +} // namespace +} // namespace flutter + +@interface FlutterEnginePlatformViewTest : XCTestCase +@end + +@implementation FlutterEnginePlatformViewTest + +- (void)setUp { + fml::MessageLoop::EnsureInitializedForCurrentThread(); +} + +- (void)tearDown { +} + +- (void)testCallsNotifyLowMemory { + flutter::MockDelegate mock_delegate; + auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); + flutter::TaskRunners runners(/*label=*/self.name.UTF8String, + /*platform=*/thread_task_runner, + /*raster=*/thread_task_runner, + /*ui=*/thread_task_runner, + /*io=*/thread_task_runner); + auto platform_view = std::make_unique( + /*delegate=*/mock_delegate, + /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware, + /*task_runners=*/runners); + + id project = OCMClassMock([FlutterDartProject class]); + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester" project:project]; + XCTAssertNotNil(engine); + id mockEngine = OCMPartialMock(engine); + OCMStub([mockEngine notifyLowMemory]); + OCMStub([mockEngine iosPlatformView]).andReturn(platform_view.get()); + + [engine setViewController:nil]; + OCMVerify([mockEngine notifyLowMemory]); + OCMReject([mockEngine notifyLowMemory]); + + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationDidReceiveMemoryWarningNotification + object:nil]; + OCMVerify([mockEngine notifyLowMemory]); + OCMReject([mockEngine notifyLowMemory]); + + [[NSNotificationCenter defaultCenter] + postNotificationName:UIApplicationDidEnterBackgroundNotification + object:nil]; + + OCMVerify([mockEngine notifyLowMemory]); +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h index e995b6f77f..52558eaf71 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h @@ -45,6 +45,8 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil; - (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil; - (void)attachView; +- (void)notifyLowMemory; +- (flutter::PlatformViewIOS*)iosPlatformView; @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 21beeffddb..f2a8c186a7 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -557,6 +557,7 @@ static void sendFakeTouchEvent(FlutterEngine* engine, [self surfaceUpdated:NO]; [[_engine.get() lifecycleChannel] sendMessage:@"AppLifecycleState.paused"]; [self flushOngoingTouches]; + [_engine.get() notifyLowMemory]; } [super viewDidDisappear:animated]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index 1ab6055767..e508d031eb 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -16,6 +16,10 @@ FLUTTER_ASSERT_ARC - (BOOL)createShell:(NSString*)entrypoint libraryURI:(NSString*)libraryURI; @end +@interface FlutterEngine (TestLowMemory) +- (void)notifyLowMemory; +@end + extern NSNotificationName const FlutterViewControllerWillDealloc; /// A simple mock class for FlutterEngine. @@ -529,4 +533,18 @@ typedef enum UIAccessibilityContrast : NSInteger { XCTAssertTrue(realVC.prefersHomeIndicatorAutoHidden, @""); } +- (void)testNotifyLowMemory { + id engine = OCMClassMock([FlutterEngine class]); + FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine + nibName:nil + bundle:nil]; + OCMStub([engine viewController]).andReturn(viewController); + id viewControllerMock = OCMPartialMock(viewController); + OCMStub([viewControllerMock surfaceUpdated:NO]); + + [viewController beginAppearanceTransition:NO animated:NO]; + [viewController endAppearanceTransition]; + OCMVerify([engine notifyLowMemory]); +} + @end