This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Support EventChannel C++ plugin API for Linux/Windows #17015
Merged
stuartmorgan-g
merged 49 commits into
flutter:master
from
Kurun-pan:topic-event-channel-support-linux-windows
May 7, 2020
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
44a638b
Initial commit
Kurun-pan 395df1e
remove std::optional (C++17)
Kurun-pan c556c7a
Fix unit test code, bugfix.
Kurun-pan 1000496
Fixed bug that the error of EventChannel register stream handler when…
Kurun-pan ae81b9d
Fix coding format.
Kurun-pan 9069e4c
Fix unit test fail.
Kurun-pan 8e96c4f
Fix coding format
Kurun-pan b8fe504
Fix build break.
Kurun-pan 251829e
Fix code format
Kurun-pan 4f5e051
Modified unit test scenario.
Kurun-pan b821d51
Fix code format
Kurun-pan 7c5e963
Fix code format
Kurun-pan 0b9446e
Fix code format
Kurun-pan 6168c67
Fix build error.
Kurun-pan 6917341
Fix code format
Kurun-pan f870a60
Fix BUILD.gn format
Kurun-pan f4f8634
Fix licenses_flutter, core_wrapper_files.gni format
Kurun-pan 02b3a3e
Fix licenses_flutter, core_wrapper_files.gni format
Kurun-pan 59c7146
Modify comments
Kurun-pan 5222fc0
Fix unit test code
Kurun-pan 50576c6
Modify comment and change from class to structure
Kurun-pan 28cc16e
Fix for code review result
Kurun-pan 0835785
Fix code format
Kurun-pan cc3f913
Fix code format
Kurun-pan 2210369
Fix build error
Kurun-pan 6c942dc
Fix code format
Kurun-pan fbaaee8
Fix code format
Kurun-pan 6c3fb5f
Fixed the points that were pointed out.
Kurun-pan 7f0e247
Fixed the points that were pointed out.
Kurun-pan a4e0caf
Fix code format
Kurun-pan 05ce3b5
- Add error handling of StreamHandler and fix unit test code.
Kurun-pan c830c9b
Fix code formatting
Kurun-pan 53077da
Merge commit 'f779f09058c695f33eaee1f4b7dcd8908ac5f195' into topic-ev…
Kurun-pan 21d9f99
Merge commit 'fb208b486ec354dc7324e4228d9c311208174b18' into topic-ev…
Kurun-pan 3768021
Fix build error, modify the comment and test case.
Kurun-pan 825ca23
Add StreamHandlerError to communicate errors, and fix the points that…
Kurun-pan 3befe52
Fix code formatting.
Kurun-pan 8a76fba
Fix code formatting.
Kurun-pan 5f09561
Recreated StreamHandler class with reference to MethodResult.
Kurun-pan 4896184
Fix build error.
Kurun-pan 41eb32f
Fix code formatting.
Kurun-pan dc68fbd
Fix code formatting.
Kurun-pan 0416d5b
Fix code formatting.
Kurun-pan b36ae30
Fix code formatting.
Kurun-pan dfb1451
Fix code formatting.
Kurun-pan 61df9ba
Modified method name and other.
Kurun-pan 090f6bf
Modify comments.
Kurun-pan 7905db1
Fixed some comments and unit test scenario name. Furthermore, don't u…
Kurun-pan f71c8a0
Merge commit '590381a00a56fdab914eeddcbcf4430b0f763ec1' into topic-ev…
Kurun-pan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h" | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h" | ||
| #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h" | ||
| #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| namespace { | ||
|
|
||
| class TestBinaryMessenger : public BinaryMessenger { | ||
| public: | ||
| void Send(const std::string& channel, | ||
| const uint8_t* message, | ||
| const size_t message_size, | ||
| BinaryReply reply) const override {} | ||
|
|
||
| void SetMessageHandler(const std::string& channel, | ||
| BinaryMessageHandler handler) override { | ||
| last_message_handler_channel_ = channel; | ||
| last_message_handler_ = handler; | ||
| } | ||
|
|
||
| std::string last_message_handler_channel() { | ||
| return last_message_handler_channel_; | ||
| } | ||
|
|
||
| BinaryMessageHandler last_message_handler() { return last_message_handler_; } | ||
|
|
||
| private: | ||
| std::string last_message_handler_channel_; | ||
| BinaryMessageHandler last_message_handler_; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| // Tests that SetStreamHandler sets a handler that correctly interacts with | ||
| // the binary messenger. | ||
| TEST(EventChannelTest, Registration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); | ||
| EventChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| bool on_listen_called = false; | ||
| auto handler = std::make_unique< | ||
| flutter::StreamHandlerFunctions<flutter::EncodableValue>>( | ||
| [&on_listen_called]( | ||
| const flutter::EncodableValue* arguments, | ||
| std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| on_listen_called = true; | ||
| return nullptr; | ||
| }, | ||
| [](const flutter::EncodableValue* arguments) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| return nullptr; | ||
| }); | ||
| channel.SetStreamHandler(std::move(handler)); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| // Send dummy listen message. | ||
| MethodCall<flutter::EncodableValue> call("listen", nullptr); | ||
| auto message = codec.EncodeMethodCall(call); | ||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
|
|
||
| // Check results. | ||
| EXPECT_EQ(on_listen_called, true); | ||
| } | ||
|
|
||
| // Tests that SetStreamHandler with a null handler unregisters the handler. | ||
| TEST(EventChannelTest, Unregistration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); | ||
| EventChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| auto handler = std::make_unique< | ||
| flutter::StreamHandlerFunctions<flutter::EncodableValue>>( | ||
| [](const flutter::EncodableValue* arguments, | ||
| std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| return nullptr; | ||
| }, | ||
| [](const flutter::EncodableValue* arguments) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| return nullptr; | ||
| }); | ||
| channel.SetStreamHandler(std::move(handler)); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| channel.SetStreamHandler(nullptr); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_EQ(messenger.last_message_handler(), nullptr); | ||
| } | ||
|
|
||
| // Test that OnCancel callback sequence. | ||
| TEST(EventChannelTest, Cancel) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); | ||
| EventChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| bool on_listen_called = false; | ||
| bool on_cancel_called = false; | ||
| auto handler = std::make_unique< | ||
| flutter::StreamHandlerFunctions<flutter::EncodableValue>>( | ||
| [&on_listen_called]( | ||
| const flutter::EncodableValue* arguments, | ||
| std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| on_listen_called = true; | ||
| return nullptr; | ||
| }, | ||
| [&on_cancel_called](const flutter::EncodableValue* arguments) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| on_cancel_called = true; | ||
| return nullptr; | ||
| }); | ||
| channel.SetStreamHandler(std::move(handler)); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| // Send dummy listen message. | ||
| MethodCall<flutter::EncodableValue> call_listen("listen", nullptr); | ||
| auto message = codec.EncodeMethodCall(call_listen); | ||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
| EXPECT_EQ(on_listen_called, true); | ||
|
|
||
| // Send dummy cancel message. | ||
| MethodCall<flutter::EncodableValue> call_cancel("cancel", nullptr); | ||
| message = codec.EncodeMethodCall(call_cancel); | ||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
|
|
||
| // Check results. | ||
| EXPECT_EQ(on_cancel_called, true); | ||
| } | ||
|
|
||
| // Pseudo test when user re-registers or call OnListen to the same channel. | ||
| // Confirm that OnCancel is called and OnListen is called again | ||
| // when user re-registers the same channel that has already started | ||
| // communication. | ||
| TEST(EventChannelTest, ReRegistration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); | ||
| EventChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| bool on_listen_called = false; | ||
| bool on_cancel_called = false; | ||
| auto handler = std::make_unique< | ||
| flutter::StreamHandlerFunctions<flutter::EncodableValue>>( | ||
| [&on_listen_called]( | ||
| const flutter::EncodableValue* arguments, | ||
| std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| on_listen_called = true; | ||
| return nullptr; | ||
| }, | ||
| [&on_cancel_called](const flutter::EncodableValue* arguments) | ||
| -> std::unique_ptr<StreamHandlerError<flutter::EncodableValue>> { | ||
| on_cancel_called = true; | ||
| return nullptr; | ||
| }); | ||
| channel.SetStreamHandler(std::move(handler)); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| // Send dummy listen message. | ||
| MethodCall<flutter::EncodableValue> call("listen", nullptr); | ||
| auto message = codec.EncodeMethodCall(call); | ||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
| EXPECT_EQ(on_listen_called, true); | ||
|
|
||
| // Send second dummy message to test StreamHandler's OnCancel | ||
| // method is called before OnListen method is called. | ||
| on_listen_called = false; | ||
| message = codec.EncodeMethodCall(call); | ||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
|
|
||
| // Check results. | ||
| EXPECT_EQ(on_cancel_called, true); | ||
| EXPECT_EQ(on_listen_called, true); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.