Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / webui_webview_browsertest.cc
blob7ce1daadc0ed99b180e00b8100d17d7c75538349
1 // Copyright 2015 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/macros.h"
6 #include "base/path_service.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/common/chrome_paths.h"
10 #include "chrome/common/url_constants.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "chrome/test/base/web_ui_browser_test.h"
13 #include "net/test/embedded_test_server/embedded_test_server.h"
15 class WebUIWebViewBrowserTest : public WebUIBrowserTest {
16 public:
17 WebUIWebViewBrowserTest() {}
19 void SetUpOnMainThread() override {
20 WebUIBrowserTest::SetUpOnMainThread();
21 AddLibrary(
22 base::FilePath(FILE_PATH_LITERAL("webview_content_script_test.js")));
23 AddLibrary(
24 base::FilePath(FILE_PATH_LITERAL("webview_basic.js")));
26 base::FilePath test_data_dir;
27 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
28 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
29 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
32 GURL GetTestUrl(const std::string& path) const {
33 return embedded_test_server()->base_url().Resolve(path);
36 GURL GetWebViewEnabledWebUIURL() const {
37 return GURL(chrome::kChromeUIChromeSigninURL);
40 private:
41 DISALLOW_COPY_AND_ASSIGN(WebUIWebViewBrowserTest);
44 // Checks that hiding and showing the WebUI host page doesn't break guests in
45 // it.
46 // Regression test for http://crbug.com/515268
47 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, DisplayNone) {
48 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
50 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
51 "testDisplayNone",
52 new base::StringValue(GetTestUrl("empty.html").spec())));
55 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, ExecuteScriptCode) {
56 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
58 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
59 "testExecuteScriptCode",
60 new base::StringValue(GetTestUrl("empty.html").spec())));
63 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, ExecuteScriptCodeFromFile) {
64 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
66 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
67 "testExecuteScriptCodeFromFile",
68 new base::StringValue(GetTestUrl("empty.html").spec())));
71 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, AddContentScript) {
72 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
74 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
75 "testAddContentScript",
76 new base::StringValue(GetTestUrl("empty.html").spec())));
79 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, AddMultiContentScripts) {
80 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
82 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
83 "testAddMultiContentScripts",
84 new base::StringValue(GetTestUrl("empty.html").spec())));
87 IN_PROC_BROWSER_TEST_F(
88 WebUIWebViewBrowserTest,
89 AddContentScriptWithSameNameShouldOverwriteTheExistingOne) {
90 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
92 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
93 "testAddContentScriptWithSameNameShouldOverwriteTheExistingOne",
94 new base::StringValue(GetTestUrl("empty.html").spec())));
97 IN_PROC_BROWSER_TEST_F(
98 WebUIWebViewBrowserTest,
99 AddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView) {
100 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
102 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
103 "testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView",
104 new base::StringValue(GetTestUrl("empty.html").spec())));
107 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, AddAndRemoveContentScripts) {
108 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
110 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
111 "testAddAndRemoveContentScripts",
112 new base::StringValue(GetTestUrl("empty.html").spec())));
115 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest,
116 AddContentScriptsWithNewWindowAPI) {
117 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
119 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
120 "testAddContentScriptsWithNewWindowAPI",
121 new base::StringValue(GetTestUrl("guest_from_opener.html").spec())));
124 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest,
125 ContentScriptIsInjectedAfterTerminateAndReloadWebView) {
126 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
128 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
129 "testContentScriptIsInjectedAfterTerminateAndReloadWebView",
130 new base::StringValue(GetTestUrl("empty.html").spec())));
133 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest,
134 ContentScriptExistsAsLongAsWebViewTagExists) {
135 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
137 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
138 "testContentScriptExistsAsLongAsWebViewTagExists",
139 new base::StringValue(GetTestUrl("empty.html").spec())));
142 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, AddContentScriptWithCode) {
143 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
145 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
146 "testAddContentScriptWithCode",
147 new base::StringValue(GetTestUrl("empty.html").spec())));
150 #if defined(OS_CHROMEOS)
151 // Right now we only have incognito WebUI on CrOS, but this should
152 // theoretically work for all platforms.
153 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest, AddContentScriptIncognito) {
154 Browser* incognito_browser =
155 OpenURLOffTheRecord(browser()->profile(), GetWebViewEnabledWebUIURL());
157 SetWebUIInstance(
158 incognito_browser->tab_strip_model()->GetActiveWebContents()->GetWebUI());
160 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
161 "testAddContentScript",
162 new base::StringValue(GetTestUrl("empty.html").spec())));
164 #endif