[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 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 | |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 5 | #include "components/gcm_driver/gcm_client_impl.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 6 | |
[email protected] | fdeaf973 | 2014-04-11 21:02:15 | [diff] [blame] | 7 | #include "base/command_line.h" |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
| 9 | #include "base/files/file_util.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 10 | #include "base/files/scoped_temp_dir.h" |
chirantan | 26436e40 | 2014-10-24 19:44:42 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
| 13 | #include "base/run_loop.h" |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/time/clock.h" |
chirantan | 26436e40 | 2014-10-24 19:44:42 | [diff] [blame] | 16 | #include "base/timer/timer.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 17 | #include "google_apis/gcm/base/fake_encryptor.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 18 | #include "google_apis/gcm/base/mcs_message.h" |
| 19 | #include "google_apis/gcm/base/mcs_util.h" |
| 20 | #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 21 | #include "google_apis/gcm/engine/fake_connection_handler.h" |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 22 | #include "google_apis/gcm/engine/gservices_settings.h" |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 23 | #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 24 | #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 25 | #include "google_apis/gcm/protocol/checkin.pb.h" |
| 26 | #include "google_apis/gcm/protocol/mcs.pb.h" |
| 27 | #include "net/url_request/test_url_fetcher_factory.h" |
| 28 | #include "net/url_request/url_fetcher_delegate.h" |
| 29 | #include "net/url_request/url_request_test_util.h" |
| 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | |
| 32 | namespace gcm { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | enum LastEvent { |
| 37 | NONE, |
| 38 | LOADING_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 39 | REGISTRATION_COMPLETED, |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 40 | UNREGISTRATION_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 41 | MESSAGE_SEND_ERROR, |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 42 | MESSAGE_SEND_ACK, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 43 | MESSAGE_RECEIVED, |
| 44 | MESSAGES_DELETED, |
| 45 | }; |
| 46 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame^] | 47 | const char kChromeVersion[] = "45.0.0.1"; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 48 | const uint64 kDeviceAndroidId = 54321; |
| 49 | const uint64 kDeviceSecurityToken = 12345; |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 50 | const uint64 kDeviceAndroidId2 = 11111; |
| 51 | const uint64 kDeviceSecurityToken2 = 2222; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 52 | const int64 kSettingsCheckinInterval = 16 * 60 * 60; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 53 | const char kAppId[] = "app_id"; |
| 54 | const char kSender[] = "project_id"; |
| 55 | const char kSender2[] = "project_id2"; |
| 56 | const char kSender3[] = "project_id3"; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 57 | const char kRegistrationResponsePrefix[] = "token="; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 58 | const char kUnregistrationResponsePrefix[] = "deleted="; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 59 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame^] | 60 | const char kInstanceID[] = "iid_1"; |
| 61 | const char kScope[] = "GCM"; |
| 62 | const char kDeleteTokenResponse[] = "token=foo"; |
| 63 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 64 | // Helper for building arbitrary data messages. |
| 65 | MCSMessage BuildDownstreamMessage( |
| 66 | const std::string& project_id, |
| 67 | const std::string& app_id, |
| 68 | const std::map<std::string, std::string>& data) { |
| 69 | mcs_proto::DataMessageStanza data_message; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 70 | data_message.set_from(project_id); |
| 71 | data_message.set_category(app_id); |
| 72 | for (std::map<std::string, std::string>::const_iterator iter = data.begin(); |
| 73 | iter != data.end(); |
| 74 | ++iter) { |
| 75 | mcs_proto::AppData* app_data = data_message.add_app_data(); |
| 76 | app_data->set_key(iter->first); |
| 77 | app_data->set_value(iter->second); |
| 78 | } |
| 79 | return MCSMessage(kDataMessageStanzaTag, data_message); |
| 80 | } |
| 81 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 82 | GCMClient::AccountTokenInfo MakeAccountToken(const std::string& email, |
| 83 | const std::string& token) { |
| 84 | GCMClient::AccountTokenInfo account_token; |
| 85 | account_token.email = email; |
| 86 | account_token.access_token = token; |
| 87 | return account_token; |
| 88 | } |
| 89 | |
| 90 | std::map<std::string, std::string> MakeEmailToTokenMap( |
| 91 | const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 92 | std::map<std::string, std::string> email_token_map; |
| 93 | for (std::vector<GCMClient::AccountTokenInfo>::const_iterator iter = |
| 94 | account_tokens.begin(); iter != account_tokens.end(); ++iter) { |
| 95 | email_token_map[iter->email] = iter->access_token; |
| 96 | } |
| 97 | return email_token_map; |
| 98 | } |
| 99 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 100 | class FakeMCSClient : public MCSClient { |
| 101 | public: |
| 102 | FakeMCSClient(base::Clock* clock, |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 103 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 104 | GCMStore* gcm_store, |
| 105 | GCMStatsRecorder* recorder); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 106 | ~FakeMCSClient() override; |
| 107 | void Login(uint64 android_id, uint64 security_token) override; |
| 108 | void SendMessage(const MCSMessage& message) override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 109 | |
| 110 | uint64 last_android_id() const { return last_android_id_; } |
| 111 | uint64 last_security_token() const { return last_security_token_; } |
| 112 | uint8 last_message_tag() const { return last_message_tag_; } |
| 113 | const mcs_proto::DataMessageStanza& last_data_message_stanza() const { |
| 114 | return last_data_message_stanza_; |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | uint64 last_android_id_; |
| 119 | uint64 last_security_token_; |
| 120 | uint8 last_message_tag_; |
| 121 | mcs_proto::DataMessageStanza last_data_message_stanza_; |
| 122 | }; |
| 123 | |
| 124 | FakeMCSClient::FakeMCSClient(base::Clock* clock, |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 125 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 126 | GCMStore* gcm_store, |
| 127 | GCMStatsRecorder* recorder) |
chirantan | 192a921 | 2014-12-06 03:30:45 | [diff] [blame] | 128 | : MCSClient("", clock, connection_factory, gcm_store, recorder), |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 129 | last_android_id_(0u), |
| 130 | last_security_token_(0u), |
| 131 | last_message_tag_(kNumProtoTypes) { |
| 132 | } |
| 133 | |
| 134 | FakeMCSClient::~FakeMCSClient() { |
| 135 | } |
| 136 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 137 | void FakeMCSClient::Login(uint64 android_id, uint64 security_token) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 138 | last_android_id_ = android_id; |
| 139 | last_security_token_ = security_token; |
| 140 | } |
| 141 | |
| 142 | void FakeMCSClient::SendMessage(const MCSMessage& message) { |
| 143 | last_message_tag_ = message.tag(); |
| 144 | if (last_message_tag_ == kDataMessageStanzaTag) { |
| 145 | last_data_message_stanza_.CopyFrom( |
| 146 | reinterpret_cast<const mcs_proto::DataMessageStanza&>( |
| 147 | message.GetProtobuf())); |
| 148 | } |
| 149 | } |
| 150 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 151 | class AutoAdvancingTestClock : public base::Clock { |
| 152 | public: |
| 153 | explicit AutoAdvancingTestClock(base::TimeDelta auto_increment_time_delta); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 154 | ~AutoAdvancingTestClock() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 155 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 156 | base::Time Now() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 157 | void Advance(TimeDelta delta); |
| 158 | int call_count() const { return call_count_; } |
| 159 | |
| 160 | private: |
| 161 | int call_count_; |
| 162 | base::TimeDelta auto_increment_time_delta_; |
| 163 | base::Time now_; |
| 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(AutoAdvancingTestClock); |
| 166 | }; |
| 167 | |
| 168 | AutoAdvancingTestClock::AutoAdvancingTestClock( |
| 169 | base::TimeDelta auto_increment_time_delta) |
| 170 | : call_count_(0), auto_increment_time_delta_(auto_increment_time_delta) { |
| 171 | } |
| 172 | |
| 173 | AutoAdvancingTestClock::~AutoAdvancingTestClock() { |
| 174 | } |
| 175 | |
| 176 | base::Time AutoAdvancingTestClock::Now() { |
| 177 | call_count_++; |
| 178 | now_ += auto_increment_time_delta_; |
| 179 | return now_; |
| 180 | } |
| 181 | |
| 182 | void AutoAdvancingTestClock::Advance(base::TimeDelta delta) { |
| 183 | now_ += delta; |
| 184 | } |
| 185 | |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 186 | class FakeGCMInternalsBuilder : public GCMInternalsBuilder { |
| 187 | public: |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 188 | FakeGCMInternalsBuilder(base::TimeDelta clock_step); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 189 | ~FakeGCMInternalsBuilder() override; |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 190 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 191 | scoped_ptr<base::Clock> BuildClock() override; |
| 192 | scoped_ptr<MCSClient> BuildMCSClient(const std::string& version, |
| 193 | base::Clock* clock, |
| 194 | ConnectionFactory* connection_factory, |
| 195 | GCMStore* gcm_store, |
| 196 | GCMStatsRecorder* recorder) override; |
| 197 | scoped_ptr<ConnectionFactory> BuildConnectionFactory( |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 198 | const std::vector<GURL>& endpoints, |
| 199 | const net::BackoffEntry::Policy& backoff_policy, |
[email protected] | 6f63606a | 2014-07-18 02:18:55 | [diff] [blame] | 200 | const scoped_refptr<net::HttpNetworkSession>& gcm_network_session, |
| 201 | const scoped_refptr<net::HttpNetworkSession>& http_network_session, |
[email protected] | 9df5b93 | 2014-04-30 00:39:06 | [diff] [blame] | 202 | net::NetLog* net_log, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 203 | GCMStatsRecorder* recorder) override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 204 | |
| 205 | private: |
| 206 | base::TimeDelta clock_step_; |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 207 | }; |
| 208 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 209 | FakeGCMInternalsBuilder::FakeGCMInternalsBuilder(base::TimeDelta clock_step) |
| 210 | : clock_step_(clock_step) { |
| 211 | } |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 212 | |
| 213 | FakeGCMInternalsBuilder::~FakeGCMInternalsBuilder() {} |
| 214 | |
| 215 | scoped_ptr<base::Clock> FakeGCMInternalsBuilder::BuildClock() { |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 216 | return make_scoped_ptr<base::Clock>(new AutoAdvancingTestClock(clock_step_)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | scoped_ptr<MCSClient> FakeGCMInternalsBuilder::BuildMCSClient( |
| 220 | const std::string& version, |
| 221 | base::Clock* clock, |
| 222 | ConnectionFactory* connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 223 | GCMStore* gcm_store, |
| 224 | GCMStatsRecorder* recorder) { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 225 | return make_scoped_ptr<MCSClient>(new FakeMCSClient(clock, |
| 226 | connection_factory, |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 227 | gcm_store, |
| 228 | recorder)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | scoped_ptr<ConnectionFactory> FakeGCMInternalsBuilder::BuildConnectionFactory( |
| 232 | const std::vector<GURL>& endpoints, |
| 233 | const net::BackoffEntry::Policy& backoff_policy, |
[email protected] | 6f63606a | 2014-07-18 02:18:55 | [diff] [blame] | 234 | const scoped_refptr<net::HttpNetworkSession>& gcm_network_session, |
| 235 | const scoped_refptr<net::HttpNetworkSession>& http_network_session, |
[email protected] | 9df5b93 | 2014-04-30 00:39:06 | [diff] [blame] | 236 | net::NetLog* net_log, |
| 237 | GCMStatsRecorder* recorder) { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 238 | return make_scoped_ptr<ConnectionFactory>(new FakeConnectionFactory()); |
| 239 | } |
| 240 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 241 | } // namespace |
| 242 | |
| 243 | class GCMClientImplTest : public testing::Test, |
| 244 | public GCMClient::Delegate { |
| 245 | public: |
| 246 | GCMClientImplTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 247 | ~GCMClientImplTest() override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 248 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 249 | void SetUp() override; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 250 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 251 | void SetUpUrlFetcherFactory(); |
| 252 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 253 | void BuildGCMClient(base::TimeDelta clock_step); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 254 | void InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 255 | void StartGCMClient(); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 256 | void Register(const std::string& app_id, |
| 257 | const std::vector<std::string>& senders); |
| 258 | void Unregister(const std::string& app_id); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 259 | void ReceiveMessageFromMCS(const MCSMessage& message); |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 260 | void ReceiveOnMessageSentToMCS( |
| 261 | const std::string& app_id, |
| 262 | const std::string& message_id, |
| 263 | const MCSClient::MessageSendStatus status); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 264 | void CompleteCheckin(uint64 android_id, |
| 265 | uint64 security_token, |
| 266 | const std::string& digest, |
| 267 | const std::map<std::string, std::string>& settings); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 268 | void CompleteRegistration(const std::string& registration_id); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 269 | void CompleteUnregistration(const std::string& app_id); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 270 | void VerifyPendingRequestFetcherDeleted(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 271 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 272 | bool ExistsRegistration(const std::string& app_id) const; |
| 273 | void AddRegistration(const std::string& app_id, |
| 274 | const std::vector<std::string>& sender_ids, |
| 275 | const std::string& registration_id); |
| 276 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 277 | // GCMClient::Delegate overrides (for verification). |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 278 | void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 279 | const std::string& registration_id, |
| 280 | GCMClient::Result result) override; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 281 | void OnUnregisterFinished( |
| 282 | const linked_ptr<RegistrationInfo>& registration_info, |
| 283 | GCMClient::Result result) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 284 | void OnSendFinished(const std::string& app_id, |
| 285 | const std::string& message_id, |
| 286 | GCMClient::Result result) override {} |
| 287 | void OnMessageReceived(const std::string& registration_id, |
| 288 | const GCMClient::IncomingMessage& message) override; |
| 289 | void OnMessagesDeleted(const std::string& app_id) override; |
| 290 | void OnMessageSendError( |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 291 | const std::string& app_id, |
mostynb | fe59f48 | 2014-10-06 15:04:46 | [diff] [blame] | 292 | const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 293 | void OnSendAcknowledged(const std::string& app_id, |
| 294 | const std::string& message_id) override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 295 | void OnGCMReady(const std::vector<AccountMapping>& account_mappings, |
| 296 | const base::Time& last_token_fetch_time) override; |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 297 | void OnActivityRecorded() override {} |
| 298 | void OnConnected(const net::IPEndPoint& ip_endpoint) override {} |
| 299 | void OnDisconnected() override {} |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 300 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 301 | GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 302 | GCMClientImpl::State gcm_client_state() const { |
| 303 | return gcm_client_->state_; |
| 304 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 305 | FakeMCSClient* mcs_client() const { |
| 306 | return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
| 307 | } |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 308 | ConnectionFactory* connection_factory() const { |
| 309 | return gcm_client_->connection_factory_.get(); |
| 310 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 311 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 312 | const GCMClientImpl::CheckinInfo& device_checkin_info() const { |
| 313 | return gcm_client_->device_checkin_info_; |
| 314 | } |
| 315 | |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 316 | void reset_last_event() { |
| 317 | last_event_ = NONE; |
| 318 | last_app_id_.clear(); |
| 319 | last_registration_id_.clear(); |
| 320 | last_message_id_.clear(); |
| 321 | last_result_ = GCMClient::UNKNOWN_ERROR; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 322 | last_account_mappings_.clear(); |
| 323 | last_token_fetch_time_ = base::Time(); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 326 | LastEvent last_event() const { return last_event_; } |
| 327 | const std::string& last_app_id() const { return last_app_id_; } |
| 328 | const std::string& last_registration_id() const { |
| 329 | return last_registration_id_; |
| 330 | } |
| 331 | const std::string& last_message_id() const { return last_message_id_; } |
| 332 | GCMClient::Result last_result() const { return last_result_; } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 333 | const GCMClient::IncomingMessage& last_message() const { |
| 334 | return last_message_; |
| 335 | } |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 336 | const GCMClient::SendErrorDetails& last_error_details() const { |
| 337 | return last_error_details_; |
| 338 | } |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 339 | const base::Time& last_token_fetch_time() const { |
| 340 | return last_token_fetch_time_; |
| 341 | } |
| 342 | const std::vector<AccountMapping>& last_account_mappings() { |
| 343 | return last_account_mappings_; |
| 344 | } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 345 | |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 346 | const GServicesSettings& gservices_settings() const { |
| 347 | return gcm_client_->gservices_settings_; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 348 | } |
| 349 | |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 350 | const base::FilePath& temp_directory_path() const { |
| 351 | return temp_directory_.path(); |
| 352 | } |
| 353 | |
[email protected] | d3ba08d9 | 2014-04-04 23:03:55 | [diff] [blame] | 354 | int64 CurrentTime(); |
| 355 | |
| 356 | // Tooling. |
| 357 | void PumpLoop(); |
| 358 | void PumpLoopUntilIdle(); |
| 359 | void QuitLoop(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 360 | void InitializeLoop(); |
| 361 | bool CreateUniqueTempDir(); |
| 362 | AutoAdvancingTestClock* clock() const { |
| 363 | return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); |
[email protected] | a0144ceb | 2014-04-04 23:49:34 | [diff] [blame] | 364 | } |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame^] | 365 | net::TestURLFetcherFactory* url_fetcher_factory() { |
| 366 | return &url_fetcher_factory_; |
| 367 | } |
[email protected] | d3ba08d9 | 2014-04-04 23:03:55 | [diff] [blame] | 368 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 369 | private: |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 370 | // Variables used for verification. |
| 371 | LastEvent last_event_; |
| 372 | std::string last_app_id_; |
| 373 | std::string last_registration_id_; |
| 374 | std::string last_message_id_; |
| 375 | GCMClient::Result last_result_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 376 | GCMClient::IncomingMessage last_message_; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 377 | GCMClient::SendErrorDetails last_error_details_; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 378 | base::Time last_token_fetch_time_; |
| 379 | std::vector<AccountMapping> last_account_mappings_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 380 | |
| 381 | scoped_ptr<GCMClientImpl> gcm_client_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 382 | |
| 383 | base::MessageLoop message_loop_; |
| 384 | scoped_ptr<base::RunLoop> run_loop_; |
| 385 | net::TestURLFetcherFactory url_fetcher_factory_; |
| 386 | |
| 387 | // Injected to GCM client: |
| 388 | base::ScopedTempDir temp_directory_; |
| 389 | scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 390 | }; |
| 391 | |
| 392 | GCMClientImplTest::GCMClientImplTest() |
| 393 | : last_event_(NONE), |
| 394 | last_result_(GCMClient::UNKNOWN_ERROR), |
| 395 | url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 396 | message_loop_.message_loop_proxy())) { |
| 397 | } |
| 398 | |
| 399 | GCMClientImplTest::~GCMClientImplTest() {} |
| 400 | |
| 401 | void GCMClientImplTest::SetUp() { |
[email protected] | fdeaf973 | 2014-04-11 21:02:15 | [diff] [blame] | 402 | testing::Test::SetUp(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 403 | ASSERT_TRUE(CreateUniqueTempDir()); |
| 404 | InitializeLoop(); |
| 405 | BuildGCMClient(base::TimeDelta()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 406 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 407 | StartGCMClient(); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 408 | SetUpUrlFetcherFactory(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 409 | CompleteCheckin(kDeviceAndroidId, |
| 410 | kDeviceSecurityToken, |
| 411 | std::string(), |
| 412 | std::map<std::string, std::string>()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 413 | } |
| 414 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 415 | void GCMClientImplTest::SetUpUrlFetcherFactory() { |
| 416 | url_fetcher_factory_.set_remove_fetcher_on_delete(true); |
| 417 | } |
| 418 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 419 | void GCMClientImplTest::PumpLoop() { |
| 420 | run_loop_->Run(); |
| 421 | run_loop_.reset(new base::RunLoop()); |
| 422 | } |
| 423 | |
| 424 | void GCMClientImplTest::PumpLoopUntilIdle() { |
| 425 | run_loop_->RunUntilIdle(); |
| 426 | run_loop_.reset(new base::RunLoop()); |
| 427 | } |
| 428 | |
| 429 | void GCMClientImplTest::QuitLoop() { |
| 430 | if (run_loop_ && run_loop_->running()) |
| 431 | run_loop_->Quit(); |
| 432 | } |
| 433 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 434 | void GCMClientImplTest::InitializeLoop() { |
| 435 | run_loop_.reset(new base::RunLoop); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 438 | bool GCMClientImplTest::CreateUniqueTempDir() { |
| 439 | return temp_directory_.CreateUniqueTempDir(); |
| 440 | } |
| 441 | |
| 442 | void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) { |
| 443 | gcm_client_.reset(new GCMClientImpl(make_scoped_ptr<GCMInternalsBuilder>( |
| 444 | new FakeGCMInternalsBuilder(clock_step)))); |
| 445 | } |
| 446 | |
| 447 | void GCMClientImplTest::CompleteCheckin( |
| 448 | uint64 android_id, |
| 449 | uint64 security_token, |
| 450 | const std::string& digest, |
| 451 | const std::map<std::string, std::string>& settings) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 452 | checkin_proto::AndroidCheckinResponse response; |
| 453 | response.set_stats_ok(true); |
| 454 | response.set_android_id(android_id); |
| 455 | response.set_security_token(security_token); |
| 456 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 457 | // For testing G-services settings. |
| 458 | if (!digest.empty()) { |
| 459 | response.set_digest(digest); |
| 460 | for (std::map<std::string, std::string>::const_iterator it = |
| 461 | settings.begin(); |
| 462 | it != settings.end(); |
| 463 | ++it) { |
| 464 | checkin_proto::GservicesSetting* setting = response.add_setting(); |
| 465 | setting->set_name(it->first); |
| 466 | setting->set_value(it->second); |
| 467 | } |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 468 | response.set_settings_diff(false); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 471 | std::string response_string; |
| 472 | response.SerializeToString(&response_string); |
| 473 | |
| 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_string); |
| 478 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void GCMClientImplTest::CompleteRegistration( |
| 482 | const std::string& registration_id) { |
| 483 | std::string response(kRegistrationResponsePrefix); |
| 484 | response.append(registration_id); |
| 485 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 486 | ASSERT_TRUE(fetcher); |
| 487 | fetcher->set_response_code(net::HTTP_OK); |
| 488 | fetcher->SetResponseString(response); |
| 489 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void GCMClientImplTest::CompleteUnregistration( |
| 493 | const std::string& app_id) { |
| 494 | std::string response(kUnregistrationResponsePrefix); |
| 495 | response.append(app_id); |
| 496 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 497 | ASSERT_TRUE(fetcher); |
| 498 | fetcher->set_response_code(net::HTTP_OK); |
| 499 | fetcher->SetResponseString(response); |
| 500 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void GCMClientImplTest::VerifyPendingRequestFetcherDeleted() { |
| 504 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 505 | EXPECT_FALSE(fetcher); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | bool GCMClientImplTest::ExistsRegistration(const std::string& app_id) const { |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 509 | return ExistsGCMRegistrationInMap(gcm_client_->registrations_, app_id); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void GCMClientImplTest::AddRegistration( |
| 513 | const std::string& app_id, |
| 514 | const std::vector<std::string>& sender_ids, |
| 515 | const std::string& registration_id) { |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 516 | linked_ptr<GCMRegistrationInfo> registration(new GCMRegistrationInfo); |
| 517 | registration->app_id = app_id; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 518 | registration->sender_ids = sender_ids; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 519 | gcm_client_->registrations_[registration] = registration_id; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void GCMClientImplTest::InitializeGCMClient() { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 523 | clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 524 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 525 | // Actual initialization. |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 526 | GCMClient::ChromeBuildInfo chrome_build_info; |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame^] | 527 | chrome_build_info.version = kChromeVersion; |
[email protected] | 8ad8051 | 2014-05-23 09:40:47 | [diff] [blame] | 528 | gcm_client_->Initialize(chrome_build_info, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 529 | temp_directory_.path(), |
| 530 | message_loop_.message_loop_proxy(), |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 531 | url_request_context_getter_, |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 532 | make_scoped_ptr<Encryptor>(new FakeEncryptor), |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 533 | this); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 534 | } |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 535 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 536 | void GCMClientImplTest::StartGCMClient() { |
[email protected] | 2bbe0a68 | 2014-03-26 00:08:31 | [diff] [blame] | 537 | // Start loading and check-in. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 538 | gcm_client_->Start(GCMClient::IMMEDIATE_START); |
[email protected] | d3a4b2e | 2014-02-27 13:46:54 | [diff] [blame] | 539 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 540 | PumpLoopUntilIdle(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 541 | } |
| 542 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 543 | void GCMClientImplTest::Register(const std::string& app_id, |
| 544 | const std::vector<std::string>& senders) { |
| 545 | scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
| 546 | gcm_info->app_id = app_id; |
| 547 | gcm_info->sender_ids = senders; |
| 548 | gcm_client()->Register(make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 549 | } |
| 550 | |
| 551 | void GCMClientImplTest::Unregister(const std::string& app_id) { |
| 552 | scoped_ptr<GCMRegistrationInfo> gcm_info(new GCMRegistrationInfo); |
| 553 | gcm_info->app_id = app_id; |
| 554 | gcm_client()->Unregister( |
| 555 | make_linked_ptr<RegistrationInfo>(gcm_info.release())); |
| 556 | } |
| 557 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 558 | void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { |
[email protected] | dd47c4ce | 2014-08-05 23:38:50 | [diff] [blame] | 559 | gcm_client_->recorder_.RecordConnectionInitiated(std::string()); |
| 560 | gcm_client_->recorder_.RecordConnectionSuccess(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 561 | gcm_client_->OnMessageReceivedFromMCS(message); |
| 562 | } |
| 563 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 564 | void GCMClientImplTest::ReceiveOnMessageSentToMCS( |
| 565 | const std::string& app_id, |
| 566 | const std::string& message_id, |
| 567 | const MCSClient::MessageSendStatus status) { |
| 568 | gcm_client_->OnMessageSentToMCS(0LL, app_id, message_id, status); |
| 569 | } |
| 570 | |
fgorski | d578c18b | 2014-09-24 23:40:17 | [diff] [blame] | 571 | void GCMClientImplTest::OnGCMReady( |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 572 | const std::vector<AccountMapping>& account_mappings, |
| 573 | const base::Time& last_token_fetch_time) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 574 | last_event_ = LOADING_COMPLETED; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 575 | last_account_mappings_ = account_mappings; |
| 576 | last_token_fetch_time_ = last_token_fetch_time; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 577 | QuitLoop(); |
| 578 | } |
| 579 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 580 | void GCMClientImplTest::OnMessageReceived( |
| 581 | const std::string& registration_id, |
| 582 | const GCMClient::IncomingMessage& message) { |
| 583 | last_event_ = MESSAGE_RECEIVED; |
| 584 | last_app_id_ = registration_id; |
| 585 | last_message_ = message; |
| 586 | QuitLoop(); |
| 587 | } |
| 588 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 589 | void GCMClientImplTest::OnRegisterFinished( |
| 590 | const linked_ptr<RegistrationInfo>& registration_info, |
| 591 | const std::string& registration_id, |
| 592 | GCMClient::Result result) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 593 | last_event_ = REGISTRATION_COMPLETED; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 594 | last_app_id_ = registration_info->app_id; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 595 | last_registration_id_ = registration_id; |
| 596 | last_result_ = result; |
| 597 | } |
| 598 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 599 | void GCMClientImplTest::OnUnregisterFinished( |
| 600 | const linked_ptr<RegistrationInfo>& registration_info, |
| 601 | GCMClient::Result result) { |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 602 | last_event_ = UNREGISTRATION_COMPLETED; |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 603 | last_app_id_ = registration_info->app_id; |
[email protected] | 0e88e1d1 | 2014-03-19 06:53:08 | [diff] [blame] | 604 | last_result_ = result; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 605 | } |
| 606 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 607 | void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { |
| 608 | last_event_ = MESSAGES_DELETED; |
| 609 | last_app_id_ = app_id; |
| 610 | } |
| 611 | |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 612 | void GCMClientImplTest::OnMessageSendError( |
| 613 | const std::string& app_id, |
| 614 | const gcm::GCMClient::SendErrorDetails& send_error_details) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 615 | last_event_ = MESSAGE_SEND_ERROR; |
| 616 | last_app_id_ = app_id; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 617 | last_error_details_ = send_error_details; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 618 | } |
| 619 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 620 | void GCMClientImplTest::OnSendAcknowledged(const std::string& app_id, |
| 621 | const std::string& message_id) { |
| 622 | last_event_ = MESSAGE_SEND_ACK; |
| 623 | last_app_id_ = app_id; |
| 624 | last_message_id_ = message_id; |
| 625 | } |
| 626 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 627 | int64 GCMClientImplTest::CurrentTime() { |
| 628 | return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; |
| 629 | } |
| 630 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 631 | TEST_F(GCMClientImplTest, LoadingCompleted) { |
| 632 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 633 | EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id()); |
| 634 | EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token()); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 635 | |
| 636 | // Checking freshly loaded CheckinInfo. |
| 637 | EXPECT_EQ(kDeviceAndroidId, device_checkin_info().android_id); |
| 638 | EXPECT_EQ(kDeviceSecurityToken, device_checkin_info().secret); |
| 639 | EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty()); |
| 640 | EXPECT_TRUE(device_checkin_info().accounts_set); |
| 641 | EXPECT_TRUE(device_checkin_info().account_tokens.empty()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 642 | } |
| 643 | |
jianli | 00b4600f | 2015-02-10 23:32:49 | [diff] [blame] | 644 | TEST_F(GCMClientImplTest, LoadingBusted) { |
| 645 | // Close the GCM store. |
| 646 | gcm_client()->Stop(); |
| 647 | PumpLoopUntilIdle(); |
| 648 | |
| 649 | // Mess up the store. |
| 650 | base::FilePath store_file_path = |
| 651 | temp_directory_path().Append(FILE_PATH_LITERAL("CURRENT")); |
| 652 | ASSERT_TRUE(base::AppendToFile(store_file_path, "A", 1)); |
| 653 | |
| 654 | // Restart GCM client. The store should be reset and the loading should |
| 655 | // complete successfully. |
| 656 | reset_last_event(); |
| 657 | BuildGCMClient(base::TimeDelta()); |
| 658 | InitializeGCMClient(); |
| 659 | StartGCMClient(); |
| 660 | CompleteCheckin(kDeviceAndroidId2, |
| 661 | kDeviceSecurityToken2, |
| 662 | std::string(), |
| 663 | std::map<std::string, std::string>()); |
| 664 | |
| 665 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 666 | EXPECT_EQ(kDeviceAndroidId2, mcs_client()->last_android_id()); |
| 667 | EXPECT_EQ(kDeviceSecurityToken2, mcs_client()->last_security_token()); |
| 668 | } |
| 669 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 670 | TEST_F(GCMClientImplTest, RegisterApp) { |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 671 | EXPECT_FALSE(ExistsRegistration(kAppId)); |
| 672 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 673 | std::vector<std::string> senders; |
| 674 | senders.push_back("sender"); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 675 | Register(kAppId, senders); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 676 | CompleteRegistration("reg_id"); |
| 677 | |
| 678 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 679 | EXPECT_EQ(kAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 680 | EXPECT_EQ("reg_id", last_registration_id()); |
| 681 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 682 | EXPECT_TRUE(ExistsRegistration(kAppId)); |
| 683 | } |
| 684 | |
[email protected] | 3904d2b | 2014-03-22 01:34:31 | [diff] [blame] | 685 | TEST_F(GCMClientImplTest, DISABLED_RegisterAppFromCache) { |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 686 | EXPECT_FALSE(ExistsRegistration(kAppId)); |
| 687 | |
| 688 | std::vector<std::string> senders; |
| 689 | senders.push_back("sender"); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 690 | Register(kAppId, senders); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 691 | CompleteRegistration("reg_id"); |
| 692 | EXPECT_TRUE(ExistsRegistration(kAppId)); |
| 693 | |
| 694 | EXPECT_EQ(kAppId, last_app_id()); |
| 695 | EXPECT_EQ("reg_id", last_registration_id()); |
| 696 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 697 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 698 | |
| 699 | // Recreate GCMClient in order to load from the persistent store. |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 700 | BuildGCMClient(base::TimeDelta()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 701 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 702 | StartGCMClient(); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 703 | |
| 704 | EXPECT_TRUE(ExistsRegistration(kAppId)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 705 | } |
| 706 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 707 | TEST_F(GCMClientImplTest, UnregisterApp) { |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 708 | EXPECT_FALSE(ExistsRegistration(kAppId)); |
| 709 | |
| 710 | std::vector<std::string> senders; |
| 711 | senders.push_back("sender"); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 712 | Register(kAppId, senders); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 713 | CompleteRegistration("reg_id"); |
| 714 | EXPECT_TRUE(ExistsRegistration(kAppId)); |
| 715 | |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 716 | Unregister(kAppId); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 717 | CompleteUnregistration(kAppId); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 718 | |
| 719 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 720 | EXPECT_EQ(kAppId, last_app_id()); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 721 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 722 | EXPECT_FALSE(ExistsRegistration(kAppId)); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 723 | } |
| 724 | |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 725 | // Tests that stopping the GCMClient also deletes pending registration requests. |
| 726 | // This is tested by checking that url fetcher contained in the request was |
| 727 | // deleted. |
| 728 | TEST_F(GCMClientImplTest, DeletePendingRequestsWhenStopping) { |
| 729 | std::vector<std::string> senders; |
| 730 | senders.push_back("sender"); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 731 | Register(kAppId, senders); |
fgorski | e45a34f | 2014-10-08 17:37:46 | [diff] [blame] | 732 | |
| 733 | gcm_client()->Stop(); |
| 734 | VerifyPendingRequestFetcherDeleted(); |
| 735 | } |
| 736 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 737 | TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 738 | // Register to receive messages from kSender and kSender2 only. |
| 739 | std::vector<std::string> senders; |
| 740 | senders.push_back(kSender); |
| 741 | senders.push_back(kSender2); |
| 742 | AddRegistration(kAppId, senders, "reg_id"); |
| 743 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 744 | std::map<std::string, std::string> expected_data; |
| 745 | expected_data["message_type"] = "gcm"; |
| 746 | expected_data["key"] = "value"; |
| 747 | expected_data["key2"] = "value2"; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 748 | |
| 749 | // Message for kSender will be received. |
| 750 | MCSMessage message(BuildDownstreamMessage(kSender, kAppId, expected_data)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 751 | EXPECT_TRUE(message.IsValid()); |
| 752 | ReceiveMessageFromMCS(message); |
| 753 | |
| 754 | expected_data.erase(expected_data.find("message_type")); |
| 755 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 756 | EXPECT_EQ(kAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 757 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 758 | EXPECT_EQ(expected_data, last_message().data); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 759 | EXPECT_EQ(kSender, last_message().sender_id); |
| 760 | |
| 761 | reset_last_event(); |
| 762 | |
| 763 | // Message for kSender2 will be received. |
| 764 | MCSMessage message2(BuildDownstreamMessage(kSender2, kAppId, expected_data)); |
| 765 | EXPECT_TRUE(message2.IsValid()); |
| 766 | ReceiveMessageFromMCS(message2); |
| 767 | |
| 768 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 769 | EXPECT_EQ(kAppId, last_app_id()); |
| 770 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 771 | EXPECT_EQ(expected_data, last_message().data); |
| 772 | EXPECT_EQ(kSender2, last_message().sender_id); |
| 773 | |
| 774 | reset_last_event(); |
| 775 | |
| 776 | // Message from kSender3 will be dropped. |
| 777 | MCSMessage message3(BuildDownstreamMessage(kSender3, kAppId, expected_data)); |
| 778 | EXPECT_TRUE(message3.IsValid()); |
| 779 | ReceiveMessageFromMCS(message3); |
| 780 | |
| 781 | EXPECT_NE(MESSAGE_RECEIVED, last_event()); |
| 782 | EXPECT_NE(kAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) { |
| 786 | std::map<std::string, std::string> expected_data; |
| 787 | expected_data["message_type"] = "send_error"; |
| 788 | expected_data["google.message_id"] = "007"; |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 789 | expected_data["error_details"] = "some details"; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 790 | MCSMessage message(BuildDownstreamMessage( |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 791 | kSender, kAppId, expected_data)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 792 | EXPECT_TRUE(message.IsValid()); |
| 793 | ReceiveMessageFromMCS(message); |
| 794 | |
| 795 | EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 796 | EXPECT_EQ(kAppId, last_app_id()); |
[email protected] | c6fe36b | 2014-03-11 10:58:12 | [diff] [blame] | 797 | EXPECT_EQ("007", last_error_details().message_id); |
| 798 | EXPECT_EQ(1UL, last_error_details().additional_data.size()); |
| 799 | GCMClient::MessageData::const_iterator iter = |
| 800 | last_error_details().additional_data.find("error_details"); |
| 801 | EXPECT_TRUE(iter != last_error_details().additional_data.end()); |
| 802 | EXPECT_EQ("some details", iter->second); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { |
| 806 | std::map<std::string, std::string> expected_data; |
| 807 | expected_data["message_type"] = "deleted_messages"; |
| 808 | MCSMessage message(BuildDownstreamMessage( |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 809 | kSender, kAppId, expected_data)); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 810 | EXPECT_TRUE(message.IsValid()); |
| 811 | ReceiveMessageFromMCS(message); |
| 812 | |
| 813 | EXPECT_EQ(MESSAGES_DELETED, last_event()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 814 | EXPECT_EQ(kAppId, last_app_id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | TEST_F(GCMClientImplTest, SendMessage) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 818 | GCMClient::OutgoingMessage message; |
| 819 | message.id = "007"; |
| 820 | message.time_to_live = 500; |
| 821 | message.data["key"] = "value"; |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 822 | gcm_client()->Send(kAppId, kSender, message); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 823 | |
| 824 | EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 825 | EXPECT_EQ(kAppId, mcs_client()->last_data_message_stanza().category()); |
| 826 | EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 827 | EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); |
| 828 | EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); |
| 829 | EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 830 | EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from()); |
[email protected] | 3a20a4d | 2014-03-21 22:54:21 | [diff] [blame] | 831 | EXPECT_EQ(kSender, mcs_client()->last_data_message_stanza().to()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 832 | EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); |
| 833 | EXPECT_EQ("value", |
| 834 | mcs_client()->last_data_message_stanza().app_data(0).value()); |
| 835 | } |
| 836 | |
[email protected] | 292af2b2 | 2014-08-06 19:42:45 | [diff] [blame] | 837 | TEST_F(GCMClientImplTest, SendMessageAcknowledged) { |
| 838 | ReceiveOnMessageSentToMCS(kAppId, "007", MCSClient::SENT); |
| 839 | EXPECT_EQ(MESSAGE_SEND_ACK, last_event()); |
| 840 | EXPECT_EQ(kAppId, last_app_id()); |
| 841 | EXPECT_EQ("007", last_message_id()); |
| 842 | } |
| 843 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 844 | class GCMClientImplCheckinTest : public GCMClientImplTest { |
| 845 | public: |
| 846 | GCMClientImplCheckinTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 847 | ~GCMClientImplCheckinTest() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 848 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 849 | void SetUp() override; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | GCMClientImplCheckinTest::GCMClientImplCheckinTest() { |
| 853 | } |
| 854 | |
| 855 | GCMClientImplCheckinTest::~GCMClientImplCheckinTest() { |
| 856 | } |
| 857 | |
| 858 | void GCMClientImplCheckinTest::SetUp() { |
| 859 | testing::Test::SetUp(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 860 | // Creating unique temp directory that will be used by GCMStore shared between |
| 861 | // GCM Client and G-services settings. |
| 862 | ASSERT_TRUE(CreateUniqueTempDir()); |
| 863 | InitializeLoop(); |
| 864 | // Time will be advancing one hour every time it is checked. |
| 865 | BuildGCMClient(base::TimeDelta::FromSeconds(kSettingsCheckinInterval)); |
| 866 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 867 | StartGCMClient(); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | TEST_F(GCMClientImplCheckinTest, GServicesSettingsAfterInitialCheckin) { |
| 871 | std::map<std::string, std::string> settings; |
| 872 | settings["checkin_interval"] = base::Int64ToString(kSettingsCheckinInterval); |
| 873 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 874 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 875 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 876 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 877 | CompleteCheckin(kDeviceAndroidId, |
| 878 | kDeviceSecurityToken, |
| 879 | GServicesSettings::CalculateDigest(settings), |
| 880 | settings); |
[email protected] | f0935451 | 2014-05-02 00:51:13 | [diff] [blame] | 881 | EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 882 | gservices_settings().GetCheckinInterval()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 883 | EXPECT_EQ(GURL("http://alternative.url/checkin"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 884 | gservices_settings().GetCheckinURL()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 885 | EXPECT_EQ(GURL("http://alternative.url/registration"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 886 | gservices_settings().GetRegistrationURL()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 887 | EXPECT_EQ(GURL("https://alternative.gcm.host:7777"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 888 | gservices_settings().GetMCSMainEndpoint()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 889 | EXPECT_EQ(GURL("https://alternative.gcm.host:443"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 890 | gservices_settings().GetMCSFallbackEndpoint()); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // This test only checks that periodic checkin happens. |
| 894 | TEST_F(GCMClientImplCheckinTest, PeriodicCheckin) { |
| 895 | std::map<std::string, std::string> settings; |
| 896 | settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval); |
| 897 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 898 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 899 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 900 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 901 | CompleteCheckin(kDeviceAndroidId, |
| 902 | kDeviceSecurityToken, |
| 903 | GServicesSettings::CalculateDigest(settings), |
| 904 | settings); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 905 | |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 906 | EXPECT_EQ(2, clock()->call_count()); |
| 907 | |
| 908 | PumpLoopUntilIdle(); |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 909 | CompleteCheckin(kDeviceAndroidId, |
| 910 | kDeviceSecurityToken, |
| 911 | GServicesSettings::CalculateDigest(settings), |
| 912 | settings); |
[email protected] | 764c044 | 2014-05-01 04:30:55 | [diff] [blame] | 913 | } |
| 914 | |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 915 | TEST_F(GCMClientImplCheckinTest, LoadGSettingsFromStore) { |
| 916 | std::map<std::string, std::string> settings; |
| 917 | settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval); |
| 918 | settings["checkin_url"] = "http://alternative.url/checkin"; |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 919 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 920 | settings["gcm_secure_port"] = "7777"; |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 921 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 922 | CompleteCheckin(kDeviceAndroidId, |
| 923 | kDeviceSecurityToken, |
| 924 | GServicesSettings::CalculateDigest(settings), |
| 925 | settings); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 926 | |
| 927 | BuildGCMClient(base::TimeDelta()); |
| 928 | InitializeGCMClient(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 929 | StartGCMClient(); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 930 | |
| 931 | EXPECT_EQ(base::TimeDelta::FromSeconds(kSettingsCheckinInterval), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 932 | gservices_settings().GetCheckinInterval()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 933 | EXPECT_EQ(GURL("http://alternative.url/checkin"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 934 | gservices_settings().GetCheckinURL()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 935 | EXPECT_EQ(GURL("http://alternative.url/registration"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 936 | gservices_settings().GetRegistrationURL()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 937 | EXPECT_EQ(GURL("https://alternative.gcm.host:7777"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 938 | gservices_settings().GetMCSMainEndpoint()); |
[email protected] | 8ab0c4b2 | 2014-05-10 20:40:13 | [diff] [blame] | 939 | EXPECT_EQ(GURL("https://alternative.gcm.host:443"), |
[email protected] | aae544d7 | 2014-05-20 06:53:10 | [diff] [blame] | 940 | gservices_settings().GetMCSFallbackEndpoint()); |
[email protected] | 06e4527 | 2014-05-06 03:41:34 | [diff] [blame] | 941 | } |
| 942 | |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 943 | // This test only checks that periodic checkin happens. |
| 944 | TEST_F(GCMClientImplCheckinTest, CheckinWithAccounts) { |
| 945 | std::map<std::string, std::string> settings; |
| 946 | settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval); |
| 947 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 948 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 949 | settings["gcm_secure_port"] = "7777"; |
| 950 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
| 951 | CompleteCheckin(kDeviceAndroidId, |
| 952 | kDeviceSecurityToken, |
| 953 | GServicesSettings::CalculateDigest(settings), |
| 954 | settings); |
| 955 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 956 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 957 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 958 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 959 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 960 | |
| 961 | EXPECT_TRUE(device_checkin_info().last_checkin_accounts.empty()); |
| 962 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 963 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 964 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 965 | |
| 966 | PumpLoopUntilIdle(); |
| 967 | CompleteCheckin(kDeviceAndroidId, |
| 968 | kDeviceSecurityToken, |
| 969 | GServicesSettings::CalculateDigest(settings), |
| 970 | settings); |
| 971 | |
| 972 | std::set<std::string> accounts; |
| 973 | accounts.insert("[email protected]"); |
| 974 | accounts.insert("[email protected]"); |
| 975 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 976 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 977 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 978 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | // This test only checks that periodic checkin happens. |
| 982 | TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountRemoved) { |
| 983 | std::map<std::string, std::string> settings; |
| 984 | settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval); |
| 985 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 986 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 987 | settings["gcm_secure_port"] = "7777"; |
| 988 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
| 989 | CompleteCheckin(kDeviceAndroidId, |
| 990 | kDeviceSecurityToken, |
| 991 | GServicesSettings::CalculateDigest(settings), |
| 992 | settings); |
| 993 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 994 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 995 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 996 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 997 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 998 | PumpLoopUntilIdle(); |
| 999 | CompleteCheckin(kDeviceAndroidId, |
| 1000 | kDeviceSecurityToken, |
| 1001 | GServicesSettings::CalculateDigest(settings), |
| 1002 | settings); |
| 1003 | |
| 1004 | EXPECT_EQ(2UL, device_checkin_info().last_checkin_accounts.size()); |
| 1005 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1006 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1007 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1008 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1009 | account_tokens.erase(account_tokens.begin() + 1); |
| 1010 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1011 | |
| 1012 | PumpLoopUntilIdle(); |
| 1013 | CompleteCheckin(kDeviceAndroidId, |
| 1014 | kDeviceSecurityToken, |
| 1015 | GServicesSettings::CalculateDigest(settings), |
| 1016 | settings); |
| 1017 | |
| 1018 | std::set<std::string> accounts; |
| 1019 | accounts.insert("[email protected]"); |
| 1020 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1021 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1022 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1023 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | // This test only checks that periodic checkin happens. |
| 1027 | TEST_F(GCMClientImplCheckinTest, CheckinWhenAccountReplaced) { |
| 1028 | std::map<std::string, std::string> settings; |
| 1029 | settings["checkin_interval"] = base::IntToString(kSettingsCheckinInterval); |
| 1030 | settings["checkin_url"] = "http://alternative.url/checkin"; |
| 1031 | settings["gcm_hostname"] = "alternative.gcm.host"; |
| 1032 | settings["gcm_secure_port"] = "7777"; |
| 1033 | settings["gcm_registration_url"] = "http://alternative.url/registration"; |
| 1034 | CompleteCheckin(kDeviceAndroidId, |
| 1035 | kDeviceSecurityToken, |
| 1036 | GServicesSettings::CalculateDigest(settings), |
| 1037 | settings); |
| 1038 | |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1039 | std::vector<GCMClient::AccountTokenInfo> account_tokens; |
| 1040 | account_tokens.push_back(MakeAccountToken("[email protected]", "token1")); |
| 1041 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1042 | |
| 1043 | PumpLoopUntilIdle(); |
| 1044 | CompleteCheckin(kDeviceAndroidId, |
| 1045 | kDeviceSecurityToken, |
| 1046 | GServicesSettings::CalculateDigest(settings), |
| 1047 | settings); |
| 1048 | |
| 1049 | std::set<std::string> accounts; |
| 1050 | accounts.insert("[email protected]"); |
| 1051 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1052 | |
| 1053 | // This should trigger another checkin, because the list of accounts is |
| 1054 | // different. |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1055 | account_tokens.clear(); |
| 1056 | account_tokens.push_back(MakeAccountToken("[email protected]", "token2")); |
| 1057 | gcm_client()->SetAccountTokens(account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1058 | |
| 1059 | PumpLoopUntilIdle(); |
| 1060 | CompleteCheckin(kDeviceAndroidId, |
| 1061 | kDeviceSecurityToken, |
| 1062 | GServicesSettings::CalculateDigest(settings), |
| 1063 | settings); |
| 1064 | |
| 1065 | accounts.clear(); |
| 1066 | accounts.insert("[email protected]"); |
| 1067 | EXPECT_EQ(accounts, device_checkin_info().last_checkin_accounts); |
| 1068 | EXPECT_TRUE(device_checkin_info().accounts_set); |
fgorski | 58b9dfc | 2014-09-29 16:46:18 | [diff] [blame] | 1069 | EXPECT_EQ(MakeEmailToTokenMap(account_tokens), |
| 1070 | device_checkin_info().account_tokens); |
[email protected] | 7df5ef2 | 2014-07-17 07:35:58 | [diff] [blame] | 1071 | } |
| 1072 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1073 | class GCMClientImplStartAndStopTest : public GCMClientImplTest { |
| 1074 | public: |
| 1075 | GCMClientImplStartAndStopTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1076 | ~GCMClientImplStartAndStopTest() override; |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1077 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 1078 | void SetUp() override; |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1079 | |
| 1080 | void DefaultCompleteCheckin(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1081 | }; |
| 1082 | |
| 1083 | GCMClientImplStartAndStopTest::GCMClientImplStartAndStopTest() { |
| 1084 | } |
| 1085 | |
| 1086 | GCMClientImplStartAndStopTest::~GCMClientImplStartAndStopTest() { |
| 1087 | } |
| 1088 | |
| 1089 | void GCMClientImplStartAndStopTest::SetUp() { |
| 1090 | testing::Test::SetUp(); |
| 1091 | ASSERT_TRUE(CreateUniqueTempDir()); |
| 1092 | InitializeLoop(); |
| 1093 | BuildGCMClient(base::TimeDelta()); |
| 1094 | InitializeGCMClient(); |
| 1095 | } |
| 1096 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1097 | void GCMClientImplStartAndStopTest::DefaultCompleteCheckin() { |
| 1098 | SetUpUrlFetcherFactory(); |
| 1099 | CompleteCheckin(kDeviceAndroidId, |
| 1100 | kDeviceSecurityToken, |
| 1101 | std::string(), |
| 1102 | std::map<std::string, std::string>()); |
| 1103 | PumpLoopUntilIdle(); |
| 1104 | } |
| 1105 | |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1106 | TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestart) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1107 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1108 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1109 | |
| 1110 | // Delay start the GCM. |
| 1111 | gcm_client()->Start(GCMClient::DELAYED_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1112 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1113 | EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1114 | |
| 1115 | // Stop the GCM. |
| 1116 | gcm_client()->Stop(); |
| 1117 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1118 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1119 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1120 | // Restart the GCM without delay. |
| 1121 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1122 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1123 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | TEST_F(GCMClientImplStartAndStopTest, StartAndStopImmediately) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1127 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1128 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1129 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1130 | // Delay start the GCM and then stop it immediately. |
| 1131 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1132 | gcm_client()->Stop(); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1133 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1134 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1135 | |
| 1136 | // Start the GCM and then stop it immediately. |
| 1137 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1138 | gcm_client()->Stop(); |
| 1139 | PumpLoopUntilIdle(); |
| 1140 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | TEST_F(GCMClientImplStartAndStopTest, StartStopAndRestartImmediately) { |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1144 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1145 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1146 | |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1147 | // Delay start the GCM and then stop and restart it immediately. |
| 1148 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1149 | gcm_client()->Stop(); |
| 1150 | gcm_client()->Start(GCMClient::DELAYED_START); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1151 | PumpLoopUntilIdle(); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1152 | EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
| 1153 | |
| 1154 | // Start the GCM and then stop and restart it immediately. |
| 1155 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1156 | gcm_client()->Stop(); |
| 1157 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1158 | PumpLoopUntilIdle(); |
| 1159 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1160 | } |
| 1161 | |
| 1162 | TEST_F(GCMClientImplStartAndStopTest, DelayStart) { |
| 1163 | // GCMClientImpl should be in INITIALIZED state at first. |
| 1164 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1165 | |
| 1166 | // Delay start the GCM. |
| 1167 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1168 | PumpLoopUntilIdle(); |
| 1169 | EXPECT_EQ(GCMClientImpl::LOADED, gcm_client_state()); |
| 1170 | |
| 1171 | // Start the GCM immediately and complete the checkin. |
| 1172 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
| 1173 | PumpLoopUntilIdle(); |
| 1174 | EXPECT_EQ(GCMClientImpl::INITIAL_DEVICE_CHECKIN, gcm_client_state()); |
| 1175 | DefaultCompleteCheckin(); |
| 1176 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1177 | |
| 1178 | // Registration. |
| 1179 | std::vector<std::string> senders; |
| 1180 | senders.push_back("sender"); |
jianli | 7a0c9b6 | 2015-05-26 23:24:47 | [diff] [blame] | 1181 | Register(kAppId, senders); |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1182 | CompleteRegistration("reg_id"); |
| 1183 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
| 1184 | |
| 1185 | // Stop the GCM. |
| 1186 | gcm_client()->Stop(); |
| 1187 | PumpLoopUntilIdle(); |
| 1188 | EXPECT_EQ(GCMClientImpl::INITIALIZED, gcm_client_state()); |
| 1189 | |
| 1190 | // Delay start the GCM. GCM is indeed started without delay because the |
| 1191 | // registration record has been found. |
| 1192 | gcm_client()->Start(GCMClient::DELAYED_START); |
| 1193 | PumpLoopUntilIdle(); |
| 1194 | EXPECT_EQ(GCMClientImpl::READY, gcm_client_state()); |
[email protected] | 1abdf20 | 2014-06-13 19:38:53 | [diff] [blame] | 1195 | } |
| 1196 | |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1197 | // Test for known account mappings and last token fetching time being passed |
| 1198 | // to OnGCMReady. |
| 1199 | TEST_F(GCMClientImplStartAndStopTest, OnGCMReadyAccountsAndTokenFetchingTime) { |
| 1200 | // Start the GCM and wait until it is ready. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1201 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1202 | PumpLoopUntilIdle(); |
| 1203 | DefaultCompleteCheckin(); |
| 1204 | |
| 1205 | base::Time expected_time = base::Time::Now(); |
| 1206 | gcm_client()->SetLastTokenFetchTime(expected_time); |
| 1207 | AccountMapping expected_mapping; |
| 1208 | expected_mapping.account_id = "accId"; |
| 1209 | expected_mapping.email = "[email protected]"; |
| 1210 | expected_mapping.status = AccountMapping::MAPPED; |
| 1211 | expected_mapping.status_change_timestamp = expected_time; |
| 1212 | gcm_client()->UpdateAccountMapping(expected_mapping); |
| 1213 | PumpLoopUntilIdle(); |
| 1214 | |
| 1215 | // Stop the GCM. |
| 1216 | gcm_client()->Stop(); |
| 1217 | PumpLoopUntilIdle(); |
| 1218 | |
| 1219 | // Restart the GCM. |
jianli | f3e52af4 | 2015-01-21 23:18:47 | [diff] [blame] | 1220 | gcm_client()->Start(GCMClient::IMMEDIATE_START); |
fgorski | 5df10170 | 2014-10-28 02:09:31 | [diff] [blame] | 1221 | PumpLoopUntilIdle(); |
| 1222 | |
| 1223 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 1224 | EXPECT_EQ(expected_time, last_token_fetch_time()); |
| 1225 | ASSERT_EQ(1UL, last_account_mappings().size()); |
| 1226 | const AccountMapping& actual_mapping = last_account_mappings()[0]; |
| 1227 | EXPECT_EQ(expected_mapping.account_id, actual_mapping.account_id); |
| 1228 | EXPECT_EQ(expected_mapping.email, actual_mapping.email); |
| 1229 | EXPECT_EQ(expected_mapping.status, actual_mapping.status); |
| 1230 | EXPECT_EQ(expected_mapping.status_change_timestamp, |
| 1231 | actual_mapping.status_change_timestamp); |
| 1232 | } |
| 1233 | |
jianli | c02d25e | 2015-05-27 22:24:31 | [diff] [blame^] | 1234 | |
| 1235 | class GCMClientInstanceIDTest : public GCMClientImplTest { |
| 1236 | public: |
| 1237 | GCMClientInstanceIDTest(); |
| 1238 | ~GCMClientInstanceIDTest() override; |
| 1239 | |
| 1240 | void AddInstanceID(const std::string& app_id, |
| 1241 | const std::string& instance_id); |
| 1242 | void RemoveInstanceID(const std::string& app_id); |
| 1243 | void GetToken(const std::string& app_id, |
| 1244 | const std::string& authorized_entity, |
| 1245 | const std::string& scope); |
| 1246 | void DeleteToken(const std::string& app_id, |
| 1247 | const std::string& authorized_entity, |
| 1248 | const std::string& scope); |
| 1249 | void CompleteDeleteToken(); |
| 1250 | bool ExistsToken(const std::string& app_id, |
| 1251 | const std::string& authorized_entity, |
| 1252 | const std::string& scope) const; |
| 1253 | }; |
| 1254 | |
| 1255 | GCMClientInstanceIDTest::GCMClientInstanceIDTest() { |
| 1256 | } |
| 1257 | |
| 1258 | GCMClientInstanceIDTest::~GCMClientInstanceIDTest() { |
| 1259 | } |
| 1260 | |
| 1261 | void GCMClientInstanceIDTest::AddInstanceID(const std::string& app_id, |
| 1262 | const std::string& instance_id) { |
| 1263 | gcm_client()->AddInstanceIDData(app_id, instance_id, "123"); |
| 1264 | } |
| 1265 | |
| 1266 | void GCMClientInstanceIDTest::RemoveInstanceID(const std::string& app_id) { |
| 1267 | gcm_client()->RemoveInstanceIDData(app_id); |
| 1268 | } |
| 1269 | |
| 1270 | void GCMClientInstanceIDTest::GetToken(const std::string& app_id, |
| 1271 | const std::string& authorized_entity, |
| 1272 | const std::string& scope) { |
| 1273 | scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo); |
| 1274 | instance_id_info->app_id = app_id; |
| 1275 | instance_id_info->authorized_entity = authorized_entity; |
| 1276 | instance_id_info->scope = scope; |
| 1277 | gcm_client()->Register( |
| 1278 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1279 | } |
| 1280 | |
| 1281 | void GCMClientInstanceIDTest::DeleteToken(const std::string& app_id, |
| 1282 | const std::string& authorized_entity, |
| 1283 | const std::string& scope) { |
| 1284 | scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo); |
| 1285 | instance_id_info->app_id = app_id; |
| 1286 | instance_id_info->authorized_entity = authorized_entity; |
| 1287 | instance_id_info->scope = scope; |
| 1288 | gcm_client()->Unregister( |
| 1289 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())); |
| 1290 | } |
| 1291 | |
| 1292 | void GCMClientInstanceIDTest::CompleteDeleteToken() { |
| 1293 | std::string response(kDeleteTokenResponse); |
| 1294 | net::TestURLFetcher* fetcher = url_fetcher_factory()->GetFetcherByID(0); |
| 1295 | ASSERT_TRUE(fetcher); |
| 1296 | fetcher->set_response_code(net::HTTP_OK); |
| 1297 | fetcher->SetResponseString(response); |
| 1298 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 1299 | } |
| 1300 | |
| 1301 | bool GCMClientInstanceIDTest::ExistsToken(const std::string& app_id, |
| 1302 | const std::string& authorized_entity, |
| 1303 | const std::string& scope) const { |
| 1304 | scoped_ptr<InstanceIDTokenInfo> instance_id_info(new InstanceIDTokenInfo); |
| 1305 | instance_id_info->app_id = app_id; |
| 1306 | instance_id_info->authorized_entity = authorized_entity; |
| 1307 | instance_id_info->scope = scope; |
| 1308 | return gcm_client()->registrations_.count( |
| 1309 | make_linked_ptr<RegistrationInfo>(instance_id_info.release())) > 0; |
| 1310 | } |
| 1311 | |
| 1312 | TEST_F(GCMClientInstanceIDTest, GetToken) { |
| 1313 | AddInstanceID(kAppId, kInstanceID); |
| 1314 | |
| 1315 | // Get a token. |
| 1316 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1317 | GetToken(kAppId, kSender, kScope); |
| 1318 | CompleteRegistration("token1"); |
| 1319 | |
| 1320 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1321 | EXPECT_EQ(kAppId, last_app_id()); |
| 1322 | EXPECT_EQ("token1", last_registration_id()); |
| 1323 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1324 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1325 | |
| 1326 | // Get another token. |
| 1327 | EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); |
| 1328 | GetToken(kAppId, kSender2, kScope); |
| 1329 | CompleteRegistration("token2"); |
| 1330 | |
| 1331 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1332 | EXPECT_EQ(kAppId, last_app_id()); |
| 1333 | EXPECT_EQ("token2", last_registration_id()); |
| 1334 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1335 | EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); |
| 1336 | // The 1st token still exists. |
| 1337 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1338 | } |
| 1339 | |
| 1340 | TEST_F(GCMClientInstanceIDTest, DeleteSingleToken) { |
| 1341 | AddInstanceID(kAppId, kInstanceID); |
| 1342 | |
| 1343 | // Get a token. |
| 1344 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1345 | GetToken(kAppId, kSender, kScope); |
| 1346 | CompleteRegistration("token1"); |
| 1347 | |
| 1348 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1349 | EXPECT_EQ(kAppId, last_app_id()); |
| 1350 | EXPECT_EQ("token1", last_registration_id()); |
| 1351 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1352 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1353 | |
| 1354 | // Get another token. |
| 1355 | EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); |
| 1356 | GetToken(kAppId, kSender2, kScope); |
| 1357 | CompleteRegistration("token2"); |
| 1358 | |
| 1359 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1360 | EXPECT_EQ(kAppId, last_app_id()); |
| 1361 | EXPECT_EQ("token2", last_registration_id()); |
| 1362 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1363 | EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); |
| 1364 | // The 1st token still exists. |
| 1365 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1366 | |
| 1367 | // Delete the 2nd token. |
| 1368 | DeleteToken(kAppId, kSender2, kScope); |
| 1369 | CompleteDeleteToken(); |
| 1370 | |
| 1371 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1372 | EXPECT_EQ(kAppId, last_app_id()); |
| 1373 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1374 | // The 2nd token is gone while the 1st token still exists. |
| 1375 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1376 | EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); |
| 1377 | |
| 1378 | // Delete the 1st token. |
| 1379 | DeleteToken(kAppId, kSender, kScope); |
| 1380 | CompleteDeleteToken(); |
| 1381 | |
| 1382 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1383 | EXPECT_EQ(kAppId, last_app_id()); |
| 1384 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1385 | // Both tokens are gone now. |
| 1386 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1387 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1388 | } |
| 1389 | |
| 1390 | TEST_F(GCMClientInstanceIDTest, DeleteMultiTokens) { |
| 1391 | AddInstanceID(kAppId, kInstanceID); |
| 1392 | |
| 1393 | // Get a token. |
| 1394 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1395 | GetToken(kAppId, kSender, kScope); |
| 1396 | CompleteRegistration("token1"); |
| 1397 | |
| 1398 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1399 | EXPECT_EQ(kAppId, last_app_id()); |
| 1400 | EXPECT_EQ("token1", last_registration_id()); |
| 1401 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1402 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1403 | |
| 1404 | // Get another token. |
| 1405 | EXPECT_FALSE(ExistsToken(kAppId, kSender2, kScope)); |
| 1406 | GetToken(kAppId, kSender2, kScope); |
| 1407 | CompleteRegistration("token2"); |
| 1408 | |
| 1409 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 1410 | EXPECT_EQ(kAppId, last_app_id()); |
| 1411 | EXPECT_EQ("token2", last_registration_id()); |
| 1412 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1413 | EXPECT_TRUE(ExistsToken(kAppId, kSender2, kScope)); |
| 1414 | // The 1st token still exists. |
| 1415 | EXPECT_TRUE(ExistsToken(kAppId, kSender, kScope)); |
| 1416 | |
| 1417 | // Delete all tokens. |
| 1418 | DeleteToken(kAppId, "*", "*"); |
| 1419 | CompleteDeleteToken(); |
| 1420 | |
| 1421 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 1422 | EXPECT_EQ(kAppId, last_app_id()); |
| 1423 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 1424 | // All tokens are gone now. |
| 1425 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1426 | EXPECT_FALSE(ExistsToken(kAppId, kSender, kScope)); |
| 1427 | } |
| 1428 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 1429 | } // namespace gcm |