Skip to content

Commit 544dddc

Browse files
bwikbsswift-kim
authored andcommitted
Unit tests for the settings channel (#146)
* Add setting channel unit test * Update minor things: * Update flutter_tizen_engine_unittest (localization) * Rename file : settings_channel_device.cc -> settings_channel_tizen.cc settings_channel_stub.cc -> settings_channel_linux.cc * Apply review's comment Signed-off-by: MuHong Byun <[email protected]>
1 parent c59006a commit 544dddc

8 files changed

+131
-38
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ template("embedder_for_profile") {
130130
"channels/localization_channel.cc",
131131
"channels/platform_channel.cc",
132132
"channels/settings_channel.cc",
133+
"channels/settings_channel_tizen.cc",
133134
"external_texture_pixel_gl.cc",
134135
"external_texture_surface_gl.cc",
135136
"tizen_log.cc",
@@ -245,7 +246,8 @@ template("embedder_executable") {
245246
sources += [
246247
"channels/localization_channel_stub.cc",
247248
"channels/platform_channel_stub.cc",
248-
"channels/settings_channel_stub.cc",
249+
"channels/settings_channel.cc",
250+
"channels/settings_channel_linux.cc",
249251
"external_texture_pixel_gl_stub.cc",
250252
"external_texture_surface_gl_stub.cc",
251253
"tizen_log_stub.cc",

shell/platform/tizen/channels/platform_view_channel.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"
1313
#include "flutter/shell/platform/tizen/tizen_log.h"
1414

15-
static constexpr char kChannelName[] = "flutter/platform_views";
16-
1715
namespace flutter {
1816

17+
namespace {
18+
constexpr char kChannelName[] = "flutter/platform_views";
19+
} // namespace
20+
1921
template <typename T>
2022
bool GetValueFromEncodableMap(const EncodableValue& arguments,
2123
std::string key,

shell/platform/tizen/channels/settings_channel.cc

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,21 @@ SettingsChannel::SettingsChannel(BinaryMessenger* messenger)
2323
messenger,
2424
kChannelName,
2525
&JsonMessageCodec::GetInstance())) {
26-
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
27-
OnSettingsChangedCallback, this);
26+
Init();
2827
SendSettingsEvent();
2928
}
3029

3130
SettingsChannel::~SettingsChannel() {
32-
system_settings_unset_changed_cb(
33-
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR);
31+
Dispose();
3432
}
3533

3634
void SettingsChannel::SendSettingsEvent() {
3735
rapidjson::Document event(rapidjson::kObjectType);
3836
auto& allocator = event.GetAllocator();
39-
bool value = false;
40-
int ret = system_settings_get_value_bool(
41-
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &value);
42-
if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
43-
event.AddMember(kTextScaleFactorKey, 1.0, allocator);
44-
event.AddMember(kPlatformBrightnessKey, "light", allocator);
45-
event.AddMember(kAlwaysUse24HourFormatKey, value, allocator);
46-
channel_->Send(event);
47-
}
48-
}
49-
50-
void SettingsChannel::OnSettingsChangedCallback(system_settings_key_e key,
51-
void* user_data) {
52-
auto settings_channel = reinterpret_cast<SettingsChannel*>(user_data);
53-
settings_channel->SendSettingsEvent();
37+
event.AddMember(kTextScaleFactorKey, 1.0, allocator);
38+
event.AddMember(kPlatformBrightnessKey, "light", allocator);
39+
event.AddMember(kAlwaysUse24HourFormatKey, Prefer24HourTime(), allocator);
40+
channel_->Send(event);
5441
}
5542

5643
} // namespace flutter

shell/platform/tizen/channels/settings_channel.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#ifndef EMBEDDER_SETTINGS_CHANNEL_H_
66
#define EMBEDDER_SETTINGS_CHANNEL_H_
77

8-
#ifndef __X64_SHELL__
9-
#include <system/system_settings.h>
10-
#endif
11-
128
#include <memory>
139

1410
#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
@@ -21,15 +17,14 @@ class SettingsChannel {
2117
public:
2218
explicit SettingsChannel(BinaryMessenger* messenger);
2319
virtual ~SettingsChannel();
20+
void SendSettingsEvent();
2421

2522
private:
26-
#ifndef __X64_SHELL__
27-
static void OnSettingsChangedCallback(system_settings_key_e key,
28-
void* user_data);
29-
void SendSettingsEvent();
23+
bool Prefer24HourTime();
24+
void Init();
25+
void Dispose();
3026

3127
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
32-
#endif
3328
};
3429

3530
} // namespace flutter

shell/platform/tizen/channels/settings_channel_stub.cc renamed to shell/platform/tizen/channels/settings_channel_linux.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44

55
#include "settings_channel.h"
66

