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  std::shared_ptr<fml::BasicTaskRunner> OnPlatformViewGetShutdownSafeIOTaskRunner() const override {
61  return nullptr;
62  }
63 
64  flutter::Settings settings_;
65 };
66 
67 } // namespace
68 } // namespace flutter
69 
70 @interface FlutterEnginePlatformViewTest : XCTestCase
71 @end
72 
73 @implementation FlutterEnginePlatformViewTest
74 std::unique_ptr<flutter::PlatformViewIOS> platform_view;
75 std::unique_ptr<fml::WeakPtrFactory<flutter::PlatformView>> weak_factory;
76 flutter::FakeDelegate fake_delegate;
77 
78 - (void)setUp {
79  fml::MessageLoop::EnsureInitializedForCurrentThread();
80  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
81  auto sync_switch = std::make_shared<fml::SyncSwitch>();
82  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
83  /*platform=*/thread_task_runner,
84  /*raster=*/thread_task_runner,
85  /*ui=*/thread_task_runner,
86  /*io=*/thread_task_runner);
87  platform_view = std::make_unique<flutter::PlatformViewIOS>(
88  /*delegate=*/fake_delegate,
89  /*platform_views_controller=*/nil,
90  /*task_runners=*/runners,
91  /*is_gpu_disabled_sync_switch=*/sync_switch);
92  weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
93 }
94 
95 - (void)tearDown {
96  weak_factory.reset();
97  platform_view.reset();
98 }
99 
100 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
101  return weak_factory->GetWeakPtr();
102 }
103 
104 - (void)testCallsNotifyLowMemory {
105  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"];
106  XCTAssertNotNil(engine);
107  id mockEngine = OCMPartialMock(engine);
108  OCMStub([mockEngine notifyLowMemory]);
109  OCMStub([mockEngine platformView]).andReturn(platform_view.get());
110 
111  [engine setViewController:nil];
112  OCMVerify([mockEngine notifyLowMemory]);
113  OCMReject([mockEngine notifyLowMemory]);
114 
115  XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
116  initWithName:UIApplicationDidReceiveMemoryWarningNotification];
117  [[NSNotificationCenter defaultCenter]
118  postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
119  object:nil];
120  [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
121  OCMVerify([mockEngine notifyLowMemory]);
122  OCMReject([mockEngine notifyLowMemory]);
123 
124  XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
125  initWithName:UIApplicationDidEnterBackgroundNotification];
126  [[NSNotificationCenter defaultCenter]
127  postNotificationName:UIApplicationDidEnterBackgroundNotification
128  object:nil];
129  [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
130 
131  OCMVerify([mockEngine notifyLowMemory]);
132 }
133 
134 @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