This source file includes following definitions.
- set_locale
- set_keyboard_layout
- StartLoadingMachineStatistics
- GetMachineStatistic
- GetMachineFlag
- Shutdown
- VerifyInitialOptions
- VerifyOptionExists
- DumpOptions
- RunLocalizationTest
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "base/task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/customization_document.h"
#include "chrome/browser/chromeos/input_method/input_method_util.h"
#include "chrome/browser/chromeos/login/login_display_host_impl.h"
#include "chrome/browser/chromeos/login/login_wizard.h"
#include "chrome/browser/chromeos/login/test/js_checker.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/ime/extension_ime_util.h"
#include "chromeos/ime/input_method_manager.h"
#include "chromeos/ime/input_method_whitelist.h"
#include "chromeos/system/statistics_provider.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
namespace base {
class TaskRunner;
}
namespace chromeos {
namespace {
const char* kLocaleSelect = "language-select";
const char* kKeyboardSelect = "keyboard-select";
const char* kUSLayout = "xkb:us::eng";
}
namespace system {
class FakeStatisticsProvider : public StatisticsProvider {
public:
virtual ~FakeStatisticsProvider() {}
void set_locale(const std::string& locale) {
initial_locale_ = locale;
}
void set_keyboard_layout(const std::string& keyboard_layout) {
keyboard_layout_ = keyboard_layout;
}
private:
virtual void StartLoadingMachineStatistics(
const scoped_refptr<base::TaskRunner>& file_task_runner,
bool load_oem_manifest) OVERRIDE {
}
virtual bool GetMachineStatistic(const std::string& name,
std::string* result) OVERRIDE {
if (name == "initial_locale")
*result = initial_locale_;
else if (name == "keyboard_layout")
*result = keyboard_layout_;
else
return false;
return true;
}
virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE {
return false;
}
virtual void Shutdown() OVERRIDE {
}
std::string initial_locale_;
std::string keyboard_layout_;
};
}
class OobeLocalizationTest : public InProcessBrowserTest {
public:
OobeLocalizationTest();
virtual ~OobeLocalizationTest();
void VerifyInitialOptions(const char* select_id,
const char* values,
bool check_separator);
void VerifyOptionExists(const char* select_id, const char* value);
std::string DumpOptions(const char* select_id);
protected:
void RunLocalizationTest(const std::string& initial_locale,
const std::string& keyboard_layout,
const std::string& expected_locale,
const std::string& expected_keyboard_layout,
const std::string& expected_keyboard_select_control);
private:
scoped_ptr<system::FakeStatisticsProvider> statistics_provider_;
test::JSChecker checker;
DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest);
};
OobeLocalizationTest::OobeLocalizationTest() {
statistics_provider_.reset(new system::FakeStatisticsProvider());
system::StatisticsProvider::SetTestProvider(statistics_provider_.get());
}
OobeLocalizationTest::~OobeLocalizationTest() {
system::StatisticsProvider::SetTestProvider(NULL);
}
void OobeLocalizationTest::VerifyInitialOptions(const char* select_id,
const char* values,
bool check_separator) {
const std::string expression = base::StringPrintf(
"(function () {\n"
" var select = document.querySelector('#%s');\n"
" if (!select)\n"
" return false;\n"
" var values = '%s'.split(',');\n"
" var correct = select.selectedIndex == 0;\n"
" for (var i = 0; i < values.length && correct; i++) {\n"
" if (select.options[i].value != values[i])\n"
" correct = false;\n"
" }\n"
" if (%d && correct)\n"
" correct = select.children[values.length].tagName === 'OPTGROUP';\n"
" return correct;\n"
"})()", select_id, values, check_separator);
ASSERT_TRUE(checker.GetBool(expression)) << expression;
}
void OobeLocalizationTest::VerifyOptionExists(const char* select_id,
const char* value) {
const std::string expression = base::StringPrintf(
"(function () {\n"
" var select = document.querySelector('#%s');\n"
" if (!select)\n"
" return false;\n"
" for (var i = 0; i < select.options.length; i++) {\n"
" if (select.options[i].value == '%s')\n"
" return true;\n"
" }\n"
" return false;\n"
"})()", select_id, value);
ASSERT_TRUE(checker.GetBool(expression)) << expression;
}
std::string OobeLocalizationTest::DumpOptions(const char* select_id) {
const std::string expression = base::StringPrintf(
"\n"
"(function () {\n"
" var selector = '#%s';\n"
" var divider = ',';\n"
" var select = document.querySelector(selector);\n"
" if (!select)\n"
" return 'document.querySelector(' + selector + ') failed.';\n"
" var dumpOptgroup = function(group) {\n"
" var result = '';\n"
" for (var i = 0; i < group.children.length; i++) {\n"
" if (i > 0) {\n"
" result += divider;\n"
" }\n"
" if (group.children[i].value) {\n"
" result += group.children[i].value;\n"
" } else {\n"
" result += '__NO_VALUE__';\n"
" }\n"
" }\n"
" return result;\n"
" };\n"
" var result = '';\n"
" var children = select.children;\n"
" for (var i = 0; i < children.length; i++) {\n"
" if (i > 0) {\n"
" result += divider;\n"
" }\n"
" if (children[i].value) {\n"
" result += children[i].value;\n"
" } else if (children[i].tagName === 'OPTGROUP') {\n"
" result += '[' + dumpOptgroup(children[i]) + ']';\n"
" } else {\n"
" result += '__NO_VALUE__';\n"
" }\n"
" }\n"
" return result;\n"
"})()\n",
select_id);
return checker.GetString(expression);
}
void OobeLocalizationTest::RunLocalizationTest(
const std::string& initial_locale,
const std::string& keyboard_layout,
const std::string& expected_locale,
const std::string& expected_keyboard_layout,
const std::string& expected_keyboard_select_control) {
statistics_provider_->set_locale(initial_locale);
statistics_provider_->set_keyboard_layout(keyboard_layout);
StartupCustomizationDocument::GetInstance()->Init(
statistics_provider_.get());
g_browser_process->local_state()->SetString(
prefs::kHardwareKeyboardLayout, keyboard_layout);
input_method::InputMethodManager::Get()
->GetInputMethodUtil()
->InitXkbInputMethodsForTesting();
std::string expected_keyboard_select = expected_keyboard_select_control;
if (extension_ime_util::UseWrappedExtensionKeyboardLayouts()) {
size_t pos = 0;
std::string repl_old = "xkb:";
std::string repl_new =
extension_ime_util::GetInputMethodIDByKeyboardLayout("xkb:");
while ((pos = expected_keyboard_select.find(repl_old, pos)) !=
std::string::npos) {
expected_keyboard_select.replace(pos, repl_old.length(), repl_new);
pos += repl_new.length();
}
}
chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName);
content::WindowedNotificationObserver(
chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
content::NotificationService::AllSources()).Wait();
checker.set_web_contents(static_cast<chromeos::LoginDisplayHostImpl*>(
chromeos::LoginDisplayHostImpl::default_host())->
GetOobeUI()->web_ui()->GetWebContents());
VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true);
VerifyInitialOptions(kKeyboardSelect,
extension_ime_util::GetInputMethodIDByKeyboardLayout(
expected_keyboard_layout).c_str(),
false);
VerifyOptionExists(
kKeyboardSelect,
extension_ime_util::GetInputMethodIDByKeyboardLayout(kUSLayout).c_str());
EXPECT_EQ(expected_keyboard_select, DumpOptions(kKeyboardSelect));
chromeos::LoginDisplayHostImpl::default_host()->Finalize();
base::MessageLoopForUI::current()->RunUntilIdle();
g_browser_process->local_state()->SetString(prefs::kApplicationLocale,
std::string());
}
IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) {
RunLocalizationTest("ru", "xkb:ru::rus",
"ru", kUSLayout,
"xkb:us::eng");
RunLocalizationTest("ru", "xkb:us::eng,xkb:ru::rus",
"ru", kUSLayout,
"xkb:us::eng");
RunLocalizationTest("ja", "xkb:jp::jpn",
"ja", "xkb:jp::jpn",
"xkb:jp::jpn,[xkb:us::eng]");
}
IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) {
RunLocalizationTest("en-US", "xkb:is::ice",
"en-US", "xkb:is::ice",
"xkb:is::ice,["
"xkb:us::eng,xkb:us:intl:eng,xkb:us:altgr-intl:eng,"
"xkb:us:dvorak:eng,xkb:us:colemak:eng]");
}
IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenFullLatin) {
RunLocalizationTest("fr", "xkb:ch:fr:fra",
"fr", "xkb:ch:fr:fra",
"xkb:ch:fr:fra,["
"xkb:fr::fra,xkb:be::fra,xkb:ca::fra,"
"xkb:ca:multix:fra,xkb:us::eng]");
RunLocalizationTest("de", "xkb:ch::ger",
"de", "xkb:ch::ger",
"xkb:ch::ger,["
"xkb:de::ger,xkb:de:neo:ger,xkb:be::ger,xkb:us::eng"
"]");
}
IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) {
RunLocalizationTest("es,en-US,nl", "xkb:be::nld",
"es,en-US,nl", "xkb:be::nld",
"xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]");
RunLocalizationTest("ru,de", "xkb:ru::rus",
"ru,de", kUSLayout,
"xkb:us::eng");
}
}