|
45 | 45 |
|
46 | 46 | namespace flutter {
|
47 | 47 | namespace testing {
|
| 48 | + |
| 49 | +using ::testing::_; |
| 50 | +using ::testing::Return; |
| 51 | + |
48 | 52 | namespace {
|
49 | 53 | class MockPlatformViewDelegate : public PlatformView::Delegate {
|
50 | 54 | MOCK_METHOD1(OnPlatformViewCreated, void(std::unique_ptr<Surface> surface));
|
@@ -119,6 +123,27 @@ class MockPlatformView : public PlatformView {
|
119 | 123 | MockPlatformView(MockPlatformViewDelegate& delegate, TaskRunners task_runners)
|
120 | 124 | : PlatformView(delegate, task_runners) {}
|
121 | 125 | MOCK_METHOD0(CreateRenderingSurface, std::unique_ptr<Surface>());
|
| 126 | + MOCK_CONST_METHOD0(GetPlatformMessageHandler, |
| 127 | + std::shared_ptr<PlatformMessageHandler>()); |
| 128 | +}; |
| 129 | + |
| 130 | +class MockPlatformMessageHandler : public PlatformMessageHandler { |
| 131 | + public: |
| 132 | + MOCK_METHOD1(HandlePlatformMessage, |
| 133 | + void(std::unique_ptr<PlatformMessage> message)); |
| 134 | + MOCK_METHOD2(InvokePlatformMessageResponseCallback, |
| 135 | + void(int response_id, std::unique_ptr<fml::Mapping> mapping)); |
| 136 | + MOCK_METHOD1(InvokePlatformMessageEmptyResponseCallback, |
| 137 | + void(int response_id)); |
| 138 | +}; |
| 139 | + |
| 140 | +class MockPlatformMessageResponse : public PlatformMessageResponse { |
| 141 | + public: |
| 142 | + static fml::RefPtr<MockPlatformMessageResponse> Create() { |
| 143 | + return fml::AdoptRef(new MockPlatformMessageResponse()); |
| 144 | + } |
| 145 | + MOCK_METHOD1(Complete, void(std::unique_ptr<fml::Mapping> data)); |
| 146 | + MOCK_METHOD0(CompleteEmpty, void()); |
122 | 147 | };
|
123 | 148 | } // namespace
|
124 | 149 |
|
@@ -3154,7 +3179,52 @@ TEST_F(ShellTest, UIWorkAfterOnPlatformViewDestroyed) {
|
3154 | 3179 | shell->GetTaskRunners().GetUITaskRunner(),
|
3155 | 3180 | [&ui_flush_latch]() { ui_flush_latch.Signal(); });
|
3156 | 3181 | ui_flush_latch.Wait();
|
| 3182 | + DestroyShell(std::move(shell)); |
| 3183 | +} |
3157 | 3184 |
|
| 3185 | +TEST_F(ShellTest, UsesPlatformMessageHandler) { |
| 3186 | + TaskRunners task_runners = GetTaskRunnersForFixture(); |
| 3187 | + auto settings = CreateSettingsForFixture(); |
| 3188 | + MockPlatformViewDelegate platform_view_delegate; |
| 3189 | + auto platform_message_handler = |
| 3190 | + std::make_shared<MockPlatformMessageHandler>(); |
| 3191 | + int message_id = 1; |
| 3192 | + EXPECT_CALL(*platform_message_handler, HandlePlatformMessage(_)); |
| 3193 | + EXPECT_CALL(*platform_message_handler, |
| 3194 | + InvokePlatformMessageEmptyResponseCallback(message_id)); |
| 3195 | + Shell::CreateCallback<PlatformView> platform_view_create_callback = |
| 3196 | + [&platform_view_delegate, task_runners, |
| 3197 | + platform_message_handler](flutter::Shell& shell) { |
| 3198 | + auto result = std::make_unique<MockPlatformView>(platform_view_delegate, |
| 3199 | + task_runners); |
| 3200 | + EXPECT_CALL(*result, GetPlatformMessageHandler()) |
| 3201 | + .WillOnce(Return(platform_message_handler)); |
| 3202 | + return result; |
| 3203 | + }; |
| 3204 | + auto shell = CreateShell( |
| 3205 | + /*settings=*/std::move(settings), |
| 3206 | + /*task_runners=*/task_runners, |
| 3207 | + /*simulate_vsync=*/false, |
| 3208 | + /*shell_test_external_view_embedder=*/nullptr, |
| 3209 | + /*is_gpu_disabled=*/false, |
| 3210 | + /*rendering_backend=*/ |
| 3211 | + ShellTestPlatformView::BackendType::kDefaultBackend, |
| 3212 | + /*platform_view_create_callback=*/platform_view_create_callback); |
| 3213 | + |
| 3214 | + EXPECT_EQ(platform_message_handler, shell->GetPlatformMessageHandler()); |
| 3215 | + PostSync(task_runners.GetUITaskRunner(), [&shell]() { |
| 3216 | + size_t data_size = 4; |
| 3217 | + fml::MallocMapping bytes = |
| 3218 | + fml::MallocMapping(static_cast<uint8_t*>(malloc(data_size)), data_size); |
| 3219 | + fml::RefPtr<MockPlatformMessageResponse> response = |
| 3220 | + MockPlatformMessageResponse::Create(); |
| 3221 | + auto message = std::make_unique<PlatformMessage>( |
| 3222 | + /*channel=*/"foo", /*data=*/std::move(bytes), /*response=*/response); |
| 3223 | + (static_cast<Engine::Delegate*>(shell.get())) |
| 3224 | + ->OnEngineHandlePlatformMessage(std::move(message)); |
| 3225 | + }); |
| 3226 | + shell->GetPlatformMessageHandler() |
| 3227 | + ->InvokePlatformMessageEmptyResponseCallback(message_id); |
3158 | 3228 | DestroyShell(std::move(shell));
|
3159 | 3229 | }
|
3160 | 3230 |
|
|
0 commit comments