Desktop User Manager: Use new Material icons for "locked" and "supervised".
[chromium-blink-merge.git] / components / gcm_driver / gcm_client_impl_unittest.cc
blobd706276a3179321aa83956418c3e36284cf8fa95
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.
5 #include "components/gcm_driver/gcm_client_impl.h"
7 #include "base/command_line.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/time/clock.h"
14 #include "base/timer/timer.h"
15 #include "google_apis/gcm/base/fake_encryptor.h"
16 #include "google_apis/gcm/base/mcs_message.h"
17 #include "google_apis/gcm/base/mcs_util.h"
18 #include "google_apis/gcm/engine/fake_connection_factory.h"
19 #include "google_apis/gcm/engine/fake_connection_handler.h"
20 #include "google_apis/gcm/engine/gservices_settings.h"
21 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
22 #include "google_apis/gcm/protocol/android_checkin.pb.h"
23 #include "google_apis/gcm/protocol/checkin.pb.h"
24 #include "google_apis/gcm/protocol/mcs.pb.h"
25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "net/url_request/url_fetcher_delegate.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 namespace gcm {
32 namespace {
34 enum LastEvent {
35 NONE,
36 LOADING_COMPLETED,
37 REGISTRATION_COMPLETED,
38 UNREGISTRATION_COMPLETED,
39 MESSAGE_SEND_ERROR,
40 MESSAGE_SEND_ACK,
41 MESSAGE_RECEIVED,
42 MESSAGES_DELETED,
45 const uint64 kDeviceAndroidId = 54321;
46 const uint64 kDeviceSecurityToken = 12345;
47 const int64 kSettingsCheckinInterval = 16 * 60 * 60;
48 const char kAppId[] = "app_id";
49 const char kSender[] = "project_id";
50 const char kSender2[] = "project_id2";
51 const char kSender3[] = "project_id3";
52 const char kRegistrationResponsePrefix[] = "token=";
53 const char kUnregistrationResponsePrefix[] = "deleted=";
55 // Helper for building arbitrary data messages.
56 MCSMessage BuildDownstreamMessage(
57 const std::string& project_id,
58 const std::string& app_id,
59 const std::map<std::string, std::string>& data) {
60 mcs_proto::DataMessageStanza data_message;
61 data_message.set_from(project_id);
62 data_message.set_category(app_id);
63 for (std::map<std::string, std::string>::const_iterator iter = data.begin();
64 iter != data.end();
65 ++iter) {
66 mcs_proto::AppData* app_data = data_message.add_app_data();
67 app_data->set_key(iter->first);
68 app_data->set_value(iter->second);
70 return MCSMessage(kDataMessageStanzaTag, data_message);
73 GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email,
74 const std::string& token) {
75 GCMClient::AccountTokenInfo account_token;
76 account_token.email = email;
77 account_token.access_token = token;
78 return account_token;
81 std::map<std::string, std::string> MakeEmailToTokenMap(
82 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) {
83 std::map<std::string, std::string> email_token_map;
84 for (std::vector<GCMClient::AccountTokenInfo>::const_iterator iter =
85 account_tokens.begin(); iter != account_tokens.end(); ++iter) {
86 email_token_map[iter->email] = iter->access_token;
88 return email_token_map;
91 class FakeMCSClient : public MCSClient {
92 public:
93 FakeMCSClient(base::Clock* clock,
94 ConnectionFactory* connection_factory,
95 GCMStore* gcm_store,
96 GCMStatsRecorder* recorder);
97 ~FakeMCSClient() override;
98 void Login(uint64 android_id, uint64 security_token) override;
99 void SendMessage(const MCSMessage& message) override;
101 uint64 last_android_id() const { return last_android_id_; }
102 uint64 last_security_token() const { return last_security_token_; }
103 uint8 last_message_tag() const { return last_message_tag_; }
104 const mcs_proto::DataMessageStanza& last_data_message_stanza() const {
105 return last_data_message_stanza_;
108 private:
109 uint64 last_android_id_;
110 uint64 last_security_token_;
111 uint8 last_message_tag_;
112 mcs_proto::DataMessageStanza last_data_message_stanza_;
115 FakeMCSClient::FakeMCSClient(base::Clock* clock,
116 ConnectionFactory* connection_factory,
117 GCMStore* gcm_store,
118 GCMStatsRecorder* recorder)
119 : MCSClient("", clock, connection_factory, gcm_store, recorder,
120 make_scoped_ptr(new base::Timer(true, false))),
121 last_android_id_(0u),
122 last_security_token_(0u),
123 last_message_tag_(kNumProtoTypes) {
126 FakeMCSClient::~FakeMCSClient() {
129 void FakeMCSClient::Login(uint64 android_id, uint64 security_token) {
130 last_android_id_ = android_id;
131 last_security_token_ = security_token;
134 void FakeMCSClient::SendMessage(const MCSMessage& message) {
135 last_message_tag_ = message.tag();
136 if (last_message_tag_ == kDataMessageStanzaTag) {
137 last_data_message_stanza_.CopyFrom(
138 reinterpret_cast<const mcs_proto::DataMessageStanza&>(
139 message.GetProtobuf()));
143 class AutoAdvancingTestClock : public base::Clock {
144 public:
145 explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta);
146 ~AutoAdvancingTestClock() override;
148 base::Time Now() override;
149 void Advance(TimeDelta delta);
150 int call_count() const { return call_count_; }
152 private:
153 int call_count_;
154 base::TimeDelta auto_increment_time_delta_;
155 base::Time now_;
157 DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock);
160 AutoAdvancingTestClock::AutoAdvancingTestClock(
161 base::TimeDelta auto_increment_time_delta)
162 : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) {
165 AutoAdvancingTestClock::~AutoAdvancingTestClock() {
168 base::Time AutoAdvancingTestClock::Now() {
169 call_count_++;
170 now_ += auto_increment_time_delta_;
171 return now_;
174 void AutoAdvancingTestClock::Advance(base::TimeDelta delta) {
175 now_ += delta;
178 class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
179 public:
180 FakeGCMInternalsBuilder(base::TimeDelta clock_step);
181 ~FakeGCMInternalsBuilder() override;
183 scoped_ptr<base::Clock> BuildClock() override;
184 scoped_ptr<MCSClient> BuildMCSClient(const std::string& version,
185 base::Clock* clock,
186 ConnectionFactory* connection_factory,
187 GCMStore* gcm_store,
188 GCMStatsRecorder* recorder) override;
189 scoped_ptr<ConnectionFactory> BuildConnectionFactory(
190 const std::vector<GURL>& endpoints,
191 const net::BackoffEntry::Policy& backoff_policy,
192 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
193 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
194 net::NetLog* net_log,
195 GCMStatsRecorder* recorder) override;
197 private:
198 base::TimeDelta clock_step_;
201 FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step)
202 : clock_step_(clock_step) {
205 FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {}
207 scoped_ptr<base::Clock> FakeGCMInternalsBuilder::BuildClock() {
208 return make_scoped_ptr<base::Clock>(new AutoAdvancingTestClock(clock_step_));
211 scoped_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient(
212 const std::string& version,
213 base::Clock* clock,
214 ConnectionFactory* connection_factory,
215 GCMStore* gcm_store,
216 GCMStatsRecorder* recorder) {
217 return make_scoped_ptr<MCSClient>(new FakeMCSClient(clock,
218 connection_factory,
219 gcm_store,
220 recorder));
223 scoped_ptr<ConnectionFactory> FakeGCMInternalsBuilder::BuildConnectionFactory(
224 const std::vector<GURL>& endpoints,
225 const net::BackoffEntry::Policy& backoff_policy,
226 const scoped_refptr<net::HttpNetworkSession>& gcm_network_session,
227 const scoped_refptr<net::HttpNetworkSession>& http_network_session,
228 net::NetLog* net_log,
229 GCMStatsRecorder* recorder) {
230 return make_scoped_ptr<ConnectionFactory>(new FakeConnectionFactory());
233 } // namespace
235 class GCMClientImplTest : public testing::Test,
236 public GCMClient::Delegate {
237 public:
238 GCMClientImplTest();
239 ~GCMClientImplTest() override;
241 void SetUp() override;
243 void SetUpUrlFetcherFactory();
245 void BuildGCMClient(base::TimeDelta clock_step);
246 void InitializeGCMClient();
247 void StartGCMClient();
248 void ReceiveMessageFromMCS(const MCSMessage& message);
249 void ReceiveOnMessageSentToMCS(
250 const std::string& app_id,
251 const std::string& message_id,
252 const MCSClient::MessageSendStatus status);
253 void CompleteCheckin(uint64 android_id,
254 uint64 security_token,
255 const std::string& digest,
256 const std::map<std::string, std::string>& settings);
257 void CompleteRegistration(const std::string& registration_id);
258 void CompleteUnregistration(const std::string& app_id);
259 void VerifyPendingRequestFetcherDeleted();
261 bool ExistsRegistration(const std::string& app_id) const;
262 void AddRegistration(const std::string& app_id,
263 const std::vector<std::string>& sender_ids,
264 const std::string& registration_id);
266 // GCMClient::Delegate overrides (for verification).
267 void OnRegisterFinished(const std::string& app_id,
268 const std::string& registration_id,
269 GCMClient::Result result) override;
270 void OnUnregisterFinished(const std::string& app_id,
271 GCMClient::Result result) override;
272 void OnSendFinished(const std::string& app_id,
273 const std::string& message_id,
274 GCMClient::Result result) override {}
275 void OnMessageReceived(const std::string& registration_id,
276 const GCMClient::IncomingMessage& message) override;
277 void OnMessagesDeleted(const std::string& app_id) override;
278 void OnMessageSendError(
279 const std::string& app_id,
280 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
281 void OnSendAcknowledged(const std::string& app_id,
282 const std::string& message_id) override;
283 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
284 const base::Time& last_token_fetch_time) override;
285 void OnActivityRecorded() override {}
286 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
287 void OnDisconnected() override {}
289 GCMClientImpl* gcm_client() const { return gcm_client_.get(); }
290 FakeMCSClient* mcs_client() const {
291 return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get());
293 ConnectionFactory* connection_factory() const {
294 return gcm_client_->connection_factory_.get();
297 const GCMClientImpl::CheckinInfo& device_checkin_info() const {
298 return gcm_client_->device_checkin_info_;
301 void reset_last_event() {
302 last_event_ = NONE;
303 last_app_id_.clear();
304 last_registration_id_.clear();
305 last_message_id_.clear();
306 last_result_ = GCMClient::UNKNOWN_ERROR;
307 last_account_mappings_.clear();
308 last_token_fetch_time_ = base::Time();
311 LastEvent last_event() const { return last_event_; }
312 const std::string& last_app_id() const { return last_app_id_; }
313 const std::string& last_registration_id() const {
314 return last_registration_id_;
316 const std::string& last_message_id() const { return last_message_id_; }
317 GCMClient::Result last_result() const { return last_result_; }
318 const GCMClient::IncomingMessage& last_message() const {
319 return last_message_;
321 const GCMClient::SendErrorDetails& last_error_details() const {
322 return last_error_details_;
324 const base::Time& last_token_fetch_time() const {
325 return last_token_fetch_time_;
327 const std::vector<AccountMapping>& last_account_mappings() {
328 return last_account_mappings_;
331 const GServicesSettings& gservices_settings() const {
332 return gcm_client_->gservices_settings_;
335 int64 CurrentTime();
337 // Tooling.
338 void PumpLoop();
339 void PumpLoopUntilIdle();
340 void QuitLoop();
341 void InitializeLoop();
342 bool CreateUniqueTempDir();
343 AutoAdvancingTestClock* clock() const {
344 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get());
347 private:
348 // Variables used for verification.
349 LastEvent last_event_;
350 std::string last_app_id_;
351 std::string last_registration_id_;
352 std::string last_message_id_;
353 GCMClient::Result last_result_;
354 GCMClient::IncomingMessage last_message_;
355 GCMClient::SendErrorDetails last_error_details_;
356 base::Time last_token_fetch_time_;
357 std::vector<AccountMapping> last_account_mappings_;
359 scoped_ptr<GCMClientImpl> gcm_client_;
361 base::MessageLoop message_loop_;
362 scoped_ptr<base::RunLoop> run_loop_;
363 net::TestURLFetcherFactory url_fetcher_factory_;
365 // Injected to GCM client:
366 base::ScopedTempDir temp_directory_;
367 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
370 GCMClientImplTest::GCMClientImplTest()
371 : last_event_(NONE),
372 last_result_(GCMClient::UNKNOWN_ERROR),
373 url_request_context_getter_(new net::TestURLRequestContextGetter(
374 message_loop_.message_loop_proxy())) {
377 GCMClientImplTest::~GCMClientImplTest() {}
379 void GCMClientImplTest::SetUp() {
380 testing::Test::SetUp();
381 ASSERT_TRUE(CreateUniqueTempDir());
382 InitializeLoop();
383 BuildGCMClient(base::TimeDelta());
384 InitializeGCMClient();
385 StartGCMClient();
386 SetUpUrlFetcherFactory();
387 CompleteCheckin(kDeviceAndroidId,
388 kDeviceSecurityToken,
389 std::string(),
390 std::map<std::string, std::string>());
393 void GCMClientImplTest::SetUpUrlFetcherFactory() {
394 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
397 void GCMClientImplTest::PumpLoop() {
398 run_loop_->Run();
399 run_loop_.reset(new base::RunLoop());
402 void GCMClientImplTest::PumpLoopUntilIdle() {
403 run_loop_->RunUntilIdle();
404 run_loop_.reset(new base::RunLoop());
407 void GCMClientImplTest::QuitLoop() {
408 if (run_loop_ && run_loop_->running())
409 run_loop_->Quit();
412 void GCMClientImplTest::InitializeLoop() {
413 run_loop_.reset(new base::RunLoop);
416 bool GCMClientImplTest::CreateUniqueTempDir() {
417 return temp_directory_.CreateUniqueTempDir();
420 void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
421 gcm_client_.reset(new GCMClientImpl(make_scoped_ptr<GCMInternalsBuilder>(
422 new FakeGCMInternalsBuilder(clock_step))));
425 void GCMClientImplTest::CompleteCheckin(
426 uint64 android_id,
427 uint64 security_token,
428 const std::string& digest,
429 const std::map<std::string, std::string>& settings) {
430 checkin_proto::AndroidCheckinResponse response;
431 response.set_stats_ok(true);
432 response.set_android_id(android_id);
433 response.set_security_token(security_token);
435 // For testing G-services settings.
436 if (!digest.empty()) {
437 response.set_digest(digest);
438 for (std::map<std::string, std::string>::const_iterator it =
439 settings.begin();
440 it != settings.end();
441 ++it) {
442 checkin_proto::GservicesSetting* setting = response.add_setting();
443 setting->set_name(it->first);
444 setting->set_value(it->second);
446 response.set_settings_diff(false);
449 std::string response_string;
450 response.SerializeToString(&response_string);
452 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
453 ASSERT_TRUE(fetcher);
454 fetcher->set_response_code(net::HTTP_OK);
455 fetcher->SetResponseString(response_string);
456 fetcher->delegate()->OnURLFetchComplete(fetcher);
459 void GCMClientImplTest::CompleteRegistration(
460 const std::string& registration_id) {
461 std::string response(kRegistrationResponsePrefix);
462 response.append(registration_id);
463 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
464 ASSERT_TRUE(fetcher);
465 fetcher->set_response_code(net::HTTP_OK);
466 fetcher->SetResponseString(response);
467 fetcher->delegate()->OnURLFetchComplete(fetcher);
470 void GCMClientImplTest::CompleteUnregistration(
471 const std::string& app_id) {
472 std::string response(kUnregistrationResponsePrefix);
473 response.append(app_id);
474 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
475 ASSERT_TRUE(fetcher);
476 fetcher->set_response_code(net::HTTP_OK);
477 fetcher->SetResponseString(response);
478 fetcher->delegate()->OnURLFetchComplete(fetcher);
481 void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() {
482 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
483 EXPECT_FALSE(fetcher);
486 bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const {
487 return gcm_client_->registrations_.count(app_id) > 0;
490 void GCMClientImplTest::AddRegistration(
491 const std::string& app_id,
492 const std::vector<std::string>& sender_ids,
493 const std::string& registration_id) {
494 linked_ptr<RegistrationInfo> registration(new RegistrationInfo);
495 registration->sender_ids = sender_ids;
496 registration->registration_id = registration_id;
497 gcm_client_->registrations_[app_id] = registration;
500 void GCMClientImplTest::InitializeGCMClient() {
501 clock()->Advance(base::TimeDelta::FromMilliseconds(1));
503 // Actual initialization.
504 GCMClient::ChromeBuildInfo chrome_build_info;
505 gcm_client_->Initialize(chrome_build_info,
506 temp_directory_.path(),
507 message_loop_.message_loop_proxy(),
508 url_request_context_getter_,
509 make_scoped_ptr<Encryptor>(new FakeEncryptor),
510 this);
513 void GCMClientImplTest::StartGCMClient() {
514 // Start loading and check-in.
515 gcm_client_->Start();
517 PumpLoopUntilIdle();
520 void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) {
521 gcm_client_->recorder_.RecordConnectionInitiated(std::string());
522 gcm_client_->recorder_.RecordConnectionSuccess();
523 gcm_client_->OnMessageReceivedFromMCS(message);
526 void GCMClientImplTest::ReceiveOnMessageSentToMCS(
527 const std::string& app_id,
528 const std::string& message_id,
529 const MCSClient::MessageSendStatus status) {
530 gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status);
533 void GCMClientImplTest::OnGCMReady(
534 const std::vector<AccountMapping>& account_mappings,
535 const base::Time& last_token_fetch_time) {
536 last_event_ = LOADING_COMPLETED;
537 last_account_mappings_ = account_mappings;
538 last_token_fetch_time_ = last_token_fetch_time;
539 QuitLoop();
542 void GCMClientImplTest::OnMessageReceived(
543 const std::string& registration_id,
544 const GCMClient::IncomingMessage& message) {
545 last_event_ = MESSAGE_RECEIVED;
546 last_app_id_ = registration_id;
547 last_message_ = message;
548 QuitLoop();
551 void GCMClientImplTest::OnRegisterFinished(const std::string& app_id,
552 const std::string& registration_id,
553 GCMClient::Result result) {
554 last_event_ = REGISTRATION_COMPLETED;
555 last_app_id_ = app_id;
556 last_registration_id_ = registration_id;
557 last_result_ = result;
560 void GCMClientImplTest::OnUnregisterFinished(const std::string& app_id,
561 GCMClient::Result result) {
562 last_event_ = UNREGISTRATION_COMPLETED;
563 last_app_id_ = app_id;
564 last_result_ = result;
567 void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) {
568 last_event_ = MESSAGES_DELETED;
569 last_app_id_ = app_id;
572 void GCMClientImplTest::OnMessageSendError(
573 const std::string& app_id,
574 const gcm::GCMClient::SendErrorDetails& send_error_details) {
575 last_event_ = MESSAGE_SEND_ERROR;
576 last_app_id_ = app_id;
577 last_error_details_ = send_error_details;
580 void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id,
581 const std::string& message_id) {
582 last_event_ = MESSAGE_SEND_ACK;
583 last_app_id_ = app_id;
584 last_message_id_ = message_id;
587 int64 GCMClientImplTest::CurrentTime() {
588 return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond;
591 TEST_F(GCMClientImplTest, LoadingCompleted) {
592 EXPECT_EQ(LOADING_COMPLETED, last_event());
593 EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id());
594 EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token());
596 // Checking freshly loaded CheckinInfo.
597 EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id);
598 EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret);
599 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
600 EXPECT_TRUE(device_checkin_info().accounts_set);
601 EXPECT_TRUE(device_checkin_info().account_tokens.empty());
604 TEST_F(GCMClientImplTest, CheckOut) {
605 EXPECT_TRUE(mcs_client());
606 EXPECT_TRUE(connection_factory());
607 gcm_client()->CheckOut();
608 EXPECT_FALSE(mcs_client());
609 EXPECT_FALSE(connection_factory());
612 TEST_F(GCMClientImplTest, RegisterApp) {
613 EXPECT_FALSE(ExistsRegistration(kAppId));
615 std::vector<std::string> senders;
616 senders.push_back("sender");
617 gcm_client()->Register(kAppId, senders);
618 CompleteRegistration("reg_id");
620 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
621 EXPECT_EQ(kAppId, last_app_id());
622 EXPECT_EQ("reg_id", last_registration_id());
623 EXPECT_EQ(GCMClient::SUCCESS, last_result());
624 EXPECT_TRUE(ExistsRegistration(kAppId));
627 TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) {
628 EXPECT_FALSE(ExistsRegistration(kAppId));
630 std::vector<std::string> senders;
631 senders.push_back("sender");
632 gcm_client()->Register(kAppId, senders);
633 CompleteRegistration("reg_id");
634 EXPECT_TRUE(ExistsRegistration(kAppId));
636 EXPECT_EQ(kAppId, last_app_id());
637 EXPECT_EQ("reg_id", last_registration_id());
638 EXPECT_EQ(GCMClient::SUCCESS, last_result());
639 EXPECT_EQ(REGISTRATION_COMPLETED, last_event());
641 // Recreate GCMClient in order to load from the persistent store.
642 BuildGCMClient(base::TimeDelta());
643 InitializeGCMClient();
644 StartGCMClient();
646 EXPECT_TRUE(ExistsRegistration(kAppId));
649 TEST_F(GCMClientImplTest, UnregisterApp) {
650 EXPECT_FALSE(ExistsRegistration(kAppId));
652 std::vector<std::string> senders;
653 senders.push_back("sender");
654 gcm_client()->Register(kAppId, senders);
655 CompleteRegistration("reg_id");
656 EXPECT_TRUE(ExistsRegistration(kAppId));
658 gcm_client()->Unregister(kAppId);
659 CompleteUnregistration(kAppId);
661 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
662 EXPECT_EQ(kAppId, last_app_id());
663 EXPECT_EQ(GCMClient::SUCCESS, last_result());
664 EXPECT_FALSE(ExistsRegistration(kAppId));
667 // Tests that stopping the GCMClient also deletes pending registration requests.
668 // This is tested by checking that url fetcher contained in the request was
669 // deleted.
670 TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) {
671 std::vector<std::string> senders;
672 senders.push_back("sender");
673 gcm_client()->Register(kAppId, senders);
675 gcm_client()->Stop();
676 VerifyPendingRequestFetcherDeleted();
679 TEST_F(GCMClientImplTest, DispatchDownstreamMessage) {
680 // Register to receive messages from kSender and kSender2 only.
681 std::vector<std::string> senders;
682 senders.push_back(kSender);
683 senders.push_back(kSender2);
684 AddRegistration(kAppId, senders, "reg_id");
686 std::map<std::string, std::string> expected_data;
687 expected_data["message_type"] = "gcm";
688 expected_data["key"] = "value";
689 expected_data["key2"] = "value2";
691 // Message for kSender will be received.
692 MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data));
693 EXPECT_TRUE(message.IsValid());
694 ReceiveMessageFromMCS(message);
696 expected_data.erase(expected_data.find("message_type"));
697 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
698 EXPECT_EQ(kAppId, last_app_id());
699 EXPECT_EQ(expected_data.size(), last_message().data.size());
700 EXPECT_EQ(expected_data, last_message().data);
701 EXPECT_EQ(kSender, last_message().sender_id);
703 reset_last_event();
705 // Message for kSender2 will be received.
706 MCSMessage message2(BuildDownstreamMessage(kSender2, kAppId, expected_data));
707 EXPECT_TRUE(message2.IsValid());
708 ReceiveMessageFromMCS(message2);
710 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
711 EXPECT_EQ(kAppId, last_app_id());
712 EXPECT_EQ(expected_data.size(), last_message().data.size());
713 EXPECT_EQ(expected_data, last_message().data);
714 EXPECT_EQ(kSender2, last_message().sender_id);
716 reset_last_event();
718 // Message from kSender3 will be dropped.
719 MCSMessage message3(BuildDownstreamMessage(kSender3, kAppId, expected_data));
720 EXPECT_TRUE(message3.IsValid());
721 ReceiveMessageFromMCS(message3);
723 EXPECT_NE(MESSAGE_RECEIVED, last_event());
724 EXPECT_NE(kAppId, last_app_id());
727 TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) {
728 std::map<std::string, std::string> expected_data;
729 expected_data["message_type"] = "send_error";
730 expected_data["google.message_id"] = "007";
731 expected_data["error_details"] = "some details";
732 MCSMessage message(BuildDownstreamMessage(
733 kSender, kAppId, expected_data));
734 EXPECT_TRUE(message.IsValid());
735 ReceiveMessageFromMCS(message);
737 EXPECT_EQ(MESSAGE_SEND_ERROR, last_event());
738 EXPECT_EQ(kAppId, last_app_id());
739 EXPECT_EQ("007", last_error_details().message_id);
740 EXPECT_EQ(1UL, last_error_details().additional_data.size());
741 GCMClient::MessageData::const_iterator iter =
742 last_error_details().additional_data.find("error_details");
743 EXPECT_TRUE(iter != last_error_details().additional_data.end());
744 EXPECT_EQ("some details", iter->second);
747 TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) {
748 std::map<std::string, std::string> expected_data;
749 expected_data["message_type"] = "deleted_messages";
750 MCSMessage message(BuildDownstreamMessage(
751 kSender, kAppId, expected_data));
752 EXPECT_TRUE(message.IsValid());
753 ReceiveMessageFromMCS(message);
755 EXPECT_EQ(MESSAGES_DELETED, last_event());
756 EXPECT_EQ(kAppId, last_app_id());
759 TEST_F(GCMClientImplTest, SendMessage) {
760 GCMClient::OutgoingMessage message;
761 message.id = "007";
762 message.time_to_live = 500;
763 message.data["key"] = "value";
764 gcm_client()->Send(kAppId, kSender, message);
766 EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag());
767 EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category());
768 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
769 EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl());
770 EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent());
771 EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id());
772 EXPECT_EQ("gcm@chrome.com", mcs_client()->last_data_message_stanza().from());
773 EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to());
774 EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key());
775 EXPECT_EQ("value",
776 mcs_client()->last_data_message_stanza().app_data(0).value());
779 TEST_F(GCMClientImplTest, SendMessageAcknowledged) {
780 ReceiveOnMessageSentToMCS(kAppId, "007", MCSClient::SENT);
781 EXPECT_EQ(MESSAGE_SEND_ACK, last_event());
782 EXPECT_EQ(kAppId, last_app_id());
783 EXPECT_EQ("007", last_message_id());
786 class GCMClientImplCheckinTest : public GCMClientImplTest {
787 public:
788 GCMClientImplCheckinTest();
789 ~GCMClientImplCheckinTest() override;
791 void SetUp() override;
794 GCMClientImplCheckinTest::GCMClientImplCheckinTest() {
797 GCMClientImplCheckinTest::~GCMClientImplCheckinTest() {
800 void GCMClientImplCheckinTest::SetUp() {
801 testing::Test::SetUp();
802 // Creating unique temp directory that will be used by GCMStore shared between
803 // GCM Client and G-services settings.
804 ASSERT_TRUE(CreateUniqueTempDir());
805 InitializeLoop();
806 // Time will be advancing one hour every time it is checked.
807 BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval));
808 InitializeGCMClient();
809 StartGCMClient();
812 TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) {
813 std::map<std::string, std::string> settings;
814 settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval);
815 settings["checkin_url"] = "http://alternative.url/checkin";
816 settings["gcm_hostname"] = "alternative.gcm.host";
817 settings["gcm_secure_port"] = "7777";
818 settings["gcm_registration_url"] = "http://alternative.url/registration";
819 CompleteCheckin(kDeviceAndroidId,
820 kDeviceSecurityToken,
821 GServicesSettings::CalculateDigest(settings),
822 settings);
823 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
824 gservices_settings().GetCheckinInterval());
825 EXPECT_EQ(GURL("http://alternative.url/checkin"),
826 gservices_settings().GetCheckinURL());
827 EXPECT_EQ(GURL("http://alternative.url/registration"),
828 gservices_settings().GetRegistrationURL());
829 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
830 gservices_settings().GetMCSMainEndpoint());
831 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
832 gservices_settings().GetMCSFallbackEndpoint());
835 // This test only checks that periodic checkin happens.
836 TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) {
837 std::map<std::string, std::string> settings;
838 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
839 settings["checkin_url"] = "http://alternative.url/checkin";
840 settings["gcm_hostname"] = "alternative.gcm.host";
841 settings["gcm_secure_port"] = "7777";
842 settings["gcm_registration_url"] = "http://alternative.url/registration";
843 CompleteCheckin(kDeviceAndroidId,
844 kDeviceSecurityToken,
845 GServicesSettings::CalculateDigest(settings),
846 settings);
848 EXPECT_EQ(2, clock()->call_count());
850 PumpLoopUntilIdle();
851 CompleteCheckin(kDeviceAndroidId,
852 kDeviceSecurityToken,
853 GServicesSettings::CalculateDigest(settings),
854 settings);
857 TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) {
858 std::map<std::string, std::string> settings;
859 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
860 settings["checkin_url"] = "http://alternative.url/checkin";
861 settings["gcm_hostname"] = "alternative.gcm.host";
862 settings["gcm_secure_port"] = "7777";
863 settings["gcm_registration_url"] = "http://alternative.url/registration";
864 CompleteCheckin(kDeviceAndroidId,
865 kDeviceSecurityToken,
866 GServicesSettings::CalculateDigest(settings),
867 settings);
869 BuildGCMClient(base::TimeDelta());
870 InitializeGCMClient();
871 StartGCMClient();
873 EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval),
874 gservices_settings().GetCheckinInterval());
875 EXPECT_EQ(GURL("http://alternative.url/checkin"),
876 gservices_settings().GetCheckinURL());
877 EXPECT_EQ(GURL("http://alternative.url/registration"),
878 gservices_settings().GetRegistrationURL());
879 EXPECT_EQ(GURL("https://alternative.gcm.host:7777"),
880 gservices_settings().GetMCSMainEndpoint());
881 EXPECT_EQ(GURL("https://alternative.gcm.host:443"),
882 gservices_settings().GetMCSFallbackEndpoint());
885 // This test only checks that periodic checkin happens.
886 TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) {
887 std::map<std::string, std::string> settings;
888 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
889 settings["checkin_url"] = "http://alternative.url/checkin";
890 settings["gcm_hostname"] = "alternative.gcm.host";
891 settings["gcm_secure_port"] = "7777";
892 settings["gcm_registration_url"] = "http://alternative.url/registration";
893 CompleteCheckin(kDeviceAndroidId,
894 kDeviceSecurityToken,
895 GServicesSettings::CalculateDigest(settings),
896 settings);
898 std::vector<GCMClient::AccountTokenInfo> account_tokens;
899 account_tokens.push_back(MakeAccountToken("test_user1@gmail.com", "token1"));
900 account_tokens.push_back(MakeAccountToken("test_user2@gmail.com", "token2"));
901 gcm_client()->SetAccountTokens(account_tokens);
903 EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty());
904 EXPECT_TRUE(device_checkin_info().accounts_set);
905 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
906 device_checkin_info().account_tokens);
908 PumpLoopUntilIdle();
909 CompleteCheckin(kDeviceAndroidId,
910 kDeviceSecurityToken,
911 GServicesSettings::CalculateDigest(settings),
912 settings);
914 std::set<std::string> accounts;
915 accounts.insert("test_user1@gmail.com");
916 accounts.insert("test_user2@gmail.com");
917 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
918 EXPECT_TRUE(device_checkin_info().accounts_set);
919 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
920 device_checkin_info().account_tokens);
923 // This test only checks that periodic checkin happens.
924 TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) {
925 std::map<std::string, std::string> settings;
926 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
927 settings["checkin_url"] = "http://alternative.url/checkin";
928 settings["gcm_hostname"] = "alternative.gcm.host";
929 settings["gcm_secure_port"] = "7777";
930 settings["gcm_registration_url"] = "http://alternative.url/registration";
931 CompleteCheckin(kDeviceAndroidId,
932 kDeviceSecurityToken,
933 GServicesSettings::CalculateDigest(settings),
934 settings);
936 std::vector<GCMClient::AccountTokenInfo> account_tokens;
937 account_tokens.push_back(MakeAccountToken("test_user1@gmail.com", "token1"));
938 account_tokens.push_back(MakeAccountToken("test_user2@gmail.com", "token2"));
939 gcm_client()->SetAccountTokens(account_tokens);
940 PumpLoopUntilIdle();
941 CompleteCheckin(kDeviceAndroidId,
942 kDeviceSecurityToken,
943 GServicesSettings::CalculateDigest(settings),
944 settings);
946 EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size());
947 EXPECT_TRUE(device_checkin_info().accounts_set);
948 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
949 device_checkin_info().account_tokens);
951 account_tokens.erase(account_tokens.begin() + 1);
952 gcm_client()->SetAccountTokens(account_tokens);
954 PumpLoopUntilIdle();
955 CompleteCheckin(kDeviceAndroidId,
956 kDeviceSecurityToken,
957 GServicesSettings::CalculateDigest(settings),
958 settings);
960 std::set<std::string> accounts;
961 accounts.insert("test_user1@gmail.com");
962 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
963 EXPECT_TRUE(device_checkin_info().accounts_set);
964 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
965 device_checkin_info().account_tokens);
968 // This test only checks that periodic checkin happens.
969 TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) {
970 std::map<std::string, std::string> settings;
971 settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval);
972 settings["checkin_url"] = "http://alternative.url/checkin";
973 settings["gcm_hostname"] = "alternative.gcm.host";
974 settings["gcm_secure_port"] = "7777";
975 settings["gcm_registration_url"] = "http://alternative.url/registration";
976 CompleteCheckin(kDeviceAndroidId,
977 kDeviceSecurityToken,
978 GServicesSettings::CalculateDigest(settings),
979 settings);
981 std::vector<GCMClient::AccountTokenInfo> account_tokens;
982 account_tokens.push_back(MakeAccountToken("test_user1@gmail.com", "token1"));
983 gcm_client()->SetAccountTokens(account_tokens);
985 PumpLoopUntilIdle();
986 CompleteCheckin(kDeviceAndroidId,
987 kDeviceSecurityToken,
988 GServicesSettings::CalculateDigest(settings),
989 settings);
991 std::set<std::string> accounts;
992 accounts.insert("test_user1@gmail.com");
993 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
995 // This should trigger another checkin, because the list of accounts is
996 // different.
997 account_tokens.clear();
998 account_tokens.push_back(MakeAccountToken("test_user2@gmail.com", "token2"));
999 gcm_client()->SetAccountTokens(account_tokens);
1001 PumpLoopUntilIdle();
1002 CompleteCheckin(kDeviceAndroidId,
1003 kDeviceSecurityToken,
1004 GServicesSettings::CalculateDigest(settings),
1005 settings);
1007 accounts.clear();
1008 accounts.insert("test_user2@gmail.com");
1009 EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts);
1010 EXPECT_TRUE(device_checkin_info().accounts_set);
1011 EXPECT_EQ(MakeEmailToTokenMap(account_tokens),
1012 device_checkin_info().account_tokens);
1015 class GCMClientImplStartAndStopTest : public GCMClientImplTest {
1016 public:
1017 GCMClientImplStartAndStopTest();
1018 ~GCMClientImplStartAndStopTest() override;
1020 void SetUp() override;
1022 void DefaultCompleteCheckin();
1025 GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() {
1028 GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() {
1031 void GCMClientImplStartAndStopTest::SetUp() {
1032 testing::Test::SetUp();
1033 ASSERT_TRUE(CreateUniqueTempDir());
1034 InitializeLoop();
1035 BuildGCMClient(base::TimeDelta());
1036 InitializeGCMClient();
1039 void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() {
1040 SetUpUrlFetcherFactory();
1041 CompleteCheckin(kDeviceAndroidId,
1042 kDeviceSecurityToken,
1043 std::string(),
1044 std::map<std::string, std::string>());
1045 PumpLoopUntilIdle();
1048 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) {
1049 // Start the GCM and wait until it is ready.
1050 gcm_client()->Start();
1051 PumpLoopUntilIdle();
1053 // Stop the GCM.
1054 gcm_client()->Stop();
1055 PumpLoopUntilIdle();
1057 // Restart the GCM.
1058 gcm_client()->Start();
1059 PumpLoopUntilIdle();
1062 TEST_F(GCMClientImplStartAndStopTest, StartAndStopImmediately) {
1063 // Start the GCM and then stop it immediately.
1064 gcm_client()->Start();
1065 gcm_client()->Stop();
1067 PumpLoopUntilIdle();
1070 TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) {
1071 // Start the GCM and then stop and restart it immediately.
1072 gcm_client()->Start();
1073 gcm_client()->Stop();
1074 gcm_client()->Start();
1076 PumpLoopUntilIdle();
1079 // Test for known account mappings and last token fetching time being passed
1080 // to OnGCMReady.
1081 TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) {
1082 // Start the GCM and wait until it is ready.
1083 gcm_client()->Start();
1084 PumpLoopUntilIdle();
1085 DefaultCompleteCheckin();
1087 base::Time expected_time = base::Time::Now();
1088 gcm_client()->SetLastTokenFetchTime(expected_time);
1089 AccountMapping expected_mapping;
1090 expected_mapping.account_id = "accId";
1091 expected_mapping.email = "email@gmail.com";
1092 expected_mapping.status = AccountMapping::MAPPED;
1093 expected_mapping.status_change_timestamp = expected_time;
1094 gcm_client()->UpdateAccountMapping(expected_mapping);
1095 PumpLoopUntilIdle();
1097 // Stop the GCM.
1098 gcm_client()->Stop();
1099 PumpLoopUntilIdle();
1101 // Restart the GCM.
1102 gcm_client()->Start();
1103 PumpLoopUntilIdle();
1105 EXPECT_EQ(LOADING_COMPLETED, last_event());
1106 EXPECT_EQ(expected_time, last_token_fetch_time());
1107 ASSERT_EQ(1UL, last_account_mappings().size());
1108 const AccountMapping& actual_mapping = last_account_mappings()[0];
1109 EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id);
1110 EXPECT_EQ(expected_mapping.email, actual_mapping.email);
1111 EXPECT_EQ(expected_mapping.status, actual_mapping.status);
1112 EXPECT_EQ(expected_mapping.status_change_timestamp,
1113 actual_mapping.status_change_timestamp);
1116 } // namespace gcm