Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chrome_service_worker_browsertest.cc
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.
4
5 // This file tests that Service Workers (a Content feature) work in the Chromium
6 // embedder.
7
8 #include "base/bind.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/service_worker_context.h"
21 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
24
25 namespace {
26
27 class ChromeServiceWorkerTest : public InProcessBrowserTest {
28  protected:
29   ChromeServiceWorkerTest() {
30     EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir());
31   }
32
33   void WriteFile(const base::FilePath::StringType& filename,
34                  base::StringPiece contents) {
35     EXPECT_EQ(base::checked_cast<int>(contents.size()),
36               base::WriteFile(service_worker_dir_.path().Append(filename),
37                               contents.data(),
38                               contents.size()));
39   }
40
41   base::ScopedTempDir service_worker_dir_;
42 };
43
44 static void ExpectResultAndRun(bool expected,
45                                const base::Closure& continuation,
46                                bool actual) {
47   EXPECT_EQ(expected, actual);
48   continuation.Run();
49 }
50
51 // http://crbug.com/368570
52 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
53                        CanShutDownWithRegisteredServiceWorker) {
54   WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
55   WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
56             "HTTP/1.1 200 OK\nContent-Type: text/javascript");
57
58   embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
59   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
60
61   content::ServiceWorkerContext* sw_context =
62       content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
63           ->GetServiceWorkerContext();
64
65   base::RunLoop run_loop;
66   sw_context->RegisterServiceWorker(
67       embedded_test_server()->GetURL("/"),
68       embedded_test_server()->GetURL("/service_worker.js"),
69       base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure()));
70   run_loop.Run();
71
72   // Leave the Service Worker registered, and make sure that the browser can
73   // shut down without DCHECK'ing. It'd be nice to check here that the SW is
74   // actually occupying a process, but we don't yet have the public interface to
75   // do that.
76 }
77
78 // http://crbug.com/419290
79 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
80                        CanCloseIncognitoWindowWithServiceWorkerController) {
81   WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
82   WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
83             "HTTP/1.1 200 OK\nContent-Type: text/javascript");
84   WriteFile(FILE_PATH_LITERAL("test.html"), "");
85
86   embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
87   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
88
89   Browser* incognito = CreateIncognitoBrowser();
90   content::ServiceWorkerContext* sw_context =
91       content::BrowserContext::GetDefaultStoragePartition(incognito->profile())
92           ->GetServiceWorkerContext();
93
94   base::RunLoop run_loop;
95   sw_context->RegisterServiceWorker(
96       embedded_test_server()->GetURL("/"),
97       embedded_test_server()->GetURL("/service_worker.js"),
98       base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure()));
99   run_loop.Run();
100
101   ui_test_utils::NavigateToURL(incognito,
102                                embedded_test_server()->GetURL("/test.html"));
103
104   content::WindowedNotificationObserver observer(
105       chrome::NOTIFICATION_BROWSER_CLOSED, content::Source<Browser>(incognito));
106   incognito->window()->Close();
107   observer.Wait();
108
109   // Test passes if we don't crash.
110 }
111
112 }  // namespace