Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "components/gcm_driver/gcm_stats_recorder_android.h" | |
| 6 | |
| 7 #include "base/test/histogram_tester.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace gcm { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const char kTestAppId[] = "test_app_id"; | |
| 15 | |
| 16 class GCMStatsRecorderAndroidTest : public ::testing::Test, | |
| 17 public GCMStatsRecorderAndroid::Delegate { | |
| 18 public: | |
| 19 GCMStatsRecorderAndroidTest() | |
| 20 : activity_recorded_calls_(0) {} | |
| 21 ~GCMStatsRecorderAndroidTest() {} | |
| 22 | |
| 23 // GCMStatsRecorderAndroid::Delegate implementation. | |
| 24 void OnActivityRecorded() override { | |
| 25 ++activity_recorded_calls_; | |
| 26 } | |
| 27 | |
| 28 size_t activity_recorded_calls() const { return activity_recorded_calls_; } | |
| 29 | |
| 30 private: | |
| 31 size_t activity_recorded_calls_; | |
| 32 }; | |
| 33 | |
| 34 TEST_F(GCMStatsRecorderAndroidTest, RecordsAndCallsDelegate) { | |
| 35 GCMStatsRecorderAndroid recorder(this /* delegate */); | |
| 36 recorder.SetRecording(true); | |
| 37 | |
| 38 ASSERT_TRUE(recorder.is_recording()); | |
| 39 | |
| 40 EXPECT_EQ(0u, activity_recorded_calls()); | |
| 41 | |
| 42 recorder.RecordRegistrationSent(kTestAppId); | |
| 43 EXPECT_EQ(1u, activity_recorded_calls()); | |
| 44 | |
| 45 recorder.RecordRegistrationResponse(kTestAppId, true /* success */); | |
| 46 EXPECT_EQ(2u, activity_recorded_calls()); | |
| 47 | |
| 48 recorder.RecordUnregistrationSent(kTestAppId); | |
| 49 EXPECT_EQ(3u, activity_recorded_calls()); | |
| 50 | |
| 51 recorder.RecordUnregistrationResponse(kTestAppId, true /* success */); | |
| 52 EXPECT_EQ(4u, activity_recorded_calls()); | |
| 53 | |
| 54 recorder.RecordDataMessageReceived(kTestAppId, 42 /* message_byte_size */); | |
| 55 EXPECT_EQ(5u, activity_recorded_calls()); | |
| 56 | |
| 57 RecordedActivities activities; | |
| 58 recorder.CollectActivities(&activities); | |
| 59 | |
| 60 EXPECT_EQ(4u, activities.registration_activities.size()); | |
| 61 EXPECT_EQ(1u, activities.receiving_activities.size()); | |
| 62 | |
| 63 recorder.CollectActivities(&activities); | |
| 64 | |
| 65 EXPECT_EQ(8u, activities.registration_activities.size()); | |
| 66 EXPECT_EQ(2u, activities.receiving_activities.size()); | |
| 67 | |
| 68 recorder.Clear(); | |
| 69 | |
| 70 RecordedActivities empty_activities; | |
| 71 recorder.CollectActivities(&empty_activities); | |
| 72 | |
| 73 EXPECT_EQ(0u, empty_activities.registration_activities.size()); | |
| 74 EXPECT_EQ(0u, empty_activities.receiving_activities.size()); | |
| 75 } | |
| 76 | |
| 77 TEST_F(GCMStatsRecorderAndroidTest, NullDelegate) { | |
| 78 GCMStatsRecorderAndroid recorder(nullptr /* delegate */); | |
| 79 recorder.SetRecording(true); | |
| 80 | |
| 81 ASSERT_TRUE(recorder.is_recording()); | |
| 82 | |
| 83 recorder.RecordRegistrationSent(kTestAppId); | |
| 84 | |
| 85 RecordedActivities activities; | |
| 86 recorder.CollectActivities(&activities); | |
| 87 | |
| 88 EXPECT_EQ(1u, activities.registration_activities.size()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(GCMStatsRecorderAndroidTest, NotRecording) { | |
| 92 GCMStatsRecorderAndroid recorder(this /* delegate */); | |
| 93 ASSERT_FALSE(recorder.is_recording()); | |
| 94 | |
| 95 recorder.RecordRegistrationSent(kTestAppId); | |
| 96 | |
| 97 RecordedActivities activities; | |
| 98 recorder.CollectActivities(&activities); | |
| 99 | |
| 100 EXPECT_EQ(0u, activities.registration_activities.size()); | |
| 101 } | |
| 102 | |
| 103 TEST_F(GCMStatsRecorderAndroidTest, HistogramCounts) { | |
| 104 GCMStatsRecorderAndroid recorder(this /* delegate */); | |
| 105 recorder.SetRecording(true); | |
| 106 | |
| 107 ASSERT_TRUE(recorder.is_recording()); | |
| 108 | |
| 109 base::HistogramTester histogram_tester; | |
| 110 | |
| 111 recorder.RecordRegistrationSent(kTestAppId); | |
| 112 histogram_tester.ExpectTotalCount("GCM.RegistrationRequest", 1); | |
| 113 | |
| 114 recorder.RecordUnregistrationSent(kTestAppId); | |
| 115 histogram_tester.ExpectTotalCount("GCM.UnregistrationRequest", 1); | |
| 116 | |
| 117 recorder.RecordDataMessageReceived(kTestAppId, 42 /* message_byte_size */); | |
| 118 histogram_tester.ExpectTotalCount("GCM.DataMessageReceived", 1); | |
| 119 } | |
| 120 | |
| 121 } // namespace | |
|
jianli
2015/12/18 00:10:00
Why wrapping all test code inside anonymous namesp
Peter Beverloo
2015/12/18 17:19:17
It became a force of habit per the following threa
| |
| 122 | |
| 123 } // namespace gcm | |
| OLD | NEW |