summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/oobe_localization_browsertest.cc
blob: cc92f1aae2484891d248281fb0ddc47c7ce68a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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/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/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 {

// OOBE constants.
const char* kLocaleSelect = "language-select";
const char* kKeyboardSelect = "keyboard-select";

const char* kUSLayout = "xkb:us::eng";

}

namespace system {

// Custom StatisticsProvider that will return each set of region settings.
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:
  // StatisticsProvider overrides.
  virtual void StartLoadingMachineStatistics(
      const scoped_refptr<base::TaskRunner>& file_task_runner,
      bool load_oem_manifest) OVERRIDE {
  }

  // Populates the named machine statistic for initial_locale and
  // keyboard_layout only.
  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_;
};

}  // namespace system

class OobeLocalizationTest : public InProcessBrowserTest {
 public:
  OobeLocalizationTest();
  virtual ~OobeLocalizationTest();

  // Verifies that the comma-separated |values| corresponds with the first
  // values in |select_id|, optionally checking for an options group label after
  // the first set of options.
  void VerifyInitialOptions(const char* select_id,
                            const char* values,
                            bool check_separator);

  // Verifies that |value| exists in |select_id|.
  void VerifyOptionExists(const char* select_id, const char* value);

  // Dumps OOBE select control (language or keyboard) to string.
  std::string DumpOptions(const char* select_id);

 protected:
  // Runs the test for the given locale and keyboard layout.
  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());
  // Set the instance returned by GetInstance() for testing.
  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);

  // Initialize StartupCustomizationDocument with fake statistics provider.
  StartupCustomizationDocument::GetInstance()->Init(
      statistics_provider_.get());

  // Bring up the OOBE network screen.
  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,
                       expected_keyboard_layout.c_str(),
                       false);

  // Make sure we have a fallback keyboard.
  VerifyOptionExists(kKeyboardSelect, kUSLayout);

  // Note, that sort order is locale-specific, but is unlikely to change.
  // Especially for keyboard layouts.
  EXPECT_EQ(expected_keyboard_select_control, DumpOptions(kKeyboardSelect));

  // Shut down the display host.
  chromeos::LoginDisplayHostImpl::default_host()->Finalize();
  base::MessageLoopForUI::current()->RunUntilIdle();

  // Clear the locale pref so the statistics provider is pinged next time.
  g_browser_process->local_state()->SetString(prefs::kApplicationLocale,
                                              std::string());
}

IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) {
  // For a non-Latin keyboard layout like Russian, we expect to see the US
  // keyboard.
  RunLocalizationTest("ru", "xkb:ru::rus",
                      "ru", kUSLayout,
                      "xkb:us::eng");

  RunLocalizationTest("ru", "xkb:us::eng,xkb:ru::rus",
                      "ru", kUSLayout,
                      "xkb:us::eng");

  // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese
  // keyboard.
  RunLocalizationTest("ja", "xkb:jp::jpn",
                      "ja", "xkb:jp::jpn",
                      "xkb:jp::jpn,[xkb:us::eng]");
}

IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) {
  // We don't use the Icelandic locale but the Icelandic keyboard layout
  // should still be selected when specified as the default.
  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) {
  // French Swiss keyboard.
  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]");

  // German Swiss keyboard.
  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");
}

}  // namespace chromeos