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/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/common/content_paths.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "content/public/test/content_browser_test.h"
17 #include "content/public/test/content_browser_test_utils.h"
18 #include "content/public/test/test_utils.h"
19 #include "content/shell/browser/shell.h"
20 #include "content/shell/browser/shell_content_browser_client.h"
21 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
22 #include "net/base/test_data_directory.h"
23 #include "net/test/spawned_test_server/spawned_test_server.h"
28 class WorkerTest
: public ContentBrowserTest
{
32 GURL
GetTestURL(const std::string
& test_case
, const std::string
& query
) {
33 base::FilePath test_file_path
= GetTestFilePath(
34 "workers", test_case
.c_str());
35 return GetFileUrlWithQuery(test_file_path
, query
);
38 void RunTest(Shell
* window
,
39 const std::string
& test_case
,
40 const std::string
& query
) {
41 GURL url
= GetTestURL(test_case
, query
);
42 const base::string16 expected_title
= base::ASCIIToUTF16("OK");
43 TitleWatcher
title_watcher(window
->web_contents(), expected_title
);
44 NavigateToURL(window
, url
);
45 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
46 EXPECT_EQ(expected_title
, final_title
);
49 void RunTest(const std::string
& test_case
, const std::string
& query
) {
50 RunTest(shell(), test_case
, query
);
53 static void QuitUIMessageLoop(base::Callback
<void()> callback
) {
54 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, callback
);
57 void NavigateAndWaitForAuth(const GURL
& url
) {
58 ShellContentBrowserClient
* browser_client
=
59 ShellContentBrowserClient::Get();
60 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner();
61 browser_client
->resource_dispatcher_host_delegate()->
62 set_login_request_callback(
63 base::Bind(&QuitUIMessageLoop
, runner
->QuitClosure()));
64 shell()->LoadURL(url
);
69 IN_PROC_BROWSER_TEST_F(WorkerTest
, SingleWorker
) {
70 RunTest("single_worker.html", std::string());
73 IN_PROC_BROWSER_TEST_F(WorkerTest
, MultipleWorkers
) {
74 RunTest("multi_worker.html", std::string());
77 IN_PROC_BROWSER_TEST_F(WorkerTest
, SingleSharedWorker
) {
78 RunTest("single_worker.html", "shared=true");
81 // http://crbug.com/96435
82 IN_PROC_BROWSER_TEST_F(WorkerTest
, MultipleSharedWorkers
) {
83 RunTest("multi_worker.html", "shared=true");
86 // Incognito windows should not share workers with non-incognito windows
87 // http://crbug.com/30021
88 IN_PROC_BROWSER_TEST_F(WorkerTest
, IncognitoSharedWorkers
) {
89 // Load a non-incognito tab and have it create a shared worker
90 RunTest("incognito_worker.html", std::string());
92 // Incognito worker should not share with non-incognito
93 RunTest(CreateOffTheRecordBrowser(), "incognito_worker.html", std::string());
96 // Make sure that auth dialog is displayed from worker context.
97 // http://crbug.com/33344
98 IN_PROC_BROWSER_TEST_F(WorkerTest
, WorkerHttpAuth
) {
99 ASSERT_TRUE(test_server()->Start());
100 GURL url
= test_server()->GetURL("files/workers/worker_auth.html");
102 NavigateAndWaitForAuth(url
);
105 // Make sure that auth dialog is displayed from shared worker context.
106 // http://crbug.com/33344
107 IN_PROC_BROWSER_TEST_F(WorkerTest
, SharedWorkerHttpAuth
) {
108 ASSERT_TRUE(test_server()->Start());
109 GURL url
= test_server()->GetURL("files/workers/shared_worker_auth.html");
110 NavigateAndWaitForAuth(url
);
113 IN_PROC_BROWSER_TEST_F(WorkerTest
, WebSocketSharedWorker
) {
114 // Launch WebSocket server.
115 net::SpawnedTestServer
ws_server(net::SpawnedTestServer::TYPE_WS
,
116 net::SpawnedTestServer::kLocalhost
,
117 net::GetWebSocketTestDataDirectory());
118 ASSERT_TRUE(ws_server
.Start());
120 // Generate test URL.
121 GURL::Replacements replacements
;
122 replacements
.SetSchemeStr("http");
123 GURL url
= ws_server
.GetURL(
124 "websocket_shared_worker.html").ReplaceComponents(replacements
);
127 Shell
* window
= shell();
128 const base::string16 expected_title
= base::ASCIIToUTF16("OK");
129 TitleWatcher
title_watcher(window
->web_contents(), expected_title
);
130 NavigateToURL(window
, url
);
131 base::string16 final_title
= title_watcher
.WaitAndGetTitle();
132 EXPECT_EQ(expected_title
, final_title
);
135 IN_PROC_BROWSER_TEST_F(WorkerTest
, PassMessagePortToSharedWorker
) {
136 RunTest("pass_messageport_to_sharedworker.html", "");
139 IN_PROC_BROWSER_TEST_F(WorkerTest
,
140 PassMessagePortToSharedWorkerDontWaitForConnect
) {
141 RunTest("pass_messageport_to_sharedworker_dont_wait_for_connect.html", "");
144 } // namespace content