Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / services / gcm / gcm_profile_service_unittest.cc
blob99b05c89ff687062262ba54cf1bf9e4a8faf4817
1 // Copyright (c) 2013 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.
5 #include <algorithm>
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/debug/leak_annotations.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/extensions/state_store.h"
13 #include "chrome/browser/extensions/test_extension_service.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/browser/services/gcm/gcm_client_factory.h"
16 #include "chrome/browser/services/gcm/gcm_client_mock.h"
17 #include "chrome/browser/services/gcm/gcm_event_router.h"
18 #include "chrome/browser/services/gcm/gcm_profile_service.h"
19 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
20 #include "chrome/browser/signin/fake_signin_manager.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/common/extensions/features/feature_channel.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "components/webdata/encryptor/encryptor.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 #if defined(OS_CHROMEOS)
33 #include "chrome/browser/chromeos/login/user_manager.h"
34 #include "chrome/browser/chromeos/settings/cros_settings.h"
35 #include "chrome/browser/chromeos/settings/device_settings_service.h"
36 #endif
38 using namespace extensions;
40 namespace gcm {
42 const char kTestExtensionName[] = "FooBar";
43 const char kTestingUsername[] = "user@example.com";
44 const char kTestingAppId[] = "test1";
45 const char kTestingSha1Cert[] = "testing_cert1";
46 const char kUserId[] = "user2";
48 class GCMProfileServiceTest;
50 class GCMEventRouterMock : public GCMEventRouter {
51 public:
52 enum Event {
53 NO_EVENT,
54 MESSAGE_EVENT,
55 MESSAGES_DELETED_EVENT,
56 SEND_ERROR_EVENT
59 explicit GCMEventRouterMock(GCMProfileServiceTest* test);
60 virtual ~GCMEventRouterMock();
62 virtual void OnMessage(const std::string& app_id,
63 const GCMClient::IncomingMessage& message) OVERRIDE;
64 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
65 virtual void OnSendError(const std::string& app_id,
66 const std::string& message_id,
67 GCMClient::Result result) OVERRIDE;
69 Event received_event() const { return received_event_; }
70 const std::string& app_id() const { return app_id_; }
71 const GCMClient::IncomingMessage incoming_message() const {
72 return incoming_message_;
74 const std::string& send_message_id() const { return send_error_message_id_; }
75 GCMClient::Result send_result() const { return send_error_result_; }
77 private:
78 GCMProfileServiceTest* test_;
79 Event received_event_;
80 std::string app_id_;
81 GCMClient::IncomingMessage incoming_message_;
82 std::string send_error_message_id_;
83 GCMClient::Result send_error_result_;
86 class GCMProfileServiceTest : public testing::Test,
87 public GCMProfileService::TestingDelegate {
88 public:
89 static BrowserContextKeyedService* BuildGCMProfileService(
90 content::BrowserContext* profile) {
91 return new GCMProfileService(
92 static_cast<Profile*>(profile), gps_testing_delegate_);
95 static GCMClient* BuildGCMClient() {
96 ANNOTATE_SCOPED_MEMORY_LEAK;
97 return new GCMClientMock();
100 GCMProfileServiceTest() : extension_service_(NULL) {
103 virtual ~GCMProfileServiceTest() {
106 // Overridden from test::Test:
107 virtual void SetUp() OVERRIDE {
108 profile_.reset(new TestingProfile);
110 // Make BrowserThread work in unittest.
111 thread_bundle_.reset(new content::TestBrowserThreadBundle(
112 content::TestBrowserThreadBundle::REAL_IO_THREAD));
114 // This is needed to create extension service under CrOS.
115 #if defined(OS_CHROMEOS)
116 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
117 #endif
119 // Create extension service in order to uninstall the extension.
120 extensions::TestExtensionSystem* extension_system(
121 static_cast<extensions::TestExtensionSystem*>(
122 extensions::ExtensionSystem::Get(profile())));
123 extension_system->CreateExtensionService(
124 CommandLine::ForCurrentProcess(), base::FilePath(), false);
125 extension_service_ = extension_system->Get(profile())->extension_service();
127 // Mock a signed-in user.
128 SigninManagerBase* signin_manager =
129 SigninManagerFactory::GetForProfile(profile());
130 signin_manager->SetAuthenticatedUsername(kTestingUsername);
132 // Encryptor ends up needing access to the keychain on OS X. So use the mock
133 // keychain to prevent prompts.
134 #if defined(OS_MACOSX)
135 Encryptor::UseMockKeychain(true);
136 #endif
138 // Mock a GCMClient.
139 GCMClientFactory::SetTestingFactory(&GCMProfileServiceTest::BuildGCMClient);
141 // Mock a GCMEventRouter.
142 gcm_event_router_mock_.reset(new GCMEventRouterMock(this));
144 // Set |gps_testing_delegate_| for it to be picked up by
145 // BuildGCMProfileService and pass it to GCMProfileService.
146 gps_testing_delegate_ = this;
148 // This will create GCMProfileService that causes check-in to be initiated.
149 GCMProfileService::enable_gcm_for_testing_ = true;
150 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
151 profile(), &GCMProfileServiceTest::BuildGCMProfileService);
153 // Wait till the asynchronous check-in is done.
154 WaitForCompleted();
157 virtual void TearDown() OVERRIDE {
158 #if defined(OS_CHROMEOS)
159 test_user_manager_.reset();
160 #endif
162 extension_service_ = NULL;
163 profile_.reset();
164 base::RunLoop().RunUntilIdle();
167 // Overridden from GCMProfileService::TestingDelegate:
168 virtual GCMEventRouter* GetEventRouter() const OVERRIDE {
169 return gcm_event_router_mock_.get();
172 virtual void CheckInFinished(const GCMClient::CheckinInfo& checkin_info,
173 GCMClient::Result result) OVERRIDE {
174 checkin_info_ = checkin_info;
175 SignalCompleted();
178 // Waits until the asynchrnous operation finishes.
179 void WaitForCompleted() {
180 run_loop_.reset(new base::RunLoop);
181 run_loop_->Run();
184 // Signals that the asynchrnous operation finishes.
185 void SignalCompleted() {
186 if (run_loop_ && run_loop_->running())
187 run_loop_->Quit();
190 // Returns a barebones test extension.
191 scoped_refptr<Extension> CreateExtension() {
192 #if defined(OS_WIN)
193 base::FilePath path(FILE_PATH_LITERAL("c:\\foo"));
194 #elif defined(OS_POSIX)
195 base::FilePath path(FILE_PATH_LITERAL("/foo"));
196 #endif
198 base::DictionaryValue manifest;
199 manifest.SetString(manifest_keys::kVersion, "1.0.0.0");
200 manifest.SetString(manifest_keys::kName, kTestExtensionName);
201 base::ListValue* permission_list = new base::ListValue;
202 permission_list->Append(base::Value::CreateStringValue("gcm"));
203 manifest.Set(manifest_keys::kPermissions, permission_list);
205 // TODO(jianli): Once the GCM API enters stable, remove |channel|.
206 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_UNKNOWN);
207 std::string error;
208 scoped_refptr<Extension> extension =
209 Extension::Create(path.AppendASCII(kTestExtensionName),
210 Manifest::INVALID_LOCATION,
211 manifest,
212 Extension::NO_FLAGS,
213 &error);
214 EXPECT_TRUE(extension.get()) << error;
215 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kGcm));
217 extension_service_->AddExtension(extension.get());
218 return extension;
221 Profile* profile() const { return profile_.get(); }
223 GCMProfileService* GetGCMProfileService() const {
224 return GCMProfileServiceFactory::GetForProfile(profile());
227 protected:
228 scoped_ptr<TestingProfile> profile_;
229 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
230 ExtensionService* extension_service_; // Not owned.
231 scoped_ptr<base::RunLoop> run_loop_;
232 scoped_ptr<GCMEventRouterMock> gcm_event_router_mock_;
233 GCMClient::CheckinInfo checkin_info_;
235 #if defined(OS_CHROMEOS)
236 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
237 chromeos::ScopedTestCrosSettings test_cros_settings_;
238 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
239 #endif
241 private:
242 static GCMProfileService::TestingDelegate* gps_testing_delegate_;
244 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceTest);
247 static GCMClientMock* GetGCMClientMock() {
248 return static_cast<GCMClientMock*>(GCMClientFactory::GetClient());
251 class ScopedGCMClientMockSimulateServerError {
252 public:
253 ScopedGCMClientMockSimulateServerError() {
254 GetGCMClientMock()->set_simulate_server_error(true);
257 ~ScopedGCMClientMockSimulateServerError() {
258 GetGCMClientMock()->set_simulate_server_error(false);
261 private:
262 DISALLOW_COPY_AND_ASSIGN(ScopedGCMClientMockSimulateServerError);
265 GCMProfileService::TestingDelegate*
266 GCMProfileServiceTest::gps_testing_delegate_ = NULL;
268 GCMEventRouterMock::GCMEventRouterMock(GCMProfileServiceTest* test)
269 : test_(test),
270 received_event_(NO_EVENT),
271 send_error_result_(GCMClient::SUCCESS) {
274 GCMEventRouterMock::~GCMEventRouterMock() {
277 void GCMEventRouterMock::OnMessage(const std::string& app_id,
278 const GCMClient::IncomingMessage& message) {
279 received_event_ = MESSAGE_EVENT;
280 app_id_ = app_id;
281 incoming_message_ = message;
282 test_->SignalCompleted();
285 void GCMEventRouterMock::OnMessagesDeleted(const std::string& app_id) {
286 received_event_ = MESSAGES_DELETED_EVENT;
287 app_id_ = app_id;
288 test_->SignalCompleted();
291 void GCMEventRouterMock::OnSendError(const std::string& app_id,
292 const std::string& message_id,
293 GCMClient::Result result) {
294 received_event_ = SEND_ERROR_EVENT;
295 app_id_ = app_id;
296 send_error_message_id_ = message_id;
297 send_error_result_ = result;
298 test_->SignalCompleted();
301 TEST_F(GCMProfileServiceTest, Incognito) {
302 EXPECT_TRUE(GCMProfileServiceFactory::GetForProfile(profile()));
304 // Create an incognito profile.
305 TestingProfile::Builder incognito_profile_builder;
306 incognito_profile_builder.SetIncognito();
307 scoped_ptr<TestingProfile> incognito_profile =
308 incognito_profile_builder.Build();
309 incognito_profile->SetOriginalProfile(profile());
311 EXPECT_FALSE(GCMProfileServiceFactory::GetForProfile(
312 incognito_profile.get()));
315 TEST_F(GCMProfileServiceTest, CheckIn) {
316 EXPECT_TRUE(checkin_info_.IsValid());
318 GCMClient::CheckinInfo expected_checkin_info =
319 GCMClientMock::GetCheckinInfoFromUsername(kTestingUsername);
320 EXPECT_EQ(expected_checkin_info.android_id, checkin_info_.android_id);
321 EXPECT_EQ(expected_checkin_info.secret, checkin_info_.secret);
324 TEST_F(GCMProfileServiceTest, CheckInFromPrefsStore) {
325 // The first check-in should be successful.
326 EXPECT_TRUE(checkin_info_.IsValid());
327 GCMClient::CheckinInfo saved_checkin_info = checkin_info_;
328 checkin_info_.Reset();
330 // Check-in should not reach the server. Forcing GCMClient server error should
331 // help catch this.
332 ScopedGCMClientMockSimulateServerError gcm_client_simulate_server_error;
334 // Recreate GCMProfileService to test reading the check-in info from the
335 // prefs store.
336 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
337 profile(), &GCMProfileServiceTest::BuildGCMProfileService);
339 EXPECT_EQ(saved_checkin_info.android_id, checkin_info_.android_id);
340 EXPECT_EQ(saved_checkin_info.secret, checkin_info_.secret);
343 TEST_F(GCMProfileServiceTest, CheckOut) {
344 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(prefs::kGCMUserAccountID));
345 EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(prefs::kGCMUserToken));
347 GetGCMProfileService()->RemoveUser();
349 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(prefs::kGCMUserAccountID));
350 EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(prefs::kGCMUserToken));
353 class GCMProfileServiceRegisterTest : public GCMProfileServiceTest {
354 public:
355 GCMProfileServiceRegisterTest()
356 : result_(GCMClient::SUCCESS),
357 has_persisted_registration_info_(false) {
360 virtual ~GCMProfileServiceRegisterTest() {
363 void Register(const std::string& app_id,
364 const std::vector<std::string>& sender_ids) {
365 GetGCMProfileService()->Register(
366 app_id,
367 sender_ids,
368 kTestingSha1Cert,
369 base::Bind(&GCMProfileServiceRegisterTest::RegisterCompleted,
370 base::Unretained(this)));
373 void RegisterCompleted(const std::string& registration_id,
374 GCMClient::Result result) {
375 registration_id_ = registration_id;
376 result_ = result;
377 SignalCompleted();
380 bool HasPersistedRegistrationInfo(const std::string& app_id) {
381 StateStore* storage = ExtensionSystem::Get(profile())->state_store();
382 if (!storage)
383 return false;
384 has_persisted_registration_info_ = false;
385 storage->GetExtensionValue(
386 app_id,
387 GCMProfileService::GetPersistentRegisterKeyForTesting(),
388 base::Bind(
389 &GCMProfileServiceRegisterTest::ReadRegistrationInfoFinished,
390 base::Unretained(this)));
391 WaitForCompleted();
392 return has_persisted_registration_info_;
395 void ReadRegistrationInfoFinished(scoped_ptr<base::Value> value) {
396 has_persisted_registration_info_ = value.get() != NULL;
397 SignalCompleted();
400 protected:
401 std::string registration_id_;
402 GCMClient::Result result_;
403 bool has_persisted_registration_info_;
405 private:
406 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceRegisterTest);
409 TEST_F(GCMProfileServiceRegisterTest, Register) {
410 std::vector<std::string> sender_ids;
411 sender_ids.push_back("sender1");
412 Register(kTestingAppId, sender_ids);
413 std::string expected_registration_id =
414 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
416 WaitForCompleted();
417 EXPECT_FALSE(registration_id_.empty());
418 EXPECT_EQ(expected_registration_id, registration_id_);
419 EXPECT_EQ(GCMClient::SUCCESS, result_);
422 TEST_F(GCMProfileServiceRegisterTest, DoubleRegister) {
423 std::vector<std::string> sender_ids;
424 sender_ids.push_back("sender1");
425 Register(kTestingAppId, sender_ids);
426 std::string expected_registration_id =
427 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
429 // Calling regsiter 2nd time without waiting 1st one to finish will fail
430 // immediately.
431 sender_ids.push_back("sender2");
432 Register(kTestingAppId, sender_ids);
433 EXPECT_TRUE(registration_id_.empty());
434 EXPECT_NE(GCMClient::SUCCESS, result_);
436 // The 1st register is still doing fine.
437 WaitForCompleted();
438 EXPECT_FALSE(registration_id_.empty());
439 EXPECT_EQ(expected_registration_id, registration_id_);
440 EXPECT_EQ(GCMClient::SUCCESS, result_);
443 TEST_F(GCMProfileServiceRegisterTest, RegisterError) {
444 std::vector<std::string> sender_ids;
445 sender_ids.push_back("sender1@error");
446 Register(kTestingAppId, sender_ids);
448 WaitForCompleted();
449 EXPECT_TRUE(registration_id_.empty());
450 EXPECT_NE(GCMClient::SUCCESS, result_);
453 TEST_F(GCMProfileServiceRegisterTest, RegisterAgainWithSameSenderIDs) {
454 std::vector<std::string> sender_ids;
455 sender_ids.push_back("sender1");
456 sender_ids.push_back("sender2");
457 Register(kTestingAppId, sender_ids);
458 std::string expected_registration_id =
459 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
461 WaitForCompleted();
462 EXPECT_EQ(expected_registration_id, registration_id_);
463 EXPECT_EQ(GCMClient::SUCCESS, result_);
465 // Clears the results the would be set by the Register callback in preparation
466 // to call register 2nd time.
467 registration_id_.clear();
468 result_ = GCMClient::UNKNOWN_ERROR;
470 // Calling register 2nd time with the same set of sender IDs but different
471 // ordering will get back the same registration ID. There is no need to wait
472 // since register simply returns the cached registration ID.
473 std::vector<std::string> another_sender_ids;
474 another_sender_ids.push_back("sender2");
475 another_sender_ids.push_back("sender1");
476 Register(kTestingAppId, another_sender_ids);
477 EXPECT_EQ(expected_registration_id, registration_id_);
478 EXPECT_EQ(GCMClient::SUCCESS, result_);
481 TEST_F(GCMProfileServiceRegisterTest, RegisterAgainWithDifferentSenderIDs) {
482 std::vector<std::string> sender_ids;
483 sender_ids.push_back("sender1");
484 Register(kTestingAppId, sender_ids);
485 std::string expected_registration_id =
486 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
488 WaitForCompleted();
489 EXPECT_EQ(expected_registration_id, registration_id_);
490 EXPECT_EQ(GCMClient::SUCCESS, result_);
492 // Make sender IDs different.
493 sender_ids.push_back("sender2");
494 std::string expected_registration_id2 =
495 GCMClientMock::GetRegistrationIdFromSenderIds(sender_ids);
497 // Calling register 2nd time with the different sender IDs will get back a new
498 // registration ID.
499 Register(kTestingAppId, sender_ids);
500 WaitForCompleted();
501 EXPECT_EQ(expected_registration_id2, registration_id_);
502 EXPECT_EQ(GCMClient::SUCCESS, result_);
505 TEST_F(GCMProfileServiceRegisterTest, ReadRegistrationFromStateStore) {
506 scoped_refptr<Extension> extension(CreateExtension());
508 std::vector<std::string> sender_ids;
509 sender_ids.push_back("sender1");
510 Register(extension->id(), sender_ids);
512 WaitForCompleted();
513 EXPECT_FALSE(registration_id_.empty());
514 EXPECT_EQ(GCMClient::SUCCESS, result_);
515 std::string old_registration_id = registration_id_;
517 // Clears the results that would be set by the Register callback in
518 // preparation to call register 2nd time.
519 registration_id_.clear();
520 result_ = GCMClient::UNKNOWN_ERROR;
522 // Simulate start-up by recreating GCMProfileService.
523 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
524 profile(), &GCMProfileServiceTest::BuildGCMProfileService);
526 // Simulate start-up by reloading extension.
527 extension_service_->UnloadExtension(extension->id(),
528 UnloadedExtensionInfo::REASON_TERMINATE);
529 extension_service_->AddExtension(extension.get());
531 // Register should not reach the server. Forcing GCMClient server error should
532 // help catch this.
533 ScopedGCMClientMockSimulateServerError gcm_client_simulate_server_error;
535 // This should read the registration info from the extension's state store.
536 // We still need to wait since the reading from state store might happen at
537 // the same time.
538 Register(extension->id(), sender_ids);
539 WaitForCompleted();
540 EXPECT_EQ(old_registration_id, registration_id_);
541 EXPECT_EQ(GCMClient::SUCCESS, result_);
544 TEST_F(GCMProfileServiceRegisterTest,
545 GCMClientLoadingCompletedAfterReadingRegistration) {
546 scoped_refptr<Extension> extension(CreateExtension());
548 std::vector<std::string> sender_ids;
549 sender_ids.push_back("sender1");
550 Register(extension->id(), sender_ids);
552 WaitForCompleted();
553 EXPECT_FALSE(registration_id_.empty());
554 EXPECT_EQ(GCMClient::SUCCESS, result_);
555 std::string old_registration_id = registration_id_;
557 // Clears the results that would be set by the Register callback in
558 // preparation to call register 2nd time.
559 registration_id_.clear();
560 result_ = GCMClient::UNKNOWN_ERROR;
562 // Mark that GCMClient is in loading state.
563 GetGCMClientMock()->SetIsLoading(true);
565 // Simulate start-up by recreating GCMProfileService.
566 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
567 profile(), &GCMProfileServiceTest::BuildGCMProfileService);
569 // Simulate start-up by reloading extension.
570 extension_service_->UnloadExtension(extension->id(),
571 UnloadedExtensionInfo::REASON_TERMINATE);
572 extension_service_->AddExtension(extension.get());
574 // Read the registration info from the extension's state store.
575 // This would hold up because GCMClient is in loading state.
576 Register(extension->id(), sender_ids);
577 base::RunLoop().RunUntilIdle();
578 EXPECT_TRUE(registration_id_.empty());
579 EXPECT_EQ(GCMClient::UNKNOWN_ERROR, result_);
581 // Register operation will be invoked after GCMClient finishes the loading.
582 GetGCMClientMock()->SetIsLoading(false);
583 WaitForCompleted();
584 EXPECT_EQ(old_registration_id, registration_id_);
585 EXPECT_EQ(GCMClient::SUCCESS, result_);
588 TEST_F(GCMProfileServiceRegisterTest, Unregister) {
589 scoped_refptr<Extension> extension(CreateExtension());
591 std::vector<std::string> sender_ids;
592 sender_ids.push_back("sender1");
593 Register(extension->id(), sender_ids);
595 WaitForCompleted();
596 EXPECT_FALSE(registration_id_.empty());
597 EXPECT_EQ(GCMClient::SUCCESS, result_);
599 // The registration info should be cached.
600 EXPECT_FALSE(GetGCMProfileService()->registration_info_map_.empty());
602 // The registration info should be persisted.
603 EXPECT_TRUE(HasPersistedRegistrationInfo(extension->id()));
605 // Uninstall the extension.
606 extension_service_->UninstallExtension(extension->id(), false, NULL);
607 base::MessageLoop::current()->RunUntilIdle();
609 // The cached registration info should be removed.
610 EXPECT_TRUE(GetGCMProfileService()->registration_info_map_.empty());
612 // The persisted registration info should be removed.
613 EXPECT_FALSE(HasPersistedRegistrationInfo(extension->id()));
616 class GCMProfileServiceSendTest : public GCMProfileServiceTest {
617 public:
618 GCMProfileServiceSendTest() : result_(GCMClient::SUCCESS) {
621 virtual ~GCMProfileServiceSendTest() {
624 void Send(const std::string& receiver_id,
625 const GCMClient::OutgoingMessage& message) {
626 GetGCMProfileService()->Send(
627 kTestingAppId,
628 receiver_id,
629 message,
630 base::Bind(&GCMProfileServiceSendTest::SendCompleted,
631 base::Unretained(this)));
634 void SendCompleted(const std::string& message_id, GCMClient::Result result) {
635 message_id_ = message_id;
636 result_ = result;
637 SignalCompleted();
640 protected:
641 std::string message_id_;
642 GCMClient::Result result_;
644 private:
645 DISALLOW_COPY_AND_ASSIGN(GCMProfileServiceSendTest);
648 TEST_F(GCMProfileServiceSendTest, Send) {
649 GCMClient::OutgoingMessage message;
650 message.id = "1";
651 message.data["key1"] = "value1";
652 message.data["key2"] = "value2";
653 Send(kUserId, message);
655 // Wait for the send callback is called.
656 WaitForCompleted();
657 EXPECT_EQ(message_id_, message.id);
658 EXPECT_EQ(GCMClient::SUCCESS, result_);
661 TEST_F(GCMProfileServiceSendTest, SendError) {
662 GCMClient::OutgoingMessage message;
663 // Embedding error in id will tell the mock to simulate the send error.
664 message.id = "1@error";
665 message.data["key1"] = "value1";
666 message.data["key2"] = "value2";
667 Send(kUserId, message);
669 // Wait for the send callback is called.
670 WaitForCompleted();
671 EXPECT_EQ(message_id_, message.id);
672 EXPECT_EQ(GCMClient::SUCCESS, result_);
674 // Wait for the send error.
675 WaitForCompleted();
676 EXPECT_EQ(GCMEventRouterMock::SEND_ERROR_EVENT,
677 gcm_event_router_mock_->received_event());
678 EXPECT_EQ(kTestingAppId, gcm_event_router_mock_->app_id());
679 EXPECT_EQ(message_id_, gcm_event_router_mock_->send_message_id());
680 EXPECT_NE(GCMClient::SUCCESS, gcm_event_router_mock_->send_result());
683 TEST_F(GCMProfileServiceTest, MessageReceived) {
684 GCMClient::IncomingMessage message;
685 message.data["key1"] = "value1";
686 message.data["key2"] = "value2";
687 GetGCMClientMock()->ReceiveMessage(kTestingUsername, kTestingAppId, message);
688 WaitForCompleted();
689 EXPECT_EQ(GCMEventRouterMock::MESSAGE_EVENT,
690 gcm_event_router_mock_->received_event());
691 EXPECT_EQ(kTestingAppId, gcm_event_router_mock_->app_id());
692 ASSERT_EQ(message.data.size(),
693 gcm_event_router_mock_->incoming_message().data.size());
694 EXPECT_EQ(
695 message.data.find("key1")->second,
696 gcm_event_router_mock_->incoming_message().data.find("key1")->second);
697 EXPECT_EQ(
698 message.data.find("key2")->second,
699 gcm_event_router_mock_->incoming_message().data.find("key2")->second);
702 TEST_F(GCMProfileServiceTest, MessagesDeleted) {
703 GetGCMClientMock()->DeleteMessages(kTestingUsername, kTestingAppId);
704 WaitForCompleted();
705 EXPECT_EQ(GCMEventRouterMock::MESSAGES_DELETED_EVENT,
706 gcm_event_router_mock_->received_event());
707 EXPECT_EQ(kTestingAppId, gcm_event_router_mock_->app_id());
710 } // namespace gcm