Flutter Engine
The Flutter Engine
platform_message_handler_ios_test.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#import <XCTest/XCTest.h>
6
7#import "flutter/shell/platform/darwin/ios/platform_message_handler_ios.h"
8
9#import "flutter/common/task_runners.h"
10#import "flutter/fml/message_loop.h"
11#import "flutter/fml/thread.h"
12#import "flutter/lib/ui/window/platform_message.h"
13#import "flutter/lib/ui/window/platform_message_response.h"
14#import "flutter/shell/common/thread_host.h"
15#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
16
18
19namespace {
20using namespace flutter;
22 auto thread = std::make_unique<fml::Thread>(name);
23 auto runner = thread->GetTaskRunner();
24 return runner;
25}
26
30}
31
33 public:
36 }
37 void Complete(std::unique_ptr<fml::Mapping> data) override { is_complete_ = true; }
38 void CompleteEmpty() override { is_complete_ = true; }
39};
40} // namespace
41
42@interface PlatformMessageHandlerIosTest : XCTestCase
43@end
44
46- (void)testCreate {
47 TaskRunners task_runners("test", GetCurrentTaskRunner(), CreateNewThread("raster"),
48 CreateNewThread("ui"), CreateNewThread("io"));
49 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
50 XCTAssertTrue(handler);
51}
52
54 ThreadHost thread_host("io.flutter.test." + std::string(self.name.UTF8String),
55 ThreadHost::Type::kRaster | ThreadHost::Type::kIo | ThreadHost::Type::kUi);
56 TaskRunners task_runners(
57 "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(),
58 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
59
60 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
61 std::string channel = "foo";
62 XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"];
63 handler->SetMessageHandler(
64 channel,
65 ^(NSData* _Nullable data, FlutterBinaryReply _Nonnull reply) {
66 reply(nil);
67 [didCallReply fulfill];
68 },
69 nil);
71 task_runners.GetUITaskRunner()->PostTask([channel, response, &handler] {
72 auto platform_message = std::make_unique<flutter::PlatformMessage>(channel, response);
73 handler->HandlePlatformMessage(std::move(platform_message));
74 });
75 [self waitForExpectationsWithTimeout:1.0 handler:nil];
76 XCTAssertTrue(response->is_complete());
77}
78
80 ThreadHost thread_host("io.flutter.test." + std::string(self.name.UTF8String),
81 ThreadHost::Type::kRaster | ThreadHost::Type::kIo | ThreadHost::Type::kUi);
82 TaskRunners task_runners(
83 "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(),
84 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
85
86 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
87 std::string channel = "foo";
88 XCTestExpectation* didCallMessage = [self expectationWithDescription:@"didCallMessage"];
89 handler->SetMessageHandler(
90 channel,
91 ^(NSData* _Nullable data, FlutterBinaryReply _Nonnull reply) {
92 XCTFail(@"This shouldn't be called");
93 reply(nil);
94 },
95 nil);
96 handler->SetMessageHandler(channel, nil, nil);
98 task_runners.GetUITaskRunner()->PostTask([channel, response, &handler, &didCallMessage] {
99 auto platform_message = std::make_unique<flutter::PlatformMessage>(channel, response);
100 handler->HandlePlatformMessage(std::move(platform_message));
101 [didCallMessage fulfill];
102 });
103 [self waitForExpectationsWithTimeout:1.0 handler:nil];
104 XCTAssertTrue(response->is_complete());
105}
106
108 ThreadHost thread_host("io.flutter.test." + std::string(self.name.UTF8String),
109 ThreadHost::Type::kRaster | ThreadHost::Type::kIo | ThreadHost::Type::kUi);
110 TaskRunners task_runners(
111 "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(),
112 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
113
114 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
115 std::string channel = "foo";
116 XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"];
117 NSObject<FlutterTaskQueue>* taskQueue = PlatformMessageHandlerIos::MakeBackgroundTaskQueue();
118 handler->SetMessageHandler(
119 channel,
120 ^(NSData* _Nullable data, FlutterBinaryReply _Nonnull reply) {
121 XCTAssertFalse([NSThread isMainThread]);
122 reply(nil);
123 [didCallReply fulfill];
124 },
125 taskQueue);
126 auto response = MockPlatformMessageResponse::Create();
127 task_runners.GetUITaskRunner()->PostTask([channel, response, &handler] {
128 auto platform_message = std::make_unique<flutter::PlatformMessage>(channel, response);
129 handler->HandlePlatformMessage(std::move(platform_message));
130 });
131 [self waitForExpectationsWithTimeout:1.0 handler:nil];
132 XCTAssertTrue(response->is_complete());
133}
134@end
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterBinaryReply)(NSData *_Nullable reply)
static sk_sp< Effect > Create()
Definition: RefCntTest.cpp:117
void Complete(std::unique_ptr< fml::Mapping > data) override
static fml::RefPtr< MockPlatformMessageResponse > Create()
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
Definition: task_runners.cc:30
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: message_loop.cc:56
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
fml::RefPtr< fml::TaskRunner > CreateNewThread(const std::string &name)
fml::RefPtr< fml::TaskRunner > GetCurrentTaskRunner()
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
RefPtr< T > AdoptRef(T *ptr)
Definition: ref_ptr.h:222
The collection of all the threads used by the engine.
Definition: thread_host.h:21
std::unique_ptr< fml::Thread > io_thread
Definition: thread_host.h:86
std::unique_ptr< fml::Thread > raster_thread
Definition: thread_host.h:85
std::unique_ptr< fml::Thread > ui_thread
Definition: thread_host.h:84
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63