Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: components/gcm_driver/gcm_client_impl_unittest.cc

Issue 2578583002: Provide a mechanism for the GCM driver to send message receipts to GCM.
Patch Set: Fix issue with rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/gcm_driver/gcm_client_impl.cc ('k') | components/gcm_driver/gcm_driver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/gcm_driver/gcm_client_impl.h" 5 #include "components/gcm_driver/gcm_client_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <initializer_list> 9 #include <initializer_list>
10 #include <memory> 10 #include <memory>
11 11
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h" 15 #include "base/files/scoped_temp_dir.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
18 #include "base/metrics/field_trial.h"
19 #include "base/metrics/field_trial_param_associator.h"
20 #include "base/metrics/field_trial_params.h"
18 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
19 #include "base/test/histogram_tester.h" 22 #include "base/test/histogram_tester.h"
23 #include "base/test/mock_entropy_provider.h"
24 #include "base/test/scoped_feature_list.h"
20 #include "base/test/test_mock_time_task_runner.h" 25 #include "base/test/test_mock_time_task_runner.h"
21 #include "base/threading/thread_task_runner_handle.h" 26 #include "base/threading/thread_task_runner_handle.h"
22 #include "base/time/clock.h" 27 #include "base/time/clock.h"
23 #include "base/timer/timer.h" 28 #include "base/timer/timer.h"
24 #include "google_apis/gcm/base/fake_encryptor.h" 29 #include "google_apis/gcm/base/fake_encryptor.h"
25 #include "google_apis/gcm/base/mcs_message.h" 30 #include "google_apis/gcm/base/mcs_message.h"
26 #include "google_apis/gcm/base/mcs_util.h" 31 #include "google_apis/gcm/base/mcs_util.h"
27 #include "google_apis/gcm/engine/fake_connection_factory.h" 32 #include "google_apis/gcm/engine/fake_connection_factory.h"
28 #include "google_apis/gcm/engine/fake_connection_handler.h" 33 #include "google_apis/gcm/engine/fake_connection_handler.h"
29 #include "google_apis/gcm/engine/gservices_settings.h" 34 #include "google_apis/gcm/engine/gservices_settings.h"
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 266
262 class GCMClientImplTest : public testing::Test, 267 class GCMClientImplTest : public testing::Test,
263 public GCMClient::Delegate { 268 public GCMClient::Delegate {
264 public: 269 public:
265 GCMClientImplTest(); 270 GCMClientImplTest();
266 ~GCMClientImplTest() override; 271 ~GCMClientImplTest() override;
267 272
268 void SetUp() override; 273 void SetUp() override;
269 274
270 void SetUpUrlFetcherFactory(); 275 void SetUpUrlFetcherFactory();
276 void InitializeFieldTrials(int messages_to_send);
271 277
272 void BuildGCMClient(base::TimeDelta clock_step); 278 void BuildGCMClient(base::TimeDelta clock_step);
273 void InitializeGCMClient(); 279 void InitializeGCMClient();
274 void StartGCMClient(); 280 void StartGCMClient();
275 void Register(const std::string& app_id, 281 void Register(const std::string& app_id,
276 const std::vector<std::string>& senders); 282 const std::vector<std::string>& senders);
277 void Unregister(const std::string& app_id); 283 void Unregister(const std::string& app_id);
278 void ReceiveMessageFromMCS(const MCSMessage& message); 284 void ReceiveMessageFromMCS(const MCSMessage& message);
279 void ReceiveOnMessageSentToMCS( 285 void ReceiveOnMessageSentToMCS(
280 const std::string& app_id, 286 const std::string& app_id,
(...skipping 21 matching lines...) Expand all
302 // GCMClient::Delegate overrides (for verification). 308 // GCMClient::Delegate overrides (for verification).
303 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info, 309 void OnRegisterFinished(const linked_ptr<RegistrationInfo>& registration_info,
304 const std::string& registration_id, 310 const std::string& registration_id,
305 GCMClient::Result result) override; 311 GCMClient::Result result) override;
306 void OnUnregisterFinished( 312 void OnUnregisterFinished(
307 const linked_ptr<RegistrationInfo>& registration_info, 313 const linked_ptr<RegistrationInfo>& registration_info,
308 GCMClient::Result result) override; 314 GCMClient::Result result) override;
309 void OnSendFinished(const std::string& app_id, 315 void OnSendFinished(const std::string& app_id,
310 const std::string& message_id, 316 const std::string& message_id,
311 GCMClient::Result result) override {} 317 GCMClient::Result result) override {}
312 void OnMessageReceived(const std::string& registration_id, 318 void OnMessageReceived(
313 const IncomingMessage& message) override; 319 const std::string& registration_id,
320 const IncomingMessage& message,
321 const MessageReceiptCallback& optional_receipt_callback) override;
314 void OnMessagesDeleted(const std::string& app_id) override; 322 void OnMessagesDeleted(const std::string& app_id) override;
315 void OnMessageSendError( 323 void OnMessageSendError(
316 const std::string& app_id, 324 const std::string& app_id,
317 const gcm::GCMClient::SendErrorDetails& send_error_details) override; 325 const gcm::GCMClient::SendErrorDetails& send_error_details) override;
318 void OnSendAcknowledged(const std::string& app_id, 326 void OnSendAcknowledged(const std::string& app_id,
319 const std::string& message_id) override; 327 const std::string& message_id) override;
320 void OnGCMReady(const std::vector<AccountMapping>& account_mappings, 328 void OnGCMReady(const std::vector<AccountMapping>& account_mappings,
321 const base::Time& last_token_fetch_time) override; 329 const base::Time& last_token_fetch_time) override;
322 void OnActivityRecorded() override {} 330 void OnActivityRecorded() override {}
323 void OnConnected(const net::IPEndPoint& ip_endpoint) override {} 331 void OnConnected(const net::IPEndPoint& ip_endpoint) override {}
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 AutoAdvancingTestClock* clock() const { 398 AutoAdvancingTestClock* clock() const {
391 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get()); 399 return reinterpret_cast<AutoAdvancingTestClock*>(gcm_client_->clock_.get());
392 } 400 }
393 net::TestURLFetcherFactory* url_fetcher_factory() { 401 net::TestURLFetcherFactory* url_fetcher_factory() {
394 return &url_fetcher_factory_; 402 return &url_fetcher_factory_;
395 } 403 }
396 base::TestMockTimeTaskRunner* task_runner() { 404 base::TestMockTimeTaskRunner* task_runner() {
397 return task_runner_.get(); 405 return task_runner_.get();
398 } 406 }
399 407
408 void set_successful_receipt_generation(bool successful_receipt_generation) {
409 successful_receipt_generation_ = successful_receipt_generation;
410 }
411
400 private: 412 private:
401 // Must be declared first so that it is destroyed last. Injected to 413 // Must be declared first so that it is destroyed last. Injected to
402 // GCM client. 414 // GCM client.
403 base::ScopedTempDir temp_directory_; 415 base::ScopedTempDir temp_directory_;
404 416
405 // Variables used for verification. 417 // Variables used for verification.
406 LastEvent last_event_; 418 LastEvent last_event_;
407 std::string last_app_id_; 419 std::string last_app_id_;
408 std::string last_registration_id_; 420 std::string last_registration_id_;
409 std::string last_message_id_; 421 std::string last_message_id_;
410 GCMClient::Result last_result_; 422 GCMClient::Result last_result_;
411 IncomingMessage last_message_; 423 IncomingMessage last_message_;
412 GCMClient::SendErrorDetails last_error_details_; 424 GCMClient::SendErrorDetails last_error_details_;
413 base::Time last_token_fetch_time_; 425 base::Time last_token_fetch_time_;
414 std::vector<AccountMapping> last_account_mappings_; 426 std::vector<AccountMapping> last_account_mappings_;
427 base::test::ScopedFeatureList scoped_feature_list_;
428 std::unique_ptr<base::FieldTrialList> field_trial_list_;
429 bool successful_receipt_generation_ = false;
415 430
416 std::unique_ptr<GCMClientImpl> gcm_client_; 431 std::unique_ptr<GCMClientImpl> gcm_client_;
417 432
418 net::TestURLFetcherFactory url_fetcher_factory_; 433 net::TestURLFetcherFactory url_fetcher_factory_;
419 434
420 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; 435 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
421 base::ThreadTaskRunnerHandle task_runner_handle_; 436 base::ThreadTaskRunnerHandle task_runner_handle_;
422 437
423 // Injected to GCM client. 438 // Injected to GCM client.
424 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; 439 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
425 }; 440 };
426 441
427 GCMClientImplTest::GCMClientImplTest() 442 GCMClientImplTest::GCMClientImplTest()
428 : last_event_(NONE), 443 : last_event_(NONE),
429 last_result_(GCMClient::UNKNOWN_ERROR), 444 last_result_(GCMClient::UNKNOWN_ERROR),
445 field_trial_list_(base::MakeUnique<base::FieldTrialList>(
446 base::MakeUnique<base::MockEntropyProvider>())),
430 task_runner_(new base::TestMockTimeTaskRunner), 447 task_runner_(new base::TestMockTimeTaskRunner),
431 task_runner_handle_(task_runner_), 448 task_runner_handle_(task_runner_),
432 url_request_context_getter_( 449 url_request_context_getter_(
433 new net::TestURLRequestContextGetter(task_runner_)) { 450 new net::TestURLRequestContextGetter(task_runner_)) {}
434 }
435 451
436 GCMClientImplTest::~GCMClientImplTest() {} 452 GCMClientImplTest::~GCMClientImplTest() {}
437 453
438 void GCMClientImplTest::SetUp() { 454 void GCMClientImplTest::SetUp() {
439 testing::Test::SetUp(); 455 testing::Test::SetUp();
440 ASSERT_TRUE(CreateUniqueTempDir()); 456 ASSERT_TRUE(CreateUniqueTempDir());
441 BuildGCMClient(base::TimeDelta()); 457 BuildGCMClient(base::TimeDelta());
442 InitializeGCMClient(); 458 InitializeGCMClient();
443 StartGCMClient(); 459 StartGCMClient();
444 SetUpUrlFetcherFactory(); 460 SetUpUrlFetcherFactory();
445 ASSERT_NO_FATAL_FAILURE( 461 ASSERT_NO_FATAL_FAILURE(
446 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(), 462 CompleteCheckin(kDeviceAndroidId, kDeviceSecurityToken, std::string(),
447 std::map<std::string, std::string>())); 463 std::map<std::string, std::string>()));
448 } 464 }
449 465
450 void GCMClientImplTest::SetUpUrlFetcherFactory() { 466 void GCMClientImplTest::SetUpUrlFetcherFactory() {
451 url_fetcher_factory_.set_remove_fetcher_on_delete(true); 467 url_fetcher_factory_.set_remove_fetcher_on_delete(true);
452 } 468 }
453 469
470 void GCMClientImplTest::InitializeFieldTrials(int messages_to_send) {
471 const std::string kTrialName = "TestGCMMessageReceipts";
472 const std::string kGroupName = "UnusedName";
473 // kFeatureName and kParam name are also defined in gcm_client_impl.cc.
474 const std::string kFeatureName = "GCMMessageReceiptsFeature";
475 const std::string kParamName = "message_receipts_to_send";
476
477 // Cleanup any old field trial state and reinitialize.
478 field_trial_list_.reset();
479 field_trial_list_ = base::MakeUnique<base::FieldTrialList>(
480 base::MakeUnique<base::MockEntropyProvider>());
481 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
482
483 // Create a field trial with message receipts set to messages_to_send.
484 scoped_refptr<base::FieldTrial> trial =
485 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName);
486 std::map<std::string, std::string> trial_params;
487 trial_params[kParamName] = base::IntToString(messages_to_send);
488 base::FieldTrialParamAssociator::GetInstance()->AssociateFieldTrialParams(
489 kTrialName, kGroupName, trial_params);
490
491 // Create a FeatureList and add the new trial to the list.
492 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
493 feature_list->RegisterFieldTrialOverride(
494 kFeatureName, base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get());
495 scoped_feature_list_.InitWithFeatureList(std::move(feature_list));
496 }
497
454 void GCMClientImplTest::PumpLoopUntilIdle() { 498 void GCMClientImplTest::PumpLoopUntilIdle() {
455 task_runner_->RunUntilIdle(); 499 task_runner_->RunUntilIdle();
456 } 500 }
457 501
458 bool GCMClientImplTest::CreateUniqueTempDir() { 502 bool GCMClientImplTest::CreateUniqueTempDir() {
459 return temp_directory_.CreateUniqueTempDir(); 503 return temp_directory_.CreateUniqueTempDir();
460 } 504 }
461 505
462 void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) { 506 void GCMClientImplTest::BuildGCMClient(base::TimeDelta clock_step) {
463 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>( 507 gcm_client_.reset(new GCMClientImpl(base::WrapUnique<GCMInternalsBuilder>(
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 } 653 }
610 654
611 void GCMClientImplTest::OnGCMReady( 655 void GCMClientImplTest::OnGCMReady(
612 const std::vector<AccountMapping>& account_mappings, 656 const std::vector<AccountMapping>& account_mappings,
613 const base::Time& last_token_fetch_time) { 657 const base::Time& last_token_fetch_time) {
614 last_event_ = LOADING_COMPLETED; 658 last_event_ = LOADING_COMPLETED;
615 last_account_mappings_ = account_mappings; 659 last_account_mappings_ = account_mappings;
616 last_token_fetch_time_ = last_token_fetch_time; 660 last_token_fetch_time_ = last_token_fetch_time;
617 } 661 }
618 662
619 void GCMClientImplTest::OnMessageReceived(const std::string& registration_id, 663 void GCMClientImplTest::OnMessageReceived(
620 const IncomingMessage& message) { 664 const std::string& registration_id,
665 const IncomingMessage& message,
666 const MessageReceiptCallback& optional_receipt_callback) {
621 last_event_ = MESSAGE_RECEIVED; 667 last_event_ = MESSAGE_RECEIVED;
622 last_app_id_ = registration_id; 668 last_app_id_ = registration_id;
623 last_message_ = message; 669 last_message_ = message;
670
671 // If receipt sending is enabled, then send success.
672 if (successful_receipt_generation_)
673 optional_receipt_callback.Run(GCMMessageStatus::GCM_SUCCESS);
624 } 674 }
625 675
626 void GCMClientImplTest::OnRegisterFinished( 676 void GCMClientImplTest::OnRegisterFinished(
627 const linked_ptr<RegistrationInfo>& registration_info, 677 const linked_ptr<RegistrationInfo>& registration_info,
628 const std::string& registration_id, 678 const std::string& registration_id,
629 GCMClient::Result result) { 679 GCMClient::Result result) {
630 last_event_ = REGISTRATION_COMPLETED; 680 last_event_ = REGISTRATION_COMPLETED;
631 last_app_id_ = registration_info->app_id; 681 last_app_id_ = registration_info->app_id;
632 last_registration_id_ = registration_id; 682 last_registration_id_ = registration_id;
633 last_result_ = result; 683 last_result_ = result;
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 979
930 reset_last_event(); 980 reset_last_event();
931 981
932 // Message from kSender3 will be dropped. 982 // Message from kSender3 will be dropped.
933 MCSMessage message3(BuildDownstreamMessage( 983 MCSMessage message3(BuildDownstreamMessage(
934 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data, 984 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data,
935 std::string() /* raw_data */)); 985 std::string() /* raw_data */));
936 EXPECT_TRUE(message3.IsValid()); 986 EXPECT_TRUE(message3.IsValid());
937 ReceiveMessageFromMCS(message3); 987 ReceiveMessageFromMCS(message3);
938 988
989 // TODO(harkness): Add a check for invalid app handler once the
990 // DefaultAppHandler is removed.
991
939 EXPECT_NE(MESSAGE_RECEIVED, last_event()); 992 EXPECT_NE(MESSAGE_RECEIVED, last_event());
940 EXPECT_NE(kExtensionAppId, last_app_id()); 993 EXPECT_NE(kExtensionAppId, last_app_id());
941 } 994 }
942 995
943 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) { 996 TEST_F(GCMClientImplTest, DispatchDownstreamMessageRawData) {
944 std::vector<std::string> senders(1, kSender); 997 std::vector<std::string> senders(1, kSender);
945 AddRegistration(kExtensionAppId, senders, "reg_id"); 998 AddRegistration(kExtensionAppId, senders, "reg_id");
946 999
947 std::map<std::string, std::string> expected_data; 1000 std::map<std::string, std::string> expected_data;
948 1001
(...skipping 816 matching lines...) Expand 10 before | Expand all | Expand 10 after
1765 // No need to call CompleteDeleteToken since unregistration request should 1818 // No need to call CompleteDeleteToken since unregistration request should
1766 // not be triggered. 1819 // not be triggered.
1767 PumpLoopUntilIdle(); 1820 PumpLoopUntilIdle();
1768 1821
1769 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event()); 1822 EXPECT_EQ(UNREGISTRATION_COMPLETED, last_event());
1770 EXPECT_EQ(kExtensionAppId, last_app_id()); 1823 EXPECT_EQ(kExtensionAppId, last_app_id());
1771 EXPECT_EQ(GCMClient::SUCCESS, last_result()); 1824 EXPECT_EQ(GCMClient::SUCCESS, last_result());
1772 } 1825 }
1773 1826
1774 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) { 1827 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithoutSubtype) {
1828 // Initialize a field trial for sending message receipts and set the messages
1829 // to send to 2 = ALL.
1830 InitializeFieldTrials(2);
1831
1775 AddInstanceID(kExtensionAppId, kInstanceID); 1832 AddInstanceID(kExtensionAppId, kInstanceID);
1776 GetToken(kExtensionAppId, kSender, kScope); 1833 GetToken(kExtensionAppId, kSender, kScope);
1777 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); 1834 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1778 GetToken(kExtensionAppId, kSender2, kScope); 1835 GetToken(kExtensionAppId, kSender2, kScope);
1779 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); 1836 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
1780 1837
1781 std::map<std::string, std::string> expected_data; 1838 std::map<std::string, std::string> expected_data;
1782 1839
1783 // Message for kSender with a subtype will be dropped. 1840 // Message for kSender with a subtype will be dropped.
1784 MCSMessage message0(BuildDownstreamMessage( 1841 MCSMessage message0(BuildDownstreamMessage(
1785 kSender, kProductCategoryForSubtypes, kExtensionAppId /* subtype */, 1842 kSender, kProductCategoryForSubtypes, kExtensionAppId /* subtype */,
1786 expected_data, std::string() /* raw_data */)); 1843 expected_data, std::string() /* raw_data */));
1787 EXPECT_TRUE(message0.IsValid()); 1844 EXPECT_TRUE(message0.IsValid());
1788 ReceiveMessageFromMCS(message0); 1845 ReceiveMessageFromMCS(message0);
1789 1846
1790 EXPECT_NE(MESSAGE_RECEIVED, last_event()); 1847 EXPECT_NE(MESSAGE_RECEIVED, last_event());
1791 1848
1849 // Check that the last message sent was a message receipt for a failed message
1850 // and that it had the values expected.
1851 mcs_proto::DataMessageStanza stanza =
1852 mcs_client()->last_data_message_stanza();
1853 EXPECT_EQ(kExtensionAppId, stanza.category());
1854 const auto& app_data = stanza.app_data();
1855 ASSERT_EQ(3, app_data.size());
1856 for (const auto& pair : app_data) {
1857 if (pair.key() == "message_id")
1858 EXPECT_EQ("", pair.value());
1859 else if (pair.key() == "status")
1860 EXPECT_EQ("2", pair.value());
1861 else if (pair.key() == "message_type")
1862 EXPECT_EQ("message_receipt", pair.value());
1863 else
1864 FAIL() << "Unexpected key: " << pair.key();
1865 }
1866
1792 reset_last_event(); 1867 reset_last_event();
1793 1868
1794 // Message for kSender will be received. 1869 // Message for kSender will be received.
1795 MCSMessage message1(BuildDownstreamMessage( 1870 MCSMessage message1(BuildDownstreamMessage(
1796 kSender, kExtensionAppId, std::string() /* subtype */, expected_data, 1871 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1797 std::string() /* raw_data */)); 1872 std::string() /* raw_data */));
1798 EXPECT_TRUE(message1.IsValid()); 1873 EXPECT_TRUE(message1.IsValid());
1799 ReceiveMessageFromMCS(message1); 1874 ReceiveMessageFromMCS(message1);
1800 1875
1801 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); 1876 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
(...skipping 23 matching lines...) Expand all
1825 MCSMessage message3(BuildDownstreamMessage( 1900 MCSMessage message3(BuildDownstreamMessage(
1826 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data, 1901 kSender3, kExtensionAppId, std::string() /* subtype */, expected_data,
1827 std::string() /* raw_data */)); 1902 std::string() /* raw_data */));
1828 EXPECT_TRUE(message3.IsValid()); 1903 EXPECT_TRUE(message3.IsValid());
1829 ReceiveMessageFromMCS(message3); 1904 ReceiveMessageFromMCS(message3);
1830 1905
1831 EXPECT_NE(MESSAGE_RECEIVED, last_event()); 1906 EXPECT_NE(MESSAGE_RECEIVED, last_event());
1832 EXPECT_NE(kExtensionAppId, last_app_id()); 1907 EXPECT_NE(kExtensionAppId, last_app_id());
1833 } 1908 }
1834 1909
1910 // Do not initialize a FieldTrial for sending receipts. Default state of the
1911 // field trial should be SEND_NONE, so check that no receipt was sent.
1912 TEST_F(GCMClientInstanceIDTest, ReceiptFieldTrialTestingSendNone) {
1913 AddInstanceID(kExtensionAppId, kInstanceID);
1914 GetToken(kExtensionAppId, kSender, kScope);
1915 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1916
1917 std::map<std::string, std::string> expected_data;
1918
1919 // Message for kSender with a subtype will be dropped and would cause an error
1920 // receipt to be generated.
1921 MCSMessage message0(BuildDownstreamMessage(
1922 kSender, kProductCategoryForSubtypes, kExtensionAppId /* subtype */,
1923 expected_data, std::string() /* raw_data */));
1924 EXPECT_TRUE(message0.IsValid());
1925 ReceiveMessageFromMCS(message0);
1926
1927 EXPECT_NE(MESSAGE_RECEIVED, last_event());
1928
1929 // The only way to check that no messages were sent is to check that the tag
1930 // is still the default value.
1931 EXPECT_EQ(MCSProtoTag::kNumProtoTypes, mcs_client()->last_message_tag());
1932 }
1933
1934 TEST_F(GCMClientInstanceIDTest, ReceiptFieldTrialTestingSendAll) {
1935 // Initialize a field trial for sending message receipts and set the messages
1936 // to send to 2 = ALL.
1937 InitializeFieldTrials(2);
1938 set_successful_receipt_generation(true);
1939
1940 AddInstanceID(kExtensionAppId, kInstanceID);
1941 GetToken(kExtensionAppId, kSender, kScope);
1942 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1943
1944 std::map<std::string, std::string> expected_data;
1945
1946 // Message for kSender will be received.
1947 MCSMessage message1(BuildDownstreamMessage(
1948 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1949 std::string() /* raw_data */));
1950 EXPECT_TRUE(message1.IsValid());
1951 ReceiveMessageFromMCS(message1);
1952
1953 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1954 EXPECT_EQ(kExtensionAppId, last_app_id());
1955 EXPECT_EQ(expected_data.size(), last_message().data.size());
1956 EXPECT_EQ(expected_data, last_message().data);
1957 EXPECT_EQ(kSender, last_message().sender_id);
1958
1959 // Check that the last message sent was a message receipt for a successful
1960 // message and that it had the values expected.
1961 mcs_proto::DataMessageStanza stanza =
1962 mcs_client()->last_data_message_stanza();
1963 EXPECT_EQ(kExtensionAppId, stanza.category());
1964 const auto& app_data = stanza.app_data();
1965 ASSERT_EQ(3, app_data.size());
1966 for (const auto& pair : app_data) {
1967 if (pair.key() == "message_id")
1968 EXPECT_EQ("", pair.value());
1969 else if (pair.key() == "status")
1970 EXPECT_EQ("1", pair.value()); // Status should be GCM_SUCCESS = 1.
1971 else if (pair.key() == "message_type")
1972 EXPECT_EQ("message_receipt", pair.value());
1973 else
1974 FAIL() << "Unexpected key: " << pair.key();
1975 }
1976 }
1977
1978 TEST_F(GCMClientInstanceIDTest, ReceiptFieldTrialTestingSendFailures) {
1979 // Initialize a field trial for sending message receipts and set the messages
1980 // to send to 1 = SEND_FAILURES. Then when a successful message generates a
1981 // receipt, it should not be sent.
1982 InitializeFieldTrials(1);
1983 set_successful_receipt_generation(true);
1984
1985 AddInstanceID(kExtensionAppId, kInstanceID);
1986 GetToken(kExtensionAppId, kSender, kScope);
1987 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1988
1989 std::map<std::string, std::string> expected_data;
1990
1991 // Message for kSender will be received.
1992 MCSMessage message1(BuildDownstreamMessage(
1993 kSender, kExtensionAppId, std::string() /* subtype */, expected_data,
1994 std::string() /* raw_data */));
1995 EXPECT_TRUE(message1.IsValid());
1996 ReceiveMessageFromMCS(message1);
1997
1998 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1999 EXPECT_EQ(kExtensionAppId, last_app_id());
2000 EXPECT_EQ(expected_data.size(), last_message().data.size());
2001 EXPECT_EQ(expected_data, last_message().data);
2002 EXPECT_EQ(kSender, last_message().sender_id);
2003
2004 // The only way to check that no messages were sent is to check that the tag
2005 // is still the default value.
2006 EXPECT_EQ(MCSProtoTag::kNumProtoTypes, mcs_client()->last_message_tag());
2007 }
2008
1835 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) { 2009 TEST_F(GCMClientInstanceIDTest, DispatchDownstreamMessageWithSubtype) {
2010 // Initialize a field trial for sending message receipts and set the messages
2011 // to send to 2 = ALL.
2012 InitializeFieldTrials(2);
2013
1836 AddInstanceID(kSubtypeAppId, kInstanceID); 2014 AddInstanceID(kSubtypeAppId, kInstanceID);
1837 GetToken(kSubtypeAppId, kSender, kScope); 2015 GetToken(kSubtypeAppId, kSender, kScope);
1838 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1")); 2016 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token1"));
1839 GetToken(kSubtypeAppId, kSender2, kScope); 2017 GetToken(kSubtypeAppId, kSender2, kScope);
1840 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2")); 2018 ASSERT_NO_FATAL_FAILURE(CompleteRegistration("token2"));
1841 2019
1842 std::map<std::string, std::string> expected_data; 2020 std::map<std::string, std::string> expected_data;
1843 2021
1844 // Message for kSender without a subtype will be dropped. 2022 // Message for kSender without a subtype will be dropped.
1845 MCSMessage message0(BuildDownstreamMessage( 2023 MCSMessage message0(BuildDownstreamMessage(
1846 kSender, kSubtypeAppId, std::string() /* subtype */, expected_data, 2024 kSender, kSubtypeAppId, std::string() /* subtype */, expected_data,
1847 std::string() /* raw_data */)); 2025 std::string() /* raw_data */));
1848 EXPECT_TRUE(message0.IsValid()); 2026 EXPECT_TRUE(message0.IsValid());
1849 ReceiveMessageFromMCS(message0); 2027 ReceiveMessageFromMCS(message0);
1850 2028
1851 EXPECT_NE(MESSAGE_RECEIVED, last_event()); 2029 EXPECT_NE(MESSAGE_RECEIVED, last_event());
1852 2030
2031 // Check that the last message sent was a message receipt for a failed message
2032 // and that it had the values expected.
2033 mcs_proto::DataMessageStanza stanza =
2034 mcs_client()->last_data_message_stanza();
2035 EXPECT_EQ(kSubtypeAppId, stanza.category());
2036 const auto& app_data = stanza.app_data();
2037 ASSERT_EQ(3, app_data.size());
2038 for (const auto& pair : app_data) {
2039 if (pair.key() == "message_id")
2040 EXPECT_EQ("", pair.value());
2041 else if (pair.key() == "status")
2042 EXPECT_EQ("2", pair.value());
2043 else if (pair.key() == "message_type")
2044 EXPECT_EQ("message_receipt", pair.value());
2045 else
2046 FAIL() << "Unexpected key: " << pair.key();
2047 }
2048
1853 reset_last_event(); 2049 reset_last_event();
1854 2050
1855 // Message for kSender will be received. 2051 // Message for kSender will be received.
1856 MCSMessage message1(BuildDownstreamMessage( 2052 MCSMessage message1(BuildDownstreamMessage(
1857 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */, 2053 kSender, kProductCategoryForSubtypes, kSubtypeAppId /* subtype */,
1858 expected_data, std::string() /* raw_data */)); 2054 expected_data, std::string() /* raw_data */));
1859 EXPECT_TRUE(message1.IsValid()); 2055 EXPECT_TRUE(message1.IsValid());
1860 ReceiveMessageFromMCS(message1); 2056 ReceiveMessageFromMCS(message1);
1861 2057
1862 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); 2058 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1916 ReceiveMessageFromMCS(message); 2112 ReceiveMessageFromMCS(message);
1917 2113
1918 EXPECT_EQ(MESSAGE_RECEIVED, last_event()); 2114 EXPECT_EQ(MESSAGE_RECEIVED, last_event());
1919 EXPECT_EQ(kExtensionAppId, last_app_id()); 2115 EXPECT_EQ(kExtensionAppId, last_app_id());
1920 EXPECT_EQ(expected_data.size(), last_message().data.size()); 2116 EXPECT_EQ(expected_data.size(), last_message().data.size());
1921 EXPECT_EQ(expected_data, last_message().data); 2117 EXPECT_EQ(expected_data, last_message().data);
1922 EXPECT_EQ(kSender, last_message().sender_id); 2118 EXPECT_EQ(kSender, last_message().sender_id);
1923 } 2119 }
1924 2120
1925 } // namespace gcm 2121 } // namespace gcm
OLDNEW
« no previous file with comments | « components/gcm_driver/gcm_client_impl.cc ('k') | components/gcm_driver/gcm_driver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698