[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 | |
| 5 | #include "google_apis/gcm/gcm_client_impl.h" |
| 6 | |
| 7 | #include "base/files/scoped_temp_dir.h" |
| 8 | #include "base/message_loop/message_loop.h" |
| 9 | #include "base/run_loop.h" |
| 10 | #include "base/test/simple_test_clock.h" |
[email protected] | 0693d516 | 2014-02-12 00:20:17 | [diff] [blame] | 11 | #include "components/webdata/encryptor/encryptor.h" |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 12 | #include "google_apis/gcm/base/mcs_message.h" |
| 13 | #include "google_apis/gcm/base/mcs_util.h" |
| 14 | #include "google_apis/gcm/engine/fake_connection_factory.h" |
| 15 | #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 16 | #include "google_apis/gcm/protocol/android_checkin.pb.h" |
| 17 | #include "google_apis/gcm/protocol/checkin.pb.h" |
| 18 | #include "google_apis/gcm/protocol/mcs.pb.h" |
| 19 | #include "net/url_request/test_url_fetcher_factory.h" |
| 20 | #include "net/url_request/url_fetcher_delegate.h" |
| 21 | #include "net/url_request/url_request_test_util.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace gcm { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | enum LastEvent { |
| 29 | NONE, |
| 30 | LOADING_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 31 | REGISTRATION_COMPLETED, |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 32 | UNREGISTRATION_COMPLETED, |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 33 | MESSAGE_SEND_ERROR, |
| 34 | MESSAGE_RECEIVED, |
| 35 | MESSAGES_DELETED, |
| 36 | }; |
| 37 | |
| 38 | const uint64 kDeviceAndroidId = 54321; |
| 39 | const uint64 kDeviceSecurityToken = 12345; |
| 40 | const char kRegistrationResponsePrefix[] = "token="; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 41 | const char kUnregistrationResponsePrefix[] = "deleted="; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 42 | |
| 43 | // Helper for building arbitrary data messages. |
| 44 | MCSMessage BuildDownstreamMessage( |
| 45 | const std::string& project_id, |
| 46 | const std::string& app_id, |
| 47 | const std::map<std::string, std::string>& data) { |
| 48 | mcs_proto::DataMessageStanza data_message; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 49 | data_message.set_from(project_id); |
| 50 | data_message.set_category(app_id); |
| 51 | for (std::map<std::string, std::string>::const_iterator iter = data.begin(); |
| 52 | iter != data.end(); |
| 53 | ++iter) { |
| 54 | mcs_proto::AppData* app_data = data_message.add_app_data(); |
| 55 | app_data->set_key(iter->first); |
| 56 | app_data->set_value(iter->second); |
| 57 | } |
| 58 | return MCSMessage(kDataMessageStanzaTag, data_message); |
| 59 | } |
| 60 | |
| 61 | class FakeMCSClient : public MCSClient { |
| 62 | public: |
| 63 | FakeMCSClient(base::Clock* clock, |
| 64 | ConnectionFactory* connection_factory); |
| 65 | virtual ~FakeMCSClient(); |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 66 | virtual void Login(uint64 android_id, uint64 security_token) OVERRIDE; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 67 | virtual void SendMessage(const MCSMessage& message) OVERRIDE; |
| 68 | void set_gcm_store(GCMStore* gcm_store); |
| 69 | |
| 70 | uint64 last_android_id() const { return last_android_id_; } |
| 71 | uint64 last_security_token() const { return last_security_token_; } |
| 72 | uint8 last_message_tag() const { return last_message_tag_; } |
| 73 | const mcs_proto::DataMessageStanza& last_data_message_stanza() const { |
| 74 | return last_data_message_stanza_; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | uint64 last_android_id_; |
| 79 | uint64 last_security_token_; |
| 80 | uint8 last_message_tag_; |
| 81 | mcs_proto::DataMessageStanza last_data_message_stanza_; |
| 82 | }; |
| 83 | |
| 84 | FakeMCSClient::FakeMCSClient(base::Clock* clock, |
| 85 | ConnectionFactory* connection_factory) |
[email protected] | e262fd1 | 2014-02-20 16:04:47 | [diff] [blame^] | 86 | : MCSClient("", clock, connection_factory, NULL), |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 87 | last_android_id_(0u), |
| 88 | last_security_token_(0u), |
| 89 | last_message_tag_(kNumProtoTypes) { |
| 90 | } |
| 91 | |
| 92 | FakeMCSClient::~FakeMCSClient() { |
| 93 | } |
| 94 | |
| 95 | void FakeMCSClient::set_gcm_store(GCMStore* gcm_store) { |
| 96 | SetGCMStoreForTesting(gcm_store); |
| 97 | } |
| 98 | |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 99 | void FakeMCSClient::Login(uint64 android_id, uint64 security_token) { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 100 | last_android_id_ = android_id; |
| 101 | last_security_token_ = security_token; |
| 102 | } |
| 103 | |
| 104 | void FakeMCSClient::SendMessage(const MCSMessage& message) { |
| 105 | last_message_tag_ = message.tag(); |
| 106 | if (last_message_tag_ == kDataMessageStanzaTag) { |
| 107 | last_data_message_stanza_.CopyFrom( |
| 108 | reinterpret_cast<const mcs_proto::DataMessageStanza&>( |
| 109 | message.GetProtobuf())); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | |
| 115 | class GCMClientImplTest : public testing::Test, |
| 116 | public GCMClient::Delegate { |
| 117 | public: |
| 118 | GCMClientImplTest(); |
| 119 | virtual ~GCMClientImplTest(); |
| 120 | |
| 121 | virtual void SetUp() OVERRIDE; |
| 122 | |
| 123 | void BuildGCMClient(); |
| 124 | void InitializeGCMClient(); |
| 125 | void ReceiveMessageFromMCS(const MCSMessage& message); |
| 126 | void CompleteCheckin(uint64 android_id, uint64 security_token); |
| 127 | void CompleteRegistration(const std::string& registration_id); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 128 | void CompleteUnregistration(const std::string& app_id); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 129 | |
| 130 | // GCMClient::Delegate overrides (for verification). |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 131 | virtual void OnRegisterFinished(const std::string& app_id, |
| 132 | const std::string& registration_id, |
| 133 | GCMClient::Result result) OVERRIDE; |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 134 | virtual void OnUnregisterFinished(const std::string& app_id, |
| 135 | bool success) OVERRIDE; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 136 | virtual void OnSendFinished(const std::string& app_id, |
| 137 | const std::string& message_id, |
| 138 | GCMClient::Result result) OVERRIDE {} |
| 139 | virtual void OnMessageReceived(const std::string& registration_id, |
| 140 | const GCMClient::IncomingMessage& message) |
| 141 | OVERRIDE; |
| 142 | virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
| 143 | virtual void OnMessageSendError(const std::string& app_id, |
| 144 | const std::string& message_id, |
| 145 | GCMClient::Result result) OVERRIDE; |
[email protected] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 146 | virtual void OnGCMReady() OVERRIDE; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 147 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 148 | GCMClientImpl* gcm_client() const { return gcm_client_.get(); } |
| 149 | FakeMCSClient* mcs_client() const { |
| 150 | return reinterpret_cast<FakeMCSClient*>(gcm_client_->mcs_client_.get()); |
| 151 | } |
| 152 | |
| 153 | LastEvent last_event() const { return last_event_; } |
| 154 | const std::string& last_app_id() const { return last_app_id_; } |
| 155 | const std::string& last_registration_id() const { |
| 156 | return last_registration_id_; |
| 157 | } |
| 158 | const std::string& last_message_id() const { return last_message_id_; } |
| 159 | GCMClient::Result last_result() const { return last_result_; } |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 160 | const GCMClient::IncomingMessage& last_message() const { |
| 161 | return last_message_; |
| 162 | } |
| 163 | |
| 164 | int64 CurrentTime(); |
| 165 | |
| 166 | private: |
| 167 | // Tooling. |
| 168 | void PumpLoop(); |
| 169 | void PumpLoopUntilIdle(); |
| 170 | void QuitLoop(); |
| 171 | |
| 172 | base::SimpleTestClock* clock() const { |
| 173 | return reinterpret_cast<base::SimpleTestClock*>(gcm_client_->clock_.get()); |
| 174 | } |
| 175 | |
| 176 | // Variables used for verification. |
| 177 | LastEvent last_event_; |
| 178 | std::string last_app_id_; |
| 179 | std::string last_registration_id_; |
| 180 | std::string last_message_id_; |
| 181 | GCMClient::Result last_result_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 182 | GCMClient::IncomingMessage last_message_; |
| 183 | |
| 184 | scoped_ptr<GCMClientImpl> gcm_client_; |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 185 | scoped_ptr<FakeConnectionFactory> connection_factory_; |
| 186 | |
| 187 | base::MessageLoop message_loop_; |
| 188 | scoped_ptr<base::RunLoop> run_loop_; |
| 189 | net::TestURLFetcherFactory url_fetcher_factory_; |
| 190 | |
| 191 | // Injected to GCM client: |
| 192 | base::ScopedTempDir temp_directory_; |
| 193 | scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 194 | }; |
| 195 | |
| 196 | GCMClientImplTest::GCMClientImplTest() |
| 197 | : last_event_(NONE), |
| 198 | last_result_(GCMClient::UNKNOWN_ERROR), |
| 199 | url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 200 | message_loop_.message_loop_proxy())) { |
| 201 | } |
| 202 | |
| 203 | GCMClientImplTest::~GCMClientImplTest() {} |
| 204 | |
| 205 | void GCMClientImplTest::SetUp() { |
| 206 | ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 207 | run_loop_.reset(new base::RunLoop); |
| 208 | BuildGCMClient(); |
| 209 | InitializeGCMClient(); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void GCMClientImplTest::PumpLoop() { |
| 213 | run_loop_->Run(); |
| 214 | run_loop_.reset(new base::RunLoop()); |
| 215 | } |
| 216 | |
| 217 | void GCMClientImplTest::PumpLoopUntilIdle() { |
| 218 | run_loop_->RunUntilIdle(); |
| 219 | run_loop_.reset(new base::RunLoop()); |
| 220 | } |
| 221 | |
| 222 | void GCMClientImplTest::QuitLoop() { |
| 223 | if (run_loop_ && run_loop_->running()) |
| 224 | run_loop_->Quit(); |
| 225 | } |
| 226 | |
| 227 | void GCMClientImplTest::BuildGCMClient() { |
| 228 | gcm_client_.reset(new GCMClientImpl()); |
| 229 | } |
| 230 | |
| 231 | void GCMClientImplTest::CompleteCheckin(uint64 android_id, |
| 232 | uint64 security_token) { |
| 233 | checkin_proto::AndroidCheckinResponse response; |
| 234 | response.set_stats_ok(true); |
| 235 | response.set_android_id(android_id); |
| 236 | response.set_security_token(security_token); |
| 237 | |
| 238 | std::string response_string; |
| 239 | response.SerializeToString(&response_string); |
| 240 | |
| 241 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 242 | ASSERT_TRUE(fetcher); |
| 243 | fetcher->set_response_code(net::HTTP_OK); |
| 244 | fetcher->SetResponseString(response_string); |
| 245 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 246 | url_fetcher_factory_.RemoveFetcherFromMap(0); |
| 247 | } |
| 248 | |
| 249 | void GCMClientImplTest::CompleteRegistration( |
| 250 | const std::string& registration_id) { |
| 251 | std::string response(kRegistrationResponsePrefix); |
| 252 | response.append(registration_id); |
| 253 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 254 | ASSERT_TRUE(fetcher); |
| 255 | fetcher->set_response_code(net::HTTP_OK); |
| 256 | fetcher->SetResponseString(response); |
| 257 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void GCMClientImplTest::CompleteUnregistration( |
| 261 | const std::string& app_id) { |
| 262 | std::string response(kUnregistrationResponsePrefix); |
| 263 | response.append(app_id); |
| 264 | net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 265 | ASSERT_TRUE(fetcher); |
| 266 | fetcher->set_response_code(net::HTTP_OK); |
| 267 | fetcher->SetResponseString(response); |
| 268 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void GCMClientImplTest::InitializeGCMClient() { |
| 272 | // Creating and advancing the clock. |
| 273 | gcm_client_->clock_.reset(new base::SimpleTestClock); |
| 274 | clock()->Advance(base::TimeDelta::FromMilliseconds(1)); |
| 275 | // Creating and injecting the mcs_client. |
| 276 | connection_factory_.reset(new FakeConnectionFactory()); |
| 277 | gcm_client_->SetMCSClientForTesting(scoped_ptr<MCSClient>( |
| 278 | new FakeMCSClient(clock(), connection_factory_.get())).Pass()); |
| 279 | // Actual initialization. |
| 280 | checkin_proto::ChromeBuildProto chrome_build_proto; |
| 281 | gcm_client_->Initialize(chrome_build_proto, |
| 282 | temp_directory_.path(), |
| 283 | message_loop_.message_loop_proxy(), |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 284 | url_request_context_getter_, |
| 285 | this); |
[email protected] | 0693d516 | 2014-02-12 00:20:17 | [diff] [blame] | 286 | #if defined(OS_MACOSX) |
| 287 | // On OSX, prevent the Keychain permissions popup during unit tests. |
| 288 | Encryptor::UseMockKeychain(true); // Must be after Initialize. |
| 289 | #endif |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 290 | // Ensuring that mcs_client is using the same gcm_store as gcm_client. |
| 291 | mcs_client()->set_gcm_store(gcm_client_->gcm_store_.get()); |
| 292 | PumpLoopUntilIdle(); |
| 293 | CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void GCMClientImplTest::ReceiveMessageFromMCS(const MCSMessage& message) { |
| 297 | gcm_client_->OnMessageReceivedFromMCS(message); |
| 298 | } |
| 299 | |
[email protected] | 86625df | 2014-01-31 03:47:58 | [diff] [blame] | 300 | void GCMClientImplTest::OnGCMReady() { |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 301 | last_event_ = LOADING_COMPLETED; |
| 302 | QuitLoop(); |
| 303 | } |
| 304 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 305 | void GCMClientImplTest::OnMessageReceived( |
| 306 | const std::string& registration_id, |
| 307 | const GCMClient::IncomingMessage& message) { |
| 308 | last_event_ = MESSAGE_RECEIVED; |
| 309 | last_app_id_ = registration_id; |
| 310 | last_message_ = message; |
| 311 | QuitLoop(); |
| 312 | } |
| 313 | |
| 314 | void GCMClientImplTest::OnRegisterFinished(const std::string& app_id, |
| 315 | const std::string& registration_id, |
| 316 | GCMClient::Result result) { |
| 317 | last_event_ = REGISTRATION_COMPLETED; |
| 318 | last_app_id_ = app_id; |
| 319 | last_registration_id_ = registration_id; |
| 320 | last_result_ = result; |
| 321 | } |
| 322 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 323 | void GCMClientImplTest::OnUnregisterFinished(const std::string& app_id, |
| 324 | bool success) { |
| 325 | last_event_ = UNREGISTRATION_COMPLETED; |
| 326 | last_app_id_ = app_id; |
| 327 | last_result_ = success ? GCMClient::SUCCESS : GCMClient::UNKNOWN_ERROR; |
| 328 | } |
| 329 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 330 | void GCMClientImplTest::OnMessagesDeleted(const std::string& app_id) { |
| 331 | last_event_ = MESSAGES_DELETED; |
| 332 | last_app_id_ = app_id; |
| 333 | } |
| 334 | |
| 335 | void GCMClientImplTest::OnMessageSendError(const std::string& app_id, |
| 336 | const std::string& message_id, |
| 337 | GCMClient::Result result) { |
| 338 | last_event_ = MESSAGE_SEND_ERROR; |
| 339 | last_app_id_ = app_id; |
| 340 | last_message_id_ = message_id; |
| 341 | last_result_ = result; |
| 342 | } |
| 343 | |
| 344 | int64 GCMClientImplTest::CurrentTime() { |
| 345 | return clock()->Now().ToInternalValue() / base::Time::kMicrosecondsPerSecond; |
| 346 | } |
| 347 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 348 | TEST_F(GCMClientImplTest, LoadingCompleted) { |
| 349 | EXPECT_EQ(LOADING_COMPLETED, last_event()); |
| 350 | EXPECT_EQ(kDeviceAndroidId, mcs_client()->last_android_id()); |
| 351 | EXPECT_EQ(kDeviceSecurityToken, mcs_client()->last_security_token()); |
| 352 | } |
| 353 | |
[email protected] | 8e3ee1da | 2014-02-13 05:21:03 | [diff] [blame] | 354 | TEST_F(GCMClientImplTest, CheckOut) { |
| 355 | EXPECT_TRUE(mcs_client()); |
| 356 | gcm_client()->CheckOut(); |
| 357 | EXPECT_FALSE(mcs_client()); |
| 358 | } |
| 359 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 360 | TEST_F(GCMClientImplTest, RegisterApp) { |
| 361 | std::vector<std::string> senders; |
| 362 | senders.push_back("sender"); |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 363 | gcm_client()->Register("app_id", "cert", senders); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 364 | CompleteRegistration("reg_id"); |
| 365 | |
| 366 | EXPECT_EQ(REGISTRATION_COMPLETED, last_event()); |
| 367 | EXPECT_EQ("app_id", last_app_id()); |
| 368 | EXPECT_EQ("reg_id", last_registration_id()); |
| 369 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 370 | } |
| 371 | |
[email protected] | e400704 | 2014-02-15 20:34:28 | [diff] [blame] | 372 | TEST_F(GCMClientImplTest, UnregisterApp) { |
| 373 | gcm_client()->Unregister("app_id"); |
| 374 | CompleteUnregistration("app_id"); |
| 375 | |
| 376 | EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); |
| 377 | EXPECT_EQ("app_id", last_app_id()); |
| 378 | EXPECT_EQ(GCMClient::SUCCESS, last_result()); |
| 379 | } |
| 380 | |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 381 | TEST_F(GCMClientImplTest, DispatchDownstreamMessage) { |
| 382 | std::map<std::string, std::string> expected_data; |
| 383 | expected_data["message_type"] = "gcm"; |
| 384 | expected_data["key"] = "value"; |
| 385 | expected_data["key2"] = "value2"; |
| 386 | MCSMessage message(BuildDownstreamMessage( |
| 387 | "project_id", "app_id", expected_data)); |
| 388 | EXPECT_TRUE(message.IsValid()); |
| 389 | ReceiveMessageFromMCS(message); |
| 390 | |
| 391 | expected_data.erase(expected_data.find("message_type")); |
| 392 | EXPECT_EQ(MESSAGE_RECEIVED, last_event()); |
| 393 | EXPECT_EQ("app_id", last_app_id()); |
| 394 | EXPECT_EQ(expected_data.size(), last_message().data.size()); |
| 395 | EXPECT_EQ(expected_data, last_message().data); |
| 396 | } |
| 397 | |
| 398 | TEST_F(GCMClientImplTest, DispatchDownstreamMessageSendError) { |
| 399 | std::map<std::string, std::string> expected_data; |
| 400 | expected_data["message_type"] = "send_error"; |
| 401 | expected_data["google.message_id"] = "007"; |
| 402 | MCSMessage message(BuildDownstreamMessage( |
| 403 | "project_id", "app_id", expected_data)); |
| 404 | EXPECT_TRUE(message.IsValid()); |
| 405 | ReceiveMessageFromMCS(message); |
| 406 | |
| 407 | EXPECT_EQ(MESSAGE_SEND_ERROR, last_event()); |
| 408 | EXPECT_EQ("app_id", last_app_id()); |
| 409 | EXPECT_EQ("007", last_message_id()); |
| 410 | } |
| 411 | |
| 412 | TEST_F(GCMClientImplTest, DispatchDownstreamMessgaesDeleted) { |
| 413 | std::map<std::string, std::string> expected_data; |
| 414 | expected_data["message_type"] = "deleted_messages"; |
| 415 | MCSMessage message(BuildDownstreamMessage( |
| 416 | "project_id", "app_id", expected_data)); |
| 417 | EXPECT_TRUE(message.IsValid()); |
| 418 | ReceiveMessageFromMCS(message); |
| 419 | |
| 420 | EXPECT_EQ(MESSAGES_DELETED, last_event()); |
| 421 | EXPECT_EQ("app_id", last_app_id()); |
| 422 | } |
| 423 | |
| 424 | TEST_F(GCMClientImplTest, SendMessage) { |
| 425 | mcs_proto::DataMessageStanza stanza; |
| 426 | stanza.set_ttl(500); |
| 427 | |
| 428 | GCMClient::OutgoingMessage message; |
| 429 | message.id = "007"; |
| 430 | message.time_to_live = 500; |
| 431 | message.data["key"] = "value"; |
[email protected] | 5799d05 | 2014-02-12 20:47:39 | [diff] [blame] | 432 | gcm_client()->Send("app_id", "project_id", message); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 433 | |
| 434 | EXPECT_EQ(kDataMessageStanzaTag, mcs_client()->last_message_tag()); |
| 435 | EXPECT_EQ("app_id", mcs_client()->last_data_message_stanza().category()); |
| 436 | EXPECT_EQ("project_id", mcs_client()->last_data_message_stanza().to()); |
| 437 | EXPECT_EQ(500, mcs_client()->last_data_message_stanza().ttl()); |
| 438 | EXPECT_EQ(CurrentTime(), mcs_client()->last_data_message_stanza().sent()); |
| 439 | EXPECT_EQ("007", mcs_client()->last_data_message_stanza().id()); |
[email protected] | 848b1b6 | 2014-01-30 23:51:04 | [diff] [blame] | 440 | EXPECT_EQ("[email protected]", mcs_client()->last_data_message_stanza().from()); |
| 441 | EXPECT_EQ("project_id", mcs_client()->last_data_message_stanza().to()); |
| 442 | EXPECT_EQ("key", mcs_client()->last_data_message_stanza().app_data(0).key()); |
| 443 | EXPECT_EQ("value", |
| 444 | mcs_client()->last_data_message_stanza().app_data(0).value()); |
| 445 | } |
| 446 | |
| 447 | } // namespace gcm |