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 "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
11 #include "components/version_info/version_info.h" | 11 #include "components/version_info/version_info.h" |
12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/content_switches.h" |
15 #include "content/public/common/page_type.h" | 16 #include "content/public/common/page_type.h" |
| 17 #include "content/public/test/background_sync_test_util.h" |
16 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
17 #include "extensions/browser/extension_host.h" | 19 #include "extensions/browser/extension_host.h" |
18 #include "extensions/browser/process_manager.h" | 20 #include "extensions/browser/process_manager.h" |
19 #include "extensions/test/background_page_watcher.h" | 21 #include "extensions/test/background_page_watcher.h" |
20 #include "extensions/test/extension_test_message_listener.h" | 22 #include "extensions/test/extension_test_message_listener.h" |
21 | 23 |
22 namespace extensions { | 24 namespace extensions { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 return ExtractInnerText(Navigate(url)); | 108 return ExtractInnerText(Navigate(url)); |
107 } | 109 } |
108 | 110 |
109 private: | 111 private: |
110 // Sets the channel to "trunk" since service workers are restricted to trunk. | 112 // Sets the channel to "trunk" since service workers are restricted to trunk. |
111 ScopedCurrentChannel current_channel_; | 113 ScopedCurrentChannel current_channel_; |
112 | 114 |
113 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); | 115 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTest); |
114 }; | 116 }; |
115 | 117 |
| 118 class ServiceWorkerBackgroundSyncTest : public ServiceWorkerTest { |
| 119 public: |
| 120 ServiceWorkerBackgroundSyncTest() {} |
| 121 ~ServiceWorkerBackgroundSyncTest() override {} |
| 122 |
| 123 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 124 // ServiceWorkerRegistration.sync requires experimental flag. |
| 125 command_line->AppendSwitch( |
| 126 switches::kEnableExperimentalWebPlatformFeatures); |
| 127 ServiceWorkerTest::SetUpCommandLine(command_line); |
| 128 } |
| 129 |
| 130 void SetUp() override { |
| 131 content::background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); |
| 132 ServiceWorkerTest::SetUp(); |
| 133 } |
| 134 |
| 135 private: |
| 136 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerBackgroundSyncTest); |
| 137 }; |
| 138 |
116 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { | 139 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterSucceedsOnTrunk) { |
117 StartTestFromBackgroundPage("register.js", kExpectSuccess); | 140 StartTestFromBackgroundPage("register.js", kExpectSuccess); |
118 } | 141 } |
119 | 142 |
120 // This feature is restricted to trunk, so on dev it should have existing | 143 // This feature is restricted to trunk, so on dev it should have existing |
121 // behavior - which is for it to fail. | 144 // behavior - which is for it to fail. |
122 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) { | 145 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, RegisterFailsOnDev) { |
123 ScopedCurrentChannel current_channel_override( | 146 ScopedCurrentChannel current_channel_override( |
124 version_info::Channel::DEV); | 147 version_info::Channel::DEV); |
125 std::string error; | 148 std::string error; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 // should fail. | 358 // should fail. |
336 // Note that this also tests that service workers can be registered from tabs. | 359 // Note that this also tests that service workers can be registered from tabs. |
337 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); | 360 EXPECT_TRUE(RunExtensionSubtest("service_worker/no_background", "page.html")); |
338 } | 361 } |
339 | 362 |
340 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) { | 363 IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, NotificationAPI) { |
341 EXPECT_TRUE(RunExtensionSubtest("service_worker/notifications/has_permission", | 364 EXPECT_TRUE(RunExtensionSubtest("service_worker/notifications/has_permission", |
342 "page.html")); | 365 "page.html")); |
343 } | 366 } |
344 | 367 |
| 368 IN_PROC_BROWSER_TEST_F(ServiceWorkerBackgroundSyncTest, Sync) { |
| 369 const Extension* extension = LoadExtensionWithFlags( |
| 370 test_data_dir_.AppendASCII("service_worker/sync"), kFlagNone); |
| 371 ASSERT_TRUE(extension); |
| 372 ui_test_utils::NavigateToURL(browser(), |
| 373 extension->GetResourceURL("page.html")); |
| 374 content::WebContents* web_contents = |
| 375 browser()->tab_strip_model()->GetActiveWebContents(); |
| 376 |
| 377 // Prevent firing by going offline. |
| 378 content::background_sync_test_util::SetOnline(web_contents, false); |
| 379 |
| 380 ExtensionTestMessageListener sync_listener("SYNC: send-chats", false); |
| 381 sync_listener.set_failure_message("FAIL"); |
| 382 |
| 383 std::string result; |
| 384 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 385 web_contents, "window.runServiceWorker()", &result)); |
| 386 ASSERT_EQ("SERVICE_WORKER_READY", result); |
| 387 |
| 388 EXPECT_FALSE(sync_listener.was_satisfied()); |
| 389 // Resume firing by going online. |
| 390 content::background_sync_test_util::SetOnline(web_contents, true); |
| 391 EXPECT_TRUE(sync_listener.WaitUntilSatisfied()); |
| 392 } |
| 393 |
345 } // namespace extensions | 394 } // namespace extensions |
OLD | NEW |