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

Side by Side Diff: chrome/browser/extensions/service_worker_apitest.cc

Issue 2943583002: [extension SW] Support lazy events from extension service workers. (Closed)
Patch Set: Created 3 years, 6 months 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 18 matching lines...) Expand all
29 #include "content/public/browser/navigation_controller.h" 29 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/navigation_entry.h" 30 #include "content/public/browser/navigation_entry.h"
31 #include "content/public/browser/service_worker_context.h" 31 #include "content/public/browser/service_worker_context.h"
32 #include "content/public/browser/storage_partition.h" 32 #include "content/public/browser/storage_partition.h"
33 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/content_switches.h" 34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/origin_util.h" 35 #include "content/public/common/origin_util.h"
36 #include "content/public/common/page_type.h" 36 #include "content/public/common/page_type.h"
37 #include "content/public/test/background_sync_test_util.h" 37 #include "content/public/test/background_sync_test_util.h"
38 #include "content/public/test/browser_test_utils.h" 38 #include "content/public/test/browser_test_utils.h"
39 #include "content/public/test/service_worker_test_helpers.h"
39 #include "extensions/browser/extension_host.h" 40 #include "extensions/browser/extension_host.h"
40 #include "extensions/browser/extension_registry.h" 41 #include "extensions/browser/extension_registry.h"
41 #include "extensions/browser/process_manager.h" 42 #include "extensions/browser/process_manager.h"
42 #include "extensions/test/background_page_watcher.h" 43 #include "extensions/test/background_page_watcher.h"
43 #include "extensions/test/extension_test_message_listener.h" 44 #include "extensions/test/extension_test_message_listener.h"
44 #include "net/dns/mock_host_resolver.h" 45 #include "net/dns/mock_host_resolver.h"
45 #include "net/test/embedded_test_server/embedded_test_server.h" 46 #include "net/test/embedded_test_server/embedded_test_server.h"
47 #include "url/url_constants.h"
46 48
47 namespace extensions { 49 namespace extensions {
48 50
49 namespace { 51 namespace {
50 52
51 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that 53 // Pass into ServiceWorkerTest::StartTestFromBackgroundPage to indicate that
52 // registration is expected to succeed. 54 // registration is expected to succeed.
53 std::string* const kExpectSuccess = nullptr; 55 std::string* const kExpectSuccess = nullptr;
54 56
55 void DoNothingWithBool(bool b) {} 57 void DoNothingWithBool(bool b) {}
56 58
57 // Returns the newly added WebContents. 59 // Returns the newly added WebContents.
58 content::WebContents* AddTab(Browser* browser, const GURL& url) { 60 content::WebContents* AddTab(Browser* browser, const GURL& url) {
59 int starting_tab_count = browser->tab_strip_model()->count(); 61 int starting_tab_count = browser->tab_strip_model()->count();
60 ui_test_utils::NavigateToURLWithDisposition( 62 ui_test_utils::NavigateToURLWithDisposition(
61 browser, url, WindowOpenDisposition::NEW_FOREGROUND_TAB, 63 browser, url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
62 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 64 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
63 int tab_count = browser->tab_strip_model()->count(); 65 int tab_count = browser->tab_strip_model()->count();
64 EXPECT_EQ(starting_tab_count + 1, tab_count); 66 EXPECT_EQ(starting_tab_count + 1, tab_count);
65 return browser->tab_strip_model()->GetActiveWebContents(); 67 return browser->tab_strip_model()->GetActiveWebContents();
66 } 68 }
67 69
70 GURL GetOriginForExtension(const ExtensionId& extension_id) {
Devlin 2017/06/16 02:34:41 Is this different than extension->url()?
lazyboy 2017/06/16 18:48:04 You're right. Fixed.
71 return GURL(std::string(kExtensionScheme) + url::kStandardSchemeSeparator +
72 extension_id);
73 }
74
68 class WebContentsLoadStopObserver : content::WebContentsObserver { 75 class WebContentsLoadStopObserver : content::WebContentsObserver {
69 public: 76 public:
70 explicit WebContentsLoadStopObserver(content::WebContents* web_contents) 77 explicit WebContentsLoadStopObserver(content::WebContents* web_contents)
71 : content::WebContentsObserver(web_contents), 78 : content::WebContentsObserver(web_contents),
72 load_stop_observed_(false) {} 79 load_stop_observed_(false) {}
73 80
74 void WaitForLoadStop() { 81 void WaitForLoadStop() {
75 if (load_stop_observed_) 82 if (load_stop_observed_)
76 return; 83 return;
77 message_loop_runner_ = new content::MessageLoopRunner; 84 message_loop_runner_ = new content::MessageLoopRunner;
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 ui_test_utils::NavigateToURL(browser(), 682 ui_test_utils::NavigateToURL(browser(),
676 extension->GetResourceURL("page.html")); 683 extension->GetResourceURL("page.html"));
677 content::WebContents* web_contents = 684 content::WebContents* web_contents =
678 browser()->tab_strip_model()->GetActiveWebContents(); 685 browser()->tab_strip_model()->GetActiveWebContents();
679 std::string result; 686 std::string result;
680 ASSERT_TRUE(content::ExecuteScriptAndExtractString( 687 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
681 web_contents, "window.runEventTest()", &result)); 688 web_contents, "window.runEventTest()", &result));
682 ASSERT_EQ("chrome.tabs.onUpdated callback", result); 689 ASSERT_EQ("chrome.tabs.onUpdated callback", result);
683 } 690 }
684 691
692 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, EventsToStoppedWorker) {
693 // Extensions APIs from SW are only enabled on trunk.
694 ScopedCurrentChannel current_channel_override(version_info::Channel::UNKNOWN);
695 const Extension* extension = LoadExtensionWithFlags(
696 test_data_dir_.AppendASCII("service_worker/events_to_stopped_worker"),
697 kFlagNone);
698 ASSERT_TRUE(extension);
699 ui_test_utils::NavigateToURL(browser(),
700 extension->GetResourceURL("page.html"));
701 content::WebContents* web_contents =
702 browser()->tab_strip_model()->GetActiveWebContents();
703 {
704 content::StoragePartition* storage_partition =
705 content::BrowserContext::GetDefaultStoragePartition(
706 browser()->profile());
707 std::string result;
708 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
709 web_contents, "window.runServiceWorker()", &result));
710 ASSERT_EQ("ready", result);
711
712 {
713 base::RunLoop stop_called_run_loop;
714 content::StopServiceWorkerAtOrigin(
Devlin 2017/06/16 02:34:42 can we move the storage_partition var into this sc
lazyboy 2017/06/16 18:48:04 Done.
715 storage_partition->GetServiceWorkerContext(),
716 GetOriginForExtension(extension->id()),
717 stop_called_run_loop.QuitClosure());
718 stop_called_run_loop.Run();
719 }
Devlin 2017/06/16 02:34:41 Is there a simple method we can use to EXPECT_ tha
lazyboy 2017/06/16 18:48:04 Not that I know of, hopefully StoppedObserver will
720 }
721
722 std::string result;
723 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
724 web_contents, "window.createTabThenUpdate()", &result));
725 ASSERT_EQ("chrome.tabs.onUpdated callback", result);
726 }
727
685 // Tests that worker ref count increments while extension API function is 728 // Tests that worker ref count increments while extension API function is
686 // active. 729 // active.
687 730
688 // Flaky on Linux and ChromeOS, https://crbug.com/702126 731 // Flaky on Linux and ChromeOS, https://crbug.com/702126
689 #if defined(OS_LINUX) 732 #if defined(OS_LINUX)
690 #define MAYBE_WorkerRefCount DISABLED_WorkerRefCount 733 #define MAYBE_WorkerRefCount DISABLED_WorkerRefCount
691 #else 734 #else
692 #define MAYBE_WorkerRefCount WorkerRefCount 735 #define MAYBE_WorkerRefCount WorkerRefCount
693 #endif 736 #endif
694 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, MAYBE_WorkerRefCount) { 737 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, MAYBE_WorkerRefCount) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 message.sender_id = "1234567890"; 941 message.sender_id = "1234567890";
899 message.raw_data = "testdata"; 942 message.raw_data = "testdata";
900 message.decrypted = true; 943 message.decrypted = true;
901 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure()); 944 push_service()->SetMessageCallbackForTesting(run_loop.QuitClosure());
902 push_service()->OnMessage(app_identifier.app_id(), message); 945 push_service()->OnMessage(app_identifier.app_id(), message);
903 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied()); 946 EXPECT_TRUE(push_message_listener.WaitUntilSatisfied());
904 run_loop.Run(); // Wait until the message is handled by push service. 947 run_loop.Run(); // Wait until the message is handled by push service.
905 } 948 }
906 949
907 } // namespace extensions 950 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698