Chromium Code Reviews| Index: chrome/browser/extensions/service_worker_apitest.cc |
| diff --git a/chrome/browser/extensions/service_worker_apitest.cc b/chrome/browser/extensions/service_worker_apitest.cc |
| index 227584f99c1a1880ce3d64f2582357edeecf58e3..a18718b653ecf6dc893d785c43deaeae0e32a28e 100644 |
| --- a/chrome/browser/extensions/service_worker_apitest.cc |
| +++ b/chrome/browser/extensions/service_worker_apitest.cc |
| @@ -24,6 +24,8 @@ |
| #include "content/public/browser/navigation_controller.h" |
| #include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/permission_type.h" |
| +#include "content/public/browser/service_worker_context.h" |
| +#include "content/public/browser/storage_partition.h" |
| #include "content/public/browser/web_contents.h" |
| #include "content/public/common/content_switches.h" |
| #include "content/public/common/origin_util.h" |
| @@ -93,6 +95,12 @@ class ServiceWorkerTest : public ExtensionApiTest { |
| ~ServiceWorkerTest() override {} |
| + void DidGetPendingExternalRequestCount(const base::Closure& callback, |
| + size_t external_request_count) { |
| + last_worker_ref_count_ = external_request_count; |
| + callback.Run(); |
| + } |
| + |
| protected: |
| // Returns the ProcessManager for the test's profile. |
| ProcessManager* process_manager() { return ProcessManager::Get(profile()); } |
| @@ -162,6 +170,8 @@ class ServiceWorkerTest : public ExtensionApiTest { |
| return ExtractInnerText(Navigate(url)); |
| } |
| + size_t last_worker_ref_count() const { return last_worker_ref_count_; } |
| + |
| private: |
| // Sets the channel to "stable". |
| // Not useful after we've opened extension Service Workers to stable |
| @@ -170,6 +180,8 @@ class ServiceWorkerTest : public ExtensionApiTest { |
| // removed. |
| ScopedCurrentChannel current_channel_; |
| + size_t last_worker_ref_count_ = 0u; |
| + |
| DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
| }; |
| @@ -630,6 +642,52 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, TabsCreate) { |
| EXPECT_EQ(starting_tab_count, browser()->tab_strip_model()->count()); |
| } |
| +// Tests that worker ref count increments while extension API function is |
| +// active. |
| +IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WorkerRefCount) { |
| + // Extensions APIs from SW are only enabled on trunk. |
| + ScopedCurrentChannel current_channel_override(version_info::Channel::UNKNOWN); |
| + const Extension* extension = LoadExtensionWithFlags( |
| + test_data_dir_.AppendASCII("service_worker/api_worker_ref_count"), |
| + kFlagNone); |
| + ASSERT_TRUE(extension); |
| + ui_test_utils::NavigateToURL(browser(), |
| + extension->GetResourceURL("page.html")); |
| + content::WebContents* web_contents = |
| + browser()->tab_strip_model()->GetActiveWebContents(); |
| + |
|
Devlin
2016/10/07 15:13:22
Check ref count is 0?
lazyboy
2016/10/07 18:50:10
Done.
|
| + ExtensionTestMessageListener worker_listener("CHECK_REF_COUNT", true); |
| + worker_listener.set_failure_message("FAILURE"); |
| + ASSERT_TRUE( |
| + content::ExecuteScript(web_contents, "window.runServiceWorker()")); |
| + ASSERT_TRUE(worker_listener.WaitUntilSatisfied()); |
| + |
| + { |
| + content::ServiceWorkerContext* sw_context = |
| + content::BrowserContext::GetDefaultStoragePartition( |
| + browser()->profile()) |
| + ->GetServiceWorkerContext(); |
| + base::RunLoop run_loop; |
| + sw_context->GetPendingExternalRequestCountForTest( |
| + extension->url(), |
| + base::Bind(&ServiceWorkerTest::DidGetPendingExternalRequestCount, |
| + // Unretained is safe because we wait until the bound method |
| + // is called. |
| + base::Unretained(this), run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + } |
| + |
| + // Service worker should have exactly one pending request because |
| + // chrome.test.sendMessage() API call is in-flight. |
| + EXPECT_EQ(1u, last_worker_ref_count()); |
| + |
| + ExtensionTestMessageListener extension_listener("SUCCESS", false); |
| + extension_listener.set_failure_message("FAILURE"); |
| + // Finish executing chrome.test.sendMessage(). |
| + worker_listener.Reply("Hello world"); |
| + ASSERT_TRUE(extension_listener.WaitUntilSatisfied()); |
|
Devlin
2016/10/07 15:13:21
Check that ref count drops to 0?
I also wonder if
lazyboy
2016/10/07 18:50:10
Done.
|
| +} |
| + |
| // This test loads a web page that has an iframe pointing to a |
| // chrome-extension:// URL. The URL is listed in the extension's |
| // web_accessible_resources. Initially the iframe is served from the extension's |