Skip to content

Unit tests for the settings channel #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ template("embedder_for_profile") {
"channels/localization_channel.cc",
"channels/platform_channel.cc",
"channels/settings_channel.cc",
"channels/settings_channel_tizen.cc",
"external_texture_pixel_gl.cc",
"external_texture_surface_gl.cc",
"tizen_log.cc",
Expand Down Expand Up @@ -245,7 +246,8 @@ template("embedder_executable") {
sources += [
"channels/localization_channel_stub.cc",
"channels/platform_channel_stub.cc",
"channels/settings_channel_stub.cc",
"channels/settings_channel.cc",
"channels/settings_channel_linux.cc",
"external_texture_pixel_gl_stub.cc",
"external_texture_surface_gl_stub.cc",
"tizen_log_stub.cc",
Expand Down
6 changes: 4 additions & 2 deletions shell/platform/tizen/channels/platform_view_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
#include "flutter/shell/platform/tizen/public/flutter_platform_view.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

static constexpr char kChannelName[] = "flutter/platform_views";

namespace flutter {

namespace {
constexpr char kChannelName[] = "flutter/platform_views";
} // namespace

template <typename T>
bool GetValueFromEncodableMap(const EncodableValue& arguments,
std::string key,
Expand Down
20 changes: 5 additions & 15 deletions shell/platform/tizen/channels/settings_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,24 @@ SettingsChannel::SettingsChannel(BinaryMessenger* messenger)
messenger,
kChannelName,
&JsonMessageCodec::GetInstance())) {
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
OnSettingsChangedCallback, this);
Init();
SendSettingsEvent();
}

SettingsChannel::~SettingsChannel() {
system_settings_unset_changed_cb(
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR);
Dispose();
}

void SettingsChannel::SendSettingsEvent() {
rapidjson::Document event(rapidjson::kObjectType);
auto& allocator = event.GetAllocator();
bool value = false;
int ret = system_settings_get_value_bool(
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &value);
if (ret == SYSTEM_SETTINGS_ERROR_NONE) {
if (GetSettingValueOf24Format(&value)) {
rapidjson::Document event(rapidjson::kObjectType);
auto& allocator = event.GetAllocator();
event.AddMember(kTextScaleFactorKey, 1.0, allocator);
event.AddMember(kPlatformBrightnessKey, "light", allocator);
event.AddMember(kAlwaysUse24HourFormatKey, value, allocator);
channel_->Send(event);
}
}

void SettingsChannel::OnSettingsChangedCallback(system_settings_key_e key,
void* user_data) {
auto settings_channel = reinterpret_cast<SettingsChannel*>(user_data);
settings_channel->SendSettingsEvent();
}

} // namespace flutter
13 changes: 4 additions & 9 deletions shell/platform/tizen/channels/settings_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#ifndef EMBEDDER_SETTINGS_CHANNEL_H_
#define EMBEDDER_SETTINGS_CHANNEL_H_

#ifndef __X64_SHELL__
#include <system/system_settings.h>
#endif

#include <memory>

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

private:
#ifndef __X64_SHELL__
static void OnSettingsChangedCallback(system_settings_key_e key,
void* user_data);
void SendSettingsEvent();
bool GetSettingValueOf24Format(bool* value);
void Init();
void Dispose();

std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
#endif
};

} // namespace flutter
Expand Down
40 changes: 40 additions & 0 deletions shell/platform/tizen/channels/settings_channel_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "settings_channel.h"

#include <chrono>
#include <ctime>
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <chrono>
#include <ctime>
#include <locale>


namespace flutter {

void SettingsChannel::Init() {
std::locale::global(std::locale(""));
}

void SettingsChannel::Dispose() {}

bool SettingsChannel::GetSettingValueOf24Format(bool* value) {
// This is a temporary implementation because I didn't know how to properly implement it.
// I believe someone will replace it with a better implementation.
// This function operates normally only in the following locale.
// en_US.UTF-8, ko_KR.UTF-8
char buf[64];
std::time_t current = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
if(!strftime(buf, sizeof(buf), "%c", std::localtime(&current))){
return false;
}
std::string current_local_time(buf);
if ((current_local_time.find("AM")!=std::string::npos)
|| (current_local_time.find("PM")!=std::string::npos)
|| (current_local_time.find("오전")!=std::string::npos)
|| (current_local_time.find("오후")!=std::string::npos)
){
*value = true;
}
*value = false;
return true;
}

} // namespace flutter
13 changes: 0 additions & 13 deletions shell/platform/tizen/channels/settings_channel_stub.cc

