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
{
17 WebUIWebViewBrowserTest() {}
19 void SetUpOnMainThread() override
{
20 WebUIBrowserTest::SetUpOnMainThread();
22 base::FilePath(FILE_PATH_LITERAL("webview_content_script_test.js")));
24 base::FilePath test_data_dir
;
25 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
26 embedded_test_server()->ServeFilesFromDirectory(test_data_dir
);
27 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
30 GURL
GetTestUrl(const std::string
& path
) const {
31 return embedded_test_server()->base_url().Resolve(path
);
34 GURL
GetWebViewEnabledWebUIURL() const {
35 return GURL(chrome::kChromeUIChromeSigninURL
);
39 DISALLOW_COPY_AND_ASSIGN(WebUIWebViewBrowserTest
);
42 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, ExecuteScriptCode
) {
43 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
45 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
46 "testExecuteScriptCode",
47 new base::StringValue(GetTestUrl("empty.html").spec())));
50 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, ExecuteScriptCodeFromFile
) {
51 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
53 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
54 "testExecuteScriptCodeFromFile",
55 new base::StringValue(GetTestUrl("empty.html").spec())));
58 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, AddContentScript
) {
59 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
61 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
62 "testAddContentScript",
63 new base::StringValue(GetTestUrl("empty.html").spec())));
66 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, AddMultiContentScripts
) {
67 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
69 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
70 "testAddMultiContentScripts",
71 new base::StringValue(GetTestUrl("empty.html").spec())));
74 IN_PROC_BROWSER_TEST_F(
75 WebUIWebViewBrowserTest
,
76 AddContentScriptWithSameNameShouldOverwriteTheExistingOne
) {
77 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
79 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
80 "testAddContentScriptWithSameNameShouldOverwriteTheExistingOne",
81 new base::StringValue(GetTestUrl("empty.html").spec())));
84 IN_PROC_BROWSER_TEST_F(
85 WebUIWebViewBrowserTest
,
86 AddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView
) {
87 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
89 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
90 "testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView",
91 new base::StringValue(GetTestUrl("empty.html").spec())));
94 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, AddAndRemoveContentScripts
) {
95 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
97 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
98 "testAddAndRemoveContentScripts",
99 new base::StringValue(GetTestUrl("empty.html").spec())));
102 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
,
103 AddContentScriptsWithNewWindowAPI
) {
104 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
106 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
107 "testAddContentScriptsWithNewWindowAPI",
108 new base::StringValue(GetTestUrl("guest_from_opener.html").spec())));
111 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
,
112 ContentScriptIsInjectedAfterTerminateAndReloadWebView
) {
113 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
115 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
116 "testContentScriptIsInjectedAfterTerminateAndReloadWebView",
117 new base::StringValue(GetTestUrl("empty.html").spec())));
120 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
,
121 ContentScriptExistsAsLongAsWebViewTagExists
) {
122 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
124 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
125 "testContentScriptExistsAsLongAsWebViewTagExists",
126 new base::StringValue(GetTestUrl("empty.html").spec())));
129 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, AddContentScriptWithCode
) {
130 ui_test_utils::NavigateToURL(browser(), GetWebViewEnabledWebUIURL());
132 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
133 "testAddContentScriptWithCode",
134 new base::StringValue(GetTestUrl("empty.html").spec())));
137 #if defined(OS_CHROMEOS)
138 // Right now we only have incognito WebUI on CrOS, but this should
139 // theoretically work for all platforms.
140 IN_PROC_BROWSER_TEST_F(WebUIWebViewBrowserTest
, AddContentScriptIncognito
) {
141 Browser
* incognito_browser
= ui_test_utils::OpenURLOffTheRecord(
142 browser()->profile(), GetWebViewEnabledWebUIURL());
145 incognito_browser
->tab_strip_model()->GetActiveWebContents()->GetWebUI());
147 ASSERT_TRUE(WebUIBrowserTest::RunJavascriptAsyncTest(
148 "testAddContentScript",
149 new base::StringValue(GetTestUrl("empty.html").spec())));