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/files/file_path.h"
6 #include "base/location.h"
7 #include "base/path_service.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/accessibility/ax_tree_id_registry.h"
12 #include "chrome/browser/extensions/api/automation_internal/automation_event_router.h"
13 #include "chrome/browser/extensions/chrome_extension_function.h"
14 #include "chrome/browser/extensions/extension_apitest.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/extensions/api/automation_internal.h"
19 #include "chrome/common/extensions/chrome_extension_messages.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/ax_event_notification_details.h"
22 #include "content/public/browser/render_widget_host.h"
23 #include "content/public/browser/render_widget_host_view.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/test/extension_test_message_listener.h"
26 #include "net/dns/mock_host_resolver.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/accessibility/ax_node.h"
30 #include "ui/accessibility/ax_serializable_tree.h"
31 #include "ui/accessibility/ax_tree.h"
32 #include "ui/accessibility/ax_tree_serializer.h"
33 #include "ui/accessibility/tree_generator.h"
35 #if defined(OS_CHROMEOS)
36 #include "ash/accelerators/accelerator_controller.h"
37 #include "ash/shell.h"
38 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
41 namespace extensions
{
44 static const char kDomain
[] = "a.com";
45 static const char kSitesDir
[] = "automation/sites";
46 static const char kGotTree
[] = "got_tree";
47 } // anonymous namespace
49 class AutomationApiTest
: public ExtensionApiTest
{
51 GURL
GetURLForPath(const std::string
& host
, const std::string
& path
) {
52 std::string port
= base::IntToString(embedded_test_server()->port());
53 GURL::Replacements replacements
;
54 replacements
.SetHostStr(host
);
55 replacements
.SetPortStr(port
);
57 embedded_test_server()->GetURL(path
).ReplaceComponents(replacements
);
61 void StartEmbeddedTestServer() {
62 base::FilePath test_data
;
63 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data
));
64 embedded_test_server()->ServeFilesFromDirectory(
65 test_data
.AppendASCII("extensions/api_test")
66 .AppendASCII(kSitesDir
));
67 ASSERT_TRUE(ExtensionApiTest::StartEmbeddedTestServer());
68 host_resolver()->AddRule("*", embedded_test_server()->base_url().host());
72 void SetUpInProcessBrowserTestFixture() override
{
73 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
77 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TestRendererAccessibilityEnabled
) {
78 StartEmbeddedTestServer();
79 const GURL url
= GetURLForPath(kDomain
, "/index.html");
80 ui_test_utils::NavigateToURL(browser(), url
);
82 ASSERT_EQ(1, browser()->tab_strip_model()->count());
83 content::WebContents
* const tab
=
84 browser()->tab_strip_model()->GetWebContentsAt(0);
85 ASSERT_FALSE(tab
->IsFullAccessibilityModeForTesting());
86 ASSERT_FALSE(tab
->IsTreeOnlyAccessibilityModeForTesting());
88 base::FilePath extension_path
=
89 test_data_dir_
.AppendASCII("automation/tests/basic");
90 ExtensionTestMessageListener
got_tree(kGotTree
, false /* no reply */);
91 LoadExtension(extension_path
);
92 ASSERT_TRUE(got_tree
.WaitUntilSatisfied());
94 ASSERT_FALSE(tab
->IsFullAccessibilityModeForTesting());
95 ASSERT_TRUE(tab
->IsTreeOnlyAccessibilityModeForTesting());
98 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, SanityCheck
) {
99 StartEmbeddedTestServer();
100 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "sanity_check.html"))
104 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, GetTreeByTabId
) {
105 StartEmbeddedTestServer();
106 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "tab_id.html"))
110 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Events
) {
111 StartEmbeddedTestServer();
112 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "events.html"))
116 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Actions
) {
117 StartEmbeddedTestServer();
118 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "actions.html"))
122 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Location
) {
123 StartEmbeddedTestServer();
124 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "location.html"))
128 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationBooleanPermissions
) {
129 StartEmbeddedTestServer();
130 ASSERT_TRUE(RunExtensionSubtest(
131 "automation/tests/tabs_automation_boolean", "permissions.html"))
135 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationBooleanActions
) {
136 StartEmbeddedTestServer();
137 ASSERT_TRUE(RunExtensionSubtest(
138 "automation/tests/tabs_automation_boolean", "actions.html"))
142 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationHostsPermissions
) {
143 StartEmbeddedTestServer();
144 ASSERT_TRUE(RunExtensionSubtest(
145 "automation/tests/tabs_automation_hosts", "permissions.html"))
149 #if defined(USE_AURA)
150 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Desktop
) {
151 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "desktop.html"))
155 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopNotRequested
) {
156 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs",
157 "desktop_not_requested.html")) << message_
;
160 #if defined(OS_CHROMEOS)
161 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopActions
) {
162 AutomationManagerAura::GetInstance()->Enable(browser()->profile());
163 // Trigger the shelf subtree to be computed.
164 ash::Shell::GetInstance()->accelerator_controller()->PerformActionIfEnabled(
167 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "actions.html"))
171 // Flaky, see http://crbug.com/435449
172 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DISABLED_DesktopLoadTabs
) {
173 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "load_tabs.html"))
176 #endif // defined(OS_CHROMEOS)
177 #else // !defined(USE_AURA)
178 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopNotSupported
) {
179 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop",
180 "desktop_not_supported.html"))
183 #endif // defined(USE_AURA)
185 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, CloseTab
) {
186 StartEmbeddedTestServer();
187 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "close_tab.html"))
191 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, QuerySelector
) {
192 StartEmbeddedTestServer();
194 RunExtensionSubtest("automation/tests/tabs", "queryselector.html"))
198 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Find
) {
199 StartEmbeddedTestServer();
200 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "find.html"))
204 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Attributes
) {
205 StartEmbeddedTestServer();
206 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "attributes.html"))
210 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TreeChange
) {
211 StartEmbeddedTestServer();
212 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "tree_change.html"))
216 } // namespace extensions