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" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
11 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
13 #include "chrome/browser/extensions/activity_log/activity_database.h" | 15 #include "chrome/browser/extensions/activity_log/activity_database.h" |
14 #include "chrome/browser/extensions/activity_log/api_actions.h" | 16 #include "chrome/browser/extensions/activity_log/api_actions.h" |
15 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | 17 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
16 #include "chrome/browser/extensions/activity_log/dom_actions.h" | 18 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/extensions/test_extension_system.h" | 20 #include "chrome/browser/extensions/test_extension_system.h" |
19 #include "chrome/common/chrome_constants.h" | 21 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 "punky", | 446 "punky", |
445 base::Time::Now(), | 447 base::Time::Now(), |
446 APIAction::CALL, | 448 APIAction::CALL, |
447 "brewster", | 449 "brewster", |
448 "woooof", | 450 "woooof", |
449 "extra"); | 451 "extra"); |
450 activity_db->RecordAction(action); | 452 activity_db->RecordAction(action); |
451 activity_db->Close(); | 453 activity_db->Close(); |
452 } | 454 } |
453 | 455 |
| 456 TEST_F(ActivityDatabaseTest, ConstructClearURLQuery) { |
| 457 std::string query; |
| 458 ActivityDatabase::ConstructRemoveURLQuery("", &query); |
| 459 ASSERT_EQ("UPDATE activitylog_urls SET url_tld='',url_title='',url_path=''", |
| 460 query); |
| 461 |
| 462 ActivityDatabase::ConstructRemoveURLQuery( |
| 463 "http://www.somewebsite.com", &query); |
| 464 ASSERT_EQ("UPDATE activitylog_urls SET url_tld='',url_title='',url_path='' " |
| 465 "WHERE url_tld='http://www.somewebsite.com'", query); |
| 466 } |
| 467 |
| 468 TEST_F(ActivityDatabaseTest, RemoveAllURLs) { |
| 469 base::ScopedTempDir temp_dir; |
| 470 base::FilePath db_file; |
| 471 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 472 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 473 base::Delete(db_file, false); |
| 474 |
| 475 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 476 // when the test is run close to local midnight. |
| 477 base::SimpleTestClock mock_clock; |
| 478 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 479 base::TimeDelta::FromHours(12)); |
| 480 |
| 481 // Record some actions |
| 482 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 483 activity_db->Init(db_file); |
| 484 activity_db->SetBatchModeForTesting(false); |
| 485 ASSERT_TRUE(activity_db->is_db_valid()); |
| 486 |
| 487 for (int i = 0; i < 3; ++i) { |
| 488 scoped_refptr<DOMAction> action = new DOMAction( |
| 489 "punky", |
| 490 base::Time::Now(), |
| 491 DomActionType::MODIFIED, |
| 492 GURL(base::StringPrintf("http://www.google%d.com/foo?bar", i)), |
| 493 base::ASCIIToUTF16("Title"), |
| 494 "lets", |
| 495 "vamoose", |
| 496 "extra"); |
| 497 activity_db->RecordAction(action); |
| 498 } |
| 499 // Clear all the urls. |
| 500 activity_db->RemoveAllURLs(); |
| 501 activity_db->Close(); |
| 502 |
| 503 sql::Connection db; |
| 504 ASSERT_TRUE(db.Open(db_file)); |
| 505 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
| 506 std::string sql_str = "SELECT * FROM " + |
| 507 std::string(DOMAction::kTableName); |
| 508 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 509 |
| 510 while (statement.Step()) { |
| 511 ASSERT_TRUE(statement.Succeeded()); |
| 512 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 513 ASSERT_EQ(DomActionType::MODIFIED, statement.ColumnInt(2)); |
| 514 // These fields should be set to empty string for all rows. |
| 515 ASSERT_EQ("", statement.ColumnString(3)); |
| 516 ASSERT_EQ("", statement.ColumnString(4)); |
| 517 ASSERT_EQ("", statement.ColumnString(5)); |
| 518 } |
| 519 } |
| 520 |
| 521 TEST_F(ActivityDatabaseTest, RemoveURL) { |
| 522 base::ScopedTempDir temp_dir; |
| 523 base::FilePath db_file; |
| 524 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 525 db_file = temp_dir.path().AppendASCII("ActivityRecord.db"); |
| 526 base::Delete(db_file, false); |
| 527 |
| 528 // Use a mock clock to ensure that events are not recorded on the wrong day |
| 529 // when the test is run close to local midnight. |
| 530 base::SimpleTestClock mock_clock; |
| 531 mock_clock.SetNow(base::Time::Now().LocalMidnight() + |
| 532 base::TimeDelta::FromHours(12)); |
| 533 |
| 534 // Record some actions |
| 535 ActivityDatabase* activity_db = OpenDatabase(db_file); |
| 536 activity_db->Init(db_file); |
| 537 activity_db->SetBatchModeForTesting(false); |
| 538 ASSERT_TRUE(activity_db->is_db_valid()); |
| 539 |
| 540 for (int i = 1; i < 4; ++i) { |
| 541 scoped_refptr<DOMAction> action = new DOMAction( |
| 542 "punky", |
| 543 base::Time::Now(), |
| 544 DomActionType::MODIFIED, |
| 545 GURL(base::StringPrintf("http://www.google%d.com/foo?bar", i)), |
| 546 base::ASCIIToUTF16("Title"), |
| 547 "lets", |
| 548 "vamoose", |
| 549 "extra"); |
| 550 activity_db->RecordAction(action); |
| 551 } |
| 552 activity_db->RemoveURL(GURL("http://www.google3.com/foo?")); |
| 553 activity_db->Close(); |
| 554 |
| 555 sql::Connection db; |
| 556 ASSERT_TRUE(db.Open(db_file)); |
| 557 ASSERT_TRUE(db.DoesTableExist(APIAction::kTableName)); |
| 558 std::string sql_str = "SELECT * FROM " + |
| 559 std::string(DOMAction::kTableName); |
| 560 sql::Statement statement(db.GetUniqueStatement(sql_str.c_str())); |
| 561 |
| 562 // These rows should still contain the URLs. |
| 563 for (int i = 1; i < 3; ++i) { |
| 564 ASSERT_TRUE(statement.Step()); |
| 565 ASSERT_TRUE(statement.Succeeded()); |
| 566 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 567 ASSERT_EQ(DomActionType::MODIFIED, statement.ColumnInt(2)); |
| 568 ASSERT_EQ(base::StringPrintf("http://www.google%d.com", i), |
| 569 statement.ColumnString(3)); |
| 570 ASSERT_EQ("Title", statement.ColumnString(5)); |
| 571 ASSERT_EQ("lets", statement.ColumnString(6)); |
| 572 ASSERT_EQ("vamoose", statement.ColumnString(7)); |
| 573 ASSERT_EQ("extra", statement.ColumnString(8)); |
| 574 } |
| 575 |
| 576 // This one should have the cleaned URLs fields. |
| 577 ASSERT_TRUE(statement.Step()); |
| 578 ASSERT_TRUE(statement.Succeeded()); |
| 579 ASSERT_EQ("punky", statement.ColumnString(0)); |
| 580 ASSERT_EQ(DomActionType::MODIFIED, statement.ColumnInt(2)); |
| 581 ASSERT_EQ("", statement.ColumnString(3)); |
| 582 ASSERT_EQ("", statement.ColumnString(4)); |
| 583 ASSERT_EQ("", statement.ColumnString(5)); |
| 584 ASSERT_EQ("lets", statement.ColumnString(6)); |
| 585 ASSERT_EQ("vamoose", statement.ColumnString(7)); |
| 586 ASSERT_EQ("extra", statement.ColumnString(8)); |
| 587 } |
| 588 |
454 } // namespace extensions | 589 } // namespace extensions |
455 | 590 |
OLD | NEW |