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

Side by Side 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: wait for updatefound and waiting worker before navigating away on second load 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind_helpers.h" 5 #include "base/bind_helpers.h"
6 #include "base/strings/stringprintf.h" 6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_apitest.h" 8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 10 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
11 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" 11 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
12 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" 12 #include "chrome/browser/push_messaging/push_messaging_service_factory.h"
13 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" 13 #include "chrome/browser/push_messaging/push_messaging_service_impl.h"
14 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" 14 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/ui_test_utils.h" 17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/version_info/version_info.h" 18 #include "components/version_info/version_info.h"
19 #include "content/public/browser/navigation_controller.h" 19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/navigation_entry.h" 20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/page_type.h" 23 #include "content/public/common/page_type.h"
24 #include "content/public/test/background_sync_test_util.h" 24 #include "content/public/test/background_sync_test_util.h"
25 #include "content/public/test/browser_test_utils.h" 25 #include "content/public/test/browser_test_utils.h"
26 #include "extensions/browser/extension_host.h" 26 #include "extensions/browser/extension_host.h"
27 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/process_manager.h" 28 #include "extensions/browser/process_manager.h"
28 #include "extensions/test/background_page_watcher.h" 29 #include "extensions/test/background_page_watcher.h"
29 #include "extensions/test/extension_test_message_listener.h" 30 #include "extensions/test/extension_test_message_listener.h"
30 #include "net/test/embedded_test_server/embedded_test_server.h" 31 #include "net/test/embedded_test_server/embedded_test_server.h"
31 32
32 namespace extensions { 33 namespace extensions {
33 34
34 namespace { 35 namespace {
35 36
36 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that 37 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that
37 // registration is expected to succeed. 38 // registration is expected to succeed.
38 std::string* const kExpectSuccess = nullptr; 39 std::string* const kExpectSuccess = nullptr;
39 40
40 void DoNothingWithBool(bool b) {} 41 void DoNothingWithBool(bool b) {}
41 42
43 // Returns the newly added WebContents.
44 content::WebContents* AddTab(Browser* browser, const GURL& url) {
45 int starting_tab_count = browser->tab_strip_model()->count();
46 ui_test_utils::NavigateToURLWithDisposition(
47 browser, url, NEW_FOREGROUND_TAB,
48 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
49 int tab_count = browser->tab_strip_model()->count();
50 EXPECT_EQ(starting_tab_count + 1, tab_count);
51 return browser->tab_strip_model()->GetWebContentsAt(tab_count - 1);
52 }
53
54 class WebContentsLoadStopObserver : content::WebContentsObserver {
55 public:
56 explicit WebContentsLoadStopObserver(content::WebContents* web_contents)
57 : content::WebContentsObserver(web_contents),
58 load_stop_observed_(false) {}
59
60 void WaitForLoadStop() {
61 if (load_stop_observed_)
62 return;
63 message_loop_runner_ = new content::MessageLoopRunner;
64 message_loop_runner_->Run();
65 }
66
67 private:
68 void DidStopLoading() override {
69 load_stop_observed_ = true;
70 if (message_loop_runner_)
71 message_loop_runner_->Quit();
72 }
73
74 bool load_stop_observed_;
75 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
76
77 DISALLOW_COPY_AND_ASSIGN(WebContentsLoadStopObserver);
78 };
79
42 } // namespace 80 } // namespace
43 81
44 class ServiceWorkerTest : public ExtensionApiTest { 82 class ServiceWorkerTest : public ExtensionApiTest {
45 public: 83 public:
46 ServiceWorkerTest() : current_channel_(version_info::Channel::UNKNOWN) {} 84 ServiceWorkerTest() : current_channel_(version_info::Channel::UNKNOWN) {}
47 85
48 ~ServiceWorkerTest() override {} 86 ~ServiceWorkerTest() override {}
49 87
50 protected: 88 protected:
51 // Returns the ProcessManager for the test's profile. 89 // Returns the ProcessManager for the test's profile.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 std::string error; 245 std::string error;
208 const Extension* extension = 246 const Extension* extension =
209 StartTestFromBackgroundPage("register.js", &error); 247 StartTestFromBackgroundPage("register.js", &error);
210 EXPECT_EQ( 248 EXPECT_EQ(
211 "Failed to register a ServiceWorker: The URL protocol of the current " 249 "Failed to register a ServiceWorker: The URL protocol of the current "
212 "origin ('chrome-extension://" + 250 "origin ('chrome-extension://" +
213 extension->id() + "') is not supported.", 251 extension->id() + "') is not supported.",
214 error); 252 error);
215 } 253 }
216 254
255 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateWithoutSkipWaiting) {
256 base::ScopedTempDir scoped_temp_dir;
257 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
258 base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker")
259 .AppendASCII("update_without_skip_waiting")
260 .AppendASCII("update_without_skip_waiting.pem");
261 base::FilePath path_v1 = PackExtensionWithOptions(
262 test_data_dir_.AppendASCII("service_worker")
263 .AppendASCII("update_without_skip_waiting")
264 .AppendASCII("v1"),
265 scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath());
266 base::FilePath path_v2 = PackExtensionWithOptions(
267 test_data_dir_.AppendASCII("service_worker")
268 .AppendASCII("update_without_skip_waiting")
269 .AppendASCII("v2"),
270 scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath());
271 const char* kId = "mhnnnflgagdakldgjpfcofkiocpdmogl";
272
273 // Install version 1.0 of the extension.
274 ASSERT_TRUE(InstallExtension(path_v1, 1));
275 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
276 ->enabled_extensions()
277 .GetByID(kId) != NULL);
falken 2015/12/10 03:41:41 nit: nullptr or maybe you can just remove the != .
lazyboy 2015/12/10 04:21:20 Done.
278 const Extension* extension = extensions::ExtensionRegistry::Get(profile())
279 ->enabled_extensions()
280 .GetByID(kId);
281
282 ExtensionTestMessageListener listener_v1("Pong from version 1", false);
283 listener_v1.set_failure_message("FAILURE_V1");
284 content::WebContents* web_contents =
285 AddTab(browser(), extension->GetResourceURL("page.html"));
286 EXPECT_TRUE(listener_v1.WaitUntilSatisfied());
287
288 // Update to version 2.0.
289 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0));
290 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile())
291 ->enabled_extensions()
292 .GetByID(kId) != NULL);
293 const Extension* extension_after_update =
294 extensions::ExtensionRegistry::Get(profile())
295 ->enabled_extensions()
296 .GetByID(kId);
297
298 // Service worker version 2 would be installed but it won't be controlling
299 // the extension page yet.
300 ExtensionTestMessageListener listener_v2("Pong from version 1 (with update)",
301 false);
302 listener_v2.set_failure_message("FAILURE_V2");
303 web_contents = AddTab(browser(), extension_after_update->GetResourceURL(
304 "page.html?#expect_update"));
305 EXPECT_TRUE(listener_v2.WaitUntilSatisfied());
306
307 // Navigate the tab away from the extension page so that no clients are
308 // using the service worker.
309 // Note that just closing the tab with WebContentsDestroyedWatcher doesn't
310 // seem to be enough because it returns too early.
311 WebContentsLoadStopObserver navigate_away_observer(web_contents);
312 web_contents->GetController().LoadURL(
313 GURL(url::kAboutBlankURL), content::Referrer(), ui::PAGE_TRANSITION_TYPED,
314 std::string());
315 navigate_away_observer.WaitForLoadStop();
316
317 // Now expect service worker version 2 to control the extension page.
318 ExtensionTestMessageListener listener_v2next("Pong from version 2", false);
319 listener_v2next.set_failure_message("FAILURE_V2");
320 web_contents =
321 AddTab(browser(), extension_after_update->GetResourceURL("page.html"));
322 EXPECT_TRUE(listener_v2next.WaitUntilSatisfied());
323 }
324
217 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { 325 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) {
218 const Extension* extension = 326 const Extension* extension =
219 StartTestFromBackgroundPage("fetch.js", kExpectSuccess); 327 StartTestFromBackgroundPage("fetch.js", kExpectSuccess);
220 328
221 // Open some arbirary paths. Their contents should be what the service worker 329 // Open some arbirary paths. Their contents should be what the service worker
222 // responds with, which in this case is the path of the fetch. 330 // responds with, which in this case is the path of the fetch.
223 EXPECT_EQ( 331 EXPECT_EQ(
224 "Caught a fetch for /index.html", 332 "Caught a fetch for /index.html",
225 NavigateAndExtractInnerText(extension->GetResourceURL("index.html"))); 333 NavigateAndExtractInnerText(extension->GetResourceURL("index.html")));
226 EXPECT_EQ("Caught a fetch for /path/to/other.html", 334 EXPECT_EQ("Caught a fetch for /path/to/other.html",
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 push_message_listener.set_failure_message("FAIL"); 609 push_message_listener.set_failure_message("FAIL");
502 gcm::IncomingMessage message; 610 gcm::IncomingMessage message;
503 message.sender_id = "1234567890"; 611 message.sender_id = "1234567890";
504 message.raw_data = "testdata"; 612 message.raw_data = "testdata";
505 message.decrypted = true; 613 message.decrypted = true;
506 push_service()->OnMessage(app_identifier.app_id(), message); 614 push_service()->OnMessage(app_identifier.app_id(), message);
507 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); 615 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
508 } 616 }
509 617
510 } // namespace extensions 618 } // namespace extensions
OLDNEW
« 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