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 "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" 19 20namespace language_options_ui_test { 21 22namespace { 23 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 27// test accessiblity. 28class LanguageOptionsWebUITest : public InProcessBrowserTest { 29 public: 30 LanguageOptionsWebUITest() {} 31 32 // This method will navigate to the language settings page and show 33 // a subset of languages from the list of available languages. 34 virtual void SetUpOnMainThread() OVERRIDE { 35#if defined(OS_CHROMEOS) 36 auto setting_name = prefs::kLanguagePreferredLanguages; 37#else 38 auto setting_name = prefs::kAcceptLanguages; 39#endif 40 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"); 44 } 45 46 protected: 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( 53 GetActiveFrame(), 54 get_element_id_script, 55 &element_id)); 56 return element_id; 57 } 58 59 content::RenderFrameHost* GetActiveFrame() { 60 return GetActiveWebContents()->GetFocusedFrame(); 61 } 62 63 content::RenderViewHost* GetRenderViewHost() { 64 return GetActiveWebContents()->GetRenderViewHost(); 65 } 66 67 content::WebContents* GetActiveWebContents() { 68 return browser()->tab_strip_model()->GetActiveWebContents(); 69 } 70 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( 75 browser(), 76 key_code, 77 false, 78 false, 79 false, 80 false, 81 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE, 82 content::Source<content::RenderViewHost>(GetRenderViewHost())); 83 } 84 85 private: 86 DISALLOW_COPY_AND_ASSIGN(LanguageOptionsWebUITest); 87}; 88 89} // namespace 90 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. 95IN_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); 99 100 content::RenderFrameHost* active_frame = GetActiveFrame(); 101 102 std::string count_deletable_items_script = 103 "domAutomationController.send(" 104 " document.activeElement.querySelectorAll('.deletable-item').length);"; 105 106 // Count the number of languages in the list. 107 int language_count = 0; 108 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( 109 active_frame, 110 count_deletable_items_script, 111 &language_count)); 112 EXPECT_EQ(3, language_count); 113 114 std::string get_children_of_current_element_script = 115 "domAutomationController.send(document.activeElement.textContent);"; 116 117 // Verify that the correct languages are added to the list. 118 std::string languages; 119 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 120 active_frame, 121 get_children_of_current_element_script, 122 &languages)); 123 EXPECT_EQ("English (United States)SpanishFrench", languages); 124} 125 126// This test will validate that the language webui is accessible through 127// the keyboard. 128// This test must be updated if the tab order of the elements on this page 129// is chagned. 130// flaky: http://crbug.com/405711 131IN_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); 135 136 // Press tab to select the next element. 137 ASSERT_TRUE(PressKey(ui::VKEY_TAB)); 138 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 142 // selected. 143 std::string new_id = GetActiveElementId(); 144 EXPECT_EQ("language-options-add-button", new_id); 145} 146 147} // namespace language_options_ui_test 148 149