Flutter Engine
The Flutter Engine
platform_view_embedder_unittests.cc
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 "flutter/shell/platform/embedder/platform_view_embedder.h"
6
7#include "flutter/shell/common/thread_host.h"
8#include "flutter/testing/testing.h"
9
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
12
13#include <cstring>
14
15namespace flutter {
16namespace testing {
17namespace {
18class MockDelegate : public PlatformView::Delegate {
19 MOCK_METHOD(void,
20 OnPlatformViewCreated,
21 (std::unique_ptr<Surface>),
22 (override));
23 MOCK_METHOD(void, OnPlatformViewDestroyed, (), (override));
24 MOCK_METHOD(void, OnPlatformViewScheduleFrame, (), (override));
25 MOCK_METHOD(void,
26 OnPlatformViewAddView,
27 (int64_t view_id,
28 const ViewportMetrics& viewport_metrics,
29 AddViewCallback callback),
30 (override));
31 MOCK_METHOD(void,
32 OnPlatformViewRemoveView,
33 (int64_t view_id, RemoveViewCallback callback),
34 (override));
35 MOCK_METHOD(void,
36 OnPlatformViewSetNextFrameCallback,
37 (const fml::closure& closure),
38 (override));
39 MOCK_METHOD(void,
40 OnPlatformViewSetViewportMetrics,
41 (int64_t view_id, const ViewportMetrics& metrics),
42 (override));
43 MOCK_METHOD(void,
44 OnPlatformViewDispatchPlatformMessage,
45 (std::unique_ptr<PlatformMessage> message),
46 (override));
47 MOCK_METHOD(void,
48 OnPlatformViewDispatchPointerDataPacket,
49 (std::unique_ptr<PointerDataPacket> packet),
50 (override));
51 MOCK_METHOD(void,
52 OnPlatformViewDispatchSemanticsAction,
54 (override));
55 MOCK_METHOD(void,
56 OnPlatformViewSetSemanticsEnabled,
57 (bool enabled),
58 (override));
59 MOCK_METHOD(void,
60 OnPlatformViewSetAccessibilityFeatures,
61 (int32_t flags),
62 (override));
63 MOCK_METHOD(void,
64 OnPlatformViewRegisterTexture,
65 (std::shared_ptr<Texture> texture),
66 (override));
67 MOCK_METHOD(void,
68 OnPlatformViewUnregisterTexture,
69 (int64_t texture_id),
70 (override));
71 MOCK_METHOD(void,
72 OnPlatformViewMarkTextureFrameAvailable,
73 (int64_t texture_id),
74 (override));
75 MOCK_METHOD(void,
77 (intptr_t loading_unit_id,
78 std::unique_ptr<const fml::Mapping> snapshot_data,
79 std::unique_ptr<const fml::Mapping> snapshot_instructions),
80 (override));
81 MOCK_METHOD(void,
82 LoadDartDeferredLibraryError,
83 (intptr_t loading_unit_id,
84 const std::string error_message,
85 bool transient),
86 (override));
87 MOCK_METHOD(void,
88 UpdateAssetResolverByType,
89 (std::unique_ptr<AssetResolver> updated_asset_resolver,
91 (override));
92 MOCK_METHOD(const Settings&,
93 OnPlatformViewGetSettings,
94 (),
95 (const, override));
96};
97
98class MockResponse : public PlatformMessageResponse {
99 public:
100 MOCK_METHOD(void, Complete, (std::unique_ptr<fml::Mapping> data), (override));
101 MOCK_METHOD(void, CompleteEmpty, (), (override));
102};
103} // namespace
104
105TEST(PlatformViewEmbedderTest, HasPlatformMessageHandler) {
106 ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
107 ThreadHost::Type::kPlatform);
109 "HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
110 nullptr, nullptr, nullptr);
112 task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners] {
113 MockDelegate delegate;
115 PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
116 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
117 auto embedder = std::make_unique<PlatformViewEmbedder>(
118 delegate, task_runners, software_dispatch_table,
119 platform_dispatch_table, external_view_embedder);
120
121 ASSERT_TRUE(embedder->GetPlatformMessageHandler());
122 latch.Signal();
123 });
124 latch.Wait();
125}
126
127TEST(PlatformViewEmbedderTest, Dispatches) {
128 ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
129 ThreadHost::Type::kPlatform);
131 "HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
132 nullptr, nullptr, nullptr);
133 bool did_call = false;
134 std::unique_ptr<PlatformViewEmbedder> embedder;
135 {
137 task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners,
138 &did_call, &embedder] {
139 MockDelegate delegate;
141 PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
142 platform_dispatch_table.platform_message_response_callback =
143 [&did_call](std::unique_ptr<PlatformMessage> message) {
144 did_call = true;
145 };
146 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
147 embedder = std::make_unique<PlatformViewEmbedder>(
148 delegate, task_runners, software_dispatch_table,
149 platform_dispatch_table, external_view_embedder);
150 auto platform_message_handler = embedder->GetPlatformMessageHandler();
152 fml::MakeRefCounted<MockResponse>();
153 std::unique_ptr<PlatformMessage> message =
154 std::make_unique<PlatformMessage>("foo", response);
155 platform_message_handler->HandlePlatformMessage(std::move(message));
156 latch.Signal();
157 });
158 latch.Wait();
159 }
160 {
162 thread_host.platform_thread->GetTaskRunner()->PostTask([&latch, &embedder] {
163 embedder.reset();
164 latch.Signal();
165 });
166 latch.Wait();
167 }
168
169 EXPECT_TRUE(did_call);
170}
171
172TEST(PlatformViewEmbedderTest, DeletionDisabledDispatch) {
173 ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
174 ThreadHost::Type::kPlatform);
176 "HasPlatformMessageHandler", thread_host.platform_thread->GetTaskRunner(),
177 nullptr, nullptr, nullptr);
178 bool did_call = false;
179 {
181 task_runners.GetPlatformTaskRunner()->PostTask([&latch, task_runners,
182 &did_call] {
183 MockDelegate delegate;
185 PlatformViewEmbedder::PlatformDispatchTable platform_dispatch_table;
186 platform_dispatch_table.platform_message_response_callback =
187 [&did_call](std::unique_ptr<PlatformMessage> message) {
188 did_call = true;
189 };
190 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder;
191 auto embedder = std::make_unique<PlatformViewEmbedder>(
192 delegate, task_runners, software_dispatch_table,
193 platform_dispatch_table, external_view_embedder);
194 auto platform_message_handler = embedder->GetPlatformMessageHandler();
196 fml::MakeRefCounted<MockResponse>();
197 std::unique_ptr<PlatformMessage> message =
198 std::make_unique<PlatformMessage>("foo", response);
199 platform_message_handler->HandlePlatformMessage(std::move(message));
200 embedder.reset();
201 latch.Signal();
202 });
203 latch.Wait();
204 }
205 {
207 thread_host.platform_thread->GetTaskRunner()->PostTask(
208 [&latch] { latch.Signal(); });
209 latch.Wait();
210 }
211
212 EXPECT_FALSE(did_call);
213}
214
215} // namespace testing
216} // namespace flutter
GLenum type
AssetResolverType
Identifies the type of AssetResolver an instance is.
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
Definition: task_runners.cc:30
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition: mapping.h:144
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Win32Message message
FlTexture * texture
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition: testing.cc:15
TEST(DisplayListComplexity, EmptyDisplayList)
static void LoadDartDeferredLibrary(JNIEnv *env, jobject obj, jlong shell_holder, jint jLoadingUnitId, jobjectArray jSearchPaths)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
std::function< void()> closure
Definition: closure.h:14
PlatformMessageResponseCallback platform_message_response_callback
The collection of all the threads used by the engine.
Definition: thread_host.h:21
std::unique_ptr< fml::Thread > platform_thread
Definition: thread_host.h:83
int64_t texture_id
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678