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 fbfc56144f5d02b1b6b9ab83fe6a6b1ecc48b547..5b60ce2693ab06fb3b503d69cd3961d1889d124f 100644 |
| --- a/chrome/browser/extensions/service_worker_apitest.cc |
| +++ b/chrome/browser/extensions/service_worker_apitest.cc |
| @@ -40,6 +40,43 @@ std::string* const kExpectSuccess = nullptr; |
| void DoNothingWithBool(bool b) {} |
| +// Returns the newly added WebContents. |
| +content::WebContents* AddTab(Browser* browser, const GURL& url) { |
| + int starting_tab_count = browser->tab_strip_model()->count(); |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser, url, NEW_FOREGROUND_TAB, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| + int tab_count = browser->tab_strip_model()->count(); |
| + EXPECT_EQ(starting_tab_count + 1, tab_count); |
| + return browser->tab_strip_model()->GetWebContentsAt(tab_count - 1); |
|
Devlin
2015/12/10 18:16:57
nit: I think GetActiveWebContents() is more clear.
lazyboy
2015/12/10 18:59:44
Done.
|
| +} |
| + |
| +class WebContentsLoadStopObserver : content::WebContentsObserver { |
| + public: |
| + explicit WebContentsLoadStopObserver(content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents), |
| + load_stop_observed_(false) {} |
| + |
| + void WaitForLoadStop() { |
| + if (load_stop_observed_) |
| + return; |
| + message_loop_runner_ = new content::MessageLoopRunner; |
| + message_loop_runner_->Run(); |
| + } |
| + |
| + private: |
| + void DidStopLoading() override { |
| + load_stop_observed_ = true; |
| + if (message_loop_runner_) |
| + message_loop_runner_->Quit(); |
| + } |
| + |
| + bool load_stop_observed_; |
| + scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebContentsLoadStopObserver); |
| +}; |
| + |
| } // namespace |
| class ServiceWorkerTest : public ExtensionApiTest { |
| @@ -253,6 +290,75 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateRefreshesServiceWorker) { |
| EXPECT_TRUE(listener_v2.WaitUntilSatisfied()); |
| } |
| +IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateWithoutSkipWaiting) { |
| + base::ScopedTempDir scoped_temp_dir; |
| + ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); |
| + base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker") |
| + .AppendASCII("update_without_skip_waiting") |
| + .AppendASCII("update_without_skip_waiting.pem"); |
| + base::FilePath path_v1 = PackExtensionWithOptions( |
| + test_data_dir_.AppendASCII("service_worker") |
| + .AppendASCII("update_without_skip_waiting") |
| + .AppendASCII("v1"), |
| + scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath()); |
| + base::FilePath path_v2 = PackExtensionWithOptions( |
| + test_data_dir_.AppendASCII("service_worker") |
| + .AppendASCII("update_without_skip_waiting") |
| + .AppendASCII("v2"), |
| + scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath()); |
| + const char* kId = "mhnnnflgagdakldgjpfcofkiocpdmogl"; |
| + |
| + // Install version 1.0 of the extension. |
| + ASSERT_TRUE(InstallExtension(path_v1, 1)); |
| + EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) |
| + ->enabled_extensions() |
| + .GetByID(kId)); |
| + const Extension* extension = extensions::ExtensionRegistry::Get(profile()) |
| + ->enabled_extensions() |
| + .GetByID(kId); |
| + |
| + ExtensionTestMessageListener listener_v1("Pong from version 1", false); |
| + listener_v1.set_failure_message("FAILURE_V1"); |
| + content::WebContents* web_contents = |
| + AddTab(browser(), extension->GetResourceURL("page.html")); |
| + EXPECT_TRUE(listener_v1.WaitUntilSatisfied()); |
| + |
| + // Update to version 2.0. |
| + EXPECT_TRUE(UpdateExtension(kId, path_v2, 0)); |
| + EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) |
| + ->enabled_extensions() |
| + .GetByID(kId)); |
| + const Extension* extension_after_update = |
| + extensions::ExtensionRegistry::Get(profile()) |
| + ->enabled_extensions() |
| + .GetByID(kId); |
| + |
| + // Service worker version 2 would be installed but it won't be controlling |
| + // the extension page yet. |
| + ExtensionTestMessageListener listener_v2("Pong from version 1", false); |
|
Devlin
2015/12/10 18:16:57
Calling this listener_v2 isn't really intuitive, s
lazyboy
2015/12/10 18:59:43
Done.
|
| + listener_v2.set_failure_message("FAILURE_V2"); |
|
Devlin
2015/12/10 18:16:57
Since failure here could mean V1 failed or V2 was
lazyboy
2015/12/10 18:59:44
Done.
|
| + web_contents = |
| + AddTab(browser(), extension_after_update->GetResourceURL("page.html")); |
| + EXPECT_TRUE(listener_v2.WaitUntilSatisfied()); |
| + |
| + // Navigate the tab away from the extension page so that no clients are |
| + // using the service worker. |
| + // Note that just closing the tab with WebContentsDestroyedWatcher doesn't |
| + // seem to be enough because it returns too early. |
| + WebContentsLoadStopObserver navigate_away_observer(web_contents); |
| + web_contents->GetController().LoadURL( |
| + GURL(url::kAboutBlankURL), content::Referrer(), ui::PAGE_TRANSITION_TYPED, |
| + std::string()); |
| + navigate_away_observer.WaitForLoadStop(); |
| + |
| + // Now expect service worker version 2 to control the extension page. |
| + ExtensionTestMessageListener listener_v2next("Pong from version 2", false); |
| + listener_v2next.set_failure_message("FAILURE_V2"); |
| + web_contents = |
| + AddTab(browser(), extension_after_update->GetResourceURL("page.html")); |
| + EXPECT_TRUE(listener_v2next.WaitUntilSatisfied()); |
| +} |
| + |
| IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { |
| const Extension* extension = |
| StartTestFromBackgroundPage("fetch.js", kExpectSuccess); |