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.
6 #include "base/files/file_path.h"
7 #include "base/logging.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/sys_info.h"
13 #include "base/test/test_timeouts.h"
14 #include "content/public/browser/browser_thread.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/test_data_directory.h"
24 #include "net/test/spawned_test_server/spawned_test_server.h"
29 class WorkerTest
: public ContentBrowserTest
{
33 GURL
GetTestURL(const std::string
& test_case
, const std::string
& query
) {
34 base::FilePath test_file_path
= GetTestFilePath(
35 "workers", test_case
.c_str());
36 return GetFileUrlWithQuery(test_file_path
, query
);
39 void RunTest(Shell
* window
,
40 const std::string
& test_case
,
41 const std::string
& query
) {
42 GURL url
= GetTestURL(test_case
, query
);
43 const base::string16 expected_title
= base::ASCIIToUTF16("OK");
44 TitleWatcher
title_watcher(window
->web_contents(), expected_title
);
45 NavigateToURL(window
, url
);
46 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
47 EXPECT_EQ(expected_title
, final_title
);
50 void RunTest(const std::string
& test_case
, const std::string
& query
) {
51 RunTest(shell(), test_case
, query
);
54 static void QuitUIMessageLoop(base::Callback
<void()> callback
) {
55 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, callback
);
58 void NavigateAndWaitForAuth(const GURL
& url
) {
59 ShellContentBrowserClient
* browser_client
=
60 ShellContentBrowserClient::Get();
61 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner();
62 browser_client
->resource_dispatcher_host_delegate()->
63 set_login_request_callback(
64 base::Bind(&QuitUIMessageLoop
, runner
->QuitClosure()));
65 shell()->LoadURL(url
);
70 IN_PROC_BROWSER_TEST_F(WorkerTest
, SingleWorker
) {
71 RunTest("single_worker.html", std::string());
74 IN_PROC_BROWSER_TEST_F(WorkerTest
, MultipleWorkers
) {
75 RunTest("multi_worker.html", std::string());
78 IN_PROC_BROWSER_TEST_F(WorkerTest
, SingleSharedWorker
) {
79 RunTest("single_worker.html", "shared=true");
82 // http://crbug.com/96435
83 IN_PROC_BROWSER_TEST_F(WorkerTest
, MultipleSharedWorkers
) {
84 RunTest("multi_worker.html", "shared=true");
87 // Incognito windows should not share workers with non-incognito windows
88 // http://crbug.com/30021
89 IN_PROC_BROWSER_TEST_F(WorkerTest
, IncognitoSharedWorkers
) {
90 // Load a non-incognito tab and have it create a shared worker
91 RunTest("incognito_worker.html", std::string());
93 // Incognito worker should not share with non-incognito
94 RunTest(CreateOffTheRecordBrowser(), "incognito_worker.html", std::string());
97 // Make sure that auth dialog is displayed from worker context.
98 // http://crbug.com/33344
99 IN_PROC_BROWSER_TEST_F(WorkerTest
, WorkerHttpAuth
) {
100 ASSERT_TRUE(test_server()->Start());
101 GURL url
= test_server()->GetURL("files/workers/worker_auth.html");
103 NavigateAndWaitForAuth(url
);
106 // Make sure that auth dialog is displayed from shared worker context.
107 // http://crbug.com/33344
108 IN_PROC_BROWSER_TEST_F(WorkerTest
, SharedWorkerHttpAuth
) {
109 ASSERT_TRUE(test_server()->Start());
110 GURL url
= test_server()->GetURL("files/workers/shared_worker_auth.html");
111 NavigateAndWaitForAuth(url
);
114 IN_PROC_BROWSER_TEST_F(WorkerTest
, WebSocketSharedWorker
) {
115 // Launch WebSocket server.
116 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
117 net::SpawnedTestServer::kLocalhost
,
118 net::GetWebSocketTestDataDirectory());
119 ASSERT_TRUE(ws_server
.Start());
121 // Generate test URL.
122 std::string
scheme("http");
123 GURL::Replacements replacements
;
124 replacements
.SetSchemeStr(scheme
);
125 GURL url
= ws_server
.GetURL(
126 "websocket_shared_worker.html").ReplaceComponents(replacements
);
129 Shell
* window
= shell();
130 const base::string16 expected_title
= base::ASCIIToUTF16("OK");
131 TitleWatcher
title_watcher(window
->web_contents(), expected_title
);
132 NavigateToURL(window
, url
);
133 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
134 EXPECT_EQ(expected_title
, final_title
);
137 IN_PROC_BROWSER_TEST_F(WorkerTest
, PassMessagePortToSharedWorker
) {
138 RunTest("pass_messageport_to_sharedworker.html", "");
141 IN_PROC_BROWSER_TEST_F(WorkerTest
,
142 PassMessagePortToSharedWorkerDontWaitForConnect
) {
143 RunTest("pass_messageport_to_sharedworker_dont_wait_for_connect.html", "");
146 } // namespace content