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

Side by Side Diff: content/browser/background_sync/background_sync_browsertest.cc

Issue 1294603003: [BackgroundSync] Trigger Background Sync events when Chrome is backgrounded on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase (Post-Blink-Chromium-Merge) Created 5 years, 3 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 <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/strings/string_split.h" 10 #include "base/strings/string_split.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/task_runner_util.h" 12 #include "base/task_runner_util.h"
13 #include "content/browser/background_sync/background_sync_manager.h" 13 #include "content/browser/background_sync/background_sync_manager.h"
14 #include "content/browser/background_sync/background_sync_network_observer.h"
14 #include "content/browser/background_sync/background_sync_registration_handle.h" 15 #include "content/browser/background_sync/background_sync_registration_handle.h"
15 #include "content/browser/background_sync/background_sync_status.h" 16 #include "content/browser/background_sync/background_sync_status.h"
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" 17 #include "content/browser/service_worker/service_worker_context_wrapper.h"
17 #include "content/browser/service_worker/service_worker_registration.h" 18 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/public/browser/background_sync_context.h" 19 #include "content/public/browser/background_sync_context.h"
19 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/storage_partition.h" 21 #include "content/public/browser/storage_partition.h"
21 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h" 23 #include "content/public/common/content_switches.h"
23 #include "content/public/test/browser_test_utils.h" 24 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/content_browser_test.h" 25 #include "content/public/test/content_browser_test.h"
25 #include "content/public/test/content_browser_test_utils.h" 26 #include "content/public/test/content_browser_test_utils.h"
26 #include "content/public/test/test_utils.h" 27 #include "content/public/test/test_utils.h"
27 #include "content/shell/browser/shell.h" 28 #include "content/shell/browser/shell.h"
28 #include "net/base/network_change_notifier.h" 29 #include "net/base/network_change_notifier.h"
29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
30 31
32 #if defined(OS_ANDROID)
jkarlin 2015/09/24 17:55:19 Remove this block
iclelland 2015/09/24 20:37:19 Done.
33 #include "content/browser/android/background_sync_network_observer_android.h"
34 #endif
35
31 using net::NetworkChangeNotifier; 36 using net::NetworkChangeNotifier;
32 37
33 namespace content { 38 namespace content {
34 39
35 namespace { 40 namespace {
36 41
37 const char kDefaultTestURL[] = "files/background_sync/test.html"; 42 const char kDefaultTestURL[] = "files/background_sync/test.html";
38 43
39 const char kSuccessfulOperationPrefix[] = "ok - "; 44 const char kSuccessfulOperationPrefix[] = "ok - ";
40 45
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag, 96 url, base::Bind(&OneShotPendingDidGetSWRegistration, sync_context, tag,
92 callback)); 97 callback));
93 } 98 }
94 99
95 class BackgroundSyncBrowserTest : public ContentBrowserTest { 100 class BackgroundSyncBrowserTest : public ContentBrowserTest {
96 public: 101 public:
97 BackgroundSyncBrowserTest() {} 102 BackgroundSyncBrowserTest() {}
98 ~BackgroundSyncBrowserTest() override {} 103 ~BackgroundSyncBrowserTest() override {}
99 104
100 void SetUp() override { 105 void SetUp() override {
101 NetworkChangeNotifier::SetTestNotificationsOnly(true); 106 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true);
102 107
103 #if defined(OS_CHROMEOS) 108 #if defined(OS_CHROMEOS)
104 // ChromeOS's NetworkChangeNotifier doesn't get created in 109 // ChromeOS's NetworkChangeNotifier doesn't get created in
105 // content_browsertests, so make one now. 110 // content_browsertests, so make one now.
106 net::NetworkChangeNotifier::CreateMock(); 111 net::NetworkChangeNotifier::CreateMock();
107 #endif 112 #endif
jkarlin 2015/09/24 17:51:30 Can you try deleting the above chromeos code? I th
iclelland 2015/09/24 20:37:19 Done.
108 113
109 ContentBrowserTest::SetUp(); 114 ContentBrowserTest::SetUp();
110 } 115 }
111 116
112 void SetIncognitoMode(bool incognito) { 117 void SetIncognitoMode(bool incognito) {
113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); 118 shell_ = incognito ? CreateOffTheRecordBrowser() : shell();
114 } 119 }
115 120
121 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) {
122 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition(
123 shell_->web_contents()->GetBrowserContext());
124 return storage->GetBackgroundSyncContext();
125 }
126
116 void SetUpCommandLine(base::CommandLine* command_line) override { 127 void SetUpCommandLine(base::CommandLine* command_line) override {
117 // TODO(jkarlin): Remove this once background sync is no longer 128 // TODO(jkarlin): Remove this once background sync is no longer
118 // experimental. 129 // experimental.
119 command_line->AppendSwitch( 130 command_line->AppendSwitch(
120 switches::kEnableExperimentalWebPlatformFeatures); 131 switches::kEnableExperimentalWebPlatformFeatures);
121 } 132 }
122 133
123 void SetUpOnMainThread() override { 134 void SetUpOnMainThread() override {
124 https_server_.reset(new net::SpawnedTestServer( 135 https_server_.reset(new net::SpawnedTestServer(
125 net::SpawnedTestServer::TYPE_HTTPS, 136 net::SpawnedTestServer::TYPE_HTTPS,
126 net::BaseTestServer::SSLOptions( 137 net::BaseTestServer::SSLOptions(
127 net::BaseTestServer::SSLOptions::CERT_OK), 138 net::BaseTestServer::SSLOptions::CERT_OK),
128 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); 139 base::FilePath(FILE_PATH_LITERAL("content/test/data/"))));
129 ASSERT_TRUE(https_server_->Start()); 140 ASSERT_TRUE(https_server_->Start());
130 141
142 SetIncognitoMode(false);
143
131 SetOnline(true); 144 SetOnline(true);
132 145
133 SetIncognitoMode(false);
134
135 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); 146 ASSERT_TRUE(LoadTestPage(kDefaultTestURL));
136 147
137 ContentBrowserTest::SetUpOnMainThread(); 148 ContentBrowserTest::SetUpOnMainThread();
138 } 149 }
139 150
140 void TearDownOnMainThread() override { https_server_.reset(); } 151 void TearDownOnMainThread() override { https_server_.reset(); }
141 152
142 bool LoadTestPage(const std::string& path) { 153 bool LoadTestPage(const std::string& path) {
143 return NavigateToURL(shell_, https_server_->GetURL(path)); 154 return NavigateToURL(shell_, https_server_->GetURL(path));
144 } 155 }
145 156
146 bool RunScript(const std::string& script, std::string* result) { 157 bool RunScript(const std::string& script, std::string* result) {
147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), 158 return content::ExecuteScriptAndExtractString(shell_->web_contents(),
148 script, result); 159 script, result);
149 } 160 }
150 161
162 // This runs asynchronously on the IO thread, but we don't need to wait for it
163 // to complete before running a background sync operation, since those also
164 // run on the IO thread.
151 void SetOnline(bool online); 165 void SetOnline(bool online);
166 void SetOnlineOnIOThread(
167 const scoped_refptr<BackgroundSyncContext>& sync_context,
168 bool online);
152 169
153 // Returns true if the one-shot sync with tag is currently pending. Fails 170 // Returns true if the one-shot sync with tag is currently pending. Fails
154 // (assertion failure) if the tag isn't registered. 171 // (assertion failure) if the tag isn't registered.
155 bool OneShotPending(const std::string& tag); 172 bool OneShotPending(const std::string& tag);
156 173
157 bool PopConsole(const std::string& expected_msg); 174 bool PopConsole(const std::string& expected_msg);
158 bool RegisterServiceWorker(); 175 bool RegisterServiceWorker();
159 bool RegisterOneShot(const std::string& tag); 176 bool RegisterOneShot(const std::string& tag);
160 bool UnregisterOneShot(const std::string& tag); 177 bool UnregisterOneShot(const std::string& tag);
161 bool UnregisterOneShotTwice(const std::string& tag); 178 bool UnregisterOneShotTwice(const std::string& tag);
162 bool GetRegistrationOneShot(const std::string& tag); 179 bool GetRegistrationOneShot(const std::string& tag);
163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); 180 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags);
164 bool CompleteDelayedOneShot(); 181 bool CompleteDelayedOneShot();
165 bool RejectDelayedOneShot(); 182 bool RejectDelayedOneShot();
166 bool NotifyWhenDoneOneShot(const std::string& tag); 183 bool NotifyWhenDoneOneShot(const std::string& tag);
167 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg); 184 bool NotifyWhenDoneImmediateOneShot(const std::string& expected_msg);
168 bool StoreRegistrationOneShot(const std::string& tag); 185 bool StoreRegistrationOneShot(const std::string& tag);
169 186
170 private: 187 private:
171 scoped_ptr<net::SpawnedTestServer> https_server_; 188 scoped_ptr<net::SpawnedTestServer> https_server_;
172 Shell* shell_ = nullptr; 189 Shell* shell_ = nullptr;
173 190
174 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); 191 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest);
175 }; 192 };
176 193
177 void BackgroundSyncBrowserTest::SetOnline(bool online) { 194 void BackgroundSyncBrowserTest::SetOnline(bool online) {
178 if (online) { 195 ASSERT_TRUE(shell_);
179 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 196 BrowserThread::PostTask(
180 NetworkChangeNotifier::CONNECTION_WIFI); 197 BrowserThread::IO, FROM_HERE,
181 } else { 198 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread,
182 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 199 base::Unretained(this),
183 NetworkChangeNotifier::CONNECTION_NONE); 200 base::Unretained(GetSyncContextFromShell(shell_)), online));
184 }
185 base::RunLoop().RunUntilIdle(); 201 base::RunLoop().RunUntilIdle();
186 } 202 }
187 203
204 void BackgroundSyncBrowserTest::SetOnlineOnIOThread(
205 const scoped_refptr<BackgroundSyncContext>& sync_context,
206 bool online) {
207 BackgroundSyncManager* sync_manager = sync_context->background_sync_manager();
208 BackgroundSyncNetworkObserver* network_observer =
209 sync_manager->GetNetworkObserverForTesting();
210 if (online) {
211 network_observer->NotifyManagerIfNetworkChanged(
212 NetworkChangeNotifier::CONNECTION_WIFI);
213 } else {
214 network_observer->NotifyManagerIfNetworkChanged(
215 NetworkChangeNotifier::CONNECTION_NONE);
216 }
217 }
218
188 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { 219 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) {
189 bool is_pending; 220 bool is_pending;
190 base::RunLoop run_loop; 221 base::RunLoop run_loop;
191 222
192 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( 223 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition(
193 shell_->web_contents()->GetBrowserContext()); 224 shell_->web_contents()->GetBrowserContext());
194 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); 225 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext();
195 ServiceWorkerContextWrapper* service_worker_context = 226 ServiceWorkerContextWrapper* service_worker_context =
196 static_cast<ServiceWorkerContextWrapper*>( 227 static_cast<ServiceWorkerContextWrapper*>(
197 storage->GetServiceWorkerContext()); 228 storage->GetServiceWorkerContext());
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 EXPECT_TRUE(StoreRegistrationOneShot("delay")); 540 EXPECT_TRUE(StoreRegistrationOneShot("delay"));
510 541
511 EXPECT_TRUE(RejectDelayedOneShot()); 542 EXPECT_TRUE(RejectDelayedOneShot());
512 EXPECT_TRUE(PopConsole("ok - delay rejected")); 543 EXPECT_TRUE(PopConsole("ok - delay rejected"));
513 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false")); 544 EXPECT_TRUE(NotifyWhenDoneImmediateOneShot("ok - delay result: false"));
514 } 545 }
515 546
516 } // namespace 547 } // namespace
517 548
518 } // namespace content 549 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698