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