This file was deleted.

35 changes: 35 additions & 0 deletions shell/platform/tizen/channels/settings_channel_tizen.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <system/system_settings.h>
#include "settings_channel.h"
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <system/system_settings.h>
#include "settings_channel.h"
#include "settings_channel.h"
#include <system/system_settings.h>


namespace flutter {

static void OnSettingsChangedCallback(system_settings_key_e key,
void* user_data) {
auto settings_channel = reinterpret_cast<SettingsChannel*>(user_data);
settings_channel->SendSettingsEvent();
}

void SettingsChannel::Init() {
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
OnSettingsChangedCallback, this);
}

void SettingsChannel::Dispose() {
system_settings_unset_changed_cb(
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR);
}

bool SettingsChannel::GetSettingValueOf24Format(bool* value) {
if (system_settings_get_value_bool(
SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, value) ==
SYSTEM_SETTINGS_ERROR_NONE) {
return true;
}
return false;
}

} // namespace flutter
69 changes: 66 additions & 3 deletions shell/platform/tizen/flutter_tizen_engine_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <filesystem>

#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
#include "flutter_tizen_engine.h"
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"

#include "gtest/gtest.h"

namespace flutter {
Expand All @@ -18,9 +22,9 @@ class FlutterTizenEngineTest : public ::testing::Test {
protected:
void SetUp() {
FlutterDesktopEngineProperties engine_prop = {};
engine_prop.assets_path = "foo/flutter_assets";
engine_prop.icu_data_path = "foo/icudtl.dat";
engine_prop.aot_library_path = "foo/libapp.so";
engine_prop.assets_path = "/foo/flutter_assets";
engine_prop.icu_data_path = "/foo/icudtl.dat";
engine_prop.aot_library_path = "/foo/libapp.so";

FlutterProjectBundle project(engine_prop);
auto engine = std::make_unique<FlutterTizenEngine>(project);
Expand Down Expand Up @@ -81,5 +85,64 @@ TEST_F(FlutterTizenEngineTestHeaded, GetTextureRegistrar) {
EXPECT_TRUE(engine_->GetTextureRegistrar() != nullptr);
}

TEST_F(FlutterTizenEngineTest, RunDoesExpectedInitialization) {
EngineModifier modifier(engine_);
bool run_called = false;
modifier.embedder_api().Run = MOCK_ENGINE_PROC(
Run, ([&run_called, engine_instance = engine_](
size_t version, const FlutterRendererConfig* config,
const FlutterProjectArgs* args, void* user_data,
FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) {
std::string current_path = std::filesystem::current_path().string();
run_called = true;
*engine_out = reinterpret_cast<FLUTTER_API_SYMBOL(FlutterEngine)>(1);

EXPECT_EQ(version, (size_t)FLUTTER_ENGINE_VERSION);
EXPECT_NE(config, nullptr);
EXPECT_EQ(user_data, engine_instance);

EXPECT_STREQ(args->assets_path, "/foo/flutter_assets");
EXPECT_STREQ(args->icu_data_path, "/foo/icudtl.dat");
EXPECT_EQ(args->dart_entrypoint_argc, 0);
EXPECT_NE(args->platform_message_callback, nullptr);
EXPECT_NE(args->custom_task_runners, nullptr);
EXPECT_EQ(args->custom_dart_entrypoint, nullptr);

return kSuccess;
}));

// It should send locale info.
bool update_locales_called = false;
modifier.embedder_api().UpdateLocales = MOCK_ENGINE_PROC(
UpdateLocales,
([&update_locales_called](auto engine, const FlutterLocale** locales,
size_t locales_count) {
update_locales_called = true;
EXPECT_GT(locales_count, (size_t)0);
EXPECT_NE(locales, nullptr);

return kSuccess;
}));

// And it should send initial settings info.
bool settings_message_sent = false;
modifier.embedder_api().SendPlatformMessage = MOCK_ENGINE_PROC(
SendPlatformMessage,
([&settings_message_sent](auto engine, auto message) {
if (std::string(message->channel) == std::string("flutter/settings")) {
settings_message_sent = true;
}

return kSuccess;
}));

engine_->RunEngine();

EXPECT_TRUE(run_called);
EXPECT_TRUE(update_locales_called);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this test does not pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will pass at #147. For the next PR, only TC was added intentionally.

EXPECT_TRUE(settings_message_sent);

modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; };
}
} // namespace testing
} // namespace flutter