Call Dart_NotifyLowMemory more on iOS (flutter/engine#19289)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 <OCMock/OCMock.h>
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
#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> surface) override {}
|
||||
void OnPlatformViewDestroyed() override {}
|
||||
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
|
||||
void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {}
|
||||
void OnPlatformViewDispatchPlatformMessage(fml::RefPtr<PlatformMessage> message) override {}
|
||||
void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
|
||||
}
|
||||
void OnPlatformViewDispatchSemanticsAction(int32_t id,
|
||||
SemanticsAction action,
|
||||
std::vector<uint8_t> args) override {}
|
||||
void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
|
||||
void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
|
||||
void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
|
||||
void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
|
||||
void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
|
||||
|
||||
std::unique_ptr<std::vector<std::string>> ComputePlatformViewResolvedLocale(
|
||||
const std::vector<std::string>& supported_locale_data) override {
|
||||
std::unique_ptr<std::vector<std::string>> out = std::make_unique<std::vector<std::string>>();
|
||||
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<flutter::PlatformViewIOS>(
|
||||
/*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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user