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

Side by Side Diff: chrome/browser/services/gcm/gcm_driver_unittest.cc

Issue 292813007: Remove dependency on content::BrowserThread from GCMDriver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use SequencedTaskRunner Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/services/gcm/gcm_driver.h" 5 #include "chrome/browser/services/gcm/gcm_driver.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/test/test_simple_task_runner.h"
16 #include "base/threading/thread.h"
14 #include "chrome/browser/services/gcm/fake_gcm_client.h" 17 #include "chrome/browser/services/gcm/fake_gcm_client.h"
15 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h" 18 #include "chrome/browser/services/gcm/fake_gcm_client_factory.h"
16 #include "chrome/browser/services/gcm/gcm_app_handler.h" 19 #include "chrome/browser/services/gcm/gcm_app_handler.h"
17 #include "components/gcm_driver/gcm_client_factory.h" 20 #include "components/gcm_driver/gcm_client_factory.h"
18 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "google_apis/gaia/fake_identity_provider.h" 22 #include "google_apis/gaia/fake_identity_provider.h"
21 #include "google_apis/gaia/fake_oauth2_token_service.h" 23 #include "google_apis/gaia/fake_oauth2_token_service.h"
22 #include "net/url_request/url_request_context_getter.h" 24 #include "net/url_request/url_request_context_getter.h"
23 #include "net/url_request/url_request_test_util.h" 25 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 27
26 namespace gcm { 28 namespace gcm {
27 29
28 namespace { 30 namespace {
29 31
30 const char kTestAccountID1[] = "user1@example.com"; 32 const char kTestAccountID1[] = "user1@example.com";
31 const char kTestAccountID2[] = "user2@example.com"; 33 const char kTestAccountID2[] = "user2@example.com";
32 const char kTestAppID1[] = "TestApp1"; 34 const char kTestAppID1[] = "TestApp1";
33 const char kTestAppID2[] = "TestApp2"; 35 const char kTestAppID2[] = "TestApp2";
34 const char kUserID1[] = "user1"; 36 const char kUserID1[] = "user1";
35 37
36 void PumpCurrentLoop() { 38 void PumpCurrentLoop() {
37 base::MessageLoop::ScopedNestableTaskAllower 39 base::MessageLoop::ScopedNestableTaskAllower
38 nestable_task_allower(base::MessageLoop::current()); 40 nestable_task_allower(base::MessageLoop::current());
39 base::RunLoop().RunUntilIdle(); 41 base::RunLoop().RunUntilIdle();
40 } 42 }
41 43
42 void PumpUILoop() { 44 void PumpUILoop() {
43 PumpCurrentLoop(); 45 PumpCurrentLoop();
44 } 46 }
45 47
46 void PumpIOLoop() {
47 base::RunLoop run_loop;
48 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO,
49 FROM_HERE,
50 base::Bind(&PumpCurrentLoop),
51 run_loop.QuitClosure());
52 run_loop.Run();
53 }
54
55 std::vector<std::string> ToSenderList(const std::string& sender_ids) { 48 std::vector<std::string> ToSenderList(const std::string& sender_ids) {
56 std::vector<std::string> senders; 49 std::vector<std::string> senders;
57 Tokenize(sender_ids, ",", &senders); 50 Tokenize(sender_ids, ",", &senders);
58 return senders; 51 return senders;
59 } 52 }
60 53
61 class FakeGCMAppHandler : public GCMAppHandler { 54 class FakeGCMAppHandler : public GCMAppHandler {
62 public: 55 public:
63 enum Event { 56 enum Event {
64 NO_EVENT, 57 NO_EVENT,
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 GCMDriver* driver() { return driver_.get(); } 164 GCMDriver* driver() { return driver_.get(); }
172 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } 165 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); }
173 const std::string& registration_id() const { return registration_id_; } 166 const std::string& registration_id() const { return registration_id_; }
174 GCMClient::Result registration_result() const { return registration_result_; } 167 GCMClient::Result registration_result() const { return registration_result_; }
175 const std::string& send_message_id() const { return send_message_id_; } 168 const std::string& send_message_id() const { return send_message_id_; }
176 GCMClient::Result send_result() const { return send_result_; } 169 GCMClient::Result send_result() const { return send_result_; }
177 GCMClient::Result unregistration_result() const { 170 GCMClient::Result unregistration_result() const {
178 return unregistration_result_; 171 return unregistration_result_;
179 } 172 }
180 173
174 void PumpIOLoop();
175
181 void ClearResults(); 176 void ClearResults();
182 177
183 bool HasAppHandlers() const; 178 bool HasAppHandlers() const;
184 FakeGCMClient* GetGCMClient(); 179 FakeGCMClient* GetGCMClient();
185 180
186 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode); 181 void CreateDriver(FakeGCMClient::StartMode gcm_client_start_mode);
187 182
188 void SignIn(const std::string& account_id); 183 void SignIn(const std::string& account_id);
189 void SignOut(); 184 void SignOut();
190 185
191 void Register(const std::string& app_id, 186 void Register(const std::string& app_id,
192 const std::vector<std::string>& sender_ids, 187 const std::vector<std::string>& sender_ids,
193 WaitToFinish wait_to_finish); 188 WaitToFinish wait_to_finish);
194 void Send(const std::string& app_id, 189 void Send(const std::string& app_id,
195 const std::string& receiver_id, 190 const std::string& receiver_id,
196 const GCMClient::OutgoingMessage& message, 191 const GCMClient::OutgoingMessage& message,
197 WaitToFinish wait_to_finish); 192 WaitToFinish wait_to_finish);
198 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish); 193 void Unregister(const std::string& app_id, WaitToFinish wait_to_finish);
199 194
200 void WaitForAsyncOperation(); 195 void WaitForAsyncOperation();
201 196
202 private: 197 private:
203 void RegisterCompleted(const std::string& registration_id, 198 void RegisterCompleted(const std::string& registration_id,
204 GCMClient::Result result); 199 GCMClient::Result result);
205 void SendCompleted(const std::string& message_id, GCMClient::Result result); 200 void SendCompleted(const std::string& message_id, GCMClient::Result result);
206 void UnregisterCompleted(GCMClient::Result result); 201 void UnregisterCompleted(GCMClient::Result result);
207 202
208 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
209 base::ScopedTempDir temp_dir_; 203 base::ScopedTempDir temp_dir_;
210 FakeOAuth2TokenService token_service_; 204 FakeOAuth2TokenService token_service_;
211 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; 205 scoped_ptr<FakeIdentityProvider> identity_provider_owner_;
212 FakeIdentityProvider* identity_provider_; 206 FakeIdentityProvider* identity_provider_;
207 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
208 base::MessageLoopForUI message_loop_;
209 base::Thread io_thread_;
Nicolas Zea 2014/05/21 23:43:29 Could you just have one task runner that you use f
jianli 2014/05/22 00:11:29 Yes, we can do that but I am afraid that we're not
Nicolas Zea 2014/05/22 17:00:42 I'm skeptical about the value of the DCHECK's, and
213 scoped_ptr<GCMDriver> driver_; 210 scoped_ptr<GCMDriver> driver_;
214 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; 211 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_;
215 212
216 base::Closure async_operation_completed_callback_; 213 base::Closure async_operation_completed_callback_;
217 214
218 std::string registration_id_; 215 std::string registration_id_;
219 GCMClient::Result registration_result_; 216 GCMClient::Result registration_result_;
220 std::string send_message_id_; 217 std::string send_message_id_;
221 GCMClient::Result send_result_; 218 GCMClient::Result send_result_;
222 GCMClient::Result unregistration_result_; 219 GCMClient::Result unregistration_result_;
223 220
224 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest); 221 DISALLOW_COPY_AND_ASSIGN(GCMDriverTest);
225 }; 222 };
226 223
227 GCMDriverTest::GCMDriverTest() 224 GCMDriverTest::GCMDriverTest()
228 : identity_provider_(NULL), 225 : identity_provider_(NULL),
226 task_runner_(new base::TestSimpleTaskRunner()),
227 io_thread_("IOThread"),
229 registration_result_(GCMClient::UNKNOWN_ERROR), 228 registration_result_(GCMClient::UNKNOWN_ERROR),
230 send_result_(GCMClient::UNKNOWN_ERROR), 229 send_result_(GCMClient::UNKNOWN_ERROR),
231 unregistration_result_(GCMClient::UNKNOWN_ERROR) { 230 unregistration_result_(GCMClient::UNKNOWN_ERROR) {
232 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_)); 231 identity_provider_owner_.reset(new FakeIdentityProvider(&token_service_));
233 identity_provider_ = identity_provider_owner_.get(); 232 identity_provider_ = identity_provider_owner_.get();
234 } 233 }
235 234
236 GCMDriverTest::~GCMDriverTest() { 235 GCMDriverTest::~GCMDriverTest() {
237 } 236 }
238 237
239 void GCMDriverTest::SetUp() { 238 void GCMDriverTest::SetUp() {
240 thread_bundle_.reset(new content::TestBrowserThreadBundle( 239 io_thread_.Start();
241 content::TestBrowserThreadBundle::REAL_IO_THREAD));
242 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 240 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
243 } 241 }
244 242
245 void GCMDriverTest::TearDown() { 243 void GCMDriverTest::TearDown() {
246 if (!driver_) 244 if (!driver_)
247 return; 245 return;
248 246
249 driver_->Shutdown(); 247 driver_->Shutdown();
250 driver_.reset(); 248 driver_.reset();
251 PumpIOLoop(); 249 PumpIOLoop();
250
251 io_thread_.Stop();
252 }
253
254 void GCMDriverTest::PumpIOLoop() {
255 base::RunLoop run_loop;
256 io_thread_.message_loop_proxy()->PostTaskAndReply(
257 FROM_HERE,
258 base::Bind(&PumpCurrentLoop),
259 run_loop.QuitClosure());
260 run_loop.Run();
252 } 261 }
253 262
254 void GCMDriverTest::ClearResults() { 263 void GCMDriverTest::ClearResults() {
255 registration_id_.clear(); 264 registration_id_.clear();
256 registration_result_ = GCMClient::UNKNOWN_ERROR; 265 registration_result_ = GCMClient::UNKNOWN_ERROR;
257 266
258 send_message_id_.clear(); 267 send_message_id_.clear();
259 send_result_ = GCMClient::UNKNOWN_ERROR; 268 send_result_ = GCMClient::UNKNOWN_ERROR;
260 269
261 unregistration_result_ = GCMClient::UNKNOWN_ERROR; 270 unregistration_result_ = GCMClient::UNKNOWN_ERROR;
262 } 271 }
263 272
264 bool GCMDriverTest::HasAppHandlers() const { 273 bool GCMDriverTest::HasAppHandlers() const {
265 return !driver_->app_handlers().empty(); 274 return !driver_->app_handlers().empty();
266 } 275 }
267 276
268 FakeGCMClient* GCMDriverTest::GetGCMClient() { 277 FakeGCMClient* GCMDriverTest::GetGCMClient() {
269 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting()); 278 return static_cast<FakeGCMClient*>(driver_->GetGCMClientForTesting());
270 } 279 }
271 280
272 void GCMDriverTest::CreateDriver( 281 void GCMDriverTest::CreateDriver(
273 FakeGCMClient::StartMode gcm_client_start_mode) { 282 FakeGCMClient::StartMode gcm_client_start_mode) {
274 scoped_refptr<net::URLRequestContextGetter> request_context = 283 scoped_refptr<net::URLRequestContextGetter> request_context =
275 new net::TestURLRequestContextGetter( 284 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
276 content::BrowserThread::GetMessageLoopProxyForThread(
277 content::BrowserThread::IO));
278 driver_.reset(new GCMDriver( 285 driver_.reset(new GCMDriver(
279 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory( 286 scoped_ptr<GCMClientFactory>(new FakeGCMClientFactory(
280 gcm_client_start_mode)).Pass(), 287 gcm_client_start_mode,
288 base::MessageLoopProxy::current(),
289 io_thread_.message_loop_proxy())).Pass(),
281 identity_provider_owner_.PassAs<IdentityProvider>(), 290 identity_provider_owner_.PassAs<IdentityProvider>(),
282 temp_dir_.path(), 291 temp_dir_.path(),
283 request_context)); 292 request_context,
293 base::MessageLoopProxy::current(),
294 io_thread_.message_loop_proxy(),
295 task_runner_));
284 296
285 gcm_app_handler_.reset(new FakeGCMAppHandler); 297 gcm_app_handler_.reset(new FakeGCMAppHandler);
286 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); 298 driver_->AddAppHandler(kTestAppID1, gcm_app_handler_.get());
287 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); 299 driver_->AddAppHandler(kTestAppID2, gcm_app_handler_.get());
288 } 300 }
289 301
290 void GCMDriverTest::SignIn(const std::string& account_id) { 302 void GCMDriverTest::SignIn(const std::string& account_id) {
291 token_service_.AddAccount(account_id); 303 token_service_.AddAccount(account_id);
292 identity_provider_->LogIn(account_id); 304 identity_provider_->LogIn(account_id);
293 PumpIOLoop(); 305 PumpIOLoop();
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 866
855 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) { 867 TEST_F(GCMDriverFunctionalTest, MessagesDeleted) {
856 GetGCMClient()->DeleteMessages(kTestAppID1); 868 GetGCMClient()->DeleteMessages(kTestAppID1);
857 gcm_app_handler()->WaitForNotification(); 869 gcm_app_handler()->WaitForNotification();
858 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, 870 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT,
859 gcm_app_handler()->received_event()); 871 gcm_app_handler()->received_event());
860 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id()); 872 EXPECT_EQ(kTestAppID1, gcm_app_handler()->app_id());
861 } 873 }
862 874
863 } // namespace gcm 875 } // namespace gcm
OLDNEW
« no previous file with comments | « chrome/browser/services/gcm/gcm_driver.cc ('k') | chrome/browser/services/gcm/gcm_profile_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698