Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / automation / automation_apitest.cc
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/files/file_path.h"
6 #include "base/path_service.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_test_message_listener.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_widget_host.h"
15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace extensions {
22
23 namespace {
24 static const char kDomain[] = "a.com";
25 static const char kSitesDir[] = "automation/sites";
26 static const char kGotTree[] = "got_tree";
27 }  // anonymous namespace
28
29 class AutomationApiTest : public ExtensionApiTest {
30  protected:
31   GURL GetURLForPath(const std::string& host, const std::string& path) {
32     std::string port = base::IntToString(embedded_test_server()->port());
33     GURL::Replacements replacements;
34     replacements.SetHostStr(host);
35     replacements.SetPortStr(port);
36     GURL url =
37         embedded_test_server()->GetURL(path).ReplaceComponents(replacements);
38     return url;
39   }
40
41   void StartEmbeddedTestServer() {
42     base::FilePath test_data;
43     ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data));
44     embedded_test_server()->ServeFilesFromDirectory(
45         test_data.AppendASCII("extensions/api_test")
46         .AppendASCII(kSitesDir));
47     ASSERT_TRUE(ExtensionApiTest::StartEmbeddedTestServer());
48     host_resolver()->AddRule("*", embedded_test_server()->base_url().host());
49   }
50
51   void LoadPage() {
52     StartEmbeddedTestServer();
53     const GURL url = GetURLForPath(kDomain, "/index.html");
54     ui_test_utils::NavigateToURL(browser(), url);
55   }
56
57  public:
58   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
59     ExtensionApiTest::SetUpCommandLine(command_line);
60     command_line->AppendSwitch(::switches::kEnableAutomationAPI);
61   }
62
63   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
64     ExtensionApiTest::SetUpInProcessBrowserTestFixture();
65   }
66 };
67
68 IN_PROC_BROWSER_TEST_F(AutomationApiTest, TestRendererAccessibilityEnabled) {
69   LoadPage();
70
71   ASSERT_EQ(1, browser()->tab_strip_model()->count());
72   content::WebContents* const tab =
73       browser()->tab_strip_model()->GetWebContentsAt(0);
74   content::RenderWidgetHost* rwh =
75       tab->GetRenderWidgetHostView()->GetRenderWidgetHost();
76   ASSERT_NE((content::RenderWidgetHost*)NULL, rwh)
77       << "Couldn't get RenderWidgetHost";
78   ASSERT_FALSE(rwh->IsFullAccessibilityModeForTesting());
79   ASSERT_FALSE(rwh->IsTreeOnlyAccessibilityModeForTesting());
80
81   base::FilePath extension_path =
82       test_data_dir_.AppendASCII("automation/basic");
83   ExtensionTestMessageListener got_tree(kGotTree, false /* no reply */);
84   LoadExtension(extension_path);
85   ASSERT_TRUE(got_tree.WaitUntilSatisfied());
86
87   rwh = tab->GetRenderWidgetHostView()->GetRenderWidgetHost();
88   ASSERT_NE((content::RenderWidgetHost*)NULL, rwh)
89       << "Couldn't get RenderWidgetHost";
90   ASSERT_FALSE(rwh->IsFullAccessibilityModeForTesting());
91   ASSERT_TRUE(rwh->IsTreeOnlyAccessibilityModeForTesting());
92 }
93
94 // TODO(dtseng): See crbug.com/360297.
95 #if defined(OS_MACOSX)
96 #define MAYBE_SanityCheck DISABLED_SanityCheck
97 #else
98 #define MAYBE_SanityCheck SanityCheck
99 #endif  // defined(OS_MACOSX)
100 IN_PROC_BROWSER_TEST_F(AutomationApiTest, MAYBE_SanityCheck) {
101   StartEmbeddedTestServer();
102   ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "sanity_check.html"))
103       << message_;
104 }
105
106 IN_PROC_BROWSER_TEST_F(AutomationApiTest, Events) {
107   LoadPage();
108   ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "events.html"))
109       << message_;
110 }
111
112 IN_PROC_BROWSER_TEST_F(AutomationApiTest, Actions) {
113   LoadPage();
114   ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "actions.html"))
115       << message_;
116 }
117
118 IN_PROC_BROWSER_TEST_F(AutomationApiTest, Location) {
119   LoadPage();
120   ASSERT_TRUE(RunExtensionSubtest("automation/tests/tabs", "location.html"))
121       << message_;
122 }
123
124 #if defined(OS_CHROMEOS)
125 IN_PROC_BROWSER_TEST_F(AutomationApiTest, Desktop) {
126   ASSERT_TRUE(RunExtensionSubtest("automation/tests/desktop", "desktop.html"))
127       << message_;
128 }
129 #endif
130
131 }  // namespace extensions