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 "base/files/file_util.h" | 22 #include "base/files/file_util.h" |
23 #include "base/files/scoped_temp_dir.h" | 23 #include "base/files/scoped_temp_dir.h" |
| 24 #include "base/location.h" |
24 #include "base/message_loop/message_loop.h" | 25 #include "base/message_loop/message_loop.h" |
| 26 #include "base/single_thread_task_runner.h" |
25 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
| 28 #include "base/thread_task_runner_handle.h" |
26 #include "components/history/core/browser/history_database_params.h" | 29 #include "components/history/core/browser/history_database_params.h" |
27 #include "components/history/core/browser/history_db_task.h" | 30 #include "components/history/core/browser/history_db_task.h" |
28 #include "components/history/core/test/database_test_utils.h" | 31 #include "components/history/core/test/database_test_utils.h" |
29 #include "components/history/core/test/test_history_database.h" | 32 #include "components/history/core/test/test_history_database.h" |
30 #include "sync/api/attachments/attachment_id.h" | 33 #include "sync/api/attachments/attachment_id.h" |
31 #include "sync/api/fake_sync_change_processor.h" | 34 #include "sync/api/fake_sync_change_processor.h" |
32 #include "sync/api/sync_change.h" | 35 #include "sync/api/sync_change.h" |
33 #include "sync/api/sync_change_processor.h" | 36 #include "sync/api/sync_change_processor.h" |
34 #include "sync/api/sync_change_processor_wrapper_for_test.h" | 37 #include "sync/api/sync_change_processor_wrapper_for_test.h" |
35 #include "sync/api/sync_error.h" | 38 #include "sync/api/sync_error.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 ADD_FAILURE(); | 71 ADD_FAILURE(); |
69 } | 72 } |
70 } | 73 } |
71 | 74 |
72 void TearDown() override { | 75 void TearDown() override { |
73 if (history_service_) | 76 if (history_service_) |
74 CleanupHistoryService(); | 77 CleanupHistoryService(); |
75 | 78 |
76 // Make sure we don't have any event pending that could disrupt the next | 79 // Make sure we don't have any event pending that could disrupt the next |
77 // test. | 80 // test. |
78 base::MessageLoop::current()->PostTask(FROM_HERE, | 81 base::ThreadTaskRunnerHandle::Get()->PostTask( |
79 base::MessageLoop::QuitClosure()); | 82 FROM_HERE, base::MessageLoop::QuitClosure()); |
80 base::MessageLoop::current()->Run(); | 83 base::MessageLoop::current()->Run(); |
81 } | 84 } |
82 | 85 |
83 void CleanupHistoryService() { | 86 void CleanupHistoryService() { |
84 DCHECK(history_service_); | 87 DCHECK(history_service_); |
85 | 88 |
86 history_service_->ClearCachedDataForContextID(0); | 89 history_service_->ClearCachedDataForContextID(0); |
87 history_service_->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure()); | 90 history_service_->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure()); |
88 history_service_->Cleanup(); | 91 history_service_->Cleanup(); |
89 history_service_.reset(); | 92 history_service_.reset(); |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 void CheckDirectiveProcessingResult( | 674 void CheckDirectiveProcessingResult( |
672 base::Time timeout, | 675 base::Time timeout, |
673 const syncer::FakeSyncChangeProcessor* change_processor, | 676 const syncer::FakeSyncChangeProcessor* change_processor, |
674 uint32 num_changes) { | 677 uint32 num_changes) { |
675 if (base::Time::Now() > timeout || | 678 if (base::Time::Now() > timeout || |
676 change_processor->changes().size() >= num_changes) { | 679 change_processor->changes().size() >= num_changes) { |
677 return; | 680 return; |
678 } | 681 } |
679 | 682 |
680 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 683 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
681 base::MessageLoop::current()->PostTask( | 684 base::ThreadTaskRunnerHandle::Get()->PostTask( |
682 FROM_HERE, | 685 FROM_HERE, base::Bind(&CheckDirectiveProcessingResult, timeout, |
683 base::Bind(&CheckDirectiveProcessingResult, timeout, | 686 change_processor, num_changes)); |
684 change_processor, num_changes)); | |
685 } | 687 } |
686 | 688 |
687 // Create a delete directive for a few specific history entries, | 689 // Create a delete directive for a few specific history entries, |
688 // including ones that don't exist. The expected entries should be | 690 // including ones that don't exist. The expected entries should be |
689 // deleted. | 691 // deleted. |
690 TEST_F(HistoryServiceTest, ProcessGlobalIdDeleteDirective) { | 692 TEST_F(HistoryServiceTest, ProcessGlobalIdDeleteDirective) { |
691 ASSERT_TRUE(history_service_.get()); | 693 ASSERT_TRUE(history_service_.get()); |
692 const GURL test_url("http://www.google.com/"); | 694 const GURL test_url("http://www.google.com/"); |
693 for (int64 i = 1; i <= 20; i++) { | 695 for (int64 i = 1; i <= 20; i++) { |
694 base::Time t = | 696 base::Time t = |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 directives, | 743 directives, |
742 scoped_ptr<syncer::SyncChangeProcessor>( | 744 scoped_ptr<syncer::SyncChangeProcessor>( |
743 new syncer::SyncChangeProcessorWrapperForTest( | 745 new syncer::SyncChangeProcessorWrapperForTest( |
744 &change_processor)), | 746 &change_processor)), |
745 scoped_ptr<syncer::SyncErrorFactory>()) | 747 scoped_ptr<syncer::SyncErrorFactory>()) |
746 .error() | 748 .error() |
747 .IsSet()); | 749 .IsSet()); |
748 | 750 |
749 // Inject a task to check status and keep message loop filled before directive | 751 // Inject a task to check status and keep message loop filled before directive |
750 // processing finishes. | 752 // processing finishes. |
751 base::MessageLoop::current()->PostTask( | 753 base::ThreadTaskRunnerHandle::Get()->PostTask( |
752 FROM_HERE, | 754 FROM_HERE, |
753 base::Bind(&CheckDirectiveProcessingResult, | 755 base::Bind(&CheckDirectiveProcessingResult, |
754 base::Time::Now() + base::TimeDelta::FromSeconds(10), | 756 base::Time::Now() + base::TimeDelta::FromSeconds(10), |
755 &change_processor, 2)); | 757 &change_processor, 2)); |
756 base::MessageLoop::current()->RunUntilIdle(); | 758 base::MessageLoop::current()->RunUntilIdle(); |
757 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 759 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
758 ASSERT_EQ(5, query_url_row_.visit_count()); | 760 ASSERT_EQ(5, query_url_row_.visit_count()); |
759 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), | 761 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), |
760 query_url_visits_[0].visit_time); | 762 query_url_visits_[0].visit_time); |
761 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2), | 763 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 directives, | 828 directives, |
827 scoped_ptr<syncer::SyncChangeProcessor>( | 829 scoped_ptr<syncer::SyncChangeProcessor>( |
828 new syncer::SyncChangeProcessorWrapperForTest( | 830 new syncer::SyncChangeProcessorWrapperForTest( |
829 &change_processor)), | 831 &change_processor)), |
830 scoped_ptr<syncer::SyncErrorFactory>()) | 832 scoped_ptr<syncer::SyncErrorFactory>()) |
831 .error() | 833 .error() |
832 .IsSet()); | 834 .IsSet()); |
833 | 835 |
834 // Inject a task to check status and keep message loop filled before | 836 // Inject a task to check status and keep message loop filled before |
835 // directive processing finishes. | 837 // directive processing finishes. |
836 base::MessageLoop::current()->PostTask( | 838 base::ThreadTaskRunnerHandle::Get()->PostTask( |
837 FROM_HERE, | 839 FROM_HERE, |
838 base::Bind(&CheckDirectiveProcessingResult, | 840 base::Bind(&CheckDirectiveProcessingResult, |
839 base::Time::Now() + base::TimeDelta::FromSeconds(10), | 841 base::Time::Now() + base::TimeDelta::FromSeconds(10), |
840 &change_processor, 2)); | 842 &change_processor, 2)); |
841 base::MessageLoop::current()->RunUntilIdle(); | 843 base::MessageLoop::current()->RunUntilIdle(); |
842 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 844 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
843 ASSERT_EQ(3, query_url_row_.visit_count()); | 845 ASSERT_EQ(3, query_url_row_.visit_count()); |
844 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), | 846 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), |
845 query_url_visits_[0].visit_time); | 847 query_url_visits_[0].visit_time); |
846 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6), | 848 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6), |
847 query_url_visits_[1].visit_time); | 849 query_url_visits_[1].visit_time); |
848 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7), | 850 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7), |
849 query_url_visits_[2].visit_time); | 851 query_url_visits_[2].visit_time); |
850 | 852 |
851 // Expect two sync changes for deleting processed directives. | 853 // Expect two sync changes for deleting processed directives. |
852 const syncer::SyncChangeList& sync_changes = change_processor.changes(); | 854 const syncer::SyncChangeList& sync_changes = change_processor.changes(); |
853 ASSERT_EQ(2u, sync_changes.size()); | 855 ASSERT_EQ(2u, sync_changes.size()); |
854 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); | 856 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); |
855 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); | 857 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); |
856 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); | 858 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); |
857 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); | 859 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); |
858 } | 860 } |
859 | 861 |
860 } // namespace history | 862 } // namespace history |
OLD | NEW |