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  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
69  /*platform=*/thread_task_runner,
70  /*raster=*/thread_task_runner,
71  /*ui=*/thread_task_runner,
72  /*io=*/thread_task_runner);
73  platform_view = std::make_unique<flutter::PlatformViewIOS>(
74  /*delegate=*/fake_delegate,
75  /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
76  /*platform_views_controller=*/nil,
77  /*task_runners=*/runners,
78  /*worker_task_runner=*/nil,
79  /*is_gpu_disabled_sync_switch=*/nil);
80  weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
81 }
82 
83 - (void)tearDown {
84  weak_factory.reset();
85  platform_view.reset();
86 }
87 
88 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
89  return weak_factory->GetWeakPtr();
90 }
91 
92 - (void)testMsaaSampleCount {
93  // Default should be 1.
94  XCTAssertEqual(platform_view->GetIosContext()->GetMsaaSampleCount(), MsaaSampleCount::kNone);
95 
96  // Verify the platform view creates a new context with updated msaa_samples.
97  // Need to use Metal, since this is ignored for Software/GL.
98  fake_delegate.settings_.msaa_samples = 4;
99 
100  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
101  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
102  /*platform=*/thread_task_runner,
103  /*raster=*/thread_task_runner,
104  /*ui=*/thread_task_runner,
105  /*io=*/thread_task_runner);
106  auto msaa_4x_platform_view = std::make_unique<flutter::PlatformViewIOS>(
107  /*delegate=*/fake_delegate,
108  /*rendering_api=*/flutter::IOSRenderingAPI::kMetal,
109  /*platform_views_controller=*/nil,
110  /*task_runners=*/runners,
111  /*worker_task_runner=*/nil,
112  /*is_gpu_disabled_sync_switch=*/nil);
113 
114  XCTAssertEqual(msaa_4x_platform_view->GetIosContext()->GetMsaaSampleCount(),
115  MsaaSampleCount::kFour);
116 }
117 
118 - (void)testCallsNotifyLowMemory {
119  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"];
120  XCTAssertNotNil(engine);
121  id mockEngine = OCMPartialMock(engine);
122  OCMStub([mockEngine notifyLowMemory]);
123  OCMStub([mockEngine iosPlatformView]).andReturn(platform_view.get());
124 
125  [engine setViewController:nil];
126  OCMVerify([mockEngine notifyLowMemory]);
127  OCMReject([mockEngine notifyLowMemory]);
128 
129  XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
130  initWithName:UIApplicationDidReceiveMemoryWarningNotification];
131  [[NSNotificationCenter defaultCenter]
132  postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
133  object:nil];
134  [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
135  OCMVerify([mockEngine notifyLowMemory]);
136  OCMReject([mockEngine notifyLowMemory]);
137 
138  XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
139  initWithName:UIApplicationDidEnterBackgroundNotification];
140  [[NSNotificationCenter defaultCenter]
141  postNotificationName:UIApplicationDidEnterBackgroundNotification
142  object:nil];
143  [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
144 
145  OCMVerify([mockEngine notifyLowMemory]);
146 }
147 
148 @end
FlutterEnginePlatformViewTest
Definition: FlutterEnginePlatformViewTest.mm:57
FlutterEngine
Definition: FlutterEngine.h:59
FLUTTER_ASSERT_NOT_ARC
Definition: VsyncWaiterIosTest.mm:15
FakeDelegate
Definition: FlutterViewTest.mm:10
FlutterEngine_Internal.h
FlutterMacros.h
platform_view
std::unique_ptr< flutter::PlatformViewIOS > platform_view
Definition: FlutterEnginePlatformViewTest.mm:61
action
SemanticsAction action
Definition: SemanticsObjectTestMocks.h:21
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