OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // 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 | 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 <sys/file.h> | 5 #include <sys/file.h> |
6 | 6 |
7 #include <base/eintr_wrapper.h> | 7 #include <base/eintr_wrapper.h> |
8 #include <base/file_util.h> | 8 #include <base/file_util.h> |
9 #include <base/logging.h> | 9 #include <base/logging.h> |
10 #include <base/string_util.h> | 10 #include <base/string_util.h> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 55 |
56 virtual void TearDown() { | 56 virtual void TearDown() { |
57 logging::SetLogMessageHandler(NULL); | 57 logging::SetLogMessageHandler(NULL); |
58 test_ = NULL; | 58 test_ = NULL; |
59 file_util::Delete(FilePath(kTestRecordFile), false); | 59 file_util::Delete(FilePath(kTestRecordFile), false); |
60 } | 60 } |
61 | 61 |
62 // Asserts that the record file contains the specified contents. | 62 // Asserts that the record file contains the specified contents. |
63 testing::AssertionResult AssertRecord(const char* expr_tag, | 63 testing::AssertionResult AssertRecord(const char* expr_tag, |
64 const char* expr_count, | 64 const char* expr_count, |
65 int expected_tag, | 65 int32 expected_tag, |
66 int expected_count) { | 66 int32 expected_count) { |
67 int fd = HANDLE_EINTR(open(kTestRecordFile, O_RDONLY)); | 67 int fd = HANDLE_EINTR(open(kTestRecordFile, O_RDONLY)); |
68 if (fd < 0) { | 68 if (fd < 0) { |
69 testing::Message msg; | 69 testing::Message msg; |
70 msg << "Unable to open " << kTestRecordFile; | 70 msg << "Unable to open " << kTestRecordFile; |
71 return testing::AssertionFailure(msg); | 71 return testing::AssertionFailure(msg); |
72 } | 72 } |
73 | 73 |
74 TaggedCounter::Record record; | 74 TaggedCounter::Record record; |
75 if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record), | 75 if (!file_util::ReadFromFD(fd, reinterpret_cast<char*>(&record), |
76 sizeof(record))) { | 76 sizeof(record))) { |
(...skipping 21 matching lines...) Expand all Loading... |
98 bool AssertNoOrEmptyRecordFile() { | 98 bool AssertNoOrEmptyRecordFile() { |
99 FilePath record_file(counter_.filename_); | 99 FilePath record_file(counter_.filename_); |
100 int64 record_file_size; | 100 int64 record_file_size; |
101 return !file_util::PathExists(record_file) || | 101 return !file_util::PathExists(record_file) || |
102 (file_util::GetFileSize(record_file, &record_file_size) && | 102 (file_util::GetFileSize(record_file, &record_file_size) && |
103 record_file_size == 0); | 103 record_file_size == 0); |
104 } | 104 } |
105 | 105 |
106 // Adds a reporter call expectation that the specified tag/count | 106 // Adds a reporter call expectation that the specified tag/count |
107 // callback will be generated. | 107 // callback will be generated. |
108 void ExpectReporterCall(int tag, int count) { | 108 void ExpectReporterCall(int32 tag, int32 count) { |
109 EXPECT_CALL(reporter_, Call(_, tag, count)) | 109 EXPECT_CALL(reporter_, Call(_, tag, count)) |
110 .Times(1) | 110 .Times(1) |
111 .RetiresOnSaturation(); | 111 .RetiresOnSaturation(); |
112 } | 112 } |
113 | 113 |
114 // The reporter callback forwards the call to the reporter mock so | 114 // The reporter callback forwards the call to the reporter mock so |
115 // that we can set call expectations. | 115 // that we can set call expectations. |
116 static void Reporter(void* handle, int tag, int count) { | 116 static void Reporter(void* handle, int32 tag, int32 count) { |
117 TaggedCounterTest* test = static_cast<TaggedCounterTest*>(handle); | 117 TaggedCounterTest* test = static_cast<TaggedCounterTest*>(handle); |
118 ASSERT_FALSE(NULL == test); | 118 ASSERT_FALSE(NULL == test); |
119 test->reporter_.Call(handle, tag, count); | 119 test->reporter_.Call(handle, tag, count); |
120 } | 120 } |
121 | 121 |
122 // Collects log messages in the |log_| member string so that they | 122 // Collects log messages in the |log_| member string so that they |
123 // can be analyzed for errors and expected behavior. | 123 // can be analyzed for errors and expected behavior. |
124 static bool HandleLogMessages(int severity, const std::string& str) { | 124 static bool HandleLogMessages(int severity, const std::string& str) { |
125 test_->log_.append(str); | 125 test_->log_.append(str); |
126 test_->log_.append("\n"); | 126 test_->log_.append("\n"); |
127 | 127 |
128 // Returning true would mute the log. | 128 // Returning true would mute the log. |
129 return false; | 129 return false; |
130 } | 130 } |
131 | 131 |
132 // Returns true if the counter log contains |pattern|, false otherwise. | 132 // Returns true if the counter log contains |pattern|, false otherwise. |
133 bool LogContains(const std::string& pattern) { | 133 bool LogContains(const std::string& pattern) const { |
134 return log_.find(pattern) != std::string::npos; | 134 return log_.find(pattern) != std::string::npos; |
135 } | 135 } |
136 | 136 |
137 // The TaggedCounter object under test. | 137 // The TaggedCounter object under test. |
138 TaggedCounter counter_; | 138 TaggedCounter counter_; |
139 | 139 |
140 // The accumulated counter log. | 140 // The accumulated counter log. |
141 std::string log_; | 141 std::string log_; |
142 | 142 |
143 // Reporter mock to set callback expectations on. | 143 // Reporter mock to set callback expectations on. |
144 StrictMock<MockFunction<void(void* handle, int tag, int count)> > reporter_; | 144 StrictMock<MockFunction<void(void* handle, |
| 145 int32 tag, int32 count)> > reporter_; |
145 | 146 |
146 // Pointer to the current test fixture. | 147 // Pointer to the current test fixture. |
147 static TaggedCounterTest* test_; | 148 static TaggedCounterTest* test_; |
148 }; | 149 }; |
149 | 150 |
150 // static | 151 // static |
151 TaggedCounterTest* TaggedCounterTest::test_ = NULL; | 152 TaggedCounterTest* TaggedCounterTest::test_ = NULL; |
152 | 153 |
153 TEST_F(RecordTest, Init) { | 154 TEST_F(RecordTest, Init) { |
154 record_.Init(/* tag */ 5, /* count */ -1); | 155 record_.Init(/* tag */ 5, /* count */ -1); |
(...skipping 11 matching lines...) Expand all Loading... |
166 | 167 |
167 record_.Add(/* count */ 5); | 168 record_.Add(/* count */ 5); |
168 EXPECT_EQ(5, record_.count()); | 169 EXPECT_EQ(5, record_.count()); |
169 | 170 |
170 record_.Add(/* count */ 10); | 171 record_.Add(/* count */ 10); |
171 EXPECT_EQ(15, record_.count()); | 172 EXPECT_EQ(15, record_.count()); |
172 | 173 |
173 record_.Add(/* count */ -2); | 174 record_.Add(/* count */ -2); |
174 EXPECT_EQ(15, record_.count()); | 175 EXPECT_EQ(15, record_.count()); |
175 | 176 |
176 record_.Add(/* count */ INT_MAX); | 177 record_.Add(/* count */ kint32max); |
177 EXPECT_EQ(INT_MAX, record_.count()); | 178 EXPECT_EQ(kint32max, record_.count()); |
178 | 179 |
179 record_.Add(/* count */ 1); | 180 record_.Add(/* count */ 1); |
180 EXPECT_EQ(INT_MAX, record_.count()); | 181 EXPECT_EQ(kint32max, record_.count()); |
181 } | 182 } |
182 | 183 |
183 TEST_F(TaggedCounterTest, BadFileLocation) { | 184 TEST_F(TaggedCounterTest, BadFileLocation) { |
184 // Checks that the counter doesn't die badly if the file can't be | 185 // Checks that the counter doesn't die badly if the file can't be |
185 // created. | 186 // created. |
186 counter_.Init(kDoesNotExistFile, | 187 counter_.Init(kDoesNotExistFile, |
187 /* reporter */ NULL, /* reporter_handle */ NULL); | 188 /* reporter */ NULL, /* reporter_handle */ NULL); |
188 counter_.Update(/* tag */ 10, /* count */ 20); | 189 counter_.Update(/* tag */ 10, /* count */ 20); |
189 EXPECT_TRUE(LogContains("Unable to open the persistent counter file: " | 190 EXPECT_TRUE(LogContains("Unable to open the persistent counter file: " |
190 "No such file or directory")); | 191 "No such file or directory")); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 EXPECT_TRUE(AssertNoOrEmptyRecordFile()); | 254 EXPECT_TRUE(AssertNoOrEmptyRecordFile()); |
254 EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); | 255 EXPECT_EQ(TaggedCounter::kRecordNull, counter_.record_state_); |
255 } | 256 } |
256 | 257 |
257 } // namespace chromeos_metrics | 258 } // namespace chromeos_metrics |
258 | 259 |
259 int main(int argc, char** argv) { | 260 int main(int argc, char** argv) { |
260 testing::InitGoogleTest(&argc, argv); | 261 testing::InitGoogleTest(&argc, argv); |
261 return RUN_ALL_TESTS(); | 262 return RUN_ALL_TESTS(); |
262 } | 263 } |
OLD | NEW |