OLD | NEW |
---|---|
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" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 namespace extensions { | 33 namespace extensions { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that | 37 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that |
38 // registration is expected to succeed. | 38 // registration is expected to succeed. |
39 std::string* const kExpectSuccess = nullptr; | 39 std::string* const kExpectSuccess = nullptr; |
40 | 40 |
41 void DoNothingWithBool(bool b) {} | 41 void DoNothingWithBool(bool b) {} |
42 | 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); | |
Devlin
2015/12/10 18:16:57
nit: I think GetActiveWebContents() is more clear.
lazyboy
2015/12/10 18:59:44
Done.
| |
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 | |
43 } // namespace | 80 } // namespace |
44 | 81 |
45 class ServiceWorkerTest : public ExtensionApiTest { | 82 class ServiceWorkerTest : public ExtensionApiTest { |
46 public: | 83 public: |
47 ServiceWorkerTest() : current_channel_(version_info::Channel::UNKNOWN) {} | 84 ServiceWorkerTest() : current_channel_(version_info::Channel::UNKNOWN) {} |
48 | 85 |
49 ~ServiceWorkerTest() override {} | 86 ~ServiceWorkerTest() override {} |
50 | 87 |
51 protected: | 88 protected: |
52 // Returns the ProcessManager for the test's profile. | 89 // Returns the ProcessManager for the test's profile. |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 listener_v2.set_failure_message("FAILURE_V2"); | 283 listener_v2.set_failure_message("FAILURE_V2"); |
247 | 284 |
248 // Update to version 2.0. | 285 // Update to version 2.0. |
249 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0)); | 286 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0)); |
250 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) | 287 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) |
251 ->enabled_extensions() | 288 ->enabled_extensions() |
252 .GetByID(kId)); | 289 .GetByID(kId)); |
253 EXPECT_TRUE(listener_v2.WaitUntilSatisfied()); | 290 EXPECT_TRUE(listener_v2.WaitUntilSatisfied()); |
254 } | 291 } |
255 | 292 |
293 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, UpdateWithoutSkipWaiting) { | |
294 base::ScopedTempDir scoped_temp_dir; | |
295 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir()); | |
296 base::FilePath pem_path = test_data_dir_.AppendASCII("service_worker") | |
297 .AppendASCII("update_without_skip_waiting") | |
298 .AppendASCII("update_without_skip_waiting.pem"); | |
299 base::FilePath path_v1 = PackExtensionWithOptions( | |
300 test_data_dir_.AppendASCII("service_worker") | |
301 .AppendASCII("update_without_skip_waiting") | |
302 .AppendASCII("v1"), | |
303 scoped_temp_dir.path().AppendASCII("v1.crx"), pem_path, base::FilePath()); | |
304 base::FilePath path_v2 = PackExtensionWithOptions( | |
305 test_data_dir_.AppendASCII("service_worker") | |
306 .AppendASCII("update_without_skip_waiting") | |
307 .AppendASCII("v2"), | |
308 scoped_temp_dir.path().AppendASCII("v2.crx"), pem_path, base::FilePath()); | |
309 const char* kId = "mhnnnflgagdakldgjpfcofkiocpdmogl"; | |
310 | |
311 // Install version 1.0 of the extension. | |
312 ASSERT_TRUE(InstallExtension(path_v1, 1)); | |
313 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) | |
314 ->enabled_extensions() | |
315 .GetByID(kId)); | |
316 const Extension* extension = extensions::ExtensionRegistry::Get(profile()) | |
317 ->enabled_extensions() | |
318 .GetByID(kId); | |
319 | |
320 ExtensionTestMessageListener listener_v1("Pong from version 1", false); | |
321 listener_v1.set_failure_message("FAILURE_V1"); | |
322 content::WebContents* web_contents = | |
323 AddTab(browser(), extension->GetResourceURL("page.html")); | |
324 EXPECT_TRUE(listener_v1.WaitUntilSatisfied()); | |
325 | |
326 // Update to version 2.0. | |
327 EXPECT_TRUE(UpdateExtension(kId, path_v2, 0)); | |
328 EXPECT_TRUE(extensions::ExtensionRegistry::Get(profile()) | |
329 ->enabled_extensions() | |
330 .GetByID(kId)); | |
331 const Extension* extension_after_update = | |
332 extensions::ExtensionRegistry::Get(profile()) | |
333 ->enabled_extensions() | |
334 .GetByID(kId); | |
335 | |
336 // Service worker version 2 would be installed but it won't be controlling | |
337 // the extension page yet. | |
338 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.
| |
339 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.
| |
340 web_contents = | |
341 AddTab(browser(), extension_after_update->GetResourceURL("page.html")); | |
342 EXPECT_TRUE(listener_v2.WaitUntilSatisfied()); | |
343 | |
344 // Navigate the tab away from the extension page so that no clients are | |
345 // using the service worker. | |
346 // Note that just closing the tab with WebContentsDestroyedWatcher doesn't | |
347 // seem to be enough because it returns too early. | |
348 WebContentsLoadStopObserver navigate_away_observer(web_contents); | |
349 web_contents->GetController().LoadURL( | |
350 GURL(url::kAboutBlankURL), content::Referrer(), ui::PAGE_TRANSITION_TYPED, | |
351 std::string()); | |
352 navigate_away_observer.WaitForLoadStop(); | |
353 | |
354 // Now expect service worker version 2 to control the extension page. | |
355 ExtensionTestMessageListener listener_v2next("Pong from version 2", false); | |
356 listener_v2next.set_failure_message("FAILURE_V2"); | |
357 web_contents = | |
358 AddTab(browser(), extension_after_update->GetResourceURL("page.html")); | |
359 EXPECT_TRUE(listener_v2next.WaitUntilSatisfied()); | |
360 } | |
361 | |
256 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { | 362 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, FetchArbitraryPaths) { |
257 const Extension* extension = | 363 const Extension* extension = |
258 StartTestFromBackgroundPage("fetch.js", kExpectSuccess); | 364 StartTestFromBackgroundPage("fetch.js", kExpectSuccess); |
259 | 365 |
260 // Open some arbirary paths. Their contents should be what the service worker | 366 // Open some arbirary paths. Their contents should be what the service worker |
261 // responds with, which in this case is the path of the fetch. | 367 // responds with, which in this case is the path of the fetch. |
262 EXPECT_EQ( | 368 EXPECT_EQ( |
263 "Caught a fetch for /index.html", | 369 "Caught a fetch for /index.html", |
264 NavigateAndExtractInnerText(extension->GetResourceURL("index.html"))); | 370 NavigateAndExtractInnerText(extension->GetResourceURL("index.html"))); |
265 EXPECT_EQ("Caught a fetch for /path/to/other.html", | 371 EXPECT_EQ("Caught a fetch for /path/to/other.html", |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
540 push_message_listener.set_failure_message("FAIL"); | 646 push_message_listener.set_failure_message("FAIL"); |
541 gcm::IncomingMessage message; | 647 gcm::IncomingMessage message; |
542 message.sender_id = "1234567890"; | 648 message.sender_id = "1234567890"; |
543 message.raw_data = "testdata"; | 649 message.raw_data = "testdata"; |
544 message.decrypted = true; | 650 message.decrypted = true; |
545 push_service()->OnMessage(app_identifier.app_id(), message); | 651 push_service()->OnMessage(app_identifier.app_id(), message); |
546 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); | 652 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); |
547 } | 653 } |
548 | 654 |
549 } // namespace extensions | 655 } // namespace extensions |
OLD | NEW |