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
Fix handler unregistration in C++ channels #16794
Merged
stuartmorgan-g
merged 3 commits into
flutter:master
from
stuartmorgan-g:cpp-channel-unregister-fix
Feb 25, 2020
Merged
Changes from all commits
Commits
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
93 changes: 93 additions & 0 deletions
93
shell/platform/common/cpp/client_wrapper/basic_message_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,93 @@ | ||
| // 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/basic_message_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/standard_message_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) const override {} | ||
|
|
||
| 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 SetMessageHandler sets a handler that correctly interacts with | ||
| // the binary messenger. | ||
| TEST(BasicMessageChannelTest, Registration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMessageCodec& codec = StandardMessageCodec::GetInstance(); | ||
| BasicMessageChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| bool callback_called = false; | ||
| const std::string message_value("hello"); | ||
| channel.SetMessageHandler( | ||
| [&callback_called, message_value](const auto& message, auto reply) { | ||
| callback_called = true; | ||
| // Ensure that the wrapper recieved a correctly decoded message and a | ||
| // reply. | ||
| EXPECT_EQ(message.StringValue(), message_value); | ||
| EXPECT_NE(reply, nullptr); | ||
| }); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
| // Send a test message to trigger the handler test assertions. | ||
| auto message = codec.EncodeMessage(EncodableValue(message_value)); | ||
|
|
||
| messenger.last_message_handler()( | ||
| message->data(), message->size(), | ||
| [](const uint8_t* reply, const size_t reply_size) {}); | ||
| EXPECT_EQ(callback_called, true); | ||
| } | ||
|
|
||
| // Tests that SetMessageHandler with a null handler unregisters the handler. | ||
| TEST(BasicMessageChannelTest, Unregistration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| BasicMessageChannel channel(&messenger, channel_name, | ||
| &flutter::StandardMessageCodec::GetInstance()); | ||
|
|
||
| channel.SetMessageHandler([](const auto& message, auto reply) {}); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| channel.SetMessageHandler(nullptr); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_EQ(messenger.last_message_handler(), nullptr); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
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
94 changes: 94 additions & 0 deletions
94
shell/platform/common/cpp/client_wrapper/method_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,94 @@ | ||
| // 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/method_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/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) const override {} | ||
|
|
||
| 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 SetMethodCallHandler sets a handler that correctly interacts with | ||
| // the binary messenger. | ||
| TEST(MethodChannelTest, Registration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); | ||
| MethodChannel channel(&messenger, channel_name, &codec); | ||
|
|
||
| bool callback_called = false; | ||
| const std::string method_name("hello"); | ||
| channel.SetMethodCallHandler( | ||
| [&callback_called, method_name](const auto& call, auto result) { | ||
| callback_called = true; | ||
| // Ensure that the wrapper recieved a correctly decoded call and a | ||
| // result. | ||
| EXPECT_EQ(call.method_name(), method_name); | ||
| EXPECT_NE(result, nullptr); | ||
| }); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
| // Send a test message to trigger the handler test assertions. | ||
| MethodCall<EncodableValue> call(method_name, 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(callback_called, true); | ||
| } | ||
|
|
||
| // Tests that SetMethodCallHandler with a null handler unregisters the handler. | ||
| TEST(MethodChannelTest, Unregistration) { | ||
| TestBinaryMessenger messenger; | ||
| const std::string channel_name("some_channel"); | ||
| MethodChannel channel(&messenger, channel_name, | ||
| &flutter::StandardMethodCodec::GetInstance()); | ||
|
|
||
| channel.SetMethodCallHandler([](const auto& call, auto result) {}); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_NE(messenger.last_message_handler(), nullptr); | ||
|
|
||
| channel.SetMethodCallHandler(nullptr); | ||
| EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); | ||
| EXPECT_EQ(messenger.last_message_handler(), nullptr); | ||
| } | ||
|
|
||
| } // namespace flutter |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
TestBinaryMessengerbe better named asMockBinaryMessenger, and use theTestprefix for the subjects being tests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's manual, Fake might be better than Mock?
I'm going to land as-is for now, since at least the registrar unit test already uses this name, and we can revisit naming for everything at once later.