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"
16 
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 OnPlatformViewSendViewFocusEvent(const ViewFocusEvent& event) override {}
32  void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
33  void OnPlatformViewSetViewportMetrics(int64_t view_id, const ViewportMetrics& metrics) override {}
34  const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; }
35  void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}
36  void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
37  }
38  HitTestResponse OnPlatformViewHitTest(int64_t view_id, const flutter::PointData offset) override {
39  return {.has_platform_view = false};
40  }
41  void OnPlatformViewDispatchSemanticsAction(int64_t view_id,
42  int32_t node_id,
43  SemanticsAction action,
44  fml::MallocMapping args) override {}
45  void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
46  void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
47  void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
48  void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
49  void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
50 
51  void LoadDartDeferredLibrary(intptr_t loading_unit_id,
52  std::unique_ptr<const fml::Mapping> snapshot_data,
53  std::unique_ptr<const fml::Mapping> snapshot_instructions) override {
54  }
55  void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
56  const std::string error_message,
57  bool transient) override {}
58  void UpdateAssetResolverByType(std::unique_ptr<AssetResolver> updated_asset_resolver,
59  AssetResolver::AssetResolverType type) override {}
60 
61  flutter::Settings settings_;
62 };
63 
64 } // namespace
65 } // namespace flutter
66 
67 @interface FlutterEnginePlatformViewTest : XCTestCase
68 @end
69 
70 @implementation FlutterEnginePlatformViewTest
71 std::unique_ptr<flutter::PlatformViewIOS> platform_view;
72 std::unique_ptr<fml::WeakPtrFactory<flutter::PlatformView>> weak_factory;
73 flutter::FakeDelegate fake_delegate;
74 
75 - (void)setUp {
76  fml::MessageLoop::EnsureInitializedForCurrentThread();
77  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
78  auto sync_switch = std::make_shared<fml::SyncSwitch>();
79  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
80  /*platform=*/thread_task_runner,
81  /*raster=*/thread_task_runner,
82  /*ui=*/thread_task_runner,
83  /*io=*/thread_task_runner);
84  platform_view = std::make_unique<flutter::PlatformViewIOS>(
85  /*delegate=*/fake_delegate,
86  /*rendering_api=*/fake_delegate.settings_.enable_impeller
89  /*platform_views_controller=*/nil,
90  /*task_runners=*/runners,
91  /*worker_task_runner=*/nil,
92  /*is_gpu_disabled_sync_switch=*/sync_switch);
93  weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
94 }
95 
96 - (void)tearDown {
97  weak_factory.reset();
98  platform_view.reset();
99 }
100 
101 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
102  return weak_factory->GetWeakPtr();
103 }
104 
105 - (void)testCallsNotifyLowMemory {
106  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"];
107  XCTAssertNotNil(engine);
108  id mockEngine = OCMPartialMock(engine);
109  OCMStub([mockEngine notifyLowMemory]);
110  OCMStub([mockEngine platformView]).andReturn(platform_view.get());
111 
112  [engine setViewController:nil];
113  OCMVerify([mockEngine notifyLowMemory]);
114  OCMReject([mockEngine notifyLowMemory]);
115 
116  XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
117  initWithName:UIApplicationDidReceiveMemoryWarningNotification];
118  [[NSNotificationCenter defaultCenter]
119  postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
120  object:nil];
121  [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
122  OCMVerify([mockEngine notifyLowMemory]);
123  OCMReject([mockEngine notifyLowMemory]);
124 
125  XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
126  initWithName:UIApplicationDidEnterBackgroundNotification];
127  [[NSNotificationCenter defaultCenter]
128  postNotificationName:UIApplicationDidEnterBackgroundNotification
129  object:nil];
130  [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
131 
132  OCMVerify([mockEngine notifyLowMemory]);
133 }
134 
135 @end
flutter::Settings settings_
flutter::FakeDelegate fake_delegate
std::unique_ptr< flutter::PlatformViewIOS > platform_view
std::unique_ptr< fml::WeakPtrFactory< flutter::PlatformView > > weak_factory
int64_t texture_id