blob: 35805166872b6958be79d42a1f09d3f6218cb6e7 [file] [log] [blame]
Darin Petkovf1e85e42010-06-10 15:59:53 -07001// Copyright (c) 2010 The Chromium OS 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 <sys/file.h>
6
7#include <base/eintr_wrapper.h>
8#include <base/file_util.h>
9#include <base/logging.h>
10#include <base/string_util.h>
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
14#include "counter.h"
Ken Mixterccd84c02010-08-16 19:57:13 -070015#include "counter_mock.h" // For TaggedCounterMock.
Ken Mixter4c5daa42010-08-26 18:35:06 -070016#include "metrics_library_mock.h"
Darin Petkovf1e85e42010-06-10 15:59:53 -070017
18using ::testing::_;
19using ::testing::MockFunction;
20using ::testing::StrictMock;
21
22namespace chromeos_metrics {
23
24static const char kTestRecordFile[] = "record-file";
25static const char kDoesNotExistFile[] = "/does/not/exist";
26
27class RecordTest : public testing::Test {
28 protected:
29 virtual void SetUp() {
30 EXPECT_EQ(0, record_.tag());
31 EXPECT_EQ(0, record_.count());
32 }
33
34 // The record under test.
35 TaggedCounter::Record record_;
36};
37
38class TaggedCounterTest : public testing::Test {
39 protected:
40 virtual void SetUp() {
Ken Mixter4c5daa42010-08-26 18:35:06 -070041 EXPECT_TRUE(counter_.filename_.empty());
Darin Petkovf1e85e42010-06-10 15:59:53 -070042 EXPECT_TRUE(NULL == counter_.reporter_);
43 EXPECT_EQ(NULL, counter_.reporter_handle_);
44 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_);
45
46 counter_.Init(kTestRecordFile, &Reporter, this);
47 EXPECT_TRUE(AssertNoOrEmptyRecordFile());
48 EXPECT_EQ(kTestRecordFile, counter_.filename_);
49 EXPECT_TRUE(Reporter == counter_.reporter_);
50 EXPECT_EQ(this, counter_.reporter_handle_);
51 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_);
52
53 // The test fixture object will be used by the log message handler.
54 test_ = this;
55 logging::SetLogMessageHandler(HandleLogMessages);
56 }
57
58 virtual void TearDown() {
59 logging::SetLogMessageHandler(NULL);
60 test_ = NULL;
61 file_util::Delete(FilePath(kTestRecordFile), false);
62 }
63
64 // Asserts that the record file contains the specified contents.
65 testing::AssertionResult AssertRecord(const char* expr_tag,
66 const char* expr_count,
Darin Petkovcd8c3172010-06-24 10:13:54 -070067 int32 expected_tag,
68 int32 expected_count) {
Darin Petkovf1e85e42010-06-10 15:59:53 -070069 int fd = HANDLE_EINTR(open(kTestRecordFile, O_RDONLY));
70 if (fd < 0) {
71 testing::Message msg;
72 msg << "Unable to open " << kTestRecordFile;
73 return testing::AssertionFailure(msg);
74 }
75
76 TaggedCounter::Record record;
77 if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record),
78 sizeof(record))) {
79 testing::Message msg;
80 msg << "Unable to read " << sizeof(record) << " bytes from "
81 << kTestRecordFile;
82 HANDLE_EINTR(close(fd));
83 return testing::AssertionFailure(msg);
84 }
85
86 if (record.tag() != expected_tag || record.count() != expected_count) {
87 testing::Message msg;
88 msg << "actual record (" << record.tag() << ", " << record.count()
89 << ") expected (" << expected_tag << ", " << expected_count << ")";
90 HANDLE_EINTR(close(fd));
91 return testing::AssertionFailure(msg);
92 }
93
94 HANDLE_EINTR(close(fd));
95 return testing::AssertionSuccess();
96 }
97
98 // Returns true if the persistent record file does not exist or is
99 // empty, false otherwise.
100 bool AssertNoOrEmptyRecordFile() {
101 FilePath record_file(counter_.filename_);
102 int64 record_file_size;
103 return !file_util::PathExists(record_file) ||
104 (file_util::GetFileSize(record_file, &record_file_size) &&
105 record_file_size == 0);
106 }
107
108 // Adds a reporter call expectation that the specified tag/count
109 // callback will be generated.
Darin Petkovcd8c3172010-06-24 10:13:54 -0700110 void ExpectReporterCall(int32 tag, int32 count) {
Darin Petkovf1e85e42010-06-10 15:59:53 -0700111 EXPECT_CALL(reporter_, Call(_, tag, count))
112 .Times(1)
113 .RetiresOnSaturation();
114 }
115
116 // The reporter callback forwards the call to the reporter mock so
117 // that we can set call expectations.
Darin Petkovcd8c3172010-06-24 10:13:54 -0700118 static void Reporter(void* handle, int32 tag, int32 count) {
Darin Petkovf1e85e42010-06-10 15:59:53 -0700119 TaggedCounterTest* test = static_cast<TaggedCounterTest*>(handle);
120 ASSERT_FALSE(NULL == test);
121 test->reporter_.Call(handle, tag, count);
122 }
123
124 // Collects log messages in the |log_| member string so that they
125 // can be analyzed for errors and expected behavior.
126 static bool HandleLogMessages(int severity, const std::string& str) {
127 test_->log_.append(str);
128 test_->log_.append("\n");
129
130 // Returning true would mute the log.
131 return false;
132 }
133
134 // Returns true if the counter log contains |pattern|, false otherwise.
Darin Petkovcd8c3172010-06-24 10:13:54 -0700135 bool LogContains(const std::string& pattern) const {
Darin Petkovf1e85e42010-06-10 15:59:53 -0700136 return log_.find(pattern) != std::string::npos;
137 }
138
139 // The TaggedCounter object under test.
140 TaggedCounter counter_;
141
142 // The accumulated counter log.
143 std::string log_;
144
145 // Reporter mock to set callback expectations on.
Darin Petkovcd8c3172010-06-24 10:13:54 -0700146 StrictMock<MockFunction<void(void* handle,
147 int32 tag, int32 count)> > reporter_;
Darin Petkovf1e85e42010-06-10 15:59:53 -0700148
149 // Pointer to the current test fixture.
150 static TaggedCounterTest* test_;
151};
152
153// static
154TaggedCounterTest* TaggedCounterTest::test_ = NULL;
155
156TEST_F(RecordTest, Init) {
157 record_.Init(/* tag */ 5, /* count */ -1);
158 EXPECT_EQ(5, record_.tag());
159 EXPECT_EQ(0, record_.count());
160
161 record_.Init(/* tag */ -2, /* count */ 10);
162 EXPECT_EQ(-2, record_.tag());
163 EXPECT_EQ(10, record_.count());
164}
165
166TEST_F(RecordTest, Add) {
167 record_.Add(/* count */ -1);
168 EXPECT_EQ(0, record_.count());
169
170 record_.Add(/* count */ 5);
171 EXPECT_EQ(5, record_.count());
172
173 record_.Add(/* count */ 10);
174 EXPECT_EQ(15, record_.count());
175
176 record_.Add(/* count */ -2);
177 EXPECT_EQ(15, record_.count());
178
Darin Petkovcd8c3172010-06-24 10:13:54 -0700179 record_.Add(/* count */ kint32max);
180 EXPECT_EQ(kint32max, record_.count());
Darin Petkovf1e85e42010-06-10 15:59:53 -0700181
182 record_.Add(/* count */ 1);
Darin Petkovcd8c3172010-06-24 10:13:54 -0700183 EXPECT_EQ(kint32max, record_.count());
Darin Petkovf1e85e42010-06-10 15:59:53 -0700184}
185
186TEST_F(TaggedCounterTest, BadFileLocation) {
187 // Checks that the counter doesn't die badly if the file can't be
188 // created.
189 counter_.Init(kDoesNotExistFile,
190 /* reporter */ NULL, /* reporter_handle */ NULL);
191 counter_.Update(/* tag */ 10, /* count */ 20);
192 EXPECT_TRUE(LogContains("Unable to open the persistent counter file: "
193 "No such file or directory"));
194 EXPECT_EQ(TaggedCounter::kRecordInvalid, counter_.record_state_);
195 file_util::Delete(FilePath(kDoesNotExistFile), false);
196}
197
198TEST_F(TaggedCounterTest, Flush) {
199 counter_.Flush();
200 EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_);
201
202 counter_.Update(/* tag */ 40, /* count */ 60);
203 ExpectReporterCall(/* tag */ 40, /* count */ 60);
204 counter_.Flush();
205 EXPECT_TRUE(AssertNoOrEmptyRecordFile());
206 EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_);
207
208 counter_.Update(/* tag */ 41, /* count */ 70);
209 counter_.record_.Init(/* tag */ 0, /* count */ 0);
210 counter_.record_state_ = TaggedCounter::kRecordInvalid;
211 ExpectReporterCall(/* tag */ 41, /* count */ 70);
212 counter_.Flush();
213 EXPECT_TRUE(AssertNoOrEmptyRecordFile());
214 EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_);
215}
216
217TEST_F(TaggedCounterTest, InitFromFile) {
218 counter_.Update(/* tag */ 30, /* count */ 50);
219 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 50);
220 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
221
222 counter_.Init(kTestRecordFile, &Reporter, this);
223 counter_.Update(/* tag */ 30, /* count */ 40);
224 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 30, /* seconds */ 90);
225 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
226
227 counter_.Init(kTestRecordFile, &Reporter, this);
228 ExpectReporterCall(/* tag */ 30, /* count */ 90);
229 counter_.Update(/* tag */ 31, /* count */ 60);
230 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 31, /* seconds */ 60);
231 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
232
233 ExpectReporterCall(/* tag */ 31, /* count */ 60);
234 counter_.Init(kTestRecordFile, &Reporter, this);
235 counter_.Update(/* tag */ 32, /* count */ 0);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700236 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 32, /* seconds */ 0);
237 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700238}
239
240TEST_F(TaggedCounterTest, Update) {
241 counter_.Update(/* tag */ 20, /* count */ 30);
242 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 30);
243 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
244
245 counter_.Update(/* tag */ 20, /* count */ 40);
246 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 20, /* seconds */ 70);
247 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
248
249 ExpectReporterCall(/* tag */ 20, /* count */ 70);
250 counter_.Update(/* tag */ 21, /* count */ 15);
251 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 21, /* seconds */ 15);
252 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
253
254 ExpectReporterCall(/* tag */ 21, /* count */ 15);
255 counter_.Update(/* tag */ 22, /* count */ 0);
Darin Petkov1bb904e2010-06-16 15:58:06 -0700256 EXPECT_PRED_FORMAT2(AssertRecord, /* day */ 22, /* seconds */ 0);
257 EXPECT_EQ(TaggedCounter::kRecordValid, counter_.record_state_);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700258}
259
Ken Mixter4c5daa42010-08-26 18:35:06 -0700260static const char kTestFilename[] = "test_filename";
261static const char kTestHistogram[] = "test_histogram";
262const int kHistogramMin = 15;
263const int kHistogramMax = 1024;
264const int kHistogramBuckets = 23;
265
266class TaggedCounterReporterTest : public testing::Test {
267 protected:
268 virtual void SetUp() {
269 tagged_counter_ = new StrictMock<TaggedCounterMock>();
270 reporter_.tagged_counter_.reset(tagged_counter_);
271 metrics_lib_.reset(new StrictMock<MetricsLibraryMock>);
272 reporter_.SetMetricsLibraryInterface(metrics_lib_.get());
273 ASSERT_TRUE(metrics_lib_.get() == reporter_.metrics_lib_);
274 }
275 virtual void TearDown() {
276 reporter_.SetMetricsLibraryInterface(NULL);
277 }
278
279 void DoInit();
280 StrictMock<TaggedCounterMock>* tagged_counter_;
281 TaggedCounterReporter reporter_;
282 scoped_ptr<MetricsLibraryMock> metrics_lib_;
283};
284
285void TaggedCounterReporterTest::DoInit() {
286 EXPECT_CALL(*tagged_counter_,
287 Init(kTestFilename,
288 TaggedCounterReporter::Report,
289 &reporter_))
290 .Times(1)
291 .RetiresOnSaturation();
292 reporter_.Init(kTestFilename,
293 kTestHistogram,
294 kHistogramMin,
295 kHistogramMax,
296 kHistogramBuckets);
297 EXPECT_EQ(kTestHistogram, reporter_.histogram_name_);
298 EXPECT_EQ(kHistogramBuckets, reporter_.buckets_);
299 EXPECT_EQ(kHistogramMax, reporter_.max_);
300 EXPECT_EQ(kHistogramMin, reporter_.min_);
301}
302
303TEST_F(TaggedCounterReporterTest, Init) {
304 DoInit();
305}
306
307TEST_F(TaggedCounterReporterTest, Update) {
308 DoInit();
309 EXPECT_CALL(*tagged_counter_, Update(1, 2))
310 .Times(1)
311 .RetiresOnSaturation();
312 reporter_.Update(1, 2);
313}
314
315TEST_F(TaggedCounterReporterTest, Flush) {
316 DoInit();
317 EXPECT_CALL(*tagged_counter_, Flush())
318 .Times(1)
319 .RetiresOnSaturation();
320 reporter_.Flush();
321}
322
323TEST_F(TaggedCounterReporterTest, Report) {
324 DoInit();
325 EXPECT_CALL(*metrics_lib_, SendToUMA(kTestHistogram,
326 301,
327 kHistogramMin,
328 kHistogramMax,
329 kHistogramBuckets))
330 .Times(1)
331 .RetiresOnSaturation();
332 reporter_.Report(&reporter_, 127, 301);
333}
334
Ken Mixterccd84c02010-08-16 19:57:13 -0700335class FrequencyCounterTest : public testing::Test {
336 protected:
337 virtual void SetUp() {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700338 tagged_counter_ = NULL;
Ken Mixterccd84c02010-08-16 19:57:13 -0700339 }
340
341 void CheckInit(int32 cycle_duration);
342 void CheckCycleNumber(int32 cycle_duration);
343
344 FrequencyCounter frequency_counter_;
345 StrictMock<TaggedCounterMock>* tagged_counter_;
346
347 TaggedCounter::Reporter reporter_;
348};
349
350void FrequencyCounterTest::CheckInit(int32 cycle_duration) {
Ken Mixter4c5daa42010-08-26 18:35:06 -0700351 tagged_counter_ = new StrictMock<TaggedCounterMock>;
352 frequency_counter_.Init(tagged_counter_, cycle_duration);
Ken Mixterccd84c02010-08-16 19:57:13 -0700353 EXPECT_EQ(cycle_duration, frequency_counter_.cycle_duration_);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700354 EXPECT_EQ(tagged_counter_, frequency_counter_.tagged_counter_.get());
Ken Mixterccd84c02010-08-16 19:57:13 -0700355}
356
357TEST_F(FrequencyCounterTest, Init) {
358 CheckInit(100);
359}
360
361void FrequencyCounterTest::CheckCycleNumber(int32 cycle_duration) {
362 CheckInit(cycle_duration);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700363 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(
364 cycle_duration * 150));
365 EXPECT_EQ(150, frequency_counter_.GetCycleNumber(
366 cycle_duration * 150 + cycle_duration - 1));
367 EXPECT_EQ(151, frequency_counter_.GetCycleNumber(
368 cycle_duration * 151 + 1));
Ken Mixterccd84c02010-08-16 19:57:13 -0700369 EXPECT_EQ(0, frequency_counter_.GetCycleNumber(0));
370}
371
372
373TEST_F(FrequencyCounterTest, GetCycleNumberForWeek) {
374 CheckCycleNumber(kSecondsPerWeek);
375}
376
377TEST_F(FrequencyCounterTest, GetCycleNumberForDay) {
378 CheckCycleNumber(kSecondsPerDay);
379}
380
381TEST_F(FrequencyCounterTest, UpdateInternal) {
382 CheckInit(kSecondsPerWeek);
Ken Mixter4c5daa42010-08-26 18:35:06 -0700383 EXPECT_CALL(*tagged_counter_, Update(150, 2))
384 .Times(1)
385 .RetiresOnSaturation();
Ken Mixterccd84c02010-08-16 19:57:13 -0700386 frequency_counter_.UpdateInternal(2, kSecondsPerWeek * 150);
387}
388
Darin Petkovf1e85e42010-06-10 15:59:53 -0700389} // namespace chromeos_metrics
390
391int main(int argc, char** argv) {
392 testing::InitGoogleTest(&argc, argv);
393 return RUN_ALL_TESTS();
394}