Flutter Engine
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"
12 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
13 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
14 #import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
15 
17 
18 namespace flutter {
19 namespace {
20 
21 class FakeDelegate : public PlatformView::Delegate {
22  void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
23  void OnPlatformViewDestroyed() override {}
24  void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
25  void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {}
26  void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}
27  void OnPlatformViewDispatchPointerDataPacket(std::unique_ptr<PointerDataPacket> packet) override {
28  }
29  void OnPlatformViewDispatchSemanticsAction(int32_t id,
31  fml::MallocMapping args) override {}
32  void OnPlatformViewSetSemanticsEnabled(bool enabled) override {}
33  void OnPlatformViewSetAccessibilityFeatures(int32_t flags) override {}
34  void OnPlatformViewRegisterTexture(std::shared_ptr<Texture> texture) override {}
35  void OnPlatformViewUnregisterTexture(int64_t texture_id) override {}
36  void OnPlatformViewMarkTextureFrameAvailable(int64_t texture_id) override {}
37 
38  void LoadDartDeferredLibrary(intptr_t loading_unit_id,
39  std::unique_ptr<const fml::Mapping> snapshot_data,
40  std::unique_ptr<const fml::Mapping> snapshot_instructions) override {
41  }
42  void LoadDartDeferredLibraryError(intptr_t loading_unit_id,
43  const std::string error_message,
44  bool transient) override {}
45  void UpdateAssetResolverByType(std::unique_ptr<AssetResolver> updated_asset_resolver,
47 };
48 
49 } // namespace
50 } // namespace flutter
51 
52 @interface FlutterEnginePlatformViewTest : XCTestCase
53 @end
54 
55 @implementation FlutterEnginePlatformViewTest
56 std::unique_ptr<flutter::PlatformViewIOS> platform_view;
57 std::unique_ptr<fml::WeakPtrFactory<flutter::PlatformView>> weak_factory;
58 flutter::FakeDelegate fake_delegate;
59 
60 - (void)setUp {
62  auto thread_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
63  flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
64  /*platform=*/thread_task_runner,
65  /*raster=*/thread_task_runner,
66  /*ui=*/thread_task_runner,
67  /*io=*/thread_task_runner);
68  platform_view = std::make_unique<flutter::PlatformViewIOS>(
69  /*delegate=*/fake_delegate,
70  /*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
71  /*platform_views_controller=*/nil,
72  /*task_runners=*/runners);
73  weak_factory = std::make_unique<fml::WeakPtrFactory<flutter::PlatformView>>(platform_view.get());
74 }
75 
76 - (void)tearDown {
77  weak_factory.reset();
78  platform_view.reset();
79 }
80 
81 - (fml::WeakPtr<flutter::PlatformView>)platformViewReplacement {
82  return weak_factory->GetWeakPtr();
83 }
84 
85 - (void)testCallsNotifyLowMemory {
86  id project = OCMClassMock([FlutterDartProject class]);
87  FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester" project:project];
88  XCTAssertNotNil(engine);
89  id mockEngine = OCMPartialMock(engine);
90  OCMStub([mockEngine notifyLowMemory]);
91  OCMStub([mockEngine iosPlatformView]).andReturn(platform_view.get());
92 
93  [engine setViewController:nil];
94  OCMVerify([mockEngine notifyLowMemory]);
95  OCMReject([mockEngine notifyLowMemory]);
96 
97  XCTNSNotificationExpectation* memoryExpectation = [[XCTNSNotificationExpectation alloc]
98  initWithName:UIApplicationDidReceiveMemoryWarningNotification];
99  [[NSNotificationCenter defaultCenter]
100  postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
101  object:nil];
102  [self waitForExpectations:@[ memoryExpectation ] timeout:5.0];
103  OCMVerify([mockEngine notifyLowMemory]);
104  OCMReject([mockEngine notifyLowMemory]);
105 
106  XCTNSNotificationExpectation* backgroundExpectation = [[XCTNSNotificationExpectation alloc]
107  initWithName:UIApplicationDidEnterBackgroundNotification];
108  [[NSNotificationCenter defaultCenter]
109  postNotificationName:UIApplicationDidEnterBackgroundNotification
110  object:nil];
111  [self waitForExpectations:@[ backgroundExpectation ] timeout:5.0];
112 
113  OCMVerify([mockEngine notifyLowMemory]);
114 }
115 
116 @end
G_BEGIN_DECLS FlValue * args
KeyCallType type
std::unique_ptr< flutter::PlatformViewIOS > platform_view
std::unique_ptr< fml::WeakPtrFactory< flutter::PlatformView > > weak_factory
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
int64_t texture_id
flutter::FakeDelegate fake_delegate
FlutterSemanticsFlag flags
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: message_loop.cc:56
AssetResolverType
Identifies the type of AssetResolver an instance is.
std::function< void()> closure
Definition: closure.h:14
SemanticsAction action
static void LoadDartDeferredLibrary(JNIEnv *env, jobject obj, jlong shell_holder, jint jLoadingUnitId, jobjectArray jSearchPaths)
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:144
FlTexture * texture