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