OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/services/gcm/gcm_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/location.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "chrome/browser/services/gcm/gcm_app_handler.h" | |
14 #include "chrome/browser/services/gcm/gcm_client_factory.h" | |
15 #include "chrome/browser/services/gcm/gcm_client_mock.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 #include "content/public/test/test_browser_thread_bundle.h" | |
18 #include "google_apis/gaia/oauth2_access_token_consumer.h" | |
19 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | |
20 #include "google_apis/gaia/oauth2_token_service.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 #include "net/url_request/url_request_test_util.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 namespace gcm { | |
26 | |
27 namespace { | |
28 | |
29 const char kTestAccountID1[] = "user1@example.com"; | |
30 const char kTestAccountID2[] = "user2@example.com"; | |
31 const char kTestAccountID3[] = "user3@example.com"; | |
32 const char kTestAppID1[] = "TestApp1"; | |
33 const char kTestAppID2[] = "TestApp2"; | |
34 const char kUserID1[] = "user1"; | |
35 const char kUserID2[] = "user2"; | |
36 | |
37 void PumpUILoop() { | |
38 base::RunLoop().RunUntilIdle(); | |
39 } | |
40 | |
41 void PumpIOLoop() { | |
42 base::RunLoop run_loop; | |
43 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO, | |
44 FROM_HERE, | |
45 base::Bind(&base::DoNothing), | |
46 run_loop.QuitClosure()); | |
47 run_loop.Run(); | |
48 } | |
49 | |
50 std::vector<std::string> ToSenderList(const std::string& sender_ids) { | |
51 std::vector<std::string> senders; | |
52 Tokenize(sender_ids, ",", &senders); | |
53 return senders; | |
54 } | |
55 | |
56 class FakeOAuth2TokenService : public OAuth2TokenService { | |
jianli
2014/04/17 00:33:19
Should we move this and FakeIdentityProvider to
gc
bartfab (slow)
2014/04/17 14:20:01
Done. While at it, I took a closer look at gcm_pro
| |
57 public: | |
58 explicit FakeOAuth2TokenService( | |
59 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
60 virtual ~FakeOAuth2TokenService(); | |
61 | |
62 // OAuth2TokenService: | |
63 virtual bool RefreshTokenIsAvailable( | |
64 const std::string& account_id) const OVERRIDE; | |
65 | |
66 protected: | |
67 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( | |
68 const std::string& account_id, | |
69 net::URLRequestContextGetter* getter, | |
70 OAuth2AccessTokenConsumer* consumer) OVERRIDE; | |
71 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; | |
72 | |
73 private: | |
74 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(FakeOAuth2TokenService); | |
77 }; | |
78 | |
79 class FakeIdentityProvider : public IdentityProvider { | |
80 public: | |
81 explicit FakeIdentityProvider( | |
82 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
83 virtual ~FakeIdentityProvider(); | |
84 | |
85 // IdentityProvider: | |
86 virtual OAuth2TokenService* GetTokenService() OVERRIDE; | |
87 virtual std::string GetUsername() OVERRIDE; | |
88 virtual std::string GetAccountId() OVERRIDE; | |
89 virtual std::vector<std::string> GetAllAccountIds() OVERRIDE; | |
90 virtual bool RequestLogin() OVERRIDE; | |
91 | |
92 void SignIn(const std::string& account_id); | |
93 void SignOut(); | |
94 | |
95 private: | |
96 std::string account_id_; | |
97 FakeOAuth2TokenService token_service_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(FakeIdentityProvider); | |
100 }; | |
101 | |
102 class FakeGCMClientFactory : public GCMClientFactory { | |
103 public: | |
104 explicit FakeGCMClientFactory( | |
105 GCMClientMock::LoadingDelay gcm_client_loading_delay); | |
106 virtual ~FakeGCMClientFactory(); | |
107 | |
108 // GCMClientFactory: | |
109 virtual scoped_ptr<GCMClient> BuildInstance() OVERRIDE; | |
110 | |
111 private: | |
112 GCMClientMock::LoadingDelay gcm_client_loading_delay_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(FakeGCMClientFactory); | |
115 }; | |
116 | |
117 class FakeGCMAppHandler : public GCMAppHandler { | |
118 public: | |
119 enum Event { | |
120 NO_EVENT, | |
121 MESSAGE_EVENT, | |
122 MESSAGES_DELETED_EVENT, | |
123 SEND_ERROR_EVENT | |
124 }; | |
125 | |
126 FakeGCMAppHandler(); | |
127 virtual ~FakeGCMAppHandler(); | |
128 | |
129 const Event& received_event() const { return received_event_; } | |
130 const std::string& app_id() const { return app_id_; } | |
131 const GCMClient::IncomingMessage& message() const { return message_; } | |
132 const GCMClient::SendErrorDetails& send_error_details() const { | |
133 return send_error_details_; | |
134 } | |
135 | |
136 void WaitForNotification(); | |
137 | |
138 // GCMAppHandler: | |
139 virtual void ShutdownHandler() OVERRIDE; | |
140 virtual void OnMessage(const std::string& app_id, | |
141 const GCMClient::IncomingMessage& message) OVERRIDE; | |
142 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | |
143 virtual void OnSendError( | |
144 const std::string& app_id, | |
145 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | |
146 | |
147 private: | |
148 void ClearResults(); | |
149 | |
150 scoped_ptr<base::RunLoop> run_loop_; | |
151 | |
152 Event received_event_; | |
153 std::string app_id_; | |
154 GCMClient::IncomingMessage message_; | |
155 GCMClient::SendErrorDetails send_error_details_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(FakeGCMAppHandler); | |
158 }; | |
159 | |
160 class TestGCMService : public GCMService { | |
161 public: | |
162 TestGCMService( | |
163 bool is_always_enabled, | |
164 scoped_ptr<IdentityProvider> identity_provider, | |
165 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
166 virtual ~TestGCMService(); | |
167 | |
168 // GCMService: | |
169 virtual void Initialize( | |
170 scoped_ptr<GCMClientFactory> gcm_client_factory) OVERRIDE; | |
171 | |
172 protected: | |
173 // GCMService: | |
174 virtual bool IsAlwaysEnabled() OVERRIDE; | |
175 virtual base::FilePath GetStorePath() OVERRIDE; | |
176 virtual scoped_refptr<net::URLRequestContextGetter> | |
177 GetURLRequestContextGetter() OVERRIDE; | |
178 | |
179 private: | |
180 base::ScopedTempDir temp_dir_; | |
181 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
182 const bool is_always_enabled_; | |
183 | |
184 DISALLOW_COPY_AND_ASSIGN(TestGCMService); | |
185 }; | |
186 | |
187 FakeOAuth2TokenService::FakeOAuth2TokenService( | |
188 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
189 : request_context_(request_context) { | |
190 } | |
191 | |
192 FakeOAuth2TokenService::~FakeOAuth2TokenService() { | |
193 } | |
194 | |
195 bool FakeOAuth2TokenService::RefreshTokenIsAvailable( | |
196 const std::string& account_id) const { | |
197 return false; | |
198 } | |
199 | |
200 OAuth2AccessTokenFetcher* FakeOAuth2TokenService::CreateAccessTokenFetcher( | |
201 const std::string& account_id, | |
202 net::URLRequestContextGetter* getter, | |
203 OAuth2AccessTokenConsumer* consumer) { | |
204 return NULL; | |
205 } | |
206 | |
207 net::URLRequestContextGetter* FakeOAuth2TokenService::GetRequestContext() { | |
208 return request_context_; | |
209 } | |
210 | |
211 FakeIdentityProvider::FakeIdentityProvider( | |
212 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
213 : token_service_(request_context) { | |
214 } | |
215 | |
216 FakeIdentityProvider::~FakeIdentityProvider() { | |
217 } | |
218 | |
219 OAuth2TokenService* FakeIdentityProvider::GetTokenService() { | |
220 return &token_service_; | |
221 } | |
222 | |
223 std::string FakeIdentityProvider::GetUsername() { | |
224 return account_id_; | |
225 } | |
226 | |
227 std::string FakeIdentityProvider::GetAccountId() { | |
228 return account_id_; | |
229 } | |
230 | |
231 std::vector<std::string> FakeIdentityProvider::GetAllAccountIds() { | |
232 std::vector<std::string> all_accounts; | |
233 all_accounts.push_back(account_id_); | |
234 return all_accounts; | |
235 } | |
236 | |
237 bool FakeIdentityProvider::RequestLogin() { | |
238 return true; | |
239 } | |
240 | |
241 void FakeIdentityProvider::SignIn(const std::string& account_id) { | |
242 account_id_ = account_id; | |
243 FireOnLogin(); | |
244 } | |
245 | |
246 void FakeIdentityProvider::SignOut() { | |
247 account_id_.clear(); | |
248 FireOnLogout(); | |
249 } | |
250 | |
251 FakeGCMClientFactory::FakeGCMClientFactory( | |
252 GCMClientMock::LoadingDelay gcm_client_loading_delay) | |
253 : gcm_client_loading_delay_(gcm_client_loading_delay) { | |
254 } | |
255 | |
256 FakeGCMClientFactory::~FakeGCMClientFactory() { | |
257 } | |
258 | |
259 scoped_ptr<GCMClient> FakeGCMClientFactory::BuildInstance() { | |
260 return scoped_ptr<GCMClient>(new GCMClientMock(gcm_client_loading_delay_)); | |
261 } | |
262 | |
263 FakeGCMAppHandler::FakeGCMAppHandler() : received_event_(NO_EVENT) { | |
264 } | |
265 | |
266 FakeGCMAppHandler::~FakeGCMAppHandler() { | |
267 } | |
268 | |
269 void FakeGCMAppHandler::WaitForNotification() { | |
270 run_loop_.reset(new base::RunLoop); | |
271 run_loop_->Run(); | |
272 run_loop_.reset(); | |
273 } | |
274 | |
275 void FakeGCMAppHandler::ShutdownHandler() { | |
276 } | |
277 | |
278 void FakeGCMAppHandler::OnMessage(const std::string& app_id, | |
279 const GCMClient::IncomingMessage& message) { | |
280 ClearResults(); | |
281 received_event_ = MESSAGE_EVENT; | |
282 app_id_ = app_id; | |
283 message_ = message; | |
284 if (run_loop_) | |
285 run_loop_->Quit(); | |
286 } | |
287 | |
288 void FakeGCMAppHandler::OnMessagesDeleted(const std::string& app_id) { | |
289 ClearResults(); | |
290 received_event_ = MESSAGES_DELETED_EVENT; | |
291 app_id_ = app_id; | |
292 if (run_loop_) | |
293 run_loop_->Quit(); | |
294 } | |
295 | |
296 void FakeGCMAppHandler::OnSendError( | |
297 const std::string& app_id, | |
298 const GCMClient::SendErrorDetails& send_error_details) { | |
299 ClearResults(); | |
300 received_event_ = SEND_ERROR_EVENT; | |
301 app_id_ = app_id; | |
302 send_error_details_ = send_error_details; | |
303 if (run_loop_) | |
304 run_loop_->Quit(); | |
305 } | |
306 | |
307 void FakeGCMAppHandler::ClearResults() { | |
308 received_event_ = NO_EVENT; | |
309 app_id_.clear(); | |
310 message_ = GCMClient::IncomingMessage(); | |
311 send_error_details_ = GCMClient::SendErrorDetails(); | |
312 } | |
313 | |
314 TestGCMService::TestGCMService( | |
315 bool is_always_enabled, | |
316 scoped_ptr<IdentityProvider> identity_provider, | |
317 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
318 : GCMService(identity_provider.Pass()), | |
319 request_context_(request_context), | |
320 is_always_enabled_(is_always_enabled) { | |
321 } | |
322 | |
323 TestGCMService::~TestGCMService() { | |
324 } | |
325 | |
326 void TestGCMService::Initialize( | |
327 scoped_ptr<GCMClientFactory> gcm_client_factory) { | |
328 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
329 GCMService::Initialize(gcm_client_factory.Pass()); | |
330 } | |
331 | |
332 bool TestGCMService::IsAlwaysEnabled() { | |
333 return is_always_enabled_; | |
334 } | |
335 | |
336 base::FilePath TestGCMService::GetStorePath() { | |
337 return temp_dir_.path(); | |
338 } | |
339 | |
340 scoped_refptr<net::URLRequestContextGetter> | |
341 TestGCMService::GetURLRequestContextGetter() { | |
342 return request_context_; | |
343 } | |
344 | |
345 } // namespace | |
346 | |
347 class TestGCMServiceWrapper { | |
348 public: | |
349 explicit TestGCMServiceWrapper( | |
350 const scoped_refptr<net::URLRequestContextGetter>& request_context); | |
351 ~TestGCMServiceWrapper(); | |
352 | |
353 TestGCMService* service() { return service_.get(); } | |
354 FakeGCMAppHandler* gcm_app_handler() { return gcm_app_handler_.get(); } | |
355 const std::string& registration_id() const { return registration_id_; } | |
356 GCMClient::Result registration_result() const { return registration_result_; } | |
357 const std::string& send_message_id() const { return send_message_id_; } | |
358 GCMClient::Result send_result() const { return send_result_; } | |
359 GCMClient::Result unregistration_result() const { | |
360 return unregistration_result_; | |
361 } | |
362 | |
363 void ClearRegistrationResult(); | |
364 void ClearUnregistrationResult(); | |
365 | |
366 bool ServiceHasAppHandlers() const; | |
367 GCMClientMock* GetGCMClient(); | |
368 | |
369 void CreateService(bool service_is_always_enabled, | |
370 GCMClientMock::LoadingDelay gcm_client_loading_delay); | |
371 | |
372 void SignIn(const std::string& account_id); | |
373 void SignOut(); | |
374 | |
375 void Register(const std::string& app_id, | |
376 const std::vector<std::string>& sender_ids, | |
377 bool wait); | |
378 void Send(const std::string& app_id, | |
379 const std::string& receiver_id, | |
380 const GCMClient::OutgoingMessage& message, | |
381 bool wait); | |
382 void Unregister(const std::string& app_id, bool wait); | |
383 | |
384 private: | |
385 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
386 scoped_ptr<FakeIdentityProvider> identity_provider_owner_; | |
387 FakeIdentityProvider* identity_provider_; | |
388 scoped_ptr<TestGCMService> service_; | |
389 scoped_ptr<FakeGCMAppHandler> gcm_app_handler_; | |
390 | |
391 std::string registration_id_; | |
392 GCMClient::Result registration_result_; | |
393 std::string send_message_id_; | |
394 GCMClient::Result send_result_; | |
395 GCMClient::Result unregistration_result_; | |
396 | |
397 void RegisterCompleted(const base::Closure& callback, | |
398 const std::string& registration_id, | |
399 GCMClient::Result result); | |
400 void SendCompleted(const base::Closure& callback, | |
401 const std::string& message_id, | |
402 GCMClient::Result result); | |
403 void UnregisterCompleted(const base::Closure& callback, | |
404 GCMClient::Result result); | |
405 | |
406 DISALLOW_COPY_AND_ASSIGN(TestGCMServiceWrapper); | |
407 }; | |
408 | |
409 TestGCMServiceWrapper::TestGCMServiceWrapper( | |
410 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
411 : request_context_(request_context), | |
412 identity_provider_(NULL), | |
413 registration_result_(GCMClient::UNKNOWN_ERROR), | |
414 send_result_(GCMClient::UNKNOWN_ERROR), | |
415 unregistration_result_(GCMClient::UNKNOWN_ERROR) { | |
416 identity_provider_owner_.reset(new FakeIdentityProvider(request_context_)); | |
417 identity_provider_ = identity_provider_owner_.get(); | |
418 } | |
419 | |
420 TestGCMServiceWrapper::~TestGCMServiceWrapper() { | |
421 if (!service_) | |
422 return; | |
423 | |
424 service_->Shutdown(); | |
425 service_.reset(); | |
426 PumpIOLoop(); | |
427 } | |
428 | |
429 void TestGCMServiceWrapper::ClearRegistrationResult() { | |
430 registration_id_.clear(); | |
431 registration_result_ = GCMClient::UNKNOWN_ERROR; | |
432 } | |
433 | |
434 void TestGCMServiceWrapper::ClearUnregistrationResult() { | |
435 unregistration_result_ = GCMClient::UNKNOWN_ERROR; | |
436 } | |
437 | |
438 bool TestGCMServiceWrapper::ServiceHasAppHandlers() const { | |
439 return !service_->app_handlers_.empty(); | |
440 } | |
441 | |
442 GCMClientMock* TestGCMServiceWrapper::GetGCMClient() { | |
443 return static_cast<GCMClientMock*>(service_->GetGCMClientForTesting()); | |
444 } | |
445 | |
446 void TestGCMServiceWrapper::CreateService( | |
447 bool service_is_always_enabled, | |
448 GCMClientMock::LoadingDelay gcm_client_loading_delay) { | |
449 service_.reset(new TestGCMService( | |
450 service_is_always_enabled, | |
451 identity_provider_owner_.PassAs<IdentityProvider>(), | |
452 request_context_)); | |
453 service_->Initialize(scoped_ptr<GCMClientFactory>( | |
454 new FakeGCMClientFactory(gcm_client_loading_delay))); | |
455 | |
456 gcm_app_handler_.reset(new FakeGCMAppHandler); | |
457 service_->AddAppHandler(kTestAppID1, gcm_app_handler_.get()); | |
458 service_->AddAppHandler(kTestAppID2, gcm_app_handler_.get()); | |
459 } | |
460 | |
461 void TestGCMServiceWrapper::SignIn(const std::string& account_id) { | |
462 identity_provider_->SignIn(account_id); | |
463 PumpIOLoop(); | |
464 PumpUILoop(); | |
465 } | |
466 | |
467 void TestGCMServiceWrapper::SignOut() { | |
468 identity_provider_->SignOut(); | |
469 PumpIOLoop(); | |
470 PumpUILoop(); | |
471 } | |
472 | |
473 void TestGCMServiceWrapper::Register(const std::string& app_id, | |
474 const std::vector<std::string>& sender_ids, | |
475 bool wait) { | |
jianli
2014/04/17 00:33:19
nit: better use enum to improve readability
bartfab (slow)
2014/04/22 17:51:07
Done.
| |
476 base::RunLoop run_loop; | |
477 service_->Register( | |
478 app_id, | |
479 sender_ids, | |
480 base::Bind(&TestGCMServiceWrapper::RegisterCompleted, | |
481 base::Unretained(this), | |
482 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing))); | |
483 if (wait) | |
484 run_loop.Run(); | |
485 } | |
486 | |
487 void TestGCMServiceWrapper::Send(const std::string& app_id, | |
488 const std::string& receiver_id, | |
489 const GCMClient::OutgoingMessage& message, | |
490 bool wait) { | |
491 base::RunLoop run_loop; | |
492 service_->Send( | |
493 app_id, | |
494 receiver_id, | |
495 message, | |
496 base::Bind(&TestGCMServiceWrapper::SendCompleted, | |
497 base::Unretained(this), | |
498 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing))); | |
499 if (wait) | |
500 run_loop.Run(); | |
501 } | |
502 | |
503 void TestGCMServiceWrapper::Unregister(const std::string& app_id, bool wait) { | |
504 base::RunLoop run_loop; | |
505 service_->Unregister( | |
506 app_id, | |
507 base::Bind(&TestGCMServiceWrapper::UnregisterCompleted, | |
508 base::Unretained(this), | |
509 wait ? run_loop.QuitClosure() : base::Bind(&base::DoNothing))); | |
510 if (wait) | |
511 run_loop.Run(); | |
512 } | |
513 | |
514 void TestGCMServiceWrapper::RegisterCompleted( | |
515 const base::Closure& callback, | |
516 const std::string& registration_id, | |
517 GCMClient::Result result) { | |
518 registration_id_ = registration_id; | |
519 registration_result_ = result; | |
520 callback.Run(); | |
521 } | |
522 | |
523 void TestGCMServiceWrapper::SendCompleted(const base::Closure& callback, | |
524 const std::string& message_id, | |
525 GCMClient::Result result) { | |
526 send_message_id_ = message_id; | |
527 send_result_ = result; | |
528 callback.Run(); | |
529 } | |
530 | |
531 void TestGCMServiceWrapper::UnregisterCompleted(const base::Closure& callback, | |
532 GCMClient::Result result) { | |
533 unregistration_result_ = result; | |
534 callback.Run(); | |
535 } | |
536 | |
537 class GCMServiceTest : public testing::Test { | |
538 protected: | |
539 GCMServiceTest(); | |
540 virtual ~GCMServiceTest(); | |
541 | |
542 // testing::Test: | |
543 void SetUp() OVERRIDE; | |
544 void TearDown() OVERRIDE; | |
545 | |
546 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_; | |
547 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
548 scoped_ptr<TestGCMServiceWrapper> wrapper_; | |
549 | |
550 private: | |
551 DISALLOW_COPY_AND_ASSIGN(GCMServiceTest); | |
552 }; | |
553 | |
554 GCMServiceTest::GCMServiceTest() { | |
555 } | |
556 | |
557 GCMServiceTest::~GCMServiceTest() { | |
558 } | |
559 | |
560 void GCMServiceTest::SetUp() { | |
561 thread_bundle_.reset(new content::TestBrowserThreadBundle( | |
562 content::TestBrowserThreadBundle::REAL_IO_THREAD)); | |
563 request_context_ = new net::TestURLRequestContextGetter( | |
564 content::BrowserThread::GetMessageLoopProxyForThread( | |
565 content::BrowserThread::IO)); | |
566 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); | |
567 } | |
568 | |
569 void GCMServiceTest::TearDown() { | |
570 wrapper_.reset(); | |
571 } | |
572 | |
573 TEST_F(GCMServiceTest, CreateGCMServiceBeforeSignIn) { | |
574 // Create CreateGMCService first. | |
575 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
576 EXPECT_FALSE(wrapper_->service()->IsStarted()); | |
577 | |
578 // Sign in. This will kick off the check-in. | |
579 wrapper_->SignIn(kTestAccountID1); | |
580 EXPECT_TRUE(wrapper_->service()->IsStarted()); | |
581 } | |
582 | |
583 TEST_F(GCMServiceTest, CreateGCMServiceAfterSignIn) { | |
584 // Sign in. This will not initiate the check-in. | |
585 wrapper_->SignIn(kTestAccountID1); | |
586 | |
587 // Create GCMeService after sign-in. | |
588 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
589 EXPECT_TRUE(wrapper_->service()->IsStarted()); | |
590 } | |
591 | |
592 TEST_F(GCMServiceTest, Shutdown) { | |
593 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
594 EXPECT_TRUE(wrapper_->ServiceHasAppHandlers()); | |
595 | |
596 wrapper_->service()->Shutdown(); | |
597 EXPECT_FALSE(wrapper_->ServiceHasAppHandlers()); | |
598 } | |
599 | |
600 TEST_F(GCMServiceTest, SignInAndSignOutUnderPositiveChannelSignal) { | |
601 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
602 wrapper_->SignIn(kTestAccountID1); | |
603 | |
604 // GCMClient should be loaded. | |
605 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
606 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
607 | |
608 wrapper_->SignOut(); | |
609 | |
610 // GCMClient should be checked out. | |
611 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
612 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); | |
613 } | |
614 | |
615 TEST_F(GCMServiceTest, SignInAndSignOutUnderNonPositiveChannelSignal) { | |
616 // Non-positive channel signal will prevent GCMClient from checking in during | |
617 // sign-in. | |
618 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); | |
619 wrapper_->SignIn(kTestAccountID1); | |
620 | |
621 // GCMClient should not be loaded. | |
622 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
623 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); | |
624 | |
625 wrapper_->SignOut(); | |
626 | |
627 // Check-out should still be performed. | |
628 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
629 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); | |
630 } | |
631 | |
632 TEST_F(GCMServiceTest, SignOutAndThenSignIn) { | |
633 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
634 wrapper_->SignIn(kTestAccountID1); | |
635 | |
636 // GCMClient should be loaded. | |
637 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
638 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
639 | |
640 wrapper_->SignOut(); | |
641 | |
642 // GCMClient should be checked out. | |
643 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
644 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); | |
645 | |
646 // Sign-in with a different account. | |
647 wrapper_->SignIn(kTestAccountID2); | |
648 | |
649 // GCMClient should be loaded again. | |
650 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
651 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
652 } | |
653 | |
654 TEST_F(GCMServiceTest, StopAndRestartGCM) { | |
655 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
656 wrapper_->SignIn(kTestAccountID1); | |
657 | |
658 // GCMClient should be loaded. | |
659 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
660 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
661 | |
662 // Stops the GCM. | |
663 wrapper_->service()->Stop(); | |
664 PumpIOLoop(); | |
665 PumpUILoop(); | |
666 | |
667 // GCMClient should be stopped. | |
668 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
669 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status()); | |
670 | |
671 // Restarts the GCM. | |
672 wrapper_->service()->Start(); | |
673 PumpIOLoop(); | |
674 PumpUILoop(); | |
675 | |
676 // GCMClient should be loaded. | |
677 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
678 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
679 | |
680 // Stops the GCM. | |
681 wrapper_->service()->Stop(); | |
682 PumpIOLoop(); | |
683 PumpUILoop(); | |
684 | |
685 // GCMClient should be stopped. | |
686 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
687 EXPECT_EQ(GCMClientMock::STOPPED, wrapper_->GetGCMClient()->status()); | |
688 | |
689 // Sign out. | |
690 wrapper_->SignOut(); | |
691 | |
692 // GCMClient should be checked out. | |
693 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
694 EXPECT_EQ(GCMClientMock::CHECKED_OUT, wrapper_->GetGCMClient()->status()); | |
695 } | |
696 | |
697 TEST_F(GCMServiceTest, RegisterWhenNotSignedIn) { | |
698 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
699 | |
700 std::vector<std::string> sender_ids; | |
701 sender_ids.push_back("sender1"); | |
702 wrapper_->Register(kTestAppID1, sender_ids, true); | |
703 | |
704 EXPECT_TRUE(wrapper_->registration_id().empty()); | |
705 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); | |
706 } | |
707 | |
708 TEST_F(GCMServiceTest, RegisterUnderNonPositiveChannelSignal) { | |
709 // Non-positive channel signal will prevent GCMClient from checking in during | |
710 // sign-in. | |
711 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); | |
712 wrapper_->SignIn(kTestAccountID1); | |
713 | |
714 // GCMClient should not be checked in. | |
715 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
716 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); | |
717 | |
718 // Invoking register will make GCMClient checked in. | |
719 std::vector<std::string> sender_ids; | |
720 sender_ids.push_back("sender1"); | |
721 wrapper_->Register(kTestAppID1, sender_ids, true); | |
722 | |
723 // GCMClient should be checked in. | |
724 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
725 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
726 | |
727 // Registration should succeed. | |
728 const std::string expected_registration_id = | |
729 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
730 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); | |
731 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
732 } | |
733 | |
734 TEST_F(GCMServiceTest, SendWhenNotSignedIn) { | |
735 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
736 | |
737 GCMClient::OutgoingMessage message; | |
738 message.id = "1"; | |
739 message.data["key1"] = "value1"; | |
740 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
741 | |
742 EXPECT_TRUE(wrapper_->send_message_id().empty()); | |
743 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); | |
744 } | |
745 | |
746 TEST_F(GCMServiceTest, SendUnderNonPositiveChannelSignal) { | |
747 // Non-positive channel signal will prevent GCMClient from checking in during | |
748 // sign-in. | |
749 wrapper_->CreateService(false, GCMClientMock::NO_DELAY_LOADING); | |
750 wrapper_->SignIn(kTestAccountID1); | |
751 | |
752 // GCMClient should not be checked in. | |
753 EXPECT_FALSE(wrapper_->service()->IsGCMClientReady()); | |
754 EXPECT_EQ(GCMClientMock::UNINITIALIZED, wrapper_->GetGCMClient()->status()); | |
755 | |
756 // Invoking send will make GCMClient checked in. | |
757 GCMClient::OutgoingMessage message; | |
758 message.id = "1"; | |
759 message.data["key1"] = "value1"; | |
760 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
761 | |
762 // GCMClient should be checked in. | |
763 EXPECT_TRUE(wrapper_->service()->IsGCMClientReady()); | |
764 EXPECT_EQ(GCMClientMock::LOADED, wrapper_->GetGCMClient()->status()); | |
765 | |
766 // Sending should succeed. | |
767 EXPECT_EQ(message.id, wrapper_->send_message_id()); | |
768 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
769 } | |
770 | |
771 // Tests a single instance of GCMService. | |
772 class GCMServiceSingleInstanceTest : public GCMServiceTest { | |
773 public: | |
774 GCMServiceSingleInstanceTest(); | |
775 virtual ~GCMServiceSingleInstanceTest(); | |
776 | |
777 // GCMServiceTest: | |
778 virtual void SetUp() OVERRIDE; | |
779 | |
780 private: | |
781 DISALLOW_COPY_AND_ASSIGN(GCMServiceSingleInstanceTest); | |
782 }; | |
783 | |
784 GCMServiceSingleInstanceTest::GCMServiceSingleInstanceTest() { | |
785 } | |
786 | |
787 GCMServiceSingleInstanceTest::~GCMServiceSingleInstanceTest() { | |
788 } | |
789 | |
790 void GCMServiceSingleInstanceTest::SetUp() { | |
791 GCMServiceTest::SetUp(); | |
792 | |
793 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
794 wrapper_->SignIn(kTestAccountID1); | |
795 } | |
796 | |
797 TEST_F(GCMServiceSingleInstanceTest, Register) { | |
798 std::vector<std::string> sender_ids; | |
799 sender_ids.push_back("sender1"); | |
800 wrapper_->Register(kTestAppID1, sender_ids, true); | |
801 const std::string expected_registration_id = | |
802 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
803 | |
804 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); | |
805 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
806 } | |
807 | |
808 TEST_F(GCMServiceSingleInstanceTest, RegisterError) { | |
809 std::vector<std::string> sender_ids; | |
810 sender_ids.push_back("sender1@error"); | |
811 wrapper_->Register(kTestAppID1, sender_ids, true); | |
812 | |
813 EXPECT_TRUE(wrapper_->registration_id().empty()); | |
814 EXPECT_NE(GCMClient::SUCCESS, wrapper_->registration_result()); | |
815 } | |
816 | |
817 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithSameSenderIDs) { | |
818 std::vector<std::string> sender_ids; | |
819 sender_ids.push_back("sender1"); | |
820 sender_ids.push_back("sender2"); | |
821 wrapper_->Register(kTestAppID1, sender_ids, true); | |
822 const std::string expected_registration_id = | |
823 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
824 | |
825 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); | |
826 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
827 | |
828 // Clears the results the would be set by the Register callback in preparation | |
829 // to call register 2nd time. | |
830 wrapper_->ClearRegistrationResult(); | |
831 | |
832 // Calling register 2nd time with the same set of sender IDs but different | |
833 // ordering will get back the same registration ID. | |
834 std::vector<std::string> another_sender_ids; | |
835 another_sender_ids.push_back("sender2"); | |
836 another_sender_ids.push_back("sender1"); | |
837 wrapper_->Register(kTestAppID1, another_sender_ids, true); | |
838 | |
839 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); | |
840 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
841 } | |
842 | |
843 TEST_F(GCMServiceSingleInstanceTest, RegisterAgainWithDifferentSenderIDs) { | |
844 std::vector<std::string> sender_ids; | |
845 sender_ids.push_back("sender1"); | |
846 wrapper_->Register(kTestAppID1, sender_ids, true); | |
847 const std::string expected_registration_id = | |
848 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
849 | |
850 EXPECT_EQ(expected_registration_id, wrapper_->registration_id()); | |
851 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
852 | |
853 // Make sender IDs different. | |
854 sender_ids.push_back("sender2"); | |
855 const std::string expected_registration_id_2 = | |
856 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids); | |
857 | |
858 // Calling register 2nd time with the different sender IDs will get back a new | |
859 // registration ID. | |
860 wrapper_->Register(kTestAppID1, sender_ids, true); | |
861 EXPECT_EQ(expected_registration_id_2, wrapper_->registration_id()); | |
862 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
863 } | |
864 | |
865 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeRegistration) { | |
866 // Make GCMClient not ready initially. | |
867 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); | |
868 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING); | |
869 wrapper_->SignIn(kTestAccountID1); | |
870 | |
871 // The registration is on hold until GCMClient is ready. | |
872 std::vector<std::string> sender_ids; | |
873 sender_ids.push_back("sender1"); | |
874 wrapper_->Register(kTestAppID1, sender_ids, false); | |
875 PumpIOLoop(); | |
876 PumpUILoop(); | |
877 EXPECT_TRUE(wrapper_->registration_id().empty()); | |
878 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->registration_result()); | |
879 | |
880 // Register operation will be invoked after GCMClient becomes ready. | |
881 wrapper_->GetGCMClient()->PerformDelayedLoading(); | |
882 PumpIOLoop(); | |
jianli
2014/04/17 00:33:19
Why so many PumpIOLoop? Please explain like in ori
bartfab (slow)
2014/04/22 17:51:07
I replaced this with a proper wait until a notific
| |
883 PumpIOLoop(); | |
884 PumpIOLoop(); | |
885 PumpUILoop(); | |
886 EXPECT_FALSE(wrapper_->registration_id().empty()); | |
887 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
888 } | |
889 | |
890 TEST_F(GCMServiceSingleInstanceTest, RegisterAfterSignOut) { | |
891 // This will trigger check-out. | |
892 wrapper_->SignOut(); | |
893 | |
894 std::vector<std::string> sender_ids; | |
895 sender_ids.push_back("sender1"); | |
896 wrapper_->Register(kTestAppID1, sender_ids, true); | |
897 | |
898 EXPECT_TRUE(wrapper_->registration_id().empty()); | |
899 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); | |
900 } | |
901 | |
902 TEST_F(GCMServiceSingleInstanceTest, UnregisterExplicitly) { | |
903 std::vector<std::string> sender_ids; | |
904 sender_ids.push_back("sender1"); | |
905 wrapper_->Register(kTestAppID1, sender_ids, true); | |
906 | |
907 EXPECT_FALSE(wrapper_->registration_id().empty()); | |
908 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
909 | |
910 wrapper_->Unregister(kTestAppID1, true); | |
911 | |
912 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); | |
913 } | |
914 | |
915 TEST_F(GCMServiceSingleInstanceTest, UnregisterWhenAsyncOperationPending) { | |
916 std::vector<std::string> sender_ids; | |
917 sender_ids.push_back("sender1"); | |
918 // First start registration without waiting for it to complete. | |
919 wrapper_->Register(kTestAppID1, sender_ids, false); | |
920 | |
921 // Test that unregistration fails with async operation pending when there is a | |
922 // registration already in progress. | |
923 wrapper_->Unregister(kTestAppID1, true); | |
924 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
925 wrapper_->unregistration_result()); | |
926 | |
927 // Complete the unregistration. | |
928 PumpIOLoop(); | |
929 PumpUILoop(); | |
930 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
931 | |
932 // Start unregistration without waiting for it to complete. This time no async | |
933 // operation is pending. | |
934 wrapper_->Unregister(kTestAppID1, false); | |
935 | |
936 // Test that unregistration fails with async operation pending when there is | |
937 // an unregistration already in progress. | |
938 wrapper_->Unregister(kTestAppID1, true); | |
939 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
940 wrapper_->unregistration_result()); | |
941 wrapper_->ClearUnregistrationResult(); | |
942 | |
943 // Complete unregistration. | |
944 PumpIOLoop(); | |
945 PumpUILoop(); | |
946 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); | |
947 } | |
948 | |
949 TEST_F(GCMServiceSingleInstanceTest, RegisterWhenAsyncOperationPending) { | |
950 std::vector<std::string> sender_ids; | |
951 sender_ids.push_back("sender1"); | |
952 // First start registration without waiting for it to complete. | |
953 wrapper_->Register(kTestAppID1, sender_ids, false); | |
954 | |
955 // Test that registration fails with async operation pending when there is a | |
956 // registration already in progress. | |
957 wrapper_->Register(kTestAppID1, sender_ids, true); | |
958 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
959 wrapper_->registration_result()); | |
960 wrapper_->ClearRegistrationResult(); | |
961 | |
962 // Complete the registration. | |
963 PumpIOLoop(); | |
964 PumpUILoop(); | |
965 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
966 | |
967 // Start unregistration without waiting for it to complete. This time no async | |
968 // operation is pending. | |
969 wrapper_->Unregister(kTestAppID1, false); | |
970 | |
971 // Test that registration fails with async operation pending when there is an | |
972 // unregistration already in progress. | |
973 wrapper_->Register(kTestAppID1, sender_ids, true); | |
974 EXPECT_EQ(GCMClient::ASYNC_OPERATION_PENDING, | |
975 wrapper_->registration_result()); | |
976 | |
977 // Complete the first unregistration expecting success. | |
978 PumpIOLoop(); | |
979 PumpUILoop(); | |
980 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->unregistration_result()); | |
981 | |
982 // Test that it is ok to register again after unregistration. | |
983 wrapper_->Register(kTestAppID1, sender_ids, true); | |
984 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
985 } | |
986 | |
987 TEST_F(GCMServiceSingleInstanceTest, Send) { | |
988 GCMClient::OutgoingMessage message; | |
989 message.id = "1"; | |
990 message.data["key1"] = "value1"; | |
991 message.data["key2"] = "value2"; | |
992 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
993 | |
994 EXPECT_EQ(message.id, wrapper_->send_message_id()); | |
995 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
996 } | |
997 | |
998 TEST_F(GCMServiceSingleInstanceTest, GCMClientNotReadyBeforeSending) { | |
999 // Make GCMClient not ready initially. | |
1000 wrapper_.reset(new TestGCMServiceWrapper(request_context_)); | |
1001 wrapper_->CreateService(true, GCMClientMock::DELAY_LOADING); | |
1002 wrapper_->SignIn(kTestAccountID1); | |
1003 | |
1004 // The sending is on hold until GCMClient is ready. | |
1005 GCMClient::OutgoingMessage message; | |
1006 message.id = "1"; | |
1007 message.data["key1"] = "value1"; | |
1008 message.data["key2"] = "value2"; | |
1009 wrapper_->Send(kTestAppID1, kUserID1, message, false); | |
1010 PumpIOLoop(); | |
1011 PumpUILoop(); | |
1012 | |
1013 EXPECT_TRUE(wrapper_->send_message_id().empty()); | |
1014 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, wrapper_->send_result()); | |
1015 | |
1016 // Send operation will be invoked after GCMClient becomes ready. | |
1017 wrapper_->GetGCMClient()->PerformDelayedLoading(); | |
1018 PumpIOLoop(); | |
jianli
2014/04/17 00:33:19
ditto
bartfab (slow)
2014/04/22 17:51:07
I replaced this with a proper wait until a notific
| |
1019 PumpIOLoop(); | |
1020 PumpIOLoop(); | |
1021 PumpUILoop(); | |
1022 EXPECT_EQ(message.id, wrapper_->send_message_id()); | |
1023 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
1024 } | |
1025 | |
1026 TEST_F(GCMServiceSingleInstanceTest, SendAfterSignOut) { | |
1027 // This will trigger check-out. | |
1028 wrapper_->SignOut(); | |
1029 | |
1030 GCMClient::OutgoingMessage message; | |
1031 message.id = "1"; | |
1032 message.data["key1"] = "value1"; | |
1033 message.data["key2"] = "value2"; | |
1034 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
1035 | |
1036 EXPECT_TRUE(wrapper_->send_message_id().empty()); | |
1037 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); | |
1038 } | |
1039 | |
1040 TEST_F(GCMServiceSingleInstanceTest, SendError) { | |
1041 GCMClient::OutgoingMessage message; | |
1042 // Embedding error in id will tell the mock to simulate the send error. | |
1043 message.id = "1@error"; | |
1044 message.data["key1"] = "value1"; | |
1045 message.data["key2"] = "value2"; | |
1046 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
1047 | |
1048 EXPECT_EQ(message.id, wrapper_->send_message_id()); | |
1049 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
1050 | |
1051 // Wait for the send error. | |
1052 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1053 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, | |
1054 wrapper_->gcm_app_handler()->received_event()); | |
1055 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1056 EXPECT_EQ(message.id, | |
1057 wrapper_->gcm_app_handler()->send_error_details().message_id); | |
1058 EXPECT_NE(GCMClient::SUCCESS, | |
1059 wrapper_->gcm_app_handler()->send_error_details().result); | |
1060 EXPECT_EQ(message.data, | |
1061 wrapper_->gcm_app_handler()->send_error_details().additional_data); | |
1062 } | |
1063 | |
1064 TEST_F(GCMServiceSingleInstanceTest, MessageReceived) { | |
1065 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true); | |
1066 GCMClient::IncomingMessage message; | |
1067 message.data["key1"] = "value1"; | |
1068 message.data["key2"] = "value2"; | |
1069 message.sender_id = "sender"; | |
1070 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); | |
1071 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1072 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1073 wrapper_->gcm_app_handler()->received_event()); | |
1074 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1075 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); | |
1076 EXPECT_TRUE(wrapper_->gcm_app_handler()->message().collapse_key.empty()); | |
1077 EXPECT_EQ(message.sender_id, | |
1078 wrapper_->gcm_app_handler()->message().sender_id); | |
1079 } | |
1080 | |
1081 TEST_F(GCMServiceSingleInstanceTest, MessageWithCollapseKeyReceived) { | |
1082 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true); | |
1083 GCMClient::IncomingMessage message; | |
1084 message.data["key1"] = "value1"; | |
1085 message.collapse_key = "collapse_key_value"; | |
1086 message.sender_id = "sender"; | |
1087 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); | |
1088 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1089 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1090 wrapper_->gcm_app_handler()->received_event()); | |
1091 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1092 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); | |
1093 EXPECT_EQ(message.collapse_key, | |
1094 wrapper_->gcm_app_handler()->message().collapse_key); | |
1095 } | |
1096 | |
1097 TEST_F(GCMServiceSingleInstanceTest, MessagesDeleted) { | |
1098 wrapper_->GetGCMClient()->DeleteMessages(kTestAppID1); | |
1099 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1100 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | |
1101 wrapper_->gcm_app_handler()->received_event()); | |
1102 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1103 } | |
1104 | |
1105 // Tests to make sure that concurrent GCMService instances work correctly | |
1106 // regardless how GCMClient is created. | |
1107 class GCMServiceMultipleInstanceTest : public GCMServiceTest { | |
1108 protected: | |
1109 GCMServiceMultipleInstanceTest(); | |
1110 virtual ~GCMServiceMultipleInstanceTest(); | |
1111 | |
1112 // GCMServiceTest: | |
1113 virtual void SetUp() OVERRIDE; | |
1114 virtual void TearDown() OVERRIDE; | |
1115 | |
1116 scoped_ptr<TestGCMServiceWrapper> wrapper_2_; | |
jianli
2014/04/17 00:33:19
wrapper2_
bartfab (slow)
2014/04/17 14:20:01
Done.
| |
1117 | |
1118 private: | |
1119 DISALLOW_COPY_AND_ASSIGN(GCMServiceMultipleInstanceTest); | |
1120 }; | |
1121 | |
1122 GCMServiceMultipleInstanceTest::GCMServiceMultipleInstanceTest() { | |
1123 } | |
1124 | |
1125 GCMServiceMultipleInstanceTest::~GCMServiceMultipleInstanceTest() { | |
1126 } | |
1127 | |
1128 void GCMServiceMultipleInstanceTest::SetUp() { | |
1129 GCMServiceTest::SetUp(); | |
1130 | |
1131 wrapper_2_.reset(new TestGCMServiceWrapper(request_context_)); | |
1132 | |
1133 wrapper_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
1134 wrapper_2_->CreateService(true, GCMClientMock::NO_DELAY_LOADING); | |
1135 | |
1136 // Initiate check-in for each instance. | |
1137 wrapper_->SignIn(kTestAccountID1); | |
1138 wrapper_2_->SignIn(kTestAccountID2); | |
1139 } | |
1140 | |
1141 void GCMServiceMultipleInstanceTest::TearDown() { | |
1142 wrapper_2_.reset(); | |
1143 } | |
1144 | |
1145 TEST_F(GCMServiceMultipleInstanceTest, Register) { | |
1146 // Register an app. | |
1147 std::vector<std::string> sender_ids; | |
1148 sender_ids.push_back("sender1"); | |
1149 wrapper_->Register(kTestAppID1, sender_ids, true); | |
1150 | |
1151 // Register the same app in a different instance. | |
1152 std::vector<std::string> sender_ids2; | |
1153 sender_ids2.push_back("foo"); | |
1154 sender_ids2.push_back("bar"); | |
1155 wrapper_2_->Register(kTestAppID1, sender_ids2, true); | |
1156 | |
1157 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids), | |
1158 wrapper_->registration_id()); | |
1159 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
1160 | |
1161 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids2), | |
1162 wrapper_2_->registration_id()); | |
1163 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result()); | |
1164 | |
1165 // Register a different app in a different instance. | |
1166 std::vector<std::string> sender_ids3; | |
1167 sender_ids3.push_back("sender1"); | |
1168 sender_ids3.push_back("sender2"); | |
1169 sender_ids3.push_back("sender3"); | |
1170 wrapper_2_->Register(kTestAppID2, sender_ids3, true); | |
1171 | |
1172 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3), | |
1173 wrapper_2_->registration_id()); | |
1174 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result()); | |
1175 } | |
1176 | |
1177 TEST_F(GCMServiceMultipleInstanceTest, Send) { | |
1178 // Send a message from one app in one instance. | |
1179 GCMClient::OutgoingMessage message; | |
1180 message.id = "1"; | |
1181 message.data["key1"] = "value1"; | |
1182 message.data["key2"] = "value2"; | |
1183 wrapper_->Send(kTestAppID1, kUserID1, message, true); | |
1184 | |
1185 // Send a message from same app in another instance. | |
1186 GCMClient::OutgoingMessage message2; | |
1187 message2.id = "2"; | |
1188 message2.data["foo"] = "bar"; | |
1189 wrapper_2_->Send(kTestAppID1, kUserID2, message2, true); | |
1190 | |
1191 EXPECT_EQ(message.id, wrapper_->send_message_id()); | |
1192 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
1193 | |
1194 EXPECT_EQ(message2.id, wrapper_2_->send_message_id()); | |
1195 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result()); | |
1196 | |
1197 // Send another message from different app in another instance. | |
1198 GCMClient::OutgoingMessage message3; | |
1199 message3.id = "3"; | |
1200 message3.data["hello"] = "world"; | |
1201 wrapper_2_->Send(kTestAppID2, kUserID1, message3, true); | |
1202 | |
1203 EXPECT_EQ(message3.id, wrapper_2_->send_message_id()); | |
1204 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result()); | |
1205 } | |
1206 | |
1207 TEST_F(GCMServiceMultipleInstanceTest, MessageReceived) { | |
1208 wrapper_->Register(kTestAppID1, ToSenderList("sender"), true); | |
1209 wrapper_2_->Register(kTestAppID1, ToSenderList("sender"), true); | |
1210 wrapper_2_->Register(kTestAppID2, ToSenderList("sender2"), true); | |
1211 | |
1212 // Trigger an incoming message for an app in one instance. | |
1213 GCMClient::IncomingMessage message; | |
1214 message.data["key1"] = "value1"; | |
1215 message.data["key2"] = "value2"; | |
1216 message.sender_id = "sender"; | |
1217 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, message); | |
1218 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1219 | |
1220 // Trigger an incoming message for the same app in another instance. | |
1221 GCMClient::IncomingMessage message2; | |
1222 message2.data["foo"] = "bar"; | |
1223 message2.sender_id = "sender"; | |
1224 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID1, message2); | |
1225 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1226 | |
1227 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1228 wrapper_->gcm_app_handler()->received_event()); | |
1229 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1230 EXPECT_EQ(message.data, wrapper_->gcm_app_handler()->message().data); | |
1231 EXPECT_EQ("sender", wrapper_->gcm_app_handler()->message().sender_id); | |
1232 | |
1233 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1234 wrapper_2_->gcm_app_handler()->received_event()); | |
1235 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id()); | |
1236 EXPECT_EQ(message2.data, wrapper_2_->gcm_app_handler()->message().data); | |
1237 EXPECT_EQ("sender", wrapper_2_->gcm_app_handler()->message().sender_id); | |
1238 | |
1239 // Trigger another incoming message for a different app in another instance. | |
1240 GCMClient::IncomingMessage message3; | |
1241 message3.data["bar1"] = "foo1"; | |
1242 message3.data["bar2"] = "foo2"; | |
1243 message3.sender_id = "sender2"; | |
1244 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID2, message3); | |
1245 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1246 | |
1247 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1248 wrapper_2_->gcm_app_handler()->received_event()); | |
1249 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id()); | |
1250 EXPECT_EQ(message3.data, wrapper_2_->gcm_app_handler()->message().data); | |
1251 EXPECT_EQ("sender2", wrapper_2_->gcm_app_handler()->message().sender_id); | |
1252 } | |
1253 | |
1254 // Test a set of GCM operations on multiple instances. | |
1255 // 1) Register 1 app in instance 1 and register 2 apps in instance 2; | |
1256 // 2) Send a message from instance 1; | |
1257 // 3) Receive a message to an app in instance 1 and receive a message for each | |
1258 // of the two apps in instance 2; | |
1259 // 4) Send a message for each of the two apps in instance 2; | |
1260 // 5) Sign out of instance 1. | |
1261 // 6) Register/send stops working for instance 1; | |
1262 // 7) The app in instance 2 can still receive these events; | |
1263 // 8) Sign into instance 1 with a different account. | |
1264 // 9) The message to the newly signed-in account will be routed. | |
1265 TEST_F(GCMServiceMultipleInstanceTest, Combined) { | |
1266 // Register an app. | |
1267 std::vector<std::string> sender_ids; | |
1268 sender_ids.push_back("sender1"); | |
1269 wrapper_->Register(kTestAppID1, sender_ids, true); | |
1270 | |
1271 // Register the same app in a different instance. | |
1272 std::vector<std::string> sender_ids2; | |
1273 sender_ids2.push_back("foo"); | |
1274 sender_ids2.push_back("bar"); | |
1275 wrapper_2_->Register(kTestAppID1, sender_ids2, true); | |
1276 | |
1277 // Register a different app in a different instance. | |
1278 std::vector<std::string> sender_ids3; | |
1279 sender_ids3.push_back("sender1"); | |
1280 sender_ids3.push_back("sender2"); | |
1281 sender_ids3.push_back("sender3"); | |
1282 wrapper_2_->Register(kTestAppID2, sender_ids3, true); | |
1283 | |
1284 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids), | |
1285 wrapper_->registration_id()); | |
1286 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->registration_result()); | |
1287 | |
1288 EXPECT_EQ(GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids3), | |
1289 wrapper_2_->registration_id()); | |
1290 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->registration_result()); | |
1291 | |
1292 // Send a message from one instance. | |
1293 GCMClient::OutgoingMessage out_message; | |
1294 out_message.id = "1"; | |
1295 out_message.data["out1"] = "out_data1"; | |
1296 out_message.data["out1_2"] = "out_data1_2"; | |
1297 wrapper_->Send(kTestAppID1, kUserID1, out_message, true); | |
1298 | |
1299 EXPECT_EQ(out_message.id, wrapper_->send_message_id()); | |
1300 EXPECT_EQ(GCMClient::SUCCESS, wrapper_->send_result()); | |
1301 | |
1302 // Trigger an incoming message for an app in one instance. | |
1303 GCMClient::IncomingMessage in_message; | |
1304 in_message.data["in1"] = "in_data1"; | |
1305 in_message.data["in1_2"] = "in_data1_2"; | |
1306 in_message.sender_id = "sender1"; | |
1307 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message); | |
1308 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1309 | |
1310 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1311 wrapper_->gcm_app_handler()->received_event()); | |
1312 EXPECT_EQ(kTestAppID1, wrapper_->gcm_app_handler()->app_id()); | |
1313 EXPECT_EQ(in_message.data, wrapper_->gcm_app_handler()->message().data); | |
1314 | |
1315 // Trigger 2 incoming messages, one for each app respectively, in another | |
1316 // instance. | |
1317 GCMClient::IncomingMessage in_message2; | |
1318 in_message2.data["in2"] = "in_data2"; | |
1319 in_message2.sender_id = "sender3"; | |
1320 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID2, in_message2); | |
1321 | |
1322 GCMClient::IncomingMessage in_message3; | |
1323 in_message3.data["in3"] = "in_data3"; | |
1324 in_message3.data["in3_2"] = "in_data3_2"; | |
1325 in_message3.sender_id = "foo"; | |
1326 wrapper_2_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message3); | |
1327 | |
1328 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1329 | |
1330 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1331 wrapper_2_->gcm_app_handler()->received_event()); | |
1332 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id()); | |
1333 EXPECT_EQ(in_message2.data, wrapper_2_->gcm_app_handler()->message().data); | |
1334 | |
1335 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1336 | |
1337 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1338 wrapper_2_->gcm_app_handler()->received_event()); | |
1339 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id()); | |
1340 EXPECT_EQ(in_message3.data, wrapper_2_->gcm_app_handler()->message().data); | |
1341 | |
1342 // Send two messages, one for each app respectively, from another instance. | |
1343 GCMClient::OutgoingMessage out_message2; | |
1344 out_message2.id = "2"; | |
1345 out_message2.data["out2"] = "out_data2"; | |
1346 wrapper_2_->Send(kTestAppID1, kUserID2, out_message2, true); | |
1347 | |
1348 GCMClient::OutgoingMessage out_message3; | |
1349 out_message3.id = "3"; | |
1350 out_message3.data["out3"] = "out_data3"; | |
1351 wrapper_2_->Send(kTestAppID2, kUserID2, out_message3, false); | |
1352 | |
1353 EXPECT_EQ(out_message2.id, wrapper_2_->send_message_id()); | |
1354 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result()); | |
1355 | |
1356 PumpIOLoop(); | |
1357 PumpUILoop(); | |
1358 | |
1359 EXPECT_EQ(out_message3.id, wrapper_2_->send_message_id()); | |
1360 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result()); | |
1361 | |
1362 // Sign out of one instance. | |
1363 wrapper_->SignOut(); | |
1364 | |
1365 // Register/send stops working for signed-out instance. | |
1366 wrapper_->Register(kTestAppID1, sender_ids, true); | |
1367 EXPECT_TRUE(wrapper_->registration_id().empty()); | |
1368 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->registration_result()); | |
1369 | |
1370 wrapper_->Send(kTestAppID2, kUserID2, out_message3, true); | |
1371 EXPECT_TRUE(wrapper_->send_message_id().empty()); | |
1372 EXPECT_EQ(GCMClient::NOT_SIGNED_IN, wrapper_->send_result()); | |
1373 | |
1374 // Deleted messages event will go through for another signed-in instance. | |
1375 wrapper_2_->GetGCMClient()->DeleteMessages(kTestAppID2); | |
1376 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1377 | |
1378 EXPECT_EQ(FakeGCMAppHandler::MESSAGES_DELETED_EVENT, | |
1379 wrapper_2_->gcm_app_handler()->received_event()); | |
1380 EXPECT_EQ(kTestAppID2, wrapper_2_->gcm_app_handler()->app_id()); | |
1381 | |
1382 // Send error event will go through for another signed-in instance. | |
1383 GCMClient::OutgoingMessage out_message4; | |
1384 out_message4.id = "1@error"; | |
1385 out_message4.data["out4"] = "out_data4"; | |
1386 wrapper_2_->Send(kTestAppID1, kUserID1, out_message4, true); | |
1387 | |
1388 EXPECT_EQ(out_message4.id, wrapper_2_->send_message_id()); | |
1389 EXPECT_EQ(GCMClient::SUCCESS, wrapper_2_->send_result()); | |
1390 | |
1391 wrapper_2_->gcm_app_handler()->WaitForNotification(); | |
1392 EXPECT_EQ(FakeGCMAppHandler::SEND_ERROR_EVENT, | |
1393 wrapper_2_->gcm_app_handler()->received_event()); | |
1394 EXPECT_EQ(kTestAppID1, wrapper_2_->gcm_app_handler()->app_id()); | |
1395 EXPECT_EQ(out_message4.id, | |
1396 wrapper_2_->gcm_app_handler()->send_error_details().message_id); | |
1397 EXPECT_NE(GCMClient::SUCCESS, | |
1398 wrapper_2_->gcm_app_handler()->send_error_details().result); | |
1399 EXPECT_EQ( | |
1400 out_message4.data, | |
1401 wrapper_2_->gcm_app_handler()->send_error_details().additional_data); | |
1402 | |
1403 // Sign in with a different account. | |
1404 wrapper_->SignIn(kTestAccountID3); | |
1405 | |
1406 // Signing out cleared all registrations, so we need to register again. | |
1407 wrapper_->Register(kTestAppID1, ToSenderList("sender1"), true); | |
1408 | |
1409 // Incoming message will go through for the new signed-in account. | |
1410 GCMClient::IncomingMessage in_message5; | |
1411 in_message5.data["in5"] = "in_data5"; | |
1412 in_message5.sender_id = "sender1"; | |
1413 wrapper_->GetGCMClient()->ReceiveMessage(kTestAppID1, in_message5); | |
1414 | |
1415 wrapper_->gcm_app_handler()->WaitForNotification(); | |
1416 | |
1417 EXPECT_EQ(FakeGCMAppHandler::MESSAGE_EVENT, | |
1418 wrapper_->gcm_app_handler()->received_event()); | |
1419 EXPECT_EQ(in_message5.data, wrapper_->gcm_app_handler()->message().data); | |
1420 } | |
1421 | |
1422 } // namespace gcm | |
OLD | NEW |