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.
5 #include "base/prefs/pref_service.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/ui/browser_window.h"
8 #include "chrome/browser/ui/chrome_pages.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/interactive_test_utils.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "ui/base/l10n/l10n_util.h"
20 namespace language_options_ui_test
{
24 // This class will test the language options settings.
25 // This test is part of the interactive_ui_tests isntead of browser_tests
26 // because it is necessary to emulate pushing a button in order to properly
28 class LanguageOptionsWebUITest
: public InProcessBrowserTest
{
30 LanguageOptionsWebUITest() {}
32 // This method will navigate to the language settings page and show
33 // a subset of languages from the list of available languages.
34 void SetUpOnMainThread() override
{
35 #if defined(OS_CHROMEOS)
36 auto setting_name
= prefs::kLanguagePreferredLanguages
;
38 auto setting_name
= prefs::kAcceptLanguages
;
41 const GURL url
= chrome::GetSettingsUrl(chrome::kLanguageOptionsSubPage
);
42 ui_test_utils::NavigateToURL(browser(), url
);
43 browser()->profile()->GetPrefs()->SetString(setting_name
, "en-US,es,fr");
47 // Will get the id of the element in the UI that has focus.
48 std::string
GetActiveElementId() {
49 std::string get_element_id_script
=
50 "domAutomationController.send(document.activeElement.id);";
51 std::string element_id
;
52 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
54 get_element_id_script
,
59 content::RenderFrameHost
* GetActiveFrame() {
60 return GetActiveWebContents()->GetFocusedFrame();
63 content::RenderViewHost
* GetRenderViewHost() {
64 return GetActiveWebContents()->GetRenderViewHost();
67 content::WebContents
* GetActiveWebContents() {
68 return browser()->tab_strip_model()->GetActiveWebContents();
71 // Press and release a key in the browser. This will wait for the element on
72 // the page to change.
73 bool PressKey(ui::KeyboardCode key_code
) {
74 return ui_test_utils::SendKeyPressAndWait(
81 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE
,
82 content::Source
<content::RenderViewHost
>(GetRenderViewHost()));
86 DISALLOW_COPY_AND_ASSIGN(LanguageOptionsWebUITest
);
91 // This test will verify that the appropriate languages are available.
92 // This test will also fail if the language page is not loaded because a random
93 // page will not have the language list.
94 // Test assumes that the default active element is the list of languages.
95 IN_PROC_BROWSER_TEST_F(LanguageOptionsWebUITest
, TestAvailableLanguages
) {
96 // Verify that the language list is focused by default.
97 std::string original_id
= GetActiveElementId();
98 EXPECT_EQ("language-options-list", original_id
);
100 content::RenderFrameHost
* active_frame
= GetActiveFrame();
102 std::string count_deletable_items_script
=
103 "domAutomationController.send("
104 " document.activeElement.querySelectorAll('.deletable-item').length);";
106 // Count the number of languages in the list.
107 int language_count
= 0;
108 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
110 count_deletable_items_script
,
112 EXPECT_EQ(3, language_count
);
114 std::string get_children_of_current_element_script
=
115 "domAutomationController.send(document.activeElement.textContent);";
117 // Verify that the correct languages are added to the list.
118 std::string languages
;
119 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
121 get_children_of_current_element_script
,
123 EXPECT_EQ("English (United States)SpanishFrench", languages
);
126 // This test will validate that the language webui is accessible through
128 // This test must be updated if the tab order of the elements on this page
130 // flaky: http://crbug.com/405711
131 IN_PROC_BROWSER_TEST_F(LanguageOptionsWebUITest
, TestListTabAccessibility
) {
132 // Verify that the language list is focused by default.
133 std::string original_id
= GetActiveElementId();
134 EXPECT_EQ("language-options-list", original_id
);
136 // Press tab to select the next element.
137 ASSERT_TRUE(PressKey(ui::VKEY_TAB
));
139 // Make sure that the element is now the button that is next in the tab order.
140 // Checking that the list is no longer selected is not sufficient to validate
141 // this use case because this test should fail if an item inside the list is
143 std::string new_id
= GetActiveElementId();
144 EXPECT_EQ("language-options-add-button", new_id
);
147 } // namespace language_options_ui_test