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

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

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

Powered by Google App Engine
This is Rietveld 408576698