5 #import <XCTest/XCTest.h>
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"
22 auto thread = std::make_unique<fml::Thread>(name);
23 auto runner = thread->GetTaskRunner();
28 fml::MessageLoop::EnsureInitializedForCurrentThread();
29 return fml::MessageLoop::GetCurrent().GetTaskRunner();
34 static fml::RefPtr<MockPlatformMessageResponse>
Create() {
37 void Complete(std::unique_ptr<fml::Mapping> data)
override { is_complete_ =
true; }
49 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
50 XCTAssertTrue(handler);
53 - (void)testSetAndCallHandler {
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(
58 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
60 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
61 std::string channel =
"foo";
62 XCTestExpectation* didCallReply = [
self expectationWithDescription:@"didCallReply"];
63 handler->SetMessageHandler(
67 [didCallReply fulfill];
70 auto response = MockPlatformMessageResponse::Create();
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));
75 [
self waitForExpectationsWithTimeout:1.0 handler:nil];
76 XCTAssertTrue(response->is_complete());
79 - (void)testSetClearAndCallHandler {
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(
84 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
86 auto handler = std::make_unique<PlatformMessageHandlerIos>(task_runners.GetPlatformTaskRunner());
87 std::string channel =
"foo";
88 XCTestExpectation* didCallMessage = [
self expectationWithDescription:@"didCallMessage"];
89 handler->SetMessageHandler(
92 XCTFail(
@"This shouldn't be called");
96 handler->SetMessageHandler(channel, nil, nil);
97 auto response = MockPlatformMessageResponse::Create();
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];
103 [
self waitForExpectationsWithTimeout:1.0 handler:nil];
104 XCTAssertTrue(response->is_complete());
107 - (void)testSetAndCallHandlerTaskQueue {
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(
112 thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner());
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(
121 XCTAssertFalse([NSThread isMainThread]);
123 [didCallReply fulfill];
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));
131 [
self waitForExpectationsWithTimeout:1.0 handler:nil];
132 XCTAssertTrue(response->is_complete());