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

Side by Side Diff: components/contextual_search/browser/ctr_aggregator_unittest.cc

Issue 2285633004: [TTS] Record CTR by week and 28-day intervals. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ctr
Patch Set: Reworked the cache to use a stored preference instead of session static data. Created 4 years, 2 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
OLDNEW
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/contextual_search/browser/ctr_aggregator.h" 5 #include "components/contextual_search/browser/ctr_aggregator.h"
6 6
7 #include <unordered_map> 7 #include <unordered_map>
8 8
9 #include "base/gtest_prod_util.h" 9 #include "base/gtest_prod_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 using base::Value; 14 using base::Value;
15 15
16 namespace { 16 namespace {
17 const int kTestWeek = 2500; 17 const int kTestWeek = 2500;
18 } 18 }
19 19
20 namespace contextual_search { 20 namespace contextual_search {
21 21
22 class CTRAggregatorTest : public testing::Test { 22 class CtrAggregatorTest : public testing::Test {
23 public: 23 public:
24 CTRAggregatorTest() {} 24 CtrAggregatorTest() {}
25 ~CTRAggregatorTest() override {} 25 ~CtrAggregatorTest() override {}
26 26
27 class WeeklyActivityStorageStub : public WeeklyActivityStorage { 27 class WeeklyActivityStorageStub : public WeeklyActivityStorage {
28 public: 28 public:
29 WeeklyActivityStorageStub(); 29 WeeklyActivityStorageStub();
30 30
31 private: 31 private:
32 int ReadStorage(std::string storage_bucket) override; 32 int ReadStorage(std::string storage_bucket) override;
33 void WriteStorage(std::string storage_key, int value) override; 33 void WriteStorage(std::string storage_key, int value) override;
34 34
35 typedef std::unordered_map<std::string, int> hashmap; 35 typedef std::unordered_map<std::string, int> hashmap;
36 hashmap weeks_; 36 hashmap weeks_;
37 }; 37 };
38 38
39 // Test helpers 39 // Test helpers
40 void Fill4Weeks(); // Fill 4 weeks with 2 impressions, 1 click. 40 void Fill4Weeks(); // Fill 4 weeks with 2 impressions, 1 click.
41 41
42 // The class under test. 42 // The class under test.
43 std::unique_ptr<CTRAggregator> aggregator_; 43 std::unique_ptr<CtrAggregator> aggregator_;
44 44
45 protected: 45 protected:
46 // The storage stub. 46 // The storage stub.
47 std::unique_ptr<WeeklyActivityStorage> storage_; 47 std::unique_ptr<WeeklyActivityStorage> storage_;
48 48
49 void SetUp() override { 49 void SetUp() override {
50 storage_.reset(new WeeklyActivityStorageStub()); 50 storage_.reset(new WeeklyActivityStorageStub());
51 aggregator_.reset(new CTRAggregator(*storage_.get(), kTestWeek)); 51 aggregator_.reset(new CtrAggregator(*storage_.get(), kTestWeek));
52 } 52 }
53 53
54 void TearDown() override {} 54 void TearDown() override {}
55 55
56 private: 56 private:
57 DISALLOW_COPY_AND_ASSIGN(CTRAggregatorTest); 57 DISALLOW_COPY_AND_ASSIGN(CtrAggregatorTest);
58 }; 58 };
59 59
60 CTRAggregatorTest::WeeklyActivityStorageStub::WeeklyActivityStorageStub() 60 CtrAggregatorTest::WeeklyActivityStorageStub::WeeklyActivityStorageStub()
61 : WeeklyActivityStorage(4) {} 61 : WeeklyActivityStorage(4) {}
62 62
63 int CTRAggregatorTest::WeeklyActivityStorageStub::ReadStorage( 63 int CtrAggregatorTest::WeeklyActivityStorageStub::ReadStorage(
64 std::string storage_bucket) { 64 std::string storage_bucket) {
65 return weeks_[storage_bucket]; 65 return weeks_[storage_bucket];
66 } 66 }
67 67
68 void CTRAggregatorTest::WeeklyActivityStorageStub::WriteStorage( 68 void CtrAggregatorTest::WeeklyActivityStorageStub::WriteStorage(
69 std::string storage_bucket, 69 std::string storage_bucket,
70 int value) { 70 int value) {
71 weeks_[storage_bucket] = value; 71 weeks_[storage_bucket] = value;
72 } 72 }
73 73
74 void CTRAggregatorTest::Fill4Weeks() { 74 void CtrAggregatorTest::Fill4Weeks() {
75 int weeks_to_record = 4; 75 int weeks_to_record = 4;
76 for (int i = 0; i < weeks_to_record; i++) { 76 for (int i = 0; i < weeks_to_record; i++) {
77 aggregator_->RecordImpression(true); 77 aggregator_->RecordImpression(true);
78 aggregator_->RecordImpression(false); 78 aggregator_->RecordImpression(false);
79 EXPECT_FALSE(aggregator_->HasPrevious28DayData()); 79 EXPECT_FALSE(aggregator_->HasPrevious28DayData());
80 aggregator_->IncrementWeek(1); 80 aggregator_->IncrementWeek(1);
81 } 81 }
82 EXPECT_TRUE(aggregator_->HasPrevious28DayData()); 82 EXPECT_TRUE(aggregator_->HasPrevious28DayData());
83 } 83 }
84 84
85 // NaN has the property that it is not equal to itself. 85 // NaN has the property that it is not equal to itself.
86 #define EXPECT_NAN(x) EXPECT_NE(x, x) 86 #define EXPECT_NAN(x) EXPECT_NE(x, x)
87 87
88 TEST_F(CTRAggregatorTest, SimpleOperationTest) { 88 TEST_F(CtrAggregatorTest, SimpleOperationTest) {
89 aggregator_->RecordImpression(true); 89 aggregator_->RecordImpression(true);
90 aggregator_->RecordImpression(false); 90 aggregator_->RecordImpression(false);
91 EXPECT_FALSE(aggregator_->HasPreviousWeekData()); 91 EXPECT_FALSE(aggregator_->HasPreviousWeekData());
92 EXPECT_EQ(0, aggregator_->GetPreviousWeekImpressions()); 92 EXPECT_EQ(0, aggregator_->GetPreviousWeekImpressions());
93 EXPECT_NAN(aggregator_->GetPreviousWeekCTR()); 93 EXPECT_NAN(aggregator_->GetPreviousWeekCtr());
94 94
95 aggregator_->IncrementWeek(1); 95 aggregator_->IncrementWeek(1);
96 EXPECT_TRUE(aggregator_->HasPreviousWeekData()); 96 EXPECT_TRUE(aggregator_->HasPreviousWeekData());
97 EXPECT_EQ(2, aggregator_->GetPreviousWeekImpressions()); 97 EXPECT_EQ(2, aggregator_->GetPreviousWeekImpressions());
98 EXPECT_FLOAT_EQ(0.5f, aggregator_->GetPreviousWeekCTR()); 98 EXPECT_FLOAT_EQ(0.5f, aggregator_->GetPreviousWeekCtr());
99 } 99 }
100 100
101 TEST_F(CTRAggregatorTest, MultiWeekTest) { 101 TEST_F(CtrAggregatorTest, MultiWeekTest) {
102 Fill4Weeks(); 102 Fill4Weeks();
103 aggregator_->RecordImpression(false); 103 aggregator_->RecordImpression(false);
104 aggregator_->IncrementWeek(1); 104 aggregator_->IncrementWeek(1);
105 EXPECT_TRUE(aggregator_->HasPrevious28DayData()); 105 EXPECT_TRUE(aggregator_->HasPrevious28DayData());
106 EXPECT_FLOAT_EQ(static_cast<float>(3.0 / 7), 106 EXPECT_FLOAT_EQ(static_cast<float>(3.0 / 7),
107 aggregator_->GetPrevious28DayCTR()); 107 aggregator_->GetPrevious28DayCtr());
108 aggregator_->RecordImpression(false); 108 aggregator_->RecordImpression(false);
109 aggregator_->IncrementWeek(1); 109 aggregator_->IncrementWeek(1);
110 EXPECT_TRUE(aggregator_->HasPrevious28DayData()); 110 EXPECT_TRUE(aggregator_->HasPrevious28DayData());
111 EXPECT_FLOAT_EQ(static_cast<float>(2.0 / 6), 111 EXPECT_FLOAT_EQ(static_cast<float>(2.0 / 6),
112 aggregator_->GetPrevious28DayCTR()); 112 aggregator_->GetPrevious28DayCtr());
113 } 113 }
114 114
115 TEST_F(CTRAggregatorTest, SkipOneWeekTest) { 115 TEST_F(CtrAggregatorTest, SkipOneWeekTest) {
116 Fill4Weeks(); 116 Fill4Weeks();
117 aggregator_->IncrementWeek(1); 117 aggregator_->IncrementWeek(1);
118 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); 118 EXPECT_EQ(0, aggregator_->GetPreviousWeekCtr());
119 EXPECT_FLOAT_EQ(static_cast<float>(3.0 / 6), 119 EXPECT_FLOAT_EQ(static_cast<float>(3.0 / 6),
120 aggregator_->GetPrevious28DayCTR()); 120 aggregator_->GetPrevious28DayCtr());
121 } 121 }
122 122
123 TEST_F(CTRAggregatorTest, SkipThreeWeeksTest) { 123 TEST_F(CtrAggregatorTest, SkipThreeWeeksTest) {
124 Fill4Weeks(); 124 Fill4Weeks();
125 aggregator_->IncrementWeek(3); 125 aggregator_->IncrementWeek(3);
126 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); 126 EXPECT_EQ(0, aggregator_->GetPreviousWeekCtr());
127 EXPECT_FLOAT_EQ(static_cast<float>(1.0 / 2), 127 EXPECT_FLOAT_EQ(static_cast<float>(1.0 / 2),
128 aggregator_->GetPrevious28DayCTR()); 128 aggregator_->GetPrevious28DayCtr());
129 } 129 }
130 130
131 TEST_F(CTRAggregatorTest, SkipFourWeeksTest) { 131 TEST_F(CtrAggregatorTest, SkipFourWeeksTest) {
132 Fill4Weeks(); 132 Fill4Weeks();
133 aggregator_->IncrementWeek(4); 133 aggregator_->IncrementWeek(4);
134 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); 134 EXPECT_EQ(0, aggregator_->GetPreviousWeekCtr());
135 EXPECT_EQ(0, aggregator_->GetPrevious28DayCTR()); 135 EXPECT_EQ(0, aggregator_->GetPrevious28DayCtr());
136 } 136 }
137 137
138 } // namespace contextual_search 138 } // namespace contextual_search
OLDNEW
« no previous file with comments | « components/contextual_search/browser/ctr_aggregator.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698