Pass parameters by value to StreamHandlerError (flutter/engine#40311)

* Pass by value/rvalue

* Test pass by value

* Formatting

* Test nullptr

* Formatting
This commit is contained in:
yaakovschectman
2023-03-16 18:00:30 +00:00
committed by GitHub
parent 246f06127b
commit c1df0b8413
3 changed files with 38 additions and 9 deletions

View File

@@ -274,4 +274,30 @@ TEST(EventChannelTest, HandlerOutlivesEventChannel) {
EXPECT_EQ(on_cancel_called, true);
}
TEST(EventChannelTest, StreamHandlerErrorPassByValue) {
std::unique_ptr<StreamHandlerError<>> error = nullptr;
{
std::string code = "Code";
std::string msg = "Message";
std::unique_ptr<EncodableValue> details =
std::make_unique<EncodableValue>("Details");
error =
std::make_unique<StreamHandlerError<>>(code, msg, std::move(details));
}
ASSERT_NE(error.get(), nullptr);
EXPECT_EQ(error->error_code, "Code");
EXPECT_EQ(error->error_message, "Message");
EXPECT_EQ(std::get<std::string>(*error->error_details), "Details");
}
TEST(EventChannelTest, StreamHandlerErrorNullptr) {
std::unique_ptr<StreamHandlerError<>> error =
std::make_unique<StreamHandlerError<>>("Code", "Message", nullptr);
ASSERT_NE(error.get(), nullptr);
EXPECT_FALSE(error->error_details);
}
} // namespace flutter

View File

@@ -106,7 +106,7 @@ class EventChannel {
if (error) {
result = codec->EncodeErrorEnvelope(error->error_code,
error->error_message,
error->error_details);
error->error_details.get());
} else {
result = codec->EncodeSuccessEnvelope();
}
@@ -119,7 +119,7 @@ class EventChannel {
if (error) {
result = codec->EncodeErrorEnvelope(error->error_code,
error->error_message,
error->error_details);
error->error_details.get());
} else {
result = codec->EncodeSuccessEnvelope();
}

View File

@@ -5,6 +5,9 @@
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_
#include <memory>
#include <string>
#include "event_sink.h"
namespace flutter {
@@ -13,16 +16,16 @@ class EncodableValue;
template <typename T = EncodableValue>
struct StreamHandlerError {
const std::string& error_code;
const std::string& error_message;
const T* error_details;
const std::string error_code;
const std::string error_message;
const std::unique_ptr<T> error_details;
StreamHandlerError(const std::string& error_code,
const std::string& error_message,
const T* error_details)
StreamHandlerError(const std::string error_code,
const std::string error_message,
std::unique_ptr<T>&& error_details)
: error_code(error_code),
error_message(error_message),
error_details(error_details) {}
error_details(std::move(error_details)) {}
};
// Handler for stream setup and teardown requests.