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

Unified Diff: chrome/browser/extensions/service_worker_apitest.cc

Issue 1510573003: Add test without skipWaiting() to demonstrate extension & service worker update. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments from Devlin Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/service_worker/update_without_skip_waiting/update_without_skip_waiting.pem » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..31e7091eef03081c352f0afb55e52fc7a5d57159 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()->GetActiveWebContents();
+}
+
+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 listener1("Pong from version 1", false);
+ listener1.set_failure_message("FAILURE");
+ content::WebContents* web_contents =
+ AddTab(browser(), extension->GetResourceURL("page.html"));
+ EXPECT_TRUE(listener1.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 listener2("Pong from version 1", false);
+ listener2.set_failure_message("FAILURE");
+ web_contents =
+ AddTab(browser(), extension_after_update->GetResourceURL("page.html"));
+ EXPECT_TRUE(listener2.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 listener3("Pong from version 2", false);
+ listener3.set_failure_message("FAILURE");
+ web_contents =
+ AddTab(browser(), extension_after_update->GetResourceURL("page.html"));
+ EXPECT_TRUE(listener3.WaitUntilSatisfied());
+}
+
IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) {
const Extension* extension =
StartTestFromBackgroundPage("fetch.js", kExpectSuccess);
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/service_worker/update_without_skip_waiting/update_without_skip_waiting.pem » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698