| Flutter iOS Embedder |

FlutterEnginePlatformViewTest.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 #include <memory>
6 #define FML_USED_ON_EMBEDDER
7
8 #import <OCMock/OCMock.h>
9 #import <XCTest/XCTest.h>
10
11 #include "flutter/fml/message_loop.h"
12 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
13 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
14 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h"
15 #import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
16
17 FLUTTER_ASSERT_ARC
18
19 namespace flutter {
20 namespace {
21
22 class FakeDelegate : public PlatformView::Delegate {
23 public:
24 void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
25 void OnPlatformViewDestroyed() override {}
26 void OnPlatformViewScheduleFrame() override {}
27 void OnPlatformViewAddView(int64_t view_id,
28 const ViewportMetrics& viewport_metrics,
29 AddViewCallback callback) override {}
30 void OnPlatformViewRemoveView(int64_t view_id, RemoveViewCallback callback) override {}
31 void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
32 void OnPlatformViewSetViewportMetrics(int64_t view_id, const ViewportMetrics& metrics) override {}
33 const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; }
34 void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}
35 void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
36 }
37 void OnPlatformViewDispatchSemanticsAction(int32_t id,
38 SemanticsAction action,
39 fml::MallocMapping args) override {}
40 void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
41 void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
42 void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
43 void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
44 void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
45
46 void LoadDartDeferredLibrary(intptr_t loading_unit_id,
47 std::unique_ptr<const fml::Mapping> snapshot_data,
48 std::unique_ptr<const fml::Mapping> snapshot_instructions) override {
49 }
50 void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
51 const std::string error_message,
52 bool transient) override {}
53 void UpdateAssetResolverByType(std::unique_ptr<AssetResolver> updated_asset_resolver,
54 AssetResolver::AssetResolverType type) override {}
55
56 flutter::Settings settings_;
57 };
58
59 } // namespace
60 } // namespace flutter
61
62 @interface FlutterEnginePlatformViewTest : XCTestCase
63 @end
64
65 @implementation FlutterEnginePlatformViewTest
66 std::unique_ptr<flutter::PlatformViewIOS> platform_view;
67 std::unique_ptr<fml::WeakPtrFactory<flutter::PlatformView>> weak_factory;
68 flutter::FakeDelegate fake_delegate;
69
70 - (void)setUp {
71 fml::MessageLoop::EnsureInitializedForCurrentThread();
72 auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
73 auto sync_switch = std::make_shared<fml::SyncSwitch>();
74 flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
75 /*platform=*/thread_task_runner,
76 /*raster=*/thread_task_runner,
77 /*ui=*/thread_task_runner,
78 /*io=*/thread_task_runner);
79 platform_view = std::make_unique<flutter::PlatformViewIOS>(
80 /*delegate=*/fake_delegate,
81 /*rendering_api=*/fake_delegate.settings_.enable_impeller
82 ? flutter::IOSRenderingAPI::kMetal
83 : flutter::IOSRenderingAPI::kSoftware,
84 /*platform_views_controller=*/nil,
85 /*task_runners=*/runners,
86 /*worker_task_runner=*/nil,
87 /*is_gpu_disabled_sync_switch=*/sync_switch);
88 weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
89 }
90
91 - (void)tearDown {
92 weak_factory.reset();
93 platform_view.reset();
94 }
95
96 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
97 return weak_factory->GetWeakPtr();
98 }
99
100 - (void)testCallsNotifyLowMemory {
101 FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"];
102 XCTAssertNotNil(engine);
103 id mockEngine = OCMPartialMock(engine);
104 OCMStub([mockEngine notifyLowMemory]);
105 OCMStub([mockEngine platformView]).andReturn(platform_view.get());
106
107 [engine setViewController:nil];
108 OCMVerify([mockEngine notifyLowMemory]);
109 OCMReject([mockEngine notifyLowMemory]);
110
111 XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
112 initWithName:UIApplicationDidReceiveMemoryWarningNotification];
113 [[NSNotificationCenter defaultCenter]
114 postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
115 object:nil];
116 [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
117 OCMVerify([mockEngine notifyLowMemory]);
118 OCMReject([mockEngine notifyLowMemory]);
119
120 XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
121 initWithName:UIApplicationDidEnterBackgroundNotification];
122 [[NSNotificationCenter defaultCenter]
123 postNotificationName:UIApplicationDidEnterBackgroundNotification
124 object:nil];
125 [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
126
127 OCMVerify([mockEngine notifyLowMemory]);
128 }
129
130 @end
FlutterEnginePlatformViewTest
Definition: FlutterEnginePlatformViewTest.mm:62
FlutterEngine
Definition: FlutterEngine.h:61
FlutterEngine_Test.h
FakeDelegate
Definition: FlutterViewTest.mm:11
FlutterEngine_Internal.h
FlutterMacros.h
platform_view
std::unique_ptr< flutter::PlatformViewIOS > platform_view
Definition: FlutterEnginePlatformViewTest.mm:66
flutter
Definition: accessibility_bridge.h:27
fake_delegate
flutter::FakeDelegate fake_delegate
Definition: FlutterEnginePlatformViewTest.mm:68
settings_
flutter::Settings settings_
Definition: FlutterEnginePlatformViewTest.mm:56
fml
Definition: profiler_metrics_ios.mm:41
flutter::IOSRenderingAPI::kMetal
@ kMetal
weak_factory
std::unique_ptr< fml::WeakPtrFactory< flutter::PlatformView > > weak_factory
Definition: FlutterEnginePlatformViewTest.mm:67
engine
id engine
Definition: FlutterTextInputPluginTest.mm:89
platform_view_ios.h
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
flutter::IOSRenderingAPI::kSoftware
@ kSoftware
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13