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