7+
#include <chrono>
8+
#include <ctime>
9+
710
namespace flutter {
811

9-
SettingsChannel::SettingsChannel(BinaryMessenger* messenger) {}
12+
void SettingsChannel::Init() {
13+
std::locale::global(std::locale(""));
14+
}
15+
16+
void SettingsChannel::Dispose() {}
1017

11-
SettingsChannel::~SettingsChannel() {}
18+
bool SettingsChannel::Prefer24HourTime() {
19+
return false;
20+
}
1221

1322
} // namespace flutter
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 <system/system_settings.h>
6+
#include "settings_channel.h"
7+
8+
namespace flutter {
9+
10+
static void OnSettingsChangedCallback(system_settings_key_e key,
11+
void* user_data) {
12+
auto settings_channel = reinterpret_cast<SettingsChannel*>(user_data);
13+
settings_channel->SendSettingsEvent();
14+
}
15+
16+
void SettingsChannel::Init() {
17+
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
18+
OnSettingsChangedCallback, this);
19+
}
20+
21+
void SettingsChannel::Dispose() {
22+
system_settings_unset_changed_cb(
23+
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR);
24+
}
25+
26+
bool SettingsChannel::Prefer24HourTime() {
27+
bool value = false;
28+
if (system_settings_get_value_bool(
29+
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &value) ==
30+
SYSTEM_SETTINGS_ERROR_NONE) {
31+
return value;
32+
}
33+
return false;
34+
}
35+
36+
} // namespace flutter

shell/platform/tizen/flutter_tizen_engine_unittest.cc

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <gtest/gtest.h>
6+
7+
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
58
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
6-
#include "gtest/gtest.h"
9+
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
710

811
namespace flutter {
912
namespace testing {
@@ -18,9 +21,9 @@ class FlutterTizenEngineTest : public ::testing::Test {
1821
protected:
1922
void SetUp() {
2023
FlutterDesktopEngineProperties engine_prop = {};
21-
engine_prop.assets_path = "foo/flutter_assets";
22-
engine_prop.icu_data_path = "foo/icudtl.dat";
23-
engine_prop.aot_library_path = "foo/libapp.so";
24+
engine_prop.assets_path = "/foo/flutter_assets";
25+
engine_prop.icu_data_path = "/foo/icudtl.dat";
26+
engine_prop.aot_library_path = "/foo/libapp.so";
2427

2528
FlutterProjectBundle project(engine_prop);
2629
auto engine = std::make_unique<FlutterTizenEngine>(project);
@@ -81,5 +84,64 @@ TEST_F(FlutterTizenEngineTestHeaded, GetTextureRegistrar) {
8184
EXPECT_TRUE(engine_->GetTextureRegistrar() != nullptr);
8285
}
8386

87+
TEST_F(FlutterTizenEngineTest, RunDoesExpectedInitialization) {
88+
EngineModifier modifier(engine_);
89+
bool run_called = false;
90+
modifier.embedder_api().Run = MOCK_ENGINE_PROC(
91+
Run, ([&run_called, engine_instance = engine_](
92+
size_t version, const FlutterRendererConfig* config,
93+
const FlutterProjectArgs* args, void* user_data,
94+
FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) {
95+
run_called = true;
96+
*engine_out = reinterpret_cast<FLUTTER_API_SYMBOL(FlutterEngine)>(1);
97+
98+
EXPECT_EQ(version, (size_t)FLUTTER_ENGINE_VERSION);
99+
EXPECT_NE(config, nullptr);
100+
EXPECT_EQ(user_data, engine_instance);
101+
102+
EXPECT_STREQ(args->assets_path, "/foo/flutter_assets");
103+
EXPECT_STREQ(args->icu_data_path, "/foo/icudtl.dat");
104+
EXPECT_EQ(args->dart_entrypoint_argc, 0);
105+
EXPECT_NE(args->platform_message_callback, nullptr);
106+
EXPECT_NE(args->custom_task_runners, nullptr);
107+
EXPECT_EQ(args->custom_dart_entrypoint, nullptr);
108+
109+
return kSuccess;
110+
}));
111+
112+
// It should send locale info.
113+
bool update_locales_called = false;
114+
modifier.embedder_api().UpdateLocales = MOCK_ENGINE_PROC(
115+
UpdateLocales,
116+
([&update_locales_called](auto engine, const FlutterLocale** locales,
117+
size_t locales_count) {
118+
update_locales_called = true;
119+
EXPECT_GT(locales_count, (size_t)0);
120+
EXPECT_NE(locales, nullptr);
121+
122+
return kSuccess;
123+
}));
124+
125+
// And it should send initial settings info.
126+
bool settings_message_sent = false;
127+
modifier.embedder_api().SendPlatformMessage = MOCK_ENGINE_PROC(
128+
SendPlatformMessage,
129+
([&settings_message_sent](auto engine, auto message) {
130+
if (std::string(message->channel) == std::string("flutter/settings")) {
131+
settings_message_sent = true;
132+
}
133+
134+
return kSuccess;
135+
}));
136+
137+
engine_->RunEngine();
138+
139+
EXPECT_TRUE(run_called);
140+
EXPECT_TRUE(update_locales_called);
141+
EXPECT_TRUE(settings_message_sent);
142+
143+
modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; };
144+
}
145+
84146
} // namespace testing
85147
} // namespace flutter

shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <gtest/gtest.h>
56
#include <iostream>
67

78
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
89
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
910
#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h"
1011
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
11-
#include "gtest/gtest.h"
1212

1313
namespace flutter {
1414
namespace testing {

0 commit comments

Comments
 (0)