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 |
| 16 namespace extensions { |
| 17 namespace { |
| 18 |
| 19 using content::BrowserThread; |
| 20 using content::ServiceWorkerContextWrapper; |
| 21 |
| 22 // Exists as a browser test because ExtensionHosts are hard to create without |
| 23 // a real browser. |
| 24 class ExtensionServiceWorkerBrowserTest : public ExtensionBrowserTest { |
| 25 protected: |
| 26 ExtensionServiceWorkerBrowserTest() |
| 27 : trunk_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} |
| 28 |
| 29 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 30 ExtensionBrowserTest::SetUpCommandLine(command_line); |
| 31 command_line->AppendSwitch(switches::kEnableServiceWorker); |
| 32 } |
| 33 extensions::ScopedCurrentChannel trunk_channel_; |
| 34 TestExtensionDir ext_dir_; |
| 35 }; |
| 36 |
| 37 content::ServiceWorkerContext* GetSWContext(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 base::ScopedClosureRunner at_exit( |
| 78 base::Bind(base::IgnoreResult(&BrowserThread::PostTask), |
| 79 BrowserThread::UI, |
| 80 FROM_HERE, |
| 81 continuation)); |
| 82 ASSERT_TRUE(registration); |
| 83 EXPECT_EQ(content::SERVICE_WORKER_OK, status); |
| 84 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/service_worker.js"), |
| 85 registration->script_url()); |
| 86 EXPECT_EQ(GURL("chrome-extension://" + ext_id_ + "/*"), |
| 87 registration->pattern()); |
| 88 EXPECT_EQ(NULL, registration->pending_version()); |
| 89 content::ServiceWorkerVersion* active_version = |
| 90 registration->active_version(); |
| 91 EXPECT_TRUE(active_version); |
| 92 } |
| 93 |
| 94 const scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 95 const ExtensionId ext_id_; |
| 96 }; |
| 97 |
| 98 // Test that installing a ServiceWorker-enabled app registers the ServiceWorker, |
| 99 // and uninstalling it unregisters the ServiceWorker. |
| 100 IN_PROC_BROWSER_TEST_F(ExtensionServiceWorkerBrowserTest, InstallAndUninstall) { |
| 101 ext_dir_.WriteManifest(kServiceWorkerManifest); |
| 102 ext_dir_.WriteFile("service_worker.js", ""); |
| 103 |
| 104 scoped_refptr<const Extension> extension = |
| 105 LoadExtension(ext_dir_.unpacked_path()); |
| 106 ASSERT_TRUE(extension.get()); |
| 107 |
| 108 IOThreadInstallUninstallTest test_obj( |
| 109 static_cast<ServiceWorkerContextWrapper*>( |
| 110 GetSWContext(profile(), extension->id())), |
| 111 extension->id()); |
| 112 scoped_refptr<content::MessageLoopRunner> runner = |
| 113 new content::MessageLoopRunner; |
| 114 BrowserThread::PostTask(BrowserThread::IO, |
| 115 FROM_HERE, |
| 116 base::Bind(&IOThreadInstallUninstallTest::TestInstall, |
| 117 base::Unretained(&test_obj), |
| 118 runner->QuitClosure())); |
| 119 runner->Run(); |
| 120 |
| 121 // Unload the extension. |
| 122 UnloadExtension(extension->id()); |
| 123 } |
| 124 |
| 125 } // namespace |
| 126 } // namespace extensions |
OLD | NEW |