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