| 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 <string> | 5 #include <string> |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 99 |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 | 102 |
| 103 // Check that the database is initialized properly. | 103 // Check that the database is initialized properly. |
| 104 TEST_F(ActivityDatabaseTest, Init) { | 104 TEST_F(ActivityDatabaseTest, Init) { |
| 105 base::ScopedTempDir temp_dir; | 105 base::ScopedTempDir temp_dir; |
| 106 base::FilePath db_file; | 106 base::FilePath db_file; |
| 107 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 107 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 108 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); | 108 db_file = temp_dir.path().AppendASCII("ActivityInit.db"); |
| 109 base::Delete(db_file, false); | 109 base::DeleteFile(db_file, false); |
| 110 | 110 |
| 111 ActivityDatabase* activity_db = OpenDatabase(db_file); | 111 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 112 activity_db->Close(); | 112 activity_db->Close(); |
| 113 | 113 |
| 114 sql::Connection db; | 114 sql::Connection db; |
| 115 ASSERT_TRUE(db.Open(db_file)); | 115 ASSERT_TRUE(db.Open(db_file)); |
| 116 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName)); | 116 ASSERT_TRUE(db.DoesTableExist(DOMAction::kTableName)); |
| 117 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); | 117 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
| 118 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); | 118 ASSERT_TRUE(db.DoesTableExist(BlockedAction::kTableName)); |
| 119 db.Close(); | 119 db.Close(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Check that API actions are recorded in the db. | 122 // Check that API actions are recorded in the db. |
| 123 TEST_F(ActivityDatabaseTest, RecordAPIAction) { | 123 TEST_F(ActivityDatabaseTest, RecordAPIAction) { |
| 124 base::ScopedTempDir temp_dir; | 124 base::ScopedTempDir temp_dir; |
| 125 base::FilePath db_file; | 125 base::FilePath db_file; |
| 126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 126 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 127 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 128 base::Delete(db_file, false); | 128 base::DeleteFile(db_file, false); |
| 129 | 129 |
| 130 ActivityDatabase* activity_db = OpenDatabase(db_file); | 130 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 131 activity_db->SetBatchModeForTesting(false); | 131 activity_db->SetBatchModeForTesting(false); |
| 132 scoped_refptr<APIAction> action = new APIAction( | 132 scoped_refptr<APIAction> action = new APIAction( |
| 133 "punky", | 133 "punky", |
| 134 base::Time::Now(), | 134 base::Time::Now(), |
| 135 APIAction::CALL, | 135 APIAction::CALL, |
| 136 "brewster", | 136 "brewster", |
| 137 "woof", | 137 "woof", |
| 138 "extra"); | 138 "extra"); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 ASSERT_EQ("brewster", statement.ColumnString(3)); | 152 ASSERT_EQ("brewster", statement.ColumnString(3)); |
| 153 ASSERT_EQ("woof", statement.ColumnString(4)); | 153 ASSERT_EQ("woof", statement.ColumnString(4)); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // Check that DOM actions are recorded in the db. | 156 // Check that DOM actions are recorded in the db. |
| 157 TEST_F(ActivityDatabaseTest, RecordDOMAction) { | 157 TEST_F(ActivityDatabaseTest, RecordDOMAction) { |
| 158 base::ScopedTempDir temp_dir; | 158 base::ScopedTempDir temp_dir; |
| 159 base::FilePath db_file; | 159 base::FilePath db_file; |
| 160 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 160 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 161 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 161 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 162 base::Delete(db_file, false); | 162 base::DeleteFile(db_file, false); |
| 163 | 163 |
| 164 ActivityDatabase* activity_db = OpenDatabase(db_file); | 164 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 165 activity_db->SetBatchModeForTesting(false); | 165 activity_db->SetBatchModeForTesting(false); |
| 166 scoped_refptr<DOMAction> action = new DOMAction( | 166 scoped_refptr<DOMAction> action = new DOMAction( |
| 167 "punky", | 167 "punky", |
| 168 base::Time::Now(), | 168 base::Time::Now(), |
| 169 DomActionType::MODIFIED, | 169 DomActionType::MODIFIED, |
| 170 GURL("http://www.google.com/foo?bar"), | 170 GURL("http://www.google.com/foo?bar"), |
| 171 string16(), | 171 string16(), |
| 172 "lets", | 172 "lets", |
| (...skipping 22 matching lines...) Expand all Loading... |
| 195 ASSERT_EQ("vamoose", statement.ColumnString(7)); | 195 ASSERT_EQ("vamoose", statement.ColumnString(7)); |
| 196 ASSERT_EQ("extra", statement.ColumnString(8)); | 196 ASSERT_EQ("extra", statement.ColumnString(8)); |
| 197 } | 197 } |
| 198 | 198 |
| 199 // Check that blocked actions are recorded in the db. | 199 // Check that blocked actions are recorded in the db. |
| 200 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { | 200 TEST_F(ActivityDatabaseTest, RecordBlockedAction) { |
| 201 base::ScopedTempDir temp_dir; | 201 base::ScopedTempDir temp_dir; |
| 202 base::FilePath db_file; | 202 base::FilePath db_file; |
| 203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 203 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 204 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 204 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 205 base::Delete(db_file, false); | 205 base::DeleteFile(db_file, false); |
| 206 | 206 |
| 207 ActivityDatabase* activity_db = OpenDatabase(db_file); | 207 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 208 scoped_refptr<BlockedAction> action = new BlockedAction( | 208 scoped_refptr<BlockedAction> action = new BlockedAction( |
| 209 "punky", | 209 "punky", |
| 210 base::Time::Now(), | 210 base::Time::Now(), |
| 211 "do.evilThings", | 211 "do.evilThings", |
| 212 "1, 2", | 212 "1, 2", |
| 213 BlockedAction::ACCESS_DENIED, | 213 BlockedAction::ACCESS_DENIED, |
| 214 "extra"); | 214 "extra"); |
| 215 activity_db->RecordAction(action); | 215 activity_db->RecordAction(action); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 229 ASSERT_EQ(1, statement.ColumnInt(4)); | 229 ASSERT_EQ(1, statement.ColumnInt(4)); |
| 230 ASSERT_EQ("extra", statement.ColumnString(5)); | 230 ASSERT_EQ("extra", statement.ColumnString(5)); |
| 231 } | 231 } |
| 232 | 232 |
| 233 // Check that we can read back recent actions in the db. | 233 // Check that we can read back recent actions in the db. |
| 234 TEST_F(ActivityDatabaseTest, GetTodaysActions) { | 234 TEST_F(ActivityDatabaseTest, GetTodaysActions) { |
| 235 base::ScopedTempDir temp_dir; | 235 base::ScopedTempDir temp_dir; |
| 236 base::FilePath db_file; | 236 base::FilePath db_file; |
| 237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 238 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 238 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 239 base::Delete(db_file, false); | 239 base::DeleteFile(db_file, false); |
| 240 | 240 |
| 241 // Use a mock clock to ensure that events are not recorded on the wrong day | 241 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 242 // when the test is run close to local midnight. | 242 // when the test is run close to local midnight. |
| 243 base::SimpleTestClock mock_clock; | 243 base::SimpleTestClock mock_clock; |
| 244 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 244 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 245 base::TimeDelta::FromHours(12)); | 245 base::TimeDelta::FromHours(12)); |
| 246 | 246 |
| 247 // Record some actions | 247 // Record some actions |
| 248 ActivityDatabase* activity_db = OpenDatabase(db_file); | 248 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 249 scoped_refptr<APIAction> api_action = new APIAction( | 249 scoped_refptr<APIAction> api_action = new APIAction( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 | 287 |
| 288 activity_db->Close(); | 288 activity_db->Close(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 // Check that we can read back recent actions in the db. | 291 // Check that we can read back recent actions in the db. |
| 292 TEST_F(ActivityDatabaseTest, GetOlderActions) { | 292 TEST_F(ActivityDatabaseTest, GetOlderActions) { |
| 293 base::ScopedTempDir temp_dir; | 293 base::ScopedTempDir temp_dir; |
| 294 base::FilePath db_file; | 294 base::FilePath db_file; |
| 295 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 295 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 296 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 296 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 297 base::Delete(db_file, false); | 297 base::DeleteFile(db_file, false); |
| 298 | 298 |
| 299 // Use a mock clock to ensure that events are not recorded on the wrong day | 299 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 300 // when the test is run close to local midnight. | 300 // when the test is run close to local midnight. |
| 301 base::SimpleTestClock mock_clock; | 301 base::SimpleTestClock mock_clock; |
| 302 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 302 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 303 base::TimeDelta::FromHours(12)); | 303 base::TimeDelta::FromHours(12)); |
| 304 | 304 |
| 305 // Record some actions | 305 // Record some actions |
| 306 ActivityDatabase* activity_db = OpenDatabase(db_file); | 306 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 307 scoped_refptr<APIAction> api_action = new APIAction( | 307 scoped_refptr<APIAction> api_action = new APIAction( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); | 355 ASSERT_EQ(api_print, actions->at(1)->PrintForDebug()); |
| 356 | 356 |
| 357 activity_db->Close(); | 357 activity_db->Close(); |
| 358 } | 358 } |
| 359 | 359 |
| 360 TEST_F(ActivityDatabaseTest, BatchModeOff) { | 360 TEST_F(ActivityDatabaseTest, BatchModeOff) { |
| 361 base::ScopedTempDir temp_dir; | 361 base::ScopedTempDir temp_dir; |
| 362 base::FilePath db_file; | 362 base::FilePath db_file; |
| 363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 364 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 364 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 365 base::Delete(db_file, false); | 365 base::DeleteFile(db_file, false); |
| 366 | 366 |
| 367 // Use a mock clock to ensure that events are not recorded on the wrong day | 367 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 368 // when the test is run close to local midnight. | 368 // when the test is run close to local midnight. |
| 369 base::SimpleTestClock mock_clock; | 369 base::SimpleTestClock mock_clock; |
| 370 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 370 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 371 base::TimeDelta::FromHours(12)); | 371 base::TimeDelta::FromHours(12)); |
| 372 | 372 |
| 373 // Record some actions | 373 // Record some actions |
| 374 ActivityDatabase* activity_db = OpenDatabase(db_file); | 374 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 375 activity_db->SetBatchModeForTesting(false); | 375 activity_db->SetBatchModeForTesting(false); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 387 activity_db->GetActions("punky", 0); | 387 activity_db->GetActions("punky", 0); |
| 388 ASSERT_EQ(1, static_cast<int>(actions->size())); | 388 ASSERT_EQ(1, static_cast<int>(actions->size())); |
| 389 activity_db->Close(); | 389 activity_db->Close(); |
| 390 } | 390 } |
| 391 | 391 |
| 392 TEST_F(ActivityDatabaseTest, BatchModeOn) { | 392 TEST_F(ActivityDatabaseTest, BatchModeOn) { |
| 393 base::ScopedTempDir temp_dir; | 393 base::ScopedTempDir temp_dir; |
| 394 base::FilePath db_file; | 394 base::FilePath db_file; |
| 395 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 395 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 396 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 396 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 397 base::Delete(db_file, false); | 397 base::DeleteFile(db_file, false); |
| 398 | 398 |
| 399 // Use a mock clock to set the time, and a special timer to control the | 399 // Use a mock clock to set the time, and a special timer to control the |
| 400 // timing and skip ahead in time. | 400 // timing and skip ahead in time. |
| 401 base::SimpleTestClock mock_clock; | 401 base::SimpleTestClock mock_clock; |
| 402 mock_clock.SetNow(base::Time::Now().LocalMidnight() + | 402 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 403 base::TimeDelta::FromHours(11)); | 403 base::TimeDelta::FromHours(11)); |
| 404 | 404 |
| 405 // Record some actions | 405 // Record some actions |
| 406 ActivityDatabase* activity_db = OpenDatabase(db_file); | 406 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 407 activity_db->SetBatchModeForTesting(true); | 407 activity_db->SetBatchModeForTesting(true); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 429 | 429 |
| 430 activity_db->Close(); | 430 activity_db->Close(); |
| 431 } | 431 } |
| 432 | 432 |
| 433 // Check that nothing explodes if the DB isn't initialized. | 433 // Check that nothing explodes if the DB isn't initialized. |
| 434 TEST_F(ActivityDatabaseTest, InitFailure) { | 434 TEST_F(ActivityDatabaseTest, InitFailure) { |
| 435 base::ScopedTempDir temp_dir; | 435 base::ScopedTempDir temp_dir; |
| 436 base::FilePath db_file; | 436 base::FilePath db_file; |
| 437 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 437 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 438 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); | 438 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 439 base::Delete(db_file, false); | 439 base::DeleteFile(db_file, false); |
| 440 | 440 |
| 441 ActivityDatabase* activity_db = | 441 ActivityDatabase* activity_db = |
| 442 new ActivityDatabase(new ActivityDatabaseTestPolicy()); | 442 new ActivityDatabase(new ActivityDatabaseTestPolicy()); |
| 443 scoped_refptr<APIAction> action = new APIAction( | 443 scoped_refptr<APIAction> action = new APIAction( |
| 444 "punky", | 444 "punky", |
| 445 base::Time::Now(), | 445 base::Time::Now(), |
| 446 APIAction::CALL, | 446 APIAction::CALL, |
| 447 "brewster", | 447 "brewster", |
| 448 "woooof", | 448 "woooof", |
| 449 "extra"); | 449 "extra"); |
| 450 activity_db->RecordAction(action); | 450 activity_db->RecordAction(action); |
| 451 activity_db->Close(); | 451 activity_db->Close(); |
| 452 } | 452 } |
| 453 | 453 |
| 454 } // namespace extensions | 454 } // namespace extensions |
| 455 | 455 |
| OLD | NEW |