OLD | NEW |
(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 "base/callback_helpers.h" |
| 7 #include "chrome/browser/extensions/extension_browsertest.h" |
| 8 #include "chrome/browser/extensions/test_extension_dir.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/public/browser/storage_partition.h" |
| 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "extensions/browser/service_worker_manager.h" |
| 16 |
| 17 namespace extensions { |
| 18 namespace { |
| 19 |
| 20 using content::BrowserThread; |
| 21 using content::ServiceWorkerContextWrapper; |
| 22 |
| 23 // Exists as a browser test because ExtensionHosts are hard to create without |
| 24 // a real browser. |
| 25 class ExtensionServiceWorkerBrowserTest : public ExtensionBrowserTest { |
| 26 protected: |
| 27 ExtensionServiceWorkerBrowserTest() |
| 28 : trunk_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} |
| 29 |
| 30 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 31 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 32 command_line->AppendSwitch(switches::kEnableServiceWorker); |
| 33 } |
| 34 extensions::ScopedCurrentChannel trunk_channel_; |
| 35 TestExtensionDir ext_dir_; |
| 36 }; |
| 37 |
| 38 content::ServiceWorkerContext* GetSWContext(content::BrowserContext* context, |
| 39 const ExtensionId& ext_id) { |
| 40 return content::BrowserContext::GetStoragePartitionForSite( |
| 41 context, Extension::GetBaseURLFromExtensionId(ext_id)) |
| 42 ->GetServiceWorkerContext(); |
| 43 } |
| 44 |
| 45 const char kServiceWorkerManifest[] = |
| 46 "{" |
| 47 " \"name\": \"\"," |
| 48 " \"manifest_version\": 2," |
| 49 " \"version\": \"1\"," |
| 50 " \"app\": {" |
| 51 " \"service_worker\": {" |
| 52 " \"script\": \"service_worker.js\"" |
| 53 " }" |
| 54 " }" |
| 55 "}"; |
| 56 |
| 57 class IOThreadInstallUninstallTest { |
| 58 public: |
| 59 IOThreadInstallUninstallTest( |
| 60 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, |
| 61 const ExtensionId& ext_id) |
| 62 : service_worker_context_(service_worker_context), ext_id_(ext_id) {} |
| 63 |
| 64 void TestInstall(const base::Closure& continuation) { |
| 65 content::ServiceWorkerStorage* sw_storage = |
| 66 service_worker_context_->context()->storage(); |
| 67 sw_storage->FindRegistrationForPattern( |
| 68 GURL("chrome-extension://" + ext_id_ + "/*"), |
| 69 base::Bind(&IOThreadInstallUninstallTest::TestInstall2, |
| 70 base::Unretained(this), |
| 71 continuation)); |
| 72 } |
| 73 |
| 74 void TestInstall2( |
| 75 const base::Closure& continuation, |
| 76 content::ServiceWorkerStatusCode status, |
| 77 const scoped_refptr<content::ServiceWorkerRegistration>& registration) { |
| 78 base::ScopedClosureRunner at_exit( |
| 79 base::Bind(base::IgnoreResult(&BrowserThread::PostTask), |
| 80 BrowserThread::UI, |
| 81 FROM_HERE, |
| 82 continuation)); |
| 83 ASSERT_TRUE(registration); |
| 84 EXPECT_EQ(content::SERVICE_WORKER_OK, status); |
| 85 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/service_worker.js"), |
| 86 registration->script_url()); |
| 87 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/*"), |
| 88 registration->pattern()); |
| 89 EXPECT_EQ(NULL, registration->pending_version()); |
| 90 content::ServiceWorkerVersion* active_version = |
| 91 registration->active_version(); |
| 92 EXPECT_TRUE(active_version); |
| 93 } |
| 94 |
| 95 const scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 96 const ExtensionId ext_id_; |
| 97 }; |
| 98 |
| 99 static void FailTest(const std::string& message, |
| 100 const base::Closure& continuation) { |
| 101 ADD_FAILURE() << message; |
| 102 } |
| 103 |
| 104 // Test that installing a ServiceWorker-enabled app registers the ServiceWorker, |
| 105 // and uninstalling it unregisters the ServiceWorker. |
| 106 IN_PROC_BROWSER_TEST_F(ExtensionServiceWorkerBrowserTest, InstallAndUninstall) { |
| 107 ext_dir_.WriteManifest(kServiceWorkerManifest); |
| 108 ext_dir_.WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); |
| 109 |
| 110 scoped_refptr<const Extension> extension = |
| 111 LoadExtension(ext_dir_.unpacked_path()); |
| 112 ASSERT_TRUE(extension.get()); |
| 113 |
| 114 base::RunLoop run_loop; |
| 115 ServiceWorkerManager::Get(profile()) |
| 116 ->WhenRegistered(extension.get(), |
| 117 FROM_HERE, |
| 118 run_loop.QuitClosure(), |
| 119 base::Bind(FailTest, |
| 120 "Extension wasn't being registered", |
| 121 run_loop.QuitClosure())); |
| 122 run_loop.Run(); |
| 123 |
| 124 IOThreadInstallUninstallTest test_obj( |
| 125 static_cast<ServiceWorkerContextWrapper*>( |
| 126 GetSWContext(profile(), extension->id())), |
| 127 extension->id()); |
| 128 scoped_refptr<content::MessageLoopRunner> runner = |
| 129 new content::MessageLoopRunner; |
| 130 BrowserThread::PostTask(BrowserThread::IO, |
| 131 FROM_HERE, |
| 132 base::Bind(&IOThreadInstallUninstallTest::TestInstall, |
| 133 base::Unretained(&test_obj), |
| 134 runner->QuitClosure())); |
| 135 runner->Run(); |
| 136 |
| 137 // Unload the extension. |
| 138 UnloadExtension(extension->id()); |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 } // namespace extensions |
OLD | NEW |