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 OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
27  void OnPlatformViewSetViewportMetrics(int64_t view_id, const ViewportMetrics& metrics) override {}
28  const flutter::Settings& OnPlatformViewGetSettings() const override { return settings_; }
29  void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}
30  void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
31  }
32  void OnPlatformViewDispatchSemanticsAction(int32_t id,
33  SemanticsAction action,
34  fml::MallocMapping args) override {}
35  void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
36  void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
37  void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
38  void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
39  void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
40 
41  void LoadDartDeferredLibrary(intptr_t loading_unit_id,
42  std::unique_ptr<const fml::Mapping> snapshot_data,
43  std::unique_ptr<const fml::Mapping> snapshot_instructions) override {
44  }
45  void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
46  const std::string error_message,
47  bool transient) override {}
48  void UpdateAssetResolverByType(std::unique_ptr<AssetResolver> updated_asset_resolver,
49  AssetResolver::AssetResolverType type) override {}
50 
51  flutter::Settings settings_;
52 };
53 
54 } // namespace
55 } // namespace flutter
56 
57 @interface FlutterEnginePlatformViewTest : XCTestCase
58 @end
59 
60 @implementation FlutterEnginePlatformViewTest
61 std::unique_ptr<flutter::PlatformViewIOS> platform_view;
62 std::unique_ptr<fml::WeakPtrFactory<flutter::PlatformView>> weak_factory;
63 flutter::FakeDelegate fake_delegate;
64 
65 - (void)setUp {
66  fml::MessageLoop::EnsureInitializedForCurrentThread();
67  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
68  auto sync_switch = std::make_shared<fml::SyncSwitch>();
69  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
70  /*platform=*/thread_task_runner,
71  /*raster=*/thread_task_runner,
72  /*ui=*/thread_task_runner,
73  /*io=*/thread_task_runner);
74  platform_view = std::make_unique<flutter::PlatformViewIOS>(
75  /*delegate=*/fake_delegate,
76  /*rendering_api=*/fake_delegate.settings_.enable_impeller
79  /*platform_views_controller=*/nil,
80  /*task_runners=*/runners,
81  /*worker_task_runner=*/nil,
82  /*is_gpu_disabled_sync_switch=*/sync_switch);
83  weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
84 }
85 
86 - (void)tearDown {
87  weak_factory.reset();
88  platform_view.reset();
89 }
90 
91 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
92  return weak_factory->GetWeakPtr();
93 }
94 
95 - (void)testMsaaSampleCount {
96  if (fake_delegate.settings_.enable_impeller) {
97  // Default should be 4 for Impeller.
98  XCTAssertEqual(platform_view->GetIosContext()->GetMsaaSampleCount(), MsaaSampleCount::kFour);
99  } else {
100  // Default should be 1 for Skia.
101  XCTAssertEqual(platform_view->GetIosContext()->GetMsaaSampleCount(), MsaaSampleCount::kNone);
102  }
103 
104  // Verify the platform view creates a new context with updated msaa_samples.
105  // Need to use Metal, since this is ignored for Software/GL.
106  fake_delegate.settings_.msaa_samples = 4;
107 
108  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
109  auto sync_switch = std::make_shared<fml::SyncSwitch>();
110  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
111  /*platform=*/thread_task_runner,
112  /*raster=*/thread_task_runner,
113  /*ui=*/thread_task_runner,
114  /*io=*/thread_task_runner);
115  auto msaa_4x_platform_view = std::make_unique<flutter::PlatformViewIOS>(
116  /*delegate=*/fake_delegate,
117  /*rendering_api=*/flutter::IOSRenderingAPI::kMetal,
118  /*platform_views_controller=*/nil,
119  /*task_runners=*/runners,
120  /*worker_task_runner=*/nil,
121  /*is_gpu_disabled_sync_switch=*/sync_switch);
122 
123  XCTAssertEqual(msaa_4x_platform_view->GetIosContext()->GetMsaaSampleCount(),
124  MsaaSampleCount::kFour);
125 }
126 
127 - (void)testCallsNotifyLowMemory {
128  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"];
129  XCTAssertNotNil(engine);
130  id mockEngine = OCMPartialMock(engine);
131  OCMStub([mockEngine notifyLowMemory]);
132  OCMStub([mockEngine iosPlatformView]).andReturn(platform_view.get());
133 
134  [engine setViewController:nil];
135  OCMVerify([mockEngine notifyLowMemory]);
136  OCMReject([mockEngine notifyLowMemory]);
137 
138  XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
139  initWithName:UIApplicationDidReceiveMemoryWarningNotification];
140  [[NSNotificationCenter defaultCenter]
141  postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
142  object:nil];
143  [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
144  OCMVerify([mockEngine notifyLowMemory]);
145  OCMReject([mockEngine notifyLowMemory]);
146 
147  XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
148  initWithName:UIApplicationDidEnterBackgroundNotification];
149  [[NSNotificationCenter defaultCenter]
150  postNotificationName:UIApplicationDidEnterBackgroundNotification
151  object:nil];
152  [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
153 
154  OCMVerify([mockEngine notifyLowMemory]);
155 }
156 
157 @end
FlutterEnginePlatformViewTest
Definition: FlutterEnginePlatformViewTest.mm:57
FlutterEngine
Definition: FlutterEngine.h:61
FakeDelegate
Definition: FlutterViewTest.mm:13
FlutterEngine_Internal.h
FlutterMacros.h
platform_view
std::unique_ptr< flutter::PlatformViewIOS > platform_view
Definition: FlutterEnginePlatformViewTest.mm:61
flutter
Definition: accessibility_bridge.h:28
fake_delegate
flutter::FakeDelegate fake_delegate
Definition: FlutterEnginePlatformViewTest.mm:63
settings_
flutter::Settings settings_
Definition: FlutterEnginePlatformViewTest.mm:51
flutter::IOSRenderingAPI::kMetal
@ kMetal
weak_factory
std::unique_ptr< fml::WeakPtrFactory< flutter::PlatformView > > weak_factory
Definition: FlutterEnginePlatformViewTest.mm:62
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: VsyncWaiterIosTest.mm:15