1 // Copyright 2014 The Chromium Authors. 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 <stddef.h>
6 
7 #include "base/bind.h"
8 #include "base/run_loop.h"
9 #include "base/stl_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/task_runner.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/customization/customization_document.h"
17 #include "chrome/browser/chromeos/login/login_wizard.h"
18 #include "chrome/browser/chromeos/login/screens/welcome_screen.h"
19 #include "chrome/browser/chromeos/login/test/js_checker.h"
20 #include "chrome/browser/chromeos/login/test/oobe_base_test.h"
21 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
22 #include "chrome/browser/chromeos/login/wizard_controller.h"
23 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
24 #include "chrome/browser/ui/webui/chromeos/login/welcome_screen_handler.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/test/base/in_process_browser_test.h"
27 #include "chromeos/system/fake_statistics_provider.h"
28 #include "chromeos/system/statistics_provider.h"
29 #include "components/language/core/browser/pref_names.h"
30 #include "components/prefs/pref_service.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/test/browser_test.h"
34 #include "content/public/test/browser_test_utils.h"
35 #include "content/public/test/test_utils.h"
36 #include "ui/base/ime/chromeos/extension_ime_util.h"
37 #include "ui/base/ime/chromeos/input_method_allowlist.h"
38 #include "ui/base/ime/chromeos/input_method_manager.h"
39 #include "ui/base/ime/chromeos/input_method_util.h"
40 
41 namespace base {
42 class TaskRunner;
43 }
44 
45 namespace chromeos {
46 
47 namespace {
48 
49 // OOBE constants.
50 const char kLanguageSelect[] = "languageSelect";
51 const char kKeyboardSelect[] = "keyboardSelect";
52 
GetGetSelectStatement(const std::string & selectId)53 std::string GetGetSelectStatement(const std::string& selectId) {
54   return "document.getElementById('connect').$." + selectId + ".$.select";
55 }
56 
57 const char kUSLayout[] = "xkb:us::eng";
58 
59 class LanguageListWaiter : public WelcomeScreen::Observer {
60  public:
LanguageListWaiter()61   LanguageListWaiter()
62       : welcome_screen_(WizardController::default_controller()
63                             ->GetScreen<WelcomeScreen>()) {
64     welcome_screen_->AddObserver(this);
65     CheckLanguageList();
66   }
67 
~LanguageListWaiter()68   ~LanguageListWaiter() override { welcome_screen_->RemoveObserver(this); }
69 
70   // WelcomeScreen::Observer implementation:
OnLanguageListReloaded()71   void OnLanguageListReloaded() override { CheckLanguageList(); }
72 
73   // Run the loop until the list is ready or the default Run() timeout expires.
RunUntilLanguageListReady()74   void RunUntilLanguageListReady() { loop_.Run(); }
75 
76  private:
LanguageListReady() const77   bool LanguageListReady() const { return welcome_screen_->language_list(); }
78 
CheckLanguageList()79   void CheckLanguageList() {
80     if (LanguageListReady())
81       loop_.Quit();
82   }
83 
84   WelcomeScreen* welcome_screen_;
85   base::RunLoop loop_;
86 };
87 
88 }  // namespace
89 
90 // These test data depend on the IME extension manifest which differs between
91 // Chromium OS and Chrome OS.
92 struct LocalizationTestParams {
93   const char* initial_locale;
94   const char* keyboard_layout;
95   const char* expected_locale;
96   const char* expected_keyboard_layout;
97   const char* expected_keyboard_select_control;
98 } const oobe_localization_test_parameters[] = {
99 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
100     // ------------------ Non-Latin setup
101     // For a non-Latin keyboard layout like Russian, we expect to see the US
102     // keyboard.
103     {"ru", "xkb:ru::rus", "ru", kUSLayout, "xkb:us::eng"},
104     {"ru", "xkb:us::eng,xkb:ru::rus", "ru", kUSLayout, "xkb:us::eng"},
105 
106     // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese
107     // keyboard.
108     {"ja", "xkb:jp::jpn", "ja", "xkb:jp::jpn", "xkb:jp::jpn,[xkb:us::eng]"},
109 
110     // We don't use the Icelandic locale but the Icelandic keyboard layout
111     // should still be selected when specified as the default.
112     {"en-US", "xkb:is::ice", "en-US", "xkb:is::ice",
113      "xkb:is::ice,[xkb:us::eng,xkb:us:intl:eng,xkb:us:intl_pc:eng,"
114      "xkb:us:altgr-intl:eng,xkb:us:dvorak:eng,xkb:us:dvp:eng,"
115      "xkb:us:colemak:eng,xkb:us:workman:eng,xkb:us:workman-intl:eng]"},
116     // ------------------ Full Latin setup
117     // French Swiss keyboard.
118     {"fr", "xkb:ch:fr:fra", "fr", "xkb:ch:fr:fra",
119      "xkb:ch:fr:fra,[xkb:fr::fra,xkb:fr:bepo:fra,xkb:be::fra,xkb:ca::fra,"
120      "xkb:ca:multix:fra,xkb:us::eng]"},
121 
122     // German Swiss keyboard.
123     {"de", "xkb:ch::ger", "de", "xkb:ch::ger",
124      "xkb:ch::ger,[xkb:de::ger,xkb:de:neo:ger,xkb:be::ger,xkb:us::eng]"},
125 
126     // WelcomeScreenMultipleLocales
127     {"es,en-US,nl", "xkb:be::nld", "es,en-US,nl", "xkb:be::nld",
128      "xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]"},
129 
130     {"ru,de", "xkb:ru::rus", "ru,de", kUSLayout, "xkb:us::eng"},
131 
132     // ------------------ Regional Locales
133     // Synthetic example to test correct merging of different locales.
134     {"fr-CH,it-CH,de-CH", "xkb:fr::fra,xkb:it::ita,xkb:de::ger",
135      "fr-CH,it-CH,de-CH", "xkb:fr::fra",
136      "xkb:fr::fra,xkb:it::ita,xkb:de::ger,"
137      "[xkb:fr:bepo:fra,xkb:be::fra,xkb:ca::fra,"
138      "xkb:ch:fr:fra,xkb:ca:multix:fra,xkb:us::eng]"},
139 
140     // Another synthetic example. Check that british keyboard is available.
141     {"en-AU", "xkb:us::eng", "en-AU", "xkb:us::eng",
142      "xkb:us::eng,[xkb:gb:extd:eng,xkb:gb:dvorak:eng]"},
143 #else
144     // ------------------ Non-Latin setup
145     // For a non-Latin keyboard layout like Russian, we expect to see the US
146     // keyboard.
147     {"ru", "xkb:ru::rus", "ru", kUSLayout, "xkb:us::eng"},
148     {"ru", "xkb:us::eng,xkb:ru::rus", "ru", kUSLayout, "xkb:us::eng"},
149 
150     // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese
151     // keyboard.
152     {"ja", "xkb:jp::jpn", "ja", "xkb:jp::jpn", "xkb:jp::jpn,[xkb:us::eng]"},
153 
154     // We don't use the Icelandic locale but the Icelandic keyboard layout
155     // should still be selected when specified as the default.
156     {"en-US", "xkb:is::ice", "en-US", "xkb:is::ice",
157      "xkb:is::ice,[xkb:us::eng,xkb:us:intl:eng,xkb:us:altgr-intl:eng,"
158      "xkb:us:dvorak:eng,xkb:us:dvp:eng,xkb:us:colemak:eng,"
159      "xkb:us:workman:eng,xkb:us:workman-intl:eng]"},
160     // ------------------ Full Latin setup
161     // French Swiss keyboard.
162     {"fr", "xkb:ch:fr:fra", "fr", "xkb:ch:fr:fra",
163      "xkb:ch:fr:fra,[xkb:fr::fra,xkb:be::fra,xkb:ca::fra,"
164      "xkb:ca:multix:fra,xkb:us::eng]"},
165 
166     // German Swiss keyboard.
167     {"de", "xkb:ch::ger", "de", "xkb:ch::ger",
168      "xkb:ch::ger,[xkb:de::ger,xkb:de:neo:ger,xkb:be::ger,xkb:us::eng]"},
169 
170     // WelcomeScreenMultipleLocales
171     {"es,en-US,nl", "xkb:be::nld", "es,en-US,nl", "xkb:be::nld",
172      "xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]"},
173 
174     {"ru,de", "xkb:ru::rus", "ru,de", kUSLayout, "xkb:us::eng"},
175 
176     // ------------------ Regional Locales
177     // Synthetic example to test correct merging of different locales.
178     {"fr-CH,it-CH,de-CH", "xkb:fr::fra,xkb:it::ita,xkb:de::ger",
179      "fr-CH,it-CH,de-CH", "xkb:fr::fra",
180      "xkb:fr::fra,xkb:it::ita,xkb:de::ger,[xkb:be::fra,xkb:ca::fra,"
181      "xkb:ch:fr:fra,xkb:ca:multix:fra,xkb:us::eng]"},
182 
183     // Another synthetic example. Check that british keyboard is available.
184     {"en-AU", "xkb:us::eng", "en-AU", "xkb:us::eng",
185      "xkb:us::eng,[xkb:gb:extd:eng,xkb:gb:dvorak:eng]"},
186 #endif
187 };
188 
189 class OobeLocalizationTest
190     : public OobeBaseTest,
191       public testing::WithParamInterface<const LocalizationTestParams*> {
192  public:
193   OobeLocalizationTest();
194 
195   // Verifies that the comma-separated `values` corresponds with the first
196   // values in `select_id`, optionally checking for an options group label after
197   // the first set of options.
198   void VerifyInitialOptions(const char* select_id,
199                             const char* values,
200                             bool check_separator);
201 
202   // Verifies that `value` exists in `select_id`.
203   void VerifyOptionExists(const char* select_id, const char* value);
204 
205   // Dumps OOBE select control (language or keyboard) to string.
206   std::string DumpOptions(const char* select_id);
207 
208  protected:
209   // Runs the test for the given locale and keyboard layout.
210   void RunLocalizationTest();
211 
212  private:
213   system::ScopedFakeStatisticsProvider fake_statistics_provider_;
214 
215   DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest);
216 };
217 
OobeLocalizationTest()218 OobeLocalizationTest::OobeLocalizationTest() : OobeBaseTest() {
219   fake_statistics_provider_.SetMachineStatistic("initial_locale",
220                                                 GetParam()->initial_locale);
221   fake_statistics_provider_.SetMachineStatistic("keyboard_layout",
222                                                 GetParam()->keyboard_layout);
223 }
224 
VerifyInitialOptions(const char * select_id,const char * values,bool check_separator)225 void OobeLocalizationTest::VerifyInitialOptions(const char* select_id,
226                                                 const char* values,
227                                                 bool check_separator) {
228   const std::string select = GetGetSelectStatement(select_id);
229   const std::string expression = base::StringPrintf(
230       "(function () {\n"
231       "  let select = %s;\n"
232       "  if (!select) {\n"
233       "    console.error('Could not find ' + `%s`);\n"
234       "    return false;\n"
235       "  }\n"
236       "  let values = '%s'.split(',');\n"
237       "  let correct = select.selectedIndex == 0;\n"
238       "  if (!correct)\n"
239       "    console.error('Wrong selected index ' + select.selectedIndex);\n"
240       "  for (var i = 0; i < values.length && correct; i++) {\n"
241       "    if (select.options[i].value != values[i]) {\n"
242       "      correct = false;\n"
243       "      console.error('Values mismatch ' + "
244       "                     select.options[i].value + ' ' + values[i]);\n"
245       "    }\n"
246       "  }\n"
247       "  if (%d && correct) {\n"
248       "    correct = select.children[values.length].tagName === 'OPTGROUP';\n"
249       "    if (!correct)\n"
250       "      console.error('Wrong tagname ' + "
251       "                     select.children[values.length].tagName);\n"
252       "  }\n"
253       "  return correct;\n"
254       "})()",
255       select.c_str(), select.c_str(), values, check_separator);
256   test::OobeJS().ExpectTrue(expression);
257 }
258 
VerifyOptionExists(const char * select_id,const char * value)259 void OobeLocalizationTest::VerifyOptionExists(const char* select_id,
260                                               const char* value) {
261   const std::string expression = base::StringPrintf(
262       "(function () {\n"
263       "  var select = %s;\n"
264       "  if (!select)\n"
265       "    return false;\n"
266       "  for (var i = 0; i < select.options.length; i++) {\n"
267       "    if (select.options[i].value == '%s')\n"
268       "      return true;\n"
269       "  }\n"
270       "  return false;\n"
271       "})()",
272       GetGetSelectStatement(select_id).c_str(), value);
273   test::OobeJS().ExpectTrue(expression);
274 }
275 
DumpOptions(const char * select_id)276 std::string OobeLocalizationTest::DumpOptions(const char* select_id) {
277   const std::string expression = base::StringPrintf(
278       "(function () {\n"
279       "  var select = %s;\n"
280       "  var divider = ',';\n"
281       "  if (!select)\n"
282       "    return 'select statement for \"%s\" failed.';\n"
283       "  var dumpOptgroup = function(group) {\n"
284       "    var result = '';\n"
285       "    for (var i = 0; i < group.children.length; i++) {\n"
286       "      if (i > 0) {\n"
287       "        result += divider;\n"
288       "      }\n"
289       "      if (group.children[i].value) {\n"
290       "        result += group.children[i].value;\n"
291       "      } else {\n"
292       "        result += '__NO_VALUE__';\n"
293       "      }\n"
294       "    }\n"
295       "    return result;\n"
296       "  };\n"
297       "  var result = '';\n"
298       "  if (select.selectedIndex != 0) {\n"
299       "    result += '(selectedIndex=' + select.selectedIndex + \n"
300       "        ', selected \"' + select.options[select.selectedIndex].value +\n"
301       "        '\")';\n"
302       "  }\n"
303       "  var children = select.children;\n"
304       "  for (var i = 0; i < children.length; i++) {\n"
305       "    if (i > 0) {\n"
306       "      result += divider;\n"
307       "    }\n"
308       "    if (children[i].value) {\n"
309       "      result += children[i].value;\n"
310       "    } else if (children[i].tagName === 'OPTGROUP') {\n"
311       "      result += '[' + dumpOptgroup(children[i]) + ']';\n"
312       "    } else {\n"
313       "      result += '__NO_VALUE__';\n"
314       "    }\n"
315       "  }\n"
316       "  return result;\n"
317       "})()\n",
318       GetGetSelectStatement(select_id).c_str(), select_id);
319   return test::OobeJS().GetString(expression);
320 }
321 
TranslateXKB2Extension(const std::string & src)322 std::string TranslateXKB2Extension(const std::string& src) {
323   std::string result(src);
324   // Modifies the expected keyboard select control options for the new
325   // extension based xkb id.
326   size_t pos = 0;
327   std::string repl_old = "xkb:";
328   std::string repl_new = extension_ime_util::GetInputMethodIDByEngineID("xkb:");
329   while ((pos = result.find(repl_old, pos)) != std::string::npos) {
330     result.replace(pos, repl_old.length(), repl_new);
331     pos += repl_new.length();
332   }
333   return result;
334 }
335 
RunLocalizationTest()336 void OobeLocalizationTest::RunLocalizationTest() {
337   const std::string initial_locale(GetParam()->initial_locale);
338   const std::string keyboard_layout(GetParam()->keyboard_layout);
339   const std::string expected_locale(GetParam()->expected_locale);
340   const std::string expected_keyboard_layout(
341       GetParam()->expected_keyboard_layout);
342   const std::string expected_keyboard_select_control(
343       GetParam()->expected_keyboard_select_control);
344 
345   const std::string expected_keyboard_select =
346       TranslateXKB2Extension(expected_keyboard_select_control);
347 
348   ASSERT_NO_FATAL_FAILURE(LanguageListWaiter().RunUntilLanguageListReady());
349 
350   const std::string first_language =
351       expected_locale.substr(0, expected_locale.find(','));
352   const std::string get_select_statement =
353       GetGetSelectStatement(kLanguageSelect);
354 
355   ASSERT_NO_FATAL_FAILURE(
356       VerifyInitialOptions(kLanguageSelect, expected_locale.c_str(), true))
357       << "Actual value of " << kLanguageSelect << ":\n"
358       << DumpOptions(kLanguageSelect);
359 
360   ASSERT_NO_FATAL_FAILURE(VerifyInitialOptions(
361       kKeyboardSelect, TranslateXKB2Extension(expected_keyboard_layout).c_str(),
362       false))
363       << "Actual value of " << kKeyboardSelect << ":\n"
364       << DumpOptions(kKeyboardSelect);
365 
366   // Make sure we have a fallback keyboard.
367   ASSERT_NO_FATAL_FAILURE(VerifyOptionExists(
368       kKeyboardSelect,
369       extension_ime_util::GetInputMethodIDByEngineID(kUSLayout).c_str()))
370       << "Actual value of " << kKeyboardSelect << ":\n"
371       << DumpOptions(kKeyboardSelect);
372 
373   // Note, that sort order is locale-specific, but is unlikely to change.
374   // Especially for keyboard layouts.
375   EXPECT_EQ(expected_keyboard_select, DumpOptions(kKeyboardSelect));
376 }
377 
IN_PROC_BROWSER_TEST_P(OobeLocalizationTest,LocalizationTest)378 IN_PROC_BROWSER_TEST_P(OobeLocalizationTest, LocalizationTest) {
379   RunLocalizationTest();
380 }
381 
382 INSTANTIATE_TEST_SUITE_P(
383     All,
384     OobeLocalizationTest,
385     testing::Range(&oobe_localization_test_parameters[0],
386                    &oobe_localization_test_parameters[base::size(
387                        oobe_localization_test_parameters)]));
388 }  // namespace chromeos
389