hasStrings on Windows (flutter/engine#27749)

This commit is contained in:
Justin McCandless
2021-08-24 17:54:01 -07:00
committed by GitHub
parent 6aae35a3d3
commit e00871d812
8 changed files with 106 additions and 6 deletions

View File

@@ -247,6 +247,7 @@ executable("flutter_windows_unittests") {
"keyboard_key_embedder_handler_unittests.cc",
"keyboard_key_handler_unittests.cc",
"keyboard_unittests.cc",
"platform_handler_unittests.cc",
"testing/flutter_window_win32_test.cc",
"testing/flutter_window_win32_test.h",
"testing/mock_window_binding_handler.cc",

View File

@@ -9,6 +9,7 @@
static constexpr char kChannelName[] = "flutter/platform";
static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings";
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
static constexpr char kTextPlainFormat[] = "text/plain";
@@ -46,6 +47,15 @@ void PlatformHandler::HandleMethodCall(
return;
}
GetPlainText(std::move(result), kTextKey);
} else if (method.compare(kHasStringsClipboardMethod) == 0) {
// Only one string argument is expected.
const rapidjson::Value& format = method_call.arguments()[0];
if (strcmp(format.GetString(), kTextPlainFormat) != 0) {
result->Error(kClipboardError, kUnknownClipboardFormatMessage);
return;
}
GetHasStrings(std::move(result));
} else if (method.compare(kSetClipboardDataMethod) == 0) {
const rapidjson::Value& document = *method_call.arguments();
rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey);

View File

@@ -31,6 +31,11 @@ class PlatformHandler {
std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) = 0;
// Provides a boolean to |result| as the value in a dictionary at key
// "value" representing whether or not the clipboard has a non-empty string.
virtual void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) = 0;
// Sets the clipboard's plain text to |text|, and reports the result (either
// an error, or null for success) to |result|.
virtual void SetPlainText(

View File

@@ -6,7 +6,6 @@
#include <memory>
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/json_method_codec.h"
#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
#include "gmock/gmock.h"
@@ -22,9 +21,11 @@ using ::testing::_;
static constexpr char kChannelName[] = "flutter/platform";
static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings";
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
static constexpr char kTextPlainFormat[] = "text/plain";
static constexpr char kFakeContentType[] = "text/madeupcontenttype";
// Test implementation of PlatformHandler to allow testing the PlatformHandler
// logic.
@@ -38,7 +39,9 @@ class TestPlatformHandler : public PlatformHandler {
// |PlatformHandler|
MOCK_METHOD2(GetPlainText,
void(std::unique_ptr<MethodResult<rapidjson::Document>>,
const char*));
std::string_view key));
MOCK_METHOD1(GetHasStrings,
void(std::unique_ptr<MethodResult<rapidjson::Document>>));
MOCK_METHOD2(SetPlainText,
void(const std::string&,
std::unique_ptr<MethodResult<rapidjson::Document>>));
@@ -61,9 +64,9 @@ TEST(PlatformHandler, GettingTextCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->PushBack(kTextPlainFormat, allocator);
args->SetString(kTextPlainFormat);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kGetClipboardDataMethod,
std::move(args)));
@@ -85,9 +88,9 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->PushBack("madeup/contenttype", allocator);
args->SetString(kFakeContentType);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kGetClipboardDataMethod,
std::move(args)));
@@ -103,6 +106,53 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) {
}));
}
TEST(PlatformHandler, GetHasStringsCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->SetString(kTextPlainFormat);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kHasStringsClipboardMethod,
std::move(args)));
// Set up a handler to call a response on |result| so that it doesn't log
// on destruction about leaking.
ON_CALL(platform_handler, GetHasStrings)
.WillByDefault(
[](std::unique_ptr<MethodResult<rapidjson::Document>> result) {
result->NotImplemented();
});
EXPECT_CALL(platform_handler, GetHasStrings(_));
EXPECT_TRUE(messenger.SimulateEngineMessage(
kChannelName, encoded->data(), encoded->size(),
[](const uint8_t* reply, size_t reply_size) {}));
}
TEST(PlatformHandler, RejectsGetHasStringsOnUnknownTypes) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->SetString(kFakeContentType);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kHasStringsClipboardMethod,
std::move(args)));
MockMethodResult result;
// Requsting an unknow content type is an error.
EXPECT_CALL(result, ErrorInternal(_, _, _));
EXPECT_TRUE(messenger.SimulateEngineMessage(
kChannelName, encoded->data(), encoded->size(),
[&](const uint8_t* reply, size_t reply_size) {
JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope(
reply, reply_size, &result);
}));
}
TEST(PlatformHandler, SettingTextCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);

View File

@@ -13,6 +13,8 @@
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/string_conversion.h"
static constexpr char kValueKey[] = "value";
namespace flutter {
namespace {
@@ -229,6 +231,24 @@ void PlatformHandlerWin32::GetPlainText(
result->Success(document);
}
void PlatformHandlerWin32::GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
ScopedClipboard clipboard;
if (!clipboard.Open(std::get<HWND>(*view_->GetRenderTarget()))) {
rapidjson::Document error_code;
error_code.SetInt(::GetLastError());
result->Error(kClipboardError, "Unable to open clipboard", error_code);
return;
}
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
document.AddMember(rapidjson::Value(kValueKey, allocator),
rapidjson::Value(clipboard.HasString()), allocator);
result->Success(document);
}
void PlatformHandlerWin32::SetPlainText(
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {

View File

@@ -28,6 +28,10 @@ class PlatformHandlerWin32 : public PlatformHandler {
void GetPlainText(std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) override;
// |PlatformHandler|
void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
// |PlatformHandler|
void SetPlainText(
const std::string& text,

View File

@@ -28,6 +28,12 @@ void PlatformHandlerWinUwp::GetPlainText(
result->NotImplemented();
}
void PlatformHandlerWinUwp::GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
// TODO: Implement. See https://github.com/flutter/flutter/issues/70214.
result->NotImplemented();
}
void PlatformHandlerWinUwp::SetPlainText(
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {

View File

@@ -28,6 +28,10 @@ class PlatformHandlerWinUwp : public PlatformHandler {
void GetPlainText(std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) override;
// |PlatformHandler|
void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
// |PlatformHandler|
void SetPlainText(
const std::string& text,