OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/previews/previews_data_savings.h" | 5 #include "components/previews/previews_data_savings.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
| 12 #include "base/test/histogram_tester.h" |
12 #include "components/data_reduction_proxy/core/common/data_savings_recorder.h" | 13 #include "components/data_reduction_proxy/core/common/data_savings_recorder.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace previews { | 16 namespace previews { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 class TestDataSavingsRecorder | 20 class TestDataSavingsRecorder |
20 : public data_reduction_proxy::DataSavingsRecorder { | 21 : public data_reduction_proxy::DataSavingsRecorder { |
21 public: | 22 public: |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 data_savings_( | 63 data_savings_( |
63 new PreviewsDataSavings(test_data_savings_recorder_.get())) {} | 64 new PreviewsDataSavings(test_data_savings_recorder_.get())) {} |
64 ~PreviewsDataSavingsTest() override {} | 65 ~PreviewsDataSavingsTest() override {} |
65 | 66 |
66 PreviewsDataSavings* data_savings() const { return data_savings_.get(); } | 67 PreviewsDataSavings* data_savings() const { return data_savings_.get(); } |
67 | 68 |
68 TestDataSavingsRecorder* test_data_savings_recorder() const { | 69 TestDataSavingsRecorder* test_data_savings_recorder() const { |
69 return test_data_savings_recorder_.get(); | 70 return test_data_savings_recorder_.get(); |
70 } | 71 } |
71 | 72 |
| 73 base::HistogramTester* histogram_tester() { return &histogram_tester_; } |
| 74 |
72 private: | 75 private: |
| 76 base::HistogramTester histogram_tester_; |
| 77 |
73 std::unique_ptr<TestDataSavingsRecorder> test_data_savings_recorder_; | 78 std::unique_ptr<TestDataSavingsRecorder> test_data_savings_recorder_; |
74 std::unique_ptr<PreviewsDataSavings> data_savings_; | 79 std::unique_ptr<PreviewsDataSavings> data_savings_; |
75 }; | 80 }; |
76 | 81 |
77 TEST_F(PreviewsDataSavingsTest, RecordDataSavings) { | 82 TEST_F(PreviewsDataSavingsTest, RecordDataSavings) { |
78 int64_t original_size = 200; | 83 int64_t original_size = 200; |
79 int64_t data_used = 100; | 84 int64_t data_used = 100; |
80 std::string host = "host"; | 85 std::string host = "host"; |
81 | 86 |
82 EXPECT_EQ(0, test_data_savings_recorder()->data_used()); | 87 EXPECT_EQ(0, test_data_savings_recorder()->data_used()); |
83 EXPECT_EQ(0, test_data_savings_recorder()->original_size()); | 88 EXPECT_EQ(0, test_data_savings_recorder()->original_size()); |
84 test_data_savings_recorder()->set_data_saver_enabled(false); | 89 test_data_savings_recorder()->set_data_saver_enabled(false); |
85 | |
86 data_savings()->RecordDataSavings(host, data_used, original_size); | 90 data_savings()->RecordDataSavings(host, data_used, original_size); |
87 | 91 |
88 EXPECT_EQ(0, test_data_savings_recorder()->data_used()); | 92 EXPECT_EQ(0, test_data_savings_recorder()->data_used()); |
89 EXPECT_EQ(0, test_data_savings_recorder()->original_size()); | 93 EXPECT_EQ(0, test_data_savings_recorder()->original_size()); |
90 EXPECT_EQ(std::string(), test_data_savings_recorder()->last_host()); | 94 EXPECT_EQ(std::string(), test_data_savings_recorder()->last_host()); |
| 95 |
91 test_data_savings_recorder()->set_data_saver_enabled(true); | 96 test_data_savings_recorder()->set_data_saver_enabled(true); |
92 | |
93 data_savings()->RecordDataSavings(host, data_used, original_size); | 97 data_savings()->RecordDataSavings(host, data_used, original_size); |
94 | 98 |
95 EXPECT_EQ(data_used, test_data_savings_recorder()->data_used()); | 99 EXPECT_EQ(data_used, test_data_savings_recorder()->data_used()); |
96 EXPECT_EQ(original_size, test_data_savings_recorder()->original_size()); | 100 EXPECT_EQ(original_size, test_data_savings_recorder()->original_size()); |
97 EXPECT_EQ(host, test_data_savings_recorder()->last_host()); | 101 EXPECT_EQ(host, test_data_savings_recorder()->last_host()); |
98 } | 102 } |
99 | 103 |
| 104 TEST_F(PreviewsDataSavingsTest, RecordDataSavingsHistograms) { |
| 105 int64_t twenty_kb = 20480; |
| 106 int64_t twenty = twenty_kb >> 10; |
| 107 int64_t ten_kb = 10240; |
| 108 int64_t ten = ten_kb >> 10; |
| 109 std::string host = "host"; |
| 110 |
| 111 test_data_savings_recorder()->set_data_saver_enabled(false); |
| 112 data_savings()->RecordDataSavings(host, ten_kb, twenty_kb); |
| 113 |
| 114 // The histograms record bytes in KB. |
| 115 histogram_tester()->ExpectUniqueSample( |
| 116 "Previews.OriginalContentLength.DataSaverDisabled", twenty, 1); |
| 117 histogram_tester()->ExpectUniqueSample( |
| 118 "Previews.ContentLength.DataSaverDisabled", ten, 1); |
| 119 histogram_tester()->ExpectUniqueSample( |
| 120 "Previews.DataSavings.DataSaverDisabled", twenty - ten, 1); |
| 121 histogram_tester()->ExpectUniqueSample( |
| 122 "Previews.DataSavingsPercent.DataSaverDisabled", |
| 123 (twenty_kb - ten_kb) * 100 / twenty_kb, 1); |
| 124 |
| 125 data_savings()->RecordDataSavings(host, twenty_kb, ten_kb); |
| 126 |
| 127 // The histograms record bytes in KB. |
| 128 histogram_tester()->ExpectBucketCount( |
| 129 "Previews.OriginalContentLength.DataSaverDisabled", ten, 1); |
| 130 histogram_tester()->ExpectBucketCount( |
| 131 "Previews.ContentLength.DataSaverDisabled", twenty, 1); |
| 132 histogram_tester()->ExpectUniqueSample( |
| 133 "Previews.DataInflation.DataSaverDisabled", twenty - ten, 1); |
| 134 histogram_tester()->ExpectUniqueSample( |
| 135 "Previews.DataInflationPercent.DataSaverDisabled", |
| 136 (twenty_kb - ten_kb) * 100 / twenty_kb, 1); |
| 137 |
| 138 test_data_savings_recorder()->set_data_saver_enabled(true); |
| 139 data_savings()->RecordDataSavings(host, ten_kb, twenty_kb); |
| 140 |
| 141 // The histograms record bytes in KB. |
| 142 histogram_tester()->ExpectUniqueSample( |
| 143 "Previews.OriginalContentLength.DataSaverEnabled", twenty, 1); |
| 144 histogram_tester()->ExpectUniqueSample( |
| 145 "Previews.ContentLength.DataSaverEnabled", ten, 1); |
| 146 histogram_tester()->ExpectUniqueSample( |
| 147 "Previews.DataSavings.DataSaverEnabled", twenty - ten, 1); |
| 148 histogram_tester()->ExpectUniqueSample( |
| 149 "Previews.DataSavingsPercent.DataSaverEnabled", |
| 150 (twenty_kb - ten_kb) * 100 / twenty_kb, 1); |
| 151 |
| 152 data_savings()->RecordDataSavings(host, twenty_kb, ten_kb); |
| 153 |
| 154 // The histograms record bytes in KB. |
| 155 histogram_tester()->ExpectBucketCount( |
| 156 "Previews.OriginalContentLength.DataSaverEnabled", ten, 1); |
| 157 histogram_tester()->ExpectBucketCount( |
| 158 "Previews.ContentLength.DataSaverEnabled", twenty, 1); |
| 159 histogram_tester()->ExpectUniqueSample( |
| 160 "Previews.DataInflation.DataSaverEnabled", twenty - ten, 1); |
| 161 histogram_tester()->ExpectUniqueSample( |
| 162 "Previews.DataInflationPercent.DataSaverEnabled", |
| 163 (twenty_kb - ten_kb) * 100 / twenty_kb, 1); |
| 164 } |
| 165 |
100 } // namespace previews | 166 } // namespace previews |
OLD | NEW |