[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / shared_worker / worker_browsertest.cc
blob0a9c34cd8d9e2d068b0fc700c5ba8089e4cb4534
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/bind.h"
6 #include "base/files/file_path.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/sys_info.h"
12 #include "base/test/test_timeouts.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/client_certificate_delegate.h"
15 #include "content/public/common/content_paths.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "content/public/test/content_browser_test.h"
18 #include "content/public/test/content_browser_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "content/shell/browser/shell.h"
21 #include "content/shell/browser/shell_content_browser_client.h"
22 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
23 #include "net/base/escape.h"
24 #include "net/base/test_data_directory.h"
25 #include "net/test/spawned_test_server/spawned_test_server.h"
26 #include "url/gurl.h"
28 namespace content {
30 namespace {
32 bool SupportsSharedWorker() {
33 #if defined(OS_ANDROID)
34 // SharedWorkers are not enabled on Android. https://crbug.com/154571
36 // TODO(davidben): Move other SharedWorker exclusions from
37 // build/android/pylib/gtest/filter/ inline.
38 return false;
39 #else
40 return true;
41 #endif
44 } // namespace
46 class WorkerTest : public ContentBrowserTest {
47 public:
48 WorkerTest() : select_certificate_count_(0) {}
50 void SetUpOnMainThread() override {
51 ShellContentBrowserClient::Get()->set_select_client_certificate_callback(
52 base::Bind(&WorkerTest::OnSelectClientCertificate,
53 base::Unretained(this)));
56 void TearDownOnMainThread() override {
57 ShellContentBrowserClient::Get()->set_select_client_certificate_callback(
58 base::Closure());
61 int select_certificate_count() const { return select_certificate_count_; }
63 GURL GetTestURL(const std::string& test_case, const std::string& query) {
64 base::FilePath test_file_path = GetTestFilePath(
65 "workers", test_case.c_str());
66 return GetFileUrlWithQuery(test_file_path, query);
69 void RunTest(Shell* window,
70 const std::string& test_case,
71 const std::string& query) {
72 GURL url = GetTestURL(test_case, query);
73 const base::string16 expected_title = base::ASCIIToUTF16("OK");
74 TitleWatcher title_watcher(window->web_contents(), expected_title);
75 NavigateToURL(window, url);
76 base::string16 final_title = title_watcher.WaitAndGetTitle();
77 EXPECT_EQ(expected_title, final_title);
80 void RunTest(const std::string& test_case, const std::string& query) {
81 RunTest(shell(), test_case, query);
84 static void QuitUIMessageLoop(base::Callback<void()> callback) {
85 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
88 void NavigateAndWaitForAuth(const GURL& url) {
89 ShellContentBrowserClient* browser_client =
90 ShellContentBrowserClient::Get();
91 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
92 browser_client->resource_dispatcher_host_delegate()->
93 set_login_request_callback(
94 base::Bind(&QuitUIMessageLoop, runner->QuitClosure()));
95 shell()->LoadURL(url);
96 runner->Run();
99 private:
100 void OnSelectClientCertificate() { select_certificate_count_++; }
102 int select_certificate_count_;
105 IN_PROC_BROWSER_TEST_F(WorkerTest, SingleWorker) {
106 RunTest("single_worker.html", std::string());
109 IN_PROC_BROWSER_TEST_F(WorkerTest, MultipleWorkers) {
110 RunTest("multi_worker.html", std::string());
113 IN_PROC_BROWSER_TEST_F(WorkerTest, SingleSharedWorker) {
114 if (!SupportsSharedWorker())
115 return;
117 RunTest("single_worker.html", "shared=true");
120 // http://crbug.com/96435
121 IN_PROC_BROWSER_TEST_F(WorkerTest, MultipleSharedWorkers) {
122 if (!SupportsSharedWorker())
123 return;
125 RunTest("multi_worker.html", "shared=true");
128 // Incognito windows should not share workers with non-incognito windows
129 // http://crbug.com/30021
130 IN_PROC_BROWSER_TEST_F(WorkerTest, IncognitoSharedWorkers) {
131 if (!SupportsSharedWorker())
132 return;
134 // Load a non-incognito tab and have it create a shared worker
135 RunTest("incognito_worker.html", std::string());
137 // Incognito worker should not share with non-incognito
138 RunTest(CreateOffTheRecordBrowser(), "incognito_worker.html", std::string());
141 // Make sure that auth dialog is displayed from worker context.
142 // http://crbug.com/33344
143 IN_PROC_BROWSER_TEST_F(WorkerTest, WorkerHttpAuth) {
144 ASSERT_TRUE(test_server()->Start());
145 GURL url = test_server()->GetURL("files/workers/worker_auth.html");
147 NavigateAndWaitForAuth(url);
150 // Make sure that HTTP auth dialog is displayed from shared worker context.
151 // http://crbug.com/33344
153 // TODO(davidben): HTTP auth dialogs are no longer displayed on shared workers,
154 // but this test only tests that the delegate is called. Move handling the
155 // WebContentsless case from chrome/ to content/ and adjust the test
156 // accordingly.
157 IN_PROC_BROWSER_TEST_F(WorkerTest, SharedWorkerHttpAuth) {
158 if (!SupportsSharedWorker())
159 return;
161 ASSERT_TRUE(test_server()->Start());
162 GURL url = test_server()->GetURL("files/workers/shared_worker_auth.html");
163 NavigateAndWaitForAuth(url);
166 // Tests that TLS client auth prompts for normal workers.
167 IN_PROC_BROWSER_TEST_F(WorkerTest, WorkerTlsClientAuth) {
168 // Launch HTTPS server.
169 net::SpawnedTestServer::SSLOptions ssl_options;
170 ssl_options.request_client_certificate = true;
171 net::SpawnedTestServer https_server(
172 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
173 base::FilePath(FILE_PATH_LITERAL("content/test/data")));
174 ASSERT_TRUE(https_server.Start());
176 RunTest("worker_tls_client_auth.html",
177 "url=" +
178 net::EscapeQueryParamValue(https_server.GetURL("").spec(), true));
179 EXPECT_EQ(1, select_certificate_count());
182 // Tests that TLS client auth does not prompt for a shared worker; shared
183 // workers are not associated with a WebContents.
184 IN_PROC_BROWSER_TEST_F(WorkerTest, SharedWorkerTlsClientAuth) {
185 if (!SupportsSharedWorker())
186 return;
188 // Launch HTTPS server.
189 net::SpawnedTestServer::SSLOptions ssl_options;
190 ssl_options.request_client_certificate = true;
191 net::SpawnedTestServer https_server(
192 net::SpawnedTestServer::TYPE_HTTPS, ssl_options,
193 base::FilePath(FILE_PATH_LITERAL("content/test/data")));
194 ASSERT_TRUE(https_server.Start());
196 RunTest("worker_tls_client_auth.html",
197 "shared=true&url=" +
198 net::EscapeQueryParamValue(https_server.GetURL("").spec(), true));
199 EXPECT_EQ(0, select_certificate_count());
202 IN_PROC_BROWSER_TEST_F(WorkerTest, WebSocketSharedWorker) {
203 if (!SupportsSharedWorker())
204 return;
206 // Launch WebSocket server.
207 net::SpawnedTestServer ws_server(net::SpawnedTestServer::TYPE_WS,
208 net::SpawnedTestServer::kLocalhost,
209 net::GetWebSocketTestDataDirectory());
210 ASSERT_TRUE(ws_server.Start());
212 // Generate test URL.
213 GURL::Replacements replacements;
214 replacements.SetSchemeStr("http");
215 GURL url = ws_server.GetURL(
216 "websocket_shared_worker.html").ReplaceComponents(replacements);
218 // Run test.
219 Shell* window = shell();
220 const base::string16 expected_title = base::ASCIIToUTF16("OK");
221 TitleWatcher title_watcher(window->web_contents(), expected_title);
222 NavigateToURL(window, url);
223 base::string16 final_title = title_watcher.WaitAndGetTitle();
224 EXPECT_EQ(expected_title, final_title);
227 IN_PROC_BROWSER_TEST_F(WorkerTest, PassMessagePortToSharedWorker) {
228 if (!SupportsSharedWorker())
229 return;
231 RunTest("pass_messageport_to_sharedworker.html", "");
234 IN_PROC_BROWSER_TEST_F(WorkerTest,
235 PassMessagePortToSharedWorkerDontWaitForConnect) {
236 if (!SupportsSharedWorker())
237 return;
239 RunTest("pass_messageport_to_sharedworker_dont_wait_for_connect.html", "");
242 } // namespace content