| 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 // History unit tests come in two flavors: | 5 // History unit tests come in two flavors: |
| 6 // | 6 // |
| 7 // 1. The more complicated style is that the unit test creates a full history | 7 // 1. The more complicated style is that the unit test creates a full history |
| 8 // service. This spawns a background thread for the history backend, and | 8 // service. This spawns a background thread for the history backend, and |
| 9 // all communication is asynchronous. This is useful for testing more | 9 // all communication is asynchronous. This is useful for testing more |
| 10 // complicated things or end-to-end behavior. | 10 // complicated things or end-to-end behavior. |
| 11 // | 11 // |
| 12 // 2. The simpler style is to create a history backend on this thread and | 12 // 2. The simpler style is to create a history backend on this thread and |
| 13 // access it directly without a HistoryService object. This is much simpler | 13 // access it directly without a HistoryService object. This is much simpler |
| 14 // because communication is synchronous. Generally, sets should go through | 14 // because communication is synchronous. Generally, sets should go through |
| 15 // the history backend (since there is a lot of logic) but gets can come | 15 // the history backend (since there is a lot of logic) but gets can come |
| 16 // directly from the HistoryDatabase. This is because the backend generally | 16 // directly from the HistoryDatabase. This is because the backend generally |
| 17 // has no logic in the getter except threading stuff, which we don't want | 17 // has no logic in the getter except threading stuff, which we don't want |
| 18 // to run. | 18 // to run. |
| 19 | 19 |
| 20 #include "components/history/core/browser/history_service.h" | 20 #include "components/history/core/browser/history_service.h" |
| 21 | 21 |
| 22 #include <stdint.h> |
| 23 |
| 22 #include "base/files/file_util.h" | 24 #include "base/files/file_util.h" |
| 23 #include "base/files/scoped_temp_dir.h" | 25 #include "base/files/scoped_temp_dir.h" |
| 24 #include "base/location.h" | 26 #include "base/location.h" |
| 27 #include "base/macros.h" |
| 25 #include "base/message_loop/message_loop.h" | 28 #include "base/message_loop/message_loop.h" |
| 26 #include "base/single_thread_task_runner.h" | 29 #include "base/single_thread_task_runner.h" |
| 27 #include "base/strings/utf_string_conversions.h" | 30 #include "base/strings/utf_string_conversions.h" |
| 28 #include "base/thread_task_runner_handle.h" | 31 #include "base/thread_task_runner_handle.h" |
| 29 #include "components/history/core/browser/history_database_params.h" | 32 #include "components/history/core/browser/history_database_params.h" |
| 30 #include "components/history/core/browser/history_db_task.h" | 33 #include "components/history/core/browser/history_db_task.h" |
| 31 #include "components/history/core/test/database_test_utils.h" | 34 #include "components/history/core/test/database_test_utils.h" |
| 32 #include "components/history/core/test/test_history_database.h" | 35 #include "components/history/core/test/test_history_database.h" |
| 33 #include "sync/api/attachments/attachment_id.h" | 36 #include "sync/api/attachments/attachment_id.h" |
| 34 #include "sync/api/fake_sync_change_processor.h" | 37 #include "sync/api/fake_sync_change_processor.h" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 history_service_->AddPage( | 216 history_service_->AddPage( |
| 214 first_redirects.back(), base::Time::Now(), | 217 first_redirects.back(), base::Time::Now(), |
| 215 reinterpret_cast<ContextID>(1), 0, GURL(), first_redirects, | 218 reinterpret_cast<ContextID>(1), 0, GURL(), first_redirects, |
| 216 ui::PAGE_TRANSITION_LINK, history::SOURCE_BROWSED, true); | 219 ui::PAGE_TRANSITION_LINK, history::SOURCE_BROWSED, true); |
| 217 | 220 |
| 218 // The first page should be added once with a link visit type (because we set | 221 // The first page should be added once with a link visit type (because we set |
| 219 // LINK when we added the original URL, and a referrer of nowhere (0). | 222 // LINK when we added the original URL, and a referrer of nowhere (0). |
| 220 EXPECT_TRUE(QueryURL(history_service_.get(), first_redirects[0])); | 223 EXPECT_TRUE(QueryURL(history_service_.get(), first_redirects[0])); |
| 221 EXPECT_EQ(1, query_url_row_.visit_count()); | 224 EXPECT_EQ(1, query_url_row_.visit_count()); |
| 222 ASSERT_EQ(1U, query_url_visits_.size()); | 225 ASSERT_EQ(1U, query_url_visits_.size()); |
| 223 int64 first_visit = query_url_visits_[0].visit_id; | 226 int64_t first_visit = query_url_visits_[0].visit_id; |
| 224 EXPECT_EQ(ui::PAGE_TRANSITION_LINK | ui::PAGE_TRANSITION_CHAIN_START, | 227 EXPECT_EQ(ui::PAGE_TRANSITION_LINK | ui::PAGE_TRANSITION_CHAIN_START, |
| 225 query_url_visits_[0].transition); | 228 query_url_visits_[0].transition); |
| 226 EXPECT_EQ(0, query_url_visits_[0].referring_visit); // No referrer. | 229 EXPECT_EQ(0, query_url_visits_[0].referring_visit); // No referrer. |
| 227 | 230 |
| 228 // The second page should be a server redirect type with a referrer of the | 231 // The second page should be a server redirect type with a referrer of the |
| 229 // first page. | 232 // first page. |
| 230 EXPECT_TRUE(QueryURL(history_service_.get(), first_redirects[1])); | 233 EXPECT_TRUE(QueryURL(history_service_.get(), first_redirects[1])); |
| 231 EXPECT_EQ(1, query_url_row_.visit_count()); | 234 EXPECT_EQ(1, query_url_row_.visit_count()); |
| 232 ASSERT_EQ(1U, query_url_visits_.size()); | 235 ASSERT_EQ(1U, query_url_visits_.size()); |
| 233 int64 second_visit = query_url_visits_[0].visit_id; | 236 int64_t second_visit = query_url_visits_[0].visit_id; |
| 234 EXPECT_EQ(ui::PAGE_TRANSITION_SERVER_REDIRECT | ui::PAGE_TRANSITION_CHAIN_END, | 237 EXPECT_EQ(ui::PAGE_TRANSITION_SERVER_REDIRECT | ui::PAGE_TRANSITION_CHAIN_END, |
| 235 query_url_visits_[0].transition); | 238 query_url_visits_[0].transition); |
| 236 EXPECT_EQ(first_visit, query_url_visits_[0].referring_visit); | 239 EXPECT_EQ(first_visit, query_url_visits_[0].referring_visit); |
| 237 | 240 |
| 238 // Check that the redirect finding function successfully reports it. | 241 // Check that the redirect finding function successfully reports it. |
| 239 saved_redirects_.clear(); | 242 saved_redirects_.clear(); |
| 240 QueryRedirectsFrom(history_service_.get(), first_redirects[0]); | 243 QueryRedirectsFrom(history_service_.get(), first_redirects[0]); |
| 241 ASSERT_EQ(1U, saved_redirects_.size()); | 244 ASSERT_EQ(1U, saved_redirects_.size()); |
| 242 EXPECT_EQ(first_redirects[1], saved_redirects_[0]); | 245 EXPECT_EQ(first_redirects[1], saved_redirects_[0]); |
| 243 | 246 |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 } | 625 } |
| 623 | 626 |
| 624 // Create a local delete directive and process it while sync is | 627 // Create a local delete directive and process it while sync is |
| 625 // online, and then when offline. The delete directive should be sent to sync, | 628 // online, and then when offline. The delete directive should be sent to sync, |
| 626 // no error should be returned for the first time, and an error should be | 629 // no error should be returned for the first time, and an error should be |
| 627 // returned for the second time. | 630 // returned for the second time. |
| 628 TEST_F(HistoryServiceTest, ProcessLocalDeleteDirectiveSyncOnline) { | 631 TEST_F(HistoryServiceTest, ProcessLocalDeleteDirectiveSyncOnline) { |
| 629 ASSERT_TRUE(history_service_.get()); | 632 ASSERT_TRUE(history_service_.get()); |
| 630 | 633 |
| 631 const GURL test_url("http://www.google.com/"); | 634 const GURL test_url("http://www.google.com/"); |
| 632 for (int64 i = 1; i <= 10; ++i) { | 635 for (int64_t i = 1; i <= 10; ++i) { |
| 633 base::Time t = | 636 base::Time t = |
| 634 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); | 637 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); |
| 635 history_service_->AddPage(test_url, t, NULL, 0, GURL(), | 638 history_service_->AddPage(test_url, t, NULL, 0, GURL(), |
| 636 history::RedirectList(), | 639 history::RedirectList(), |
| 637 ui::PAGE_TRANSITION_LINK, | 640 ui::PAGE_TRANSITION_LINK, |
| 638 history::SOURCE_BROWSED, false); | 641 history::SOURCE_BROWSED, false); |
| 639 } | 642 } |
| 640 | 643 |
| 641 sync_pb::HistoryDeleteDirectiveSpecifics delete_directive; | 644 sync_pb::HistoryDeleteDirectiveSpecifics delete_directive; |
| 642 sync_pb::GlobalIdDirective* global_id_directive = | 645 sync_pb::GlobalIdDirective* global_id_directive = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 668 EXPECT_TRUE(err.IsSet()); | 671 EXPECT_TRUE(err.IsSet()); |
| 669 EXPECT_EQ(1u, change_processor.changes().size()); | 672 EXPECT_EQ(1u, change_processor.changes().size()); |
| 670 } | 673 } |
| 671 | 674 |
| 672 // Closure function that runs periodically to check result of delete directive | 675 // Closure function that runs periodically to check result of delete directive |
| 673 // processing. Stop when timeout or processing ends indicated by the creation | 676 // processing. Stop when timeout or processing ends indicated by the creation |
| 674 // of sync changes. | 677 // of sync changes. |
| 675 void CheckDirectiveProcessingResult( | 678 void CheckDirectiveProcessingResult( |
| 676 base::Time timeout, | 679 base::Time timeout, |
| 677 const syncer::FakeSyncChangeProcessor* change_processor, | 680 const syncer::FakeSyncChangeProcessor* change_processor, |
| 678 uint32 num_changes) { | 681 uint32_t num_changes) { |
| 679 if (base::Time::Now() > timeout || | 682 if (base::Time::Now() > timeout || |
| 680 change_processor->changes().size() >= num_changes) { | 683 change_processor->changes().size() >= num_changes) { |
| 681 return; | 684 return; |
| 682 } | 685 } |
| 683 | 686 |
| 684 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 687 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 685 base::ThreadTaskRunnerHandle::Get()->PostTask( | 688 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 686 FROM_HERE, base::Bind(&CheckDirectiveProcessingResult, timeout, | 689 FROM_HERE, base::Bind(&CheckDirectiveProcessingResult, timeout, |
| 687 change_processor, num_changes)); | 690 change_processor, num_changes)); |
| 688 } | 691 } |
| 689 | 692 |
| 690 // Create a delete directive for a few specific history entries, | 693 // Create a delete directive for a few specific history entries, |
| 691 // including ones that don't exist. The expected entries should be | 694 // including ones that don't exist. The expected entries should be |
| 692 // deleted. | 695 // deleted. |
| 693 TEST_F(HistoryServiceTest, ProcessGlobalIdDeleteDirective) { | 696 TEST_F(HistoryServiceTest, ProcessGlobalIdDeleteDirective) { |
| 694 ASSERT_TRUE(history_service_.get()); | 697 ASSERT_TRUE(history_service_.get()); |
| 695 const GURL test_url("http://www.google.com/"); | 698 const GURL test_url("http://www.google.com/"); |
| 696 for (int64 i = 1; i <= 20; i++) { | 699 for (int64_t i = 1; i <= 20; i++) { |
| 697 base::Time t = | 700 base::Time t = |
| 698 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); | 701 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); |
| 699 history_service_->AddPage(test_url, t, NULL, 0, GURL(), | 702 history_service_->AddPage(test_url, t, NULL, 0, GURL(), |
| 700 history::RedirectList(), | 703 history::RedirectList(), |
| 701 ui::PAGE_TRANSITION_LINK, | 704 ui::PAGE_TRANSITION_LINK, |
| 702 history::SOURCE_BROWSED, false); | 705 history::SOURCE_BROWSED, false); |
| 703 } | 706 } |
| 704 | 707 |
| 705 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 708 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
| 706 EXPECT_EQ(20, query_url_row_.visit_count()); | 709 EXPECT_EQ(20, query_url_row_.visit_count()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); | 780 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); |
| 778 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); | 781 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); |
| 779 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); | 782 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); |
| 780 } | 783 } |
| 781 | 784 |
| 782 // Create delete directives for time ranges. The expected entries should be | 785 // Create delete directives for time ranges. The expected entries should be |
| 783 // deleted. | 786 // deleted. |
| 784 TEST_F(HistoryServiceTest, ProcessTimeRangeDeleteDirective) { | 787 TEST_F(HistoryServiceTest, ProcessTimeRangeDeleteDirective) { |
| 785 ASSERT_TRUE(history_service_.get()); | 788 ASSERT_TRUE(history_service_.get()); |
| 786 const GURL test_url("http://www.google.com/"); | 789 const GURL test_url("http://www.google.com/"); |
| 787 for (int64 i = 1; i <= 10; ++i) { | 790 for (int64_t i = 1; i <= 10; ++i) { |
| 788 base::Time t = | 791 base::Time t = |
| 789 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); | 792 base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(i); |
| 790 history_service_->AddPage(test_url, t, NULL, 0, GURL(), | 793 history_service_->AddPage(test_url, t, NULL, 0, GURL(), |
| 791 history::RedirectList(), | 794 history::RedirectList(), |
| 792 ui::PAGE_TRANSITION_LINK, | 795 ui::PAGE_TRANSITION_LINK, |
| 793 history::SOURCE_BROWSED, false); | 796 history::SOURCE_BROWSED, false); |
| 794 } | 797 } |
| 795 | 798 |
| 796 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 799 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
| 797 EXPECT_EQ(10, query_url_row_.visit_count()); | 800 EXPECT_EQ(10, query_url_row_.visit_count()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 // Expect two sync changes for deleting processed directives. | 857 // Expect two sync changes for deleting processed directives. |
| 855 const syncer::SyncChangeList& sync_changes = change_processor.changes(); | 858 const syncer::SyncChangeList& sync_changes = change_processor.changes(); |
| 856 ASSERT_EQ(2u, sync_changes.size()); | 859 ASSERT_EQ(2u, sync_changes.size()); |
| 857 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); | 860 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); |
| 858 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); | 861 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); |
| 859 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); | 862 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); |
| 860 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); | 863 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); |
| 861 } | 864 } |
| 862 | 865 |
| 863 } // namespace history | 866 } // namespace history |
| OLD | NEW |