| 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 "chrome/browser/history/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 virtual void NotifyVisitDBObserversOnAddVisit( | 97 virtual void NotifyVisitDBObserversOnAddVisit( |
| 98 const BriefVisitInfo& info) OVERRIDE {} | 98 const BriefVisitInfo& info) OVERRIDE {} |
| 99 | 99 |
| 100 private: | 100 private: |
| 101 // Not owned by us. | 101 // Not owned by us. |
| 102 HistoryBackendTestBase* test_; | 102 HistoryBackendTestBase* test_; |
| 103 | 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestDelegate); | 104 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestDelegate); |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 class HistoryBackendCancelableRequest | |
| 108 : public CancelableRequestProvider, | |
| 109 public CancelableRequestConsumerTSimple<int> { | |
| 110 public: | |
| 111 HistoryBackendCancelableRequest() {} | |
| 112 | |
| 113 template<class RequestType> | |
| 114 CancelableRequestProvider::Handle MockScheduleOfRequest( | |
| 115 RequestType* request) { | |
| 116 AddRequest(request, this); | |
| 117 return request->handle(); | |
| 118 } | |
| 119 }; | |
| 120 | |
| 121 class HistoryBackendTestBase : public testing::Test { | 107 class HistoryBackendTestBase : public testing::Test { |
| 122 public: | 108 public: |
| 123 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; | 109 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; |
| 124 | 110 |
| 125 HistoryBackendTestBase() | 111 HistoryBackendTestBase() |
| 126 : loaded_(false), | 112 : loaded_(false), |
| 127 ui_thread_(content::BrowserThread::UI, &message_loop_) {} | 113 ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| 128 | 114 |
| 129 virtual ~HistoryBackendTestBase() { | 115 virtual ~HistoryBackendTestBase() { |
| 130 STLDeleteValues(&broadcasted_notifications_); | 116 STLDeleteValues(&broadcasted_notifications_); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 199 |
| 214 void HistoryBackendTestDelegate::DBLoaded() { | 200 void HistoryBackendTestDelegate::DBLoaded() { |
| 215 test_->loaded_ = true; | 201 test_->loaded_ = true; |
| 216 } | 202 } |
| 217 | 203 |
| 218 class HistoryBackendTest : public HistoryBackendTestBase { | 204 class HistoryBackendTest : public HistoryBackendTestBase { |
| 219 public: | 205 public: |
| 220 HistoryBackendTest() {} | 206 HistoryBackendTest() {} |
| 221 virtual ~HistoryBackendTest() {} | 207 virtual ~HistoryBackendTest() {} |
| 222 | 208 |
| 223 // Callback for QueryFiltered. | |
| 224 void OnQueryFiltered(CancelableRequestProvider::Handle handle, | |
| 225 const history::FilteredURLList& data) { | |
| 226 filtered_list_ = data; | |
| 227 } | |
| 228 | |
| 229 protected: | 209 protected: |
| 230 const history::FilteredURLList& get_filtered_list() const { | |
| 231 return filtered_list_; | |
| 232 } | |
| 233 | |
| 234 void AddRedirectChain(const char* sequence[], int page_id) { | 210 void AddRedirectChain(const char* sequence[], int page_id) { |
| 235 AddRedirectChainWithTransitionAndTime(sequence, page_id, | 211 AddRedirectChainWithTransitionAndTime(sequence, page_id, |
| 236 content::PAGE_TRANSITION_LINK, | 212 content::PAGE_TRANSITION_LINK, |
| 237 Time::Now()); | 213 Time::Now()); |
| 238 } | 214 } |
| 239 | 215 |
| 240 void AddRedirectChainWithTransitionAndTime( | 216 void AddRedirectChainWithTransitionAndTime( |
| 241 const char* sequence[], | 217 const char* sequence[], |
| 242 int page_id, | 218 int page_id, |
| 243 content::PageTransition transition, | 219 content::PageTransition transition, |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 396 |
| 421 // Returns true if |bitmap_data| is equal to |expected_data|. | 397 // Returns true if |bitmap_data| is equal to |expected_data|. |
| 422 bool BitmapDataEqual(char expected_data, | 398 bool BitmapDataEqual(char expected_data, |
| 423 scoped_refptr<base::RefCountedMemory> bitmap_data) { | 399 scoped_refptr<base::RefCountedMemory> bitmap_data) { |
| 424 return bitmap_data.get() && | 400 return bitmap_data.get() && |
| 425 bitmap_data->size() == 1u && | 401 bitmap_data->size() == 1u && |
| 426 *bitmap_data->front() == expected_data; | 402 *bitmap_data->front() == expected_data; |
| 427 } | 403 } |
| 428 | 404 |
| 429 private: | 405 private: |
| 430 history::FilteredURLList filtered_list_; | |
| 431 | |
| 432 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTest); | 406 DISALLOW_COPY_AND_ASSIGN(HistoryBackendTest); |
| 433 }; | 407 }; |
| 434 | 408 |
| 435 class InMemoryHistoryBackendTest : public HistoryBackendTestBase { | 409 class InMemoryHistoryBackendTest : public HistoryBackendTestBase { |
| 436 public: | 410 public: |
| 437 InMemoryHistoryBackendTest() {} | 411 InMemoryHistoryBackendTest() {} |
| 438 virtual ~InMemoryHistoryBackendTest() {} | 412 virtual ~InMemoryHistoryBackendTest() {} |
| 439 | 413 |
| 440 protected: | 414 protected: |
| 441 void SimulateNotification(int type, | 415 void SimulateNotification(int type, |
| (...skipping 2302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2744 false, | 2718 false, |
| 2745 tested_time - one_day + half_an_hour * 2, | 2719 tested_time - one_day + half_an_hour * 2, |
| 2746 &transition1, &transition2); | 2720 &transition1, &transition2); |
| 2747 | 2721 |
| 2748 redirect_sequence[0] = yahoo_sports_soccer; | 2722 redirect_sequence[0] = yahoo_sports_soccer; |
| 2749 AddRedirectChainWithTransitionAndTime(redirect_sequence, 0, | 2723 AddRedirectChainWithTransitionAndTime(redirect_sequence, 0, |
| 2750 kTypedTransition, | 2724 kTypedTransition, |
| 2751 tested_time - half_an_hour); | 2725 tested_time - half_an_hour); |
| 2752 backend_->Commit(); | 2726 backend_->Commit(); |
| 2753 | 2727 |
| 2754 scoped_refptr<QueryFilteredURLsRequest> request1 = | |
| 2755 new history::QueryFilteredURLsRequest( | |
| 2756 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2757 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2758 HistoryBackendCancelableRequest cancellable_request; | |
| 2759 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2760 request1.get()); | |
| 2761 | |
| 2762 VisitFilter filter; | 2728 VisitFilter filter; |
| 2729 FilteredURLList filtered_list; |
| 2763 // Time limit is |tested_time| +/- 45 min. | 2730 // Time limit is |tested_time| +/- 45 min. |
| 2764 base::TimeDelta three_quarters_of_an_hour = base::TimeDelta::FromMinutes(45); | 2731 base::TimeDelta three_quarters_of_an_hour = base::TimeDelta::FromMinutes(45); |
| 2765 filter.SetFilterTime(tested_time); | 2732 filter.SetFilterTime(tested_time); |
| 2766 filter.SetFilterWidth(three_quarters_of_an_hour); | 2733 filter.SetFilterWidth(three_quarters_of_an_hour); |
| 2767 backend_->QueryFilteredURLs(request1, 100, filter, false); | 2734 backend_->QueryFilteredURLs(100, filter, false, &filtered_list); |
| 2768 | 2735 |
| 2769 ASSERT_EQ(4U, get_filtered_list().size()); | 2736 ASSERT_EQ(4U, filtered_list.size()); |
| 2770 EXPECT_EQ(std::string(google), get_filtered_list()[0].url.spec()); | 2737 EXPECT_EQ(std::string(google), filtered_list[0].url.spec()); |
| 2771 EXPECT_EQ(std::string(yahoo_sports_soccer), | 2738 EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec()); |
| 2772 get_filtered_list()[1].url.spec()); | 2739 EXPECT_EQ(std::string(yahoo), filtered_list[2].url.spec()); |
| 2773 EXPECT_EQ(std::string(yahoo), get_filtered_list()[2].url.spec()); | 2740 EXPECT_EQ(std::string(yahoo_sports), filtered_list[3].url.spec()); |
| 2774 EXPECT_EQ(std::string(yahoo_sports), | |
| 2775 get_filtered_list()[3].url.spec()); | |
| 2776 | 2741 |
| 2777 // Time limit is between |tested_time| and |tested_time| + 2 hours. | 2742 // Time limit is between |tested_time| and |tested_time| + 2 hours. |
| 2778 scoped_refptr<QueryFilteredURLsRequest> request2 = | |
| 2779 new history::QueryFilteredURLsRequest( | |
| 2780 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2781 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2782 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2783 request2.get()); | |
| 2784 filter.SetFilterTime(tested_time + one_hour); | 2743 filter.SetFilterTime(tested_time + one_hour); |
| 2785 filter.SetFilterWidth(one_hour); | 2744 filter.SetFilterWidth(one_hour); |
| 2786 backend_->QueryFilteredURLs(request2, 100, filter, false); | 2745 backend_->QueryFilteredURLs(100, filter, false, &filtered_list); |
| 2787 | 2746 |
| 2788 ASSERT_EQ(3U, get_filtered_list().size()); | 2747 ASSERT_EQ(3U, filtered_list.size()); |
| 2789 EXPECT_EQ(std::string(google), get_filtered_list()[0].url.spec()); | 2748 EXPECT_EQ(std::string(google), filtered_list[0].url.spec()); |
| 2790 EXPECT_EQ(std::string(yahoo), get_filtered_list()[1].url.spec()); | 2749 EXPECT_EQ(std::string(yahoo), filtered_list[1].url.spec()); |
| 2791 EXPECT_EQ(std::string(yahoo_sports), get_filtered_list()[2].url.spec()); | 2750 EXPECT_EQ(std::string(yahoo_sports), filtered_list[2].url.spec()); |
| 2792 | 2751 |
| 2793 // Time limit is between |tested_time| - 2 hours and |tested_time|. | 2752 // Time limit is between |tested_time| - 2 hours and |tested_time|. |
| 2794 scoped_refptr<QueryFilteredURLsRequest> request3 = | |
| 2795 new history::QueryFilteredURLsRequest( | |
| 2796 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2797 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2798 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2799 request3.get()); | |
| 2800 filter.SetFilterTime(tested_time - one_hour); | 2753 filter.SetFilterTime(tested_time - one_hour); |
| 2801 filter.SetFilterWidth(one_hour); | 2754 filter.SetFilterWidth(one_hour); |
| 2802 backend_->QueryFilteredURLs(request3, 100, filter, false); | 2755 backend_->QueryFilteredURLs(100, filter, false, &filtered_list); |
| 2803 | 2756 |
| 2804 ASSERT_EQ(3U, get_filtered_list().size()); | 2757 ASSERT_EQ(3U, filtered_list.size()); |
| 2805 EXPECT_EQ(std::string(google), get_filtered_list()[0].url.spec()); | 2758 EXPECT_EQ(std::string(google), filtered_list[0].url.spec()); |
| 2806 EXPECT_EQ(std::string(yahoo_sports_soccer), | 2759 EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec()); |
| 2807 get_filtered_list()[1].url.spec()); | 2760 EXPECT_EQ(std::string(yahoo_sports), filtered_list[2].url.spec()); |
| 2808 EXPECT_EQ(std::string(yahoo_sports), get_filtered_list()[2].url.spec()); | |
| 2809 | 2761 |
| 2810 filter.ClearFilters(); | 2762 filter.ClearFilters(); |
| 2811 base::Time::Exploded exploded_time; | 2763 base::Time::Exploded exploded_time; |
| 2812 tested_time.LocalExplode(&exploded_time); | 2764 tested_time.LocalExplode(&exploded_time); |
| 2813 | 2765 |
| 2814 // Today. | 2766 // Today. |
| 2815 scoped_refptr<QueryFilteredURLsRequest> request4 = | |
| 2816 new history::QueryFilteredURLsRequest( | |
| 2817 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2818 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2819 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2820 request4.get()); | |
| 2821 filter.SetFilterTime(tested_time); | 2767 filter.SetFilterTime(tested_time); |
| 2822 filter.SetDayOfTheWeekFilter(static_cast<int>(exploded_time.day_of_week)); | 2768 filter.SetDayOfTheWeekFilter(static_cast<int>(exploded_time.day_of_week)); |
| 2823 backend_->QueryFilteredURLs(request4, 100, filter, false); | 2769 backend_->QueryFilteredURLs(100, filter, false, &filtered_list); |
| 2824 | 2770 |
| 2825 ASSERT_EQ(2U, get_filtered_list().size()); | 2771 ASSERT_EQ(2U, filtered_list.size()); |
| 2826 EXPECT_EQ(std::string(google), get_filtered_list()[0].url.spec()); | 2772 EXPECT_EQ(std::string(google), filtered_list[0].url.spec()); |
| 2827 EXPECT_EQ(std::string(yahoo_sports_soccer), | 2773 EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec()); |
| 2828 get_filtered_list()[1].url.spec()); | |
| 2829 | 2774 |
| 2830 // Today + time limit - only yahoo_sports_soccer should fit. | 2775 // Today + time limit - only yahoo_sports_soccer should fit. |
| 2831 scoped_refptr<QueryFilteredURLsRequest> request5 = | |
| 2832 new history::QueryFilteredURLsRequest( | |
| 2833 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2834 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2835 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2836 request5.get()); | |
| 2837 filter.SetFilterTime(tested_time - base::TimeDelta::FromMinutes(40)); | 2776 filter.SetFilterTime(tested_time - base::TimeDelta::FromMinutes(40)); |
| 2838 filter.SetFilterWidth(base::TimeDelta::FromMinutes(20)); | 2777 filter.SetFilterWidth(base::TimeDelta::FromMinutes(20)); |
| 2839 backend_->QueryFilteredURLs(request5, 100, filter, false); | 2778 backend_->QueryFilteredURLs(100, filter, false, &filtered_list); |
| 2840 | 2779 |
| 2841 ASSERT_EQ(1U, get_filtered_list().size()); | 2780 ASSERT_EQ(1U, filtered_list.size()); |
| 2842 EXPECT_EQ(std::string(yahoo_sports_soccer), | 2781 EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[0].url.spec()); |
| 2843 get_filtered_list()[0].url.spec()); | |
| 2844 | 2782 |
| 2845 // Make sure we get debug data if we request it. | 2783 // Make sure we get debug data if we request it. |
| 2846 scoped_refptr<QueryFilteredURLsRequest> request6 = | |
| 2847 new history::QueryFilteredURLsRequest( | |
| 2848 base::Bind(&HistoryBackendTest::OnQueryFiltered, | |
| 2849 base::Unretained(static_cast<HistoryBackendTest*>(this)))); | |
| 2850 cancellable_request.MockScheduleOfRequest<QueryFilteredURLsRequest>( | |
| 2851 request6.get()); | |
| 2852 filter.SetFilterTime(tested_time); | 2784 filter.SetFilterTime(tested_time); |
| 2853 filter.SetFilterWidth(one_hour * 2); | 2785 filter.SetFilterWidth(one_hour * 2); |
| 2854 backend_->QueryFilteredURLs(request6, 100, filter, true); | 2786 backend_->QueryFilteredURLs(100, filter, true, &filtered_list); |
| 2855 | 2787 |
| 2856 // If the SegmentID is used by QueryFilteredURLs when generating the debug | 2788 // If the SegmentID is used by QueryFilteredURLs when generating the debug |
| 2857 // data instead of the URLID, the |total_visits| for the |yahoo_sports_soccer| | 2789 // data instead of the URLID, the |total_visits| for the |yahoo_sports_soccer| |
| 2858 // entry will be zero instead of 1. | 2790 // entry will be zero instead of 1. |
| 2859 ASSERT_GE(get_filtered_list().size(), 2U); | 2791 ASSERT_GE(filtered_list.size(), 2U); |
| 2860 EXPECT_EQ(std::string(google), get_filtered_list()[0].url.spec()); | 2792 EXPECT_EQ(std::string(google), filtered_list[0].url.spec()); |
| 2861 EXPECT_EQ(std::string(yahoo_sports_soccer), | 2793 EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec()); |
| 2862 get_filtered_list()[1].url.spec()); | 2794 EXPECT_EQ(4U, filtered_list[0].extended_info.total_visits); |
| 2863 EXPECT_EQ(4U, get_filtered_list()[0].extended_info.total_visits); | 2795 EXPECT_EQ(1U, filtered_list[1].extended_info.total_visits); |
| 2864 EXPECT_EQ(1U, get_filtered_list()[1].extended_info.total_visits); | |
| 2865 } | 2796 } |
| 2866 | 2797 |
| 2867 TEST_F(HistoryBackendTest, UpdateVisitDuration) { | 2798 TEST_F(HistoryBackendTest, UpdateVisitDuration) { |
| 2868 // This unit test will test adding and deleting visit details information. | 2799 // This unit test will test adding and deleting visit details information. |
| 2869 ASSERT_TRUE(backend_.get()); | 2800 ASSERT_TRUE(backend_.get()); |
| 2870 | 2801 |
| 2871 GURL url1("http://www.cnn.com"); | 2802 GURL url1("http://www.cnn.com"); |
| 2872 std::vector<VisitInfo> visit_info1, visit_info2; | 2803 std::vector<VisitInfo> visit_info1, visit_info2; |
| 2873 Time start_ts = Time::Now() - base::TimeDelta::FromDays(5); | 2804 Time start_ts = Time::Now() - base::TimeDelta::FromDays(5); |
| 2874 Time end_ts = start_ts + base::TimeDelta::FromDays(2); | 2805 Time end_ts = start_ts + base::TimeDelta::FromDays(2); |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3405 // Verify that the second term is no longer returned as result, and also check | 3336 // Verify that the second term is no longer returned as result, and also check |
| 3406 // at the low level that it is gone for good. The term corresponding to the | 3337 // at the low level that it is gone for good. The term corresponding to the |
| 3407 // first URLRow should not be affected. | 3338 // first URLRow should not be affected. |
| 3408 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); | 3339 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); |
| 3409 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); | 3340 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); |
| 3410 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); | 3341 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); |
| 3411 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); | 3342 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); |
| 3412 } | 3343 } |
| 3413 | 3344 |
| 3414 } // namespace history | 3345 } // namespace history |
| OLD | NEW |