OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 #endif | 170 #endif |
171 | 171 |
172 ActivityDatabaseTestPolicy* db_delegate_; | 172 ActivityDatabaseTestPolicy* db_delegate_; |
173 }; | 173 }; |
174 | 174 |
175 // Check that the database is initialized properly. | 175 // Check that the database is initialized properly. |
176 TEST_F(ActivityDatabaseTest, Init) { | 176 TEST_F(ActivityDatabaseTest, Init) { |
177 base::ScopedTempDir temp_dir; | 177 base::ScopedTempDir temp_dir; |
178 base::FilePath db_file; | 178 base::FilePath db_file; |
179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
180 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); | 180 db_file = temp_dir.GetPath().AppendASCII("ActivityInit.db"); |
181 sql::Connection::Delete(db_file); | 181 sql::Connection::Delete(db_file); |
182 | 182 |
183 ActivityDatabase* activity_db = OpenDatabase(db_file); | 183 ActivityDatabase* activity_db = OpenDatabase(db_file); |
184 activity_db->Close(); | 184 activity_db->Close(); |
185 | 185 |
186 sql::Connection db; | 186 sql::Connection db; |
187 ASSERT_TRUE(db.Open(db_file)); | 187 ASSERT_TRUE(db.Open(db_file)); |
188 ASSERT_TRUE(db.DoesTableExist(ActivityDatabaseTestPolicy::kTableName)); | 188 ASSERT_TRUE(db.DoesTableExist(ActivityDatabaseTestPolicy::kTableName)); |
189 db.Close(); | 189 db.Close(); |
190 } | 190 } |
191 | 191 |
192 // Check that actions are recorded in the db. | 192 // Check that actions are recorded in the db. |
193 TEST_F(ActivityDatabaseTest, RecordAction) { | 193 TEST_F(ActivityDatabaseTest, RecordAction) { |
194 base::ScopedTempDir temp_dir; | 194 base::ScopedTempDir temp_dir; |
195 base::FilePath db_file; | 195 base::FilePath db_file; |
196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
197 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 197 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); |
198 sql::Connection::Delete(db_file); | 198 sql::Connection::Delete(db_file); |
199 | 199 |
200 ActivityDatabase* activity_db = OpenDatabase(db_file); | 200 ActivityDatabase* activity_db = OpenDatabase(db_file); |
201 activity_db->SetBatchModeForTesting(false); | 201 activity_db->SetBatchModeForTesting(false); |
202 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); | 202 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); |
203 Record(activity_db, action); | 203 Record(activity_db, action); |
204 activity_db->Close(); | 204 activity_db->Close(); |
205 | 205 |
206 sql::Connection db; | 206 sql::Connection db; |
207 ASSERT_TRUE(db.Open(db_file)); | 207 ASSERT_TRUE(db.Open(db_file)); |
208 | 208 |
209 ASSERT_EQ(1, CountActions(&db, "brewster")); | 209 ASSERT_EQ(1, CountActions(&db, "brewster")); |
210 } | 210 } |
211 | 211 |
212 TEST_F(ActivityDatabaseTest, BatchModeOff) { | 212 TEST_F(ActivityDatabaseTest, BatchModeOff) { |
213 base::ScopedTempDir temp_dir; | 213 base::ScopedTempDir temp_dir; |
214 base::FilePath db_file; | 214 base::FilePath db_file; |
215 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 215 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
216 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 216 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); |
217 sql::Connection::Delete(db_file); | 217 sql::Connection::Delete(db_file); |
218 | 218 |
219 // Record some actions | 219 // Record some actions |
220 ActivityDatabase* activity_db = OpenDatabase(db_file); | 220 ActivityDatabase* activity_db = OpenDatabase(db_file); |
221 activity_db->SetBatchModeForTesting(false); | 221 activity_db->SetBatchModeForTesting(false); |
222 | 222 |
223 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); | 223 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); |
224 Record(activity_db, action); | 224 Record(activity_db, action); |
225 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 225 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); |
226 | 226 |
227 activity_db->Close(); | 227 activity_db->Close(); |
228 } | 228 } |
229 | 229 |
230 TEST_F(ActivityDatabaseTest, BatchModeOn) { | 230 TEST_F(ActivityDatabaseTest, BatchModeOn) { |
231 base::ScopedTempDir temp_dir; | 231 base::ScopedTempDir temp_dir; |
232 base::FilePath db_file; | 232 base::FilePath db_file; |
233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 233 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
234 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 234 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); |
235 sql::Connection::Delete(db_file); | 235 sql::Connection::Delete(db_file); |
236 | 236 |
237 // Record some actions | 237 // Record some actions |
238 ActivityDatabase* activity_db = OpenDatabase(db_file); | 238 ActivityDatabase* activity_db = OpenDatabase(db_file); |
239 activity_db->SetBatchModeForTesting(true); | 239 activity_db->SetBatchModeForTesting(true); |
240 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); | 240 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); |
241 Record(activity_db, action); | 241 Record(activity_db, action); |
242 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); | 242 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); |
243 | 243 |
244 // Artificially trigger and then stop the timer. | 244 // Artificially trigger and then stop the timer. |
245 activity_db->SetTimerForTesting(0); | 245 activity_db->SetTimerForTesting(0); |
246 base::RunLoop().RunUntilIdle(); | 246 base::RunLoop().RunUntilIdle(); |
247 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 247 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); |
248 | 248 |
249 activity_db->Close(); | 249 activity_db->Close(); |
250 } | 250 } |
251 | 251 |
252 TEST_F(ActivityDatabaseTest, BatchModeFlush) { | 252 TEST_F(ActivityDatabaseTest, BatchModeFlush) { |
253 base::ScopedTempDir temp_dir; | 253 base::ScopedTempDir temp_dir; |
254 base::FilePath db_file; | 254 base::FilePath db_file; |
255 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 255 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
256 db_file = temp_dir.path().AppendASCII("ActivityFlush.db"); | 256 db_file = temp_dir.GetPath().AppendASCII("ActivityFlush.db"); |
257 sql::Connection::Delete(db_file); | 257 sql::Connection::Delete(db_file); |
258 | 258 |
259 // Record some actions | 259 // Record some actions |
260 ActivityDatabase* activity_db = OpenDatabase(db_file); | 260 ActivityDatabase* activity_db = OpenDatabase(db_file); |
261 activity_db->SetBatchModeForTesting(true); | 261 activity_db->SetBatchModeForTesting(true); |
262 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); | 262 scoped_refptr<Action> action = CreateAction(base::Time::Now(), "brewster"); |
263 Record(activity_db, action); | 263 Record(activity_db, action); |
264 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); | 264 ASSERT_EQ(0, CountActions(&activity_db->db_, "brewster")); |
265 | 265 |
266 // Request an immediate database flush. | 266 // Request an immediate database flush. |
267 activity_db->AdviseFlush(ActivityDatabase::kFlushImmediately); | 267 activity_db->AdviseFlush(ActivityDatabase::kFlushImmediately); |
268 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); | 268 ASSERT_EQ(1, CountActions(&activity_db->db_, "brewster")); |
269 | 269 |
270 activity_db->Close(); | 270 activity_db->Close(); |
271 } | 271 } |
272 | 272 |
273 // Check that nothing explodes if the DB isn't initialized. | 273 // Check that nothing explodes if the DB isn't initialized. |
274 TEST_F(ActivityDatabaseTest, InitFailure) { | 274 TEST_F(ActivityDatabaseTest, InitFailure) { |
275 base::ScopedTempDir temp_dir; | 275 base::ScopedTempDir temp_dir; |
276 base::FilePath db_file; | 276 base::FilePath db_file; |
277 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 277 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
278 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 278 db_file = temp_dir.GetPath().AppendASCII("ActivityRecord.db"); |
279 sql::Connection::Delete(db_file); | 279 sql::Connection::Delete(db_file); |
280 | 280 |
281 ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy(); | 281 ActivityDatabaseTestPolicy* delegate = new ActivityDatabaseTestPolicy(); |
282 ActivityDatabase* activity_db = new ActivityDatabase(delegate); | 282 ActivityDatabase* activity_db = new ActivityDatabase(delegate); |
283 scoped_refptr<Action> action = new Action( | 283 scoped_refptr<Action> action = new Action( |
284 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"); | 284 "punky", base::Time::Now(), Action::ACTION_API_CALL, "brewster"); |
285 action->mutable_args()->AppendString("woof"); | 285 action->mutable_args()->AppendString("woof"); |
286 delegate->Record(activity_db, action); | 286 delegate->Record(activity_db, action); |
287 activity_db->Close(); | 287 activity_db->Close(); |
288 } | 288 } |
289 | 289 |
290 } // namespace extensions | 290 } // namespace extensions |
OLD | NEW |