Skip to content

Commit 564b766

Browse files
committed
Add SettingsChannel unit test
* Take test_binary_messenger.h from Windows implementation Signed-off-by: Boram Bae <[email protected]>
1 parent 9102445 commit 564b766

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

shell/platform/tizen/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ executable("flutter_tizen_unittests") {
268268
testonly = true
269269

270270
sources = [
271+
"channels/settings_channel_unittests.cc",
271272
"flutter_project_bundle_unittests.cc",
272273
"flutter_tizen_engine_unittest.cc",
273274
"flutter_tizen_texture_registrar_unittests.cc",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/common/json_message_codec.h"
6+
#include "flutter/shell/platform/tizen/channels/settings_channel.h"
7+
#include "flutter/shell/platform/tizen/testing/test_binary_messenger.h"
8+
#include "gtest/gtest.h"
9+
10+
namespace {
11+
12+
constexpr char kChannelName[] = "flutter/settings";
13+
constexpr char kAlwaysUse24HourFormatKey[] = "alwaysUse24HourFormat";
14+
constexpr char kPlatformBrightnessKey[] = "platformBrightness";
15+
constexpr char kTextScaleFactorKey[] = "textScaleFactor";
16+
17+
} // namespace
18+
19+
namespace flutter {
20+
namespace testing {
21+
22+
TEST(SettingsChannel, SendSettingsEventOnceWhenInitialization) {
23+
bool always_use24_hour_format = true;
24+
std::string platform_brightness;
25+
double scale_factor = 0;
26+
int call_count = 0;
27+
28+
TestBinaryMessenger messenger(
29+
[&always_use24_hour_format, &platform_brightness, &scale_factor,
30+
&call_count](const std::string& channel, const uint8_t* message,
31+
size_t message_size, BinaryReply reply) {
32+
if (channel == kChannelName) {
33+
auto message_doc = JsonMessageCodec::GetInstance().DecodeMessage(
34+
message, message_size);
35+
always_use24_hour_format =
36+
(*message_doc)[kAlwaysUse24HourFormatKey].GetBool();
37+
scale_factor = (*message_doc)[kTextScaleFactorKey].GetDouble();
38+
platform_brightness =
39+
(*message_doc)[kPlatformBrightnessKey].GetString();
40+
call_count++;
41+
} else {
42+
// Should not be reached.
43+
EXPECT_TRUE(false);
44+
}
45+
});
46+
47+
SettingsChannel setting_channel(&messenger);
48+
49+
// The Linux implementation always returns false.
50+
EXPECT_TRUE(always_use24_hour_format == false);
51+
52+
EXPECT_TRUE(platform_brightness == "light");
53+
EXPECT_TRUE(scale_factor == 1.0);
54+
EXPECT_TRUE(call_count == 1);
55+
}
56+
57+
} // namespace testing
58+
} // namespace flutter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
7+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
8+
9+
#include <cassert>
10+
#include <functional>
11+
#include <map>
12+
#include <string>
13+
14+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
15+
16+
namespace flutter {
17+
18+
// A trivial BinaryMessenger implementation for use in tests.
19+
class TestBinaryMessenger : public BinaryMessenger {
20+
public:
21+
using SendHandler = std::function<void(const std::string& channel,
22+
const uint8_t* message,
23+
size_t message_size,
24+
BinaryReply reply)>;
25+
26+
// Creates a new messenge that forwards all calls to |send_handler|.
27+
explicit TestBinaryMessenger(SendHandler send_handler = nullptr)
28+
: send_handler_(std::move(send_handler)) {}
29+
30+
virtual ~TestBinaryMessenger() = default;
31+
32+
// Prevent copying.
33+
TestBinaryMessenger(TestBinaryMessenger const&) = delete;
34+
TestBinaryMessenger& operator=(TestBinaryMessenger const&) = delete;
35+
36+
// Simulates a message from the engine on the given channel.
37+
//
38+
// Returns false if no handler is registered on that channel.
39+
bool SimulateEngineMessage(const std::string& channel,
40+
const uint8_t* message,
41+
size_t message_size,
42+
BinaryReply reply) {
43+
auto handler = registered_handlers_.find(channel);
44+
if (handler == registered_handlers_.end()) {
45+
return false;
46+
}
47+
(handler->second)(message, message_size, reply);
48+
return true;
49+
}
50+
51+
// |flutter::BinaryMessenger|
52+
void Send(const std::string& channel,
53+
const uint8_t* message,
54+
size_t message_size,
55+
BinaryReply reply) const override {
56+
// If something under test sends a message, the test should be handling it.
57+
assert(send_handler_);
58+
send_handler_(channel, message, message_size, reply);
59+
}
60+
61+
// |flutter::BinaryMessenger|
62+
void SetMessageHandler(const std::string& channel,
63+
BinaryMessageHandler handler) override {
64+
if (handler) {
65+
registered_handlers_[channel] = handler;
66+
} else {
67+
registered_handlers_.erase(channel);
68+
}
69+
}
70+
71+
private:
72+
// Handler to call for SendMessage.
73+
SendHandler send_handler_;
74+
75+
// Mapping of channel name to registered handlers.
76+
std::map<std::string, BinaryMessageHandler> registered_handlers_;
77+
};
78+
79+
} // namespace flutter
80+
81+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_

0 commit comments

Comments
 (0)