Windows: Add SystemSound support (flutter/engine#29811)

This commit is contained in:
moko256
2022-04-08 07:51:03 +09:00
committed by GitHub
parent 92e109f579
commit 743edc1ccc
7 changed files with 71 additions and 0 deletions

View File

@@ -11,6 +11,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 kPlaySoundMethod[] = "SystemSound.play";
static constexpr char kTextPlainFormat[] = "text/plain";
static constexpr char kTextKey[] = "text";
@@ -64,6 +65,11 @@ void PlatformHandler::HandleMethodCall(
return;
}
SetPlainText(itr->value.GetString(), std::move(result));
} else if (method.compare(kPlaySoundMethod) == 0) {
// Only one string argument is expected.
const rapidjson::Value& sound_type = method_call.arguments()[0];
SystemSoundPlay(sound_type.GetString(), std::move(result));
} else {
result->NotImplemented();
}

View File

@@ -42,9 +42,15 @@ class PlatformHandler {
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) = 0;
virtual void SystemSoundPlay(
const std::string& sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) = 0;
// A error type to use for error responses.
static constexpr char kClipboardError[] = "Clipboard error";
static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert";
private:
// Called when a method is called on |channel_|;
void HandleMethodCall(

View File

@@ -23,10 +23,13 @@ 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 kPlaySoundMethod[] = "SystemSound.play";
static constexpr char kTextPlainFormat[] = "text/plain";
static constexpr char kFakeContentType[] = "text/madeupcontenttype";
static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert";
// Test implementation of PlatformHandler to allow testing the PlatformHandler
// logic.
class TestPlatformHandler : public PlatformHandler {
@@ -45,6 +48,9 @@ class TestPlatformHandler : public PlatformHandler {
MOCK_METHOD2(SetPlainText,
void(const std::string&,
std::unique_ptr<MethodResult<rapidjson::Document>>));
MOCK_METHOD2(SystemSoundPlay,
void(const std::string&,
std::unique_ptr<MethodResult<rapidjson::Document>>));
};
// Mock result to inspect results of PlatformHandler calls.
@@ -201,5 +207,31 @@ TEST(PlatformHandler, RejectsSettingUnknownTypes) {
}));
}
TEST(PlatformHandler, PlayingSystemSoundCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->SetString(kSoundTypeAlert);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kPlaySoundMethod, 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, SystemSoundPlay)
.WillByDefault(
[](auto sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
result->NotImplemented();
});
EXPECT_CALL(platform_handler,
SystemSoundPlay(::testing::StrEq(kSoundTypeAlert), _));
EXPECT_TRUE(messenger.SimulateEngineMessage(
kChannelName, encoded->data(), encoded->size(),
[](const uint8_t* reply, size_t reply_size) {}));
}
} // namespace testing
} // namespace flutter

View File

@@ -269,4 +269,15 @@ void PlatformHandlerWin32::SetPlainText(
result->Success();
}
void PlatformHandlerWin32::SystemSoundPlay(
const std::string& sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
if (sound_type.compare(kSoundTypeAlert) == 0) {
MessageBeep(MB_OK);
result->Success();
} else {
result->NotImplemented();
}
}
} // namespace flutter

View File

@@ -37,6 +37,11 @@ class PlatformHandlerWin32 : public PlatformHandler {
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
// |PlatformHandler|
void SystemSoundPlay(
const std::string& sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
private:
// A reference to the Flutter view.
FlutterWindowsView* view_;

View File

@@ -108,4 +108,10 @@ void PlatformHandlerWinUwp::SetPlainText(
result->Success();
}
void PlatformHandlerWinUwp::SystemSoundPlay(
const std::string& sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
result->NotImplemented();
}
} // namespace flutter

View File

@@ -37,6 +37,11 @@ class PlatformHandlerWinUwp : public PlatformHandler {
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
// |PlatformHandler|
void SystemSoundPlay(
const std::string& sound_type,
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;
private:
// A reference to the Flutter view.
FlutterWindowsView* view_;