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/extensions/api/automation_internal/automation_event_router.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/api/automation_internal.h"
18 #include "chrome/common/extensions/chrome_extension_messages.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/ax_event_notification_details.h"
21 #include "content/public/browser/render_widget_host.h"
22 #include "content/public/browser/render_widget_host_view.h"
23 #include "content/public/browser/web_contents.h"
24 #include "extensions/test/extension_test_message_listener.h"
25 #include "net/dns/mock_host_resolver.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/accessibility/ax_node.h"
29 #include "ui/accessibility/ax_serializable_tree.h"
30 #include "ui/accessibility/ax_tree.h"
31 #include "ui/accessibility/ax_tree_serializer.h"
32 #include "ui/accessibility/tree_generator.h"
34 #if defined(OS_CHROMEOS)
35 #include "ash/accelerators/accelerator_controller.h"
36 #include "ash/shell.h"
37 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
40 namespace extensions
{
43 static const char kDomain
[] = "a.com";
44 static const char kSitesDir
[] = "automation/sites";
45 static const char kGotTree
[] = "got_tree";
46 } // anonymous namespace
48 class AutomationApiTest
: public ExtensionApiTest
{
50 GURL
GetURLForPath(const std::string
& host
, const std::string
& path
) {
51 std::string port
= base::IntToString(embedded_test_server()->port());
52 GURL::Replacements replacements
;
53 replacements
.SetHostStr(host
);
54 replacements
.SetPortStr(port
);
56 embedded_test_server()->GetURL(path
).ReplaceComponents(replacements
);
60 void StartEmbeddedTestServer() {
61 base::FilePath test_data
;
62 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data
));
63 embedded_test_server()->ServeFilesFromDirectory(
64 test_data
.AppendASCII("extensions/api_test")
65 .AppendASCII(kSitesDir
));
66 ASSERT_TRUE(ExtensionApiTest::StartEmbeddedTestServer());
67 host_resolver()->AddRule("*", embedded_test_server()->base_url().host());
71 void SetUpInProcessBrowserTestFixture() override
{
72 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
76 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TestRendererAccessibilityEnabled
) {
77 StartEmbeddedTestServer();
78 const GURL url
= GetURLForPath(kDomain
, "/index.html");
79 ui_test_utils::NavigateToURL(browser(), url
);
81 ASSERT_EQ(1, browser()->tab_strip_model()->count());
82 content::WebContents
* const tab
=
83 browser()->tab_strip_model()->GetWebContentsAt(0);
84 ASSERT_FALSE(tab
->IsFullAccessibilityModeForTesting());
85 ASSERT_FALSE(tab
->IsTreeOnlyAccessibilityModeForTesting());
87 base::FilePath extension_path
=
88 test_data_dir_
.AppendASCII("automation/tests/basic");
89 ExtensionTestMessageListener
got_tree(kGotTree
, false /* no reply */);
90 LoadExtension(extension_path
);
91 ASSERT_TRUE(got_tree
.WaitUntilSatisfied());
93 ASSERT_FALSE(tab
->IsFullAccessibilityModeForTesting());
94 ASSERT_TRUE(tab
->IsTreeOnlyAccessibilityModeForTesting());
97 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, SanityCheck
) {
98 StartEmbeddedTestServer();
99 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "sanity_check.html"))
103 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, GetTreeByTabId
) {
104 StartEmbeddedTestServer();
105 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "tab_id.html"))
109 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Events
) {
110 StartEmbeddedTestServer();
111 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "events.html"))
115 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Actions
) {
116 StartEmbeddedTestServer();
117 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "actions.html"))
121 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Location
) {
122 StartEmbeddedTestServer();
123 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "location.html"))
127 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationBooleanPermissions
) {
128 StartEmbeddedTestServer();
129 ASSERT_TRUE(RunExtensionSubtest(
130 "automation/tests/tabs_automation_boolean", "permissions.html"))
134 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationBooleanActions
) {
135 StartEmbeddedTestServer();
136 ASSERT_TRUE(RunExtensionSubtest(
137 "automation/tests/tabs_automation_boolean", "actions.html"))
141 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TabsAutomationHostsPermissions
) {
142 StartEmbeddedTestServer();
143 ASSERT_TRUE(RunExtensionSubtest(
144 "automation/tests/tabs_automation_hosts", "permissions.html"))
148 #if defined(USE_AURA)
149 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Desktop
) {
150 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "desktop.html"))
154 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopNotRequested
) {
155 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs",
156 "desktop_not_requested.html")) << message_
;
159 #if defined(OS_CHROMEOS)
160 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopActions
) {
161 AutomationManagerAura::GetInstance()->Enable(browser()->profile());
162 // Trigger the shelf subtree to be computed.
163 ash::Shell::GetInstance()->accelerator_controller()->PerformActionIfEnabled(
166 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "actions.html"))
170 // Flaky, see http://crbug.com/435449
171 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DISABLED_DesktopLoadTabs
) {
172 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "load_tabs.html"))
175 #endif // defined(OS_CHROMEOS)
176 #else // !defined(USE_AURA)
177 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, DesktopNotSupported
) {
178 ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop",
179 "desktop_not_supported.html"))
182 #endif // defined(USE_AURA)
184 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, CloseTab
) {
185 StartEmbeddedTestServer();
186 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "close_tab.html"))
190 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, QuerySelector
) {
191 StartEmbeddedTestServer();
193 RunExtensionSubtest("automation/tests/tabs", "queryselector.html"))
197 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Find
) {
198 StartEmbeddedTestServer();
199 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "find.html"))
203 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, Attributes
) {
204 StartEmbeddedTestServer();
205 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "attributes.html"))
209 IN_PROC_BROWSER_TEST_F(AutomationApiTest
, TreeChange
) {
210 StartEmbeddedTestServer();
211 ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "tree_change.html"))
215 } // namespace extensions