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

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: Switch to single autodetect, register native observers, change observer creation pattern 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)
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 104
100 void SetUp() override { 105 void SetUp() override {
101 NetworkChangeNotifier::SetTestNotificationsOnly(true); 106 NetworkChangeNotifier::SetTestNotificationsOnly(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
108 113
114 #if defined(OS_ANDROID)
115 // On Android, the actual device connectivity status can interfere with some
116 // of the browser tests
117 BackgroundSyncNetworkObserverAndroid::DisableNotificationForTesting();
jkarlin 2015/09/19 00:16:07 What if you added IgnoreNetworkChangeNotifierForTe
iclelland 2015/09/21 18:49:30 I'll try that approach; it might be cleaner than p
iclelland 2015/09/22 13:18:15 This is done now, in the latest patch set.
118 #endif
119
109 ContentBrowserTest::SetUp(); 120 ContentBrowserTest::SetUp();
110 } 121 }
111 122
112 void SetIncognitoMode(bool incognito) { 123 void SetIncognitoMode(bool incognito) {
113 shell_ = incognito ? CreateOffTheRecordBrowser() : shell(); 124 shell_ = incognito ? CreateOffTheRecordBrowser() : shell();
114 } 125 }
115 126
127 BackgroundSyncContext* GetSyncContextFromShell(Shell* shell) {
jkarlin 2015/09/19 00:16:07 Can you use this new function in OneShotPending?
iclelland 2015/09/21 18:49:30 I could, but we need the storage partition separat
128 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition(
129 shell_->web_contents()->GetBrowserContext());
130 return storage->GetBackgroundSyncContext();
131 }
132
116 void SetUpCommandLine(base::CommandLine* command_line) override { 133 void SetUpCommandLine(base::CommandLine* command_line) override {
117 // TODO(jkarlin): Remove this once background sync is no longer 134 // TODO(jkarlin): Remove this once background sync is no longer
118 // experimental. 135 // experimental.
119 command_line->AppendSwitch( 136 command_line->AppendSwitch(
120 switches::kEnableExperimentalWebPlatformFeatures); 137 switches::kEnableExperimentalWebPlatformFeatures);
121 } 138 }
122 139
123 void SetUpOnMainThread() override { 140 void SetUpOnMainThread() override {
124 https_server_.reset(new net::SpawnedTestServer( 141 https_server_.reset(new net::SpawnedTestServer(
125 net::SpawnedTestServer::TYPE_HTTPS, 142 net::SpawnedTestServer::TYPE_HTTPS,
126 net::BaseTestServer::SSLOptions( 143 net::BaseTestServer::SSLOptions(
127 net::BaseTestServer::SSLOptions::CERT_OK), 144 net::BaseTestServer::SSLOptions::CERT_OK),
128 base::FilePath(FILE_PATH_LITERAL("content/test/data/")))); 145 base::FilePath(FILE_PATH_LITERAL("content/test/data/"))));
129 ASSERT_TRUE(https_server_->Start()); 146 ASSERT_TRUE(https_server_->Start());
130 147
148 SetIncognitoMode(false);
149
131 SetOnline(true); 150 SetOnline(true);
132 151
133 SetIncognitoMode(false);
134
135 ASSERT_TRUE(LoadTestPage(kDefaultTestURL)); 152 ASSERT_TRUE(LoadTestPage(kDefaultTestURL));
136 153
137 ContentBrowserTest::SetUpOnMainThread(); 154 ContentBrowserTest::SetUpOnMainThread();
138 } 155 }
139 156
140 void TearDownOnMainThread() override { https_server_.reset(); } 157 void TearDownOnMainThread() override { https_server_.reset(); }
141 158
142 bool LoadTestPage(const std::string& path) { 159 bool LoadTestPage(const std::string& path) {
143 return NavigateToURL(shell_, https_server_->GetURL(path)); 160 return NavigateToURL(shell_, https_server_->GetURL(path));
144 } 161 }
145 162
146 bool RunScript(const std::string& script, std::string* result) { 163 bool RunScript(const std::string& script, std::string* result) {
147 return content::ExecuteScriptAndExtractString(shell_->web_contents(), 164 return content::ExecuteScriptAndExtractString(shell_->web_contents(),
148 script, result); 165 script, result);
149 } 166 }
150 167
151 void SetOnline(bool online); 168 void SetOnline(bool online);
jkarlin 2015/09/19 00:16:07 Please add comment noting that this runs asynchron
169 void SetOnlineOnIOThread(
170 const scoped_refptr<BackgroundSyncContext>& sync_context,
171 bool online);
152 172
153 // Returns true if the one-shot sync with tag is currently pending. Fails 173 // Returns true if the one-shot sync with tag is currently pending. Fails
154 // (assertion failure) if the tag isn't registered. 174 // (assertion failure) if the tag isn't registered.
155 bool OneShotPending(const std::string& tag); 175 bool OneShotPending(const std::string& tag);
156 176
157 bool PopConsole(const std::string& expected_msg); 177 bool PopConsole(const std::string& expected_msg);
158 bool RegisterServiceWorker(); 178 bool RegisterServiceWorker();
159 bool RegisterOneShot(const std::string& tag); 179 bool RegisterOneShot(const std::string& tag);
160 bool UnregisterOneShot(const std::string& tag); 180 bool UnregisterOneShot(const std::string& tag);
161 bool UnregisterOneShotTwice(const std::string& tag); 181 bool UnregisterOneShotTwice(const std::string& tag);
162 bool GetRegistrationOneShot(const std::string& tag); 182 bool GetRegistrationOneShot(const std::string& tag);
163 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags); 183 bool GetRegistrationsOneShot(const std::vector<std::string>& expected_tags);
164 bool CompleteDelayedOneShot(); 184 bool CompleteDelayedOneShot();
165 bool RejectDelayedOneShot(); 185 bool RejectDelayedOneShot();
166 186
167 private: 187 private:
168 scoped_ptr<net::SpawnedTestServer> https_server_; 188 scoped_ptr<net::SpawnedTestServer> https_server_;
169 Shell* shell_ = nullptr; 189 Shell* shell_ = nullptr;
170 190
171 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest); 191 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncBrowserTest);
172 }; 192 };
173 193
174 void BackgroundSyncBrowserTest::SetOnline(bool online) { 194 void BackgroundSyncBrowserTest::SetOnline(bool online) {
175 if (online) { 195 ASSERT_TRUE(shell_);
176 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 196 BrowserThread::PostTask(
177 NetworkChangeNotifier::CONNECTION_WIFI); 197 BrowserThread::IO, FROM_HERE,
178 } else { 198 base::Bind(&BackgroundSyncBrowserTest::SetOnlineOnIOThread,
179 NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 199 base::Unretained(this),
180 NetworkChangeNotifier::CONNECTION_NONE); 200 base::Unretained(GetSyncContextFromShell(shell_)), online));
181 }
182 base::RunLoop().RunUntilIdle(); 201 base::RunLoop().RunUntilIdle();
183 } 202 }
184 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->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI);
212 } else {
213 network_observer->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_NONE);
214 }
215 }
216
185 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) { 217 bool BackgroundSyncBrowserTest::OneShotPending(const std::string& tag) {
186 bool is_pending; 218 bool is_pending;
187 base::RunLoop run_loop; 219 base::RunLoop run_loop;
188 220
189 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition( 221 StoragePartition* storage = BrowserContext::GetDefaultStoragePartition(
190 shell_->web_contents()->GetBrowserContext()); 222 shell_->web_contents()->GetBrowserContext());
191 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext(); 223 BackgroundSyncContext* sync_context = storage->GetBackgroundSyncContext();
192 ServiceWorkerContextWrapper* service_worker_context = 224 ServiceWorkerContextWrapper* service_worker_context =
193 static_cast<ServiceWorkerContextWrapper*>( 225 static_cast<ServiceWorkerContextWrapper*>(
194 storage->GetServiceWorkerContext()); 226 storage->GetServiceWorkerContext());
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 EXPECT_TRUE(RegisterServiceWorker()); 449 EXPECT_TRUE(RegisterServiceWorker());
418 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page. 450 EXPECT_TRUE(LoadTestPage(kDefaultTestURL)); // Control the page.
419 451
420 EXPECT_TRUE(RegisterOneShot("unregister")); 452 EXPECT_TRUE(RegisterOneShot("unregister"));
421 EXPECT_TRUE(PopConsole("ok - unregister completed")); 453 EXPECT_TRUE(PopConsole("ok - unregister completed"));
422 } 454 }
423 455
424 } // namespace 456 } // namespace
425 457
426 } // namespace content 458 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698