Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: chrome/browser/extensions/service_worker_browsertest.cc

Issue 182253010: Register a Service Worker when an extension is enabled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Roughly wire things up and add a test Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 #include "base/bind_helpers.h"
6 #include "chrome/browser/extensions/extension_browsertest.h"
7 #include "chrome/browser/extensions/test_extension_dir.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/public/browser/storage_partition.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/test/test_utils.h"
14
15 namespace extensions {
16 namespace {
17
18 using content::BrowserThread;
19 using content::ServiceWorkerContextWrapper;
20
21 // Exists as a browser test because ExtensionHosts are hard to create without
22 // a real browser.
23 class ExtensionServiceWorkerBrowserTest : public ExtensionBrowserTest {
24 protected:
25 ExtensionServiceWorkerBrowserTest()
26 : trunk_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
27
28 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29 ExtensionBrowserTest::SetUpCommandLine(command_line);
30 command_line->AppendSwitch(switches::kEnableServiceWorker);
31 }
32 extensions::ScopedCurrentChannel trunk_channel_;
33 TestExtensionDir ext_dir_;
34 };
35
36 scoped_refptr<content::ServiceWorkerContextWrapper> GetSWContext(
37 content::BrowserContext* context,
38 const ExtensionId& ext_id) {
39 return content::BrowserContext::GetStoragePartitionForSite(
40 context, Extension::GetBaseURLFromExtensionId(ext_id))
41 ->GetServiceWorkerContext();
42 }
43
44 const char kServiceWorkerManifest[] =
45 "{"
46 " \"name\": \"\","
47 " \"manifest_version\": 2,"
48 " \"version\": \"1\","
49 " \"app\": {"
50 " \"service_worker\": {"
51 " \"script\": \"service_worker.js\""
52 " }"
53 " }"
54 "}";
55
56 class IOThreadInstallUninstallTest {
57 public:
58 IOThreadInstallUninstallTest(
59 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
60 const ExtensionId& ext_id)
61 : service_worker_context_(service_worker_context), ext_id_(ext_id) {}
62
63 void TestInstall(const base::Closure& continuation) {
64 content::ServiceWorkerStorage* sw_storage =
65 service_worker_context_->context()->storage();
66 sw_storage->FindRegistrationForPattern(
67 GURL("chrome-extension://" + ext_id_ + "/*"),
68 base::Bind(&IOThreadInstallUninstallTest::TestInstall2,
69 base::Unretained(this),
70 continuation));
71 }
72
73 void TestInstall2(
74 const base::Closure& continuation,
75 content::ServiceWorkerStatusCode status,
76 const scoped_refptr<content::ServiceWorkerRegistration>& registration) {
77 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/service_worker.js"),
78 registration->script_url());
79 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/*"),
80 registration->pattern());
81 EXPECT_EQ(NULL, registration->pending_version());
82 content::ServiceWorkerVersion* active_version =
83 registration->active_version();
84 EXPECT_TRUE(active_version);
85 EXPECT_EQ(content::ServiceWorkerVersion::RUNNING, active_version->status());
86 continuation.Run();
87 }
88
89 const scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
90 const ExtensionId ext_id_;
91 };
92
93 // Test that installing a ServiceWorker-enabled app registers the ServiceWorker,
94 // and uninstalling it unregisters the ServiceWorker.
95 IN_PROC_BROWSER_TEST_F(ExtensionServiceWorkerBrowserTest, InstallAndUninstall) {
96 ext_dir_.WriteManifest(kServiceWorkerManifest);
97 ext_dir_.WriteFile("service_worker.js", "");
scheib 2014/03/12 21:42:48 You will need: FILE_PATH_LITERAL("service_worker.
98
99 scoped_refptr<const Extension> extension =
100 LoadExtension(ext_dir_.unpacked_path());
101 ASSERT_TRUE(extension.get());
102
103 IOThreadInstallUninstallTest test_obj(
104 GetSWContext(profile(), extension->id()), extension->id());
105 scoped_refptr<content::MessageLoopRunner> runner =
106 new content::MessageLoopRunner;
107 BrowserThread::PostTask(BrowserThread::IO,
108 FROM_HERE,
109 base::Bind(&IOThreadInstallUninstallTest::TestInstall,
110 base::Unretained(&test_obj),
111 runner->QuitClosure()));
112 runner->Run();
113
114 // Unload the extension.
115 UnloadExtension(extension->id());
116 }
117
118 } // namespace
119 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698