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 14 matching lines...) Expand all Loading... |
25 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 25 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
26 #include "chrome/browser/bookmarks/bookmark_utils.h" | 26 #include "chrome/browser/bookmarks/bookmark_utils.h" |
27 #include "chrome/browser/history/history_notifications.h" | 27 #include "chrome/browser/history/history_notifications.h" |
28 #include "chrome/browser/history/history_service.h" | 28 #include "chrome/browser/history/history_service.h" |
29 #include "chrome/browser/history/history_service_factory.h" | 29 #include "chrome/browser/history/history_service_factory.h" |
30 #include "chrome/browser/history/in_memory_database.h" | 30 #include "chrome/browser/history/in_memory_database.h" |
31 #include "chrome/browser/history/in_memory_history_backend.h" | 31 #include "chrome/browser/history/in_memory_history_backend.h" |
32 #include "chrome/browser/history/visit_filter.h" | 32 #include "chrome/browser/history/visit_filter.h" |
33 #include "chrome/common/chrome_constants.h" | 33 #include "chrome/common/chrome_constants.h" |
34 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
35 #include "chrome/common/chrome_switches.h" | |
36 #include "chrome/common/importer/imported_favicon_usage.h" | 35 #include "chrome/common/importer/imported_favicon_usage.h" |
37 #include "chrome/test/base/testing_profile.h" | 36 #include "chrome/test/base/testing_profile.h" |
38 #include "content/public/browser/notification_details.h" | 37 #include "content/public/browser/notification_details.h" |
39 #include "content/public/browser/notification_source.h" | 38 #include "content/public/browser/notification_source.h" |
40 #include "content/public/test/test_browser_thread.h" | 39 #include "content/public/test/test_browser_thread.h" |
41 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
42 #include "url/gurl.h" | 41 #include "url/gurl.h" |
43 | 42 |
44 using base::Time; | 43 using base::Time; |
45 using base::TimeDelta; | |
46 | 44 |
47 // This file only tests functionality where it is most convenient to call the | 45 // This file only tests functionality where it is most convenient to call the |
48 // backend directly. Most of the history backend functions are tested by the | 46 // backend directly. Most of the history backend functions are tested by the |
49 // history unit test. Because of the elaborate callbacks involved, this is no | 47 // history unit test. Because of the elaborate callbacks involved, this is no |
50 // harder than calling it directly for many things. | 48 // harder than calling it directly for many things. |
51 | 49 |
52 namespace { | 50 namespace { |
53 | 51 |
54 static const gfx::Size kTinySize = gfx::Size(10, 10); | 52 static const gfx::Size kTinySize = gfx::Size(10, 10); |
55 static const gfx::Size kSmallSize = gfx::Size(16, 16); | 53 static const gfx::Size kSmallSize = gfx::Size(16, 16); |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 bool BitmapDataEqual(char expected_data, | 351 bool BitmapDataEqual(char expected_data, |
354 scoped_refptr<base::RefCountedMemory> bitmap_data) { | 352 scoped_refptr<base::RefCountedMemory> bitmap_data) { |
355 return bitmap_data.get() && | 353 return bitmap_data.get() && |
356 bitmap_data->size() == 1u && | 354 bitmap_data->size() == 1u && |
357 *bitmap_data->front() == expected_data; | 355 *bitmap_data->front() == expected_data; |
358 } | 356 } |
359 | 357 |
360 BookmarkModel bookmark_model_; | 358 BookmarkModel bookmark_model_; |
361 | 359 |
362 protected: | 360 protected: |
| 361 bool loaded_; |
| 362 |
| 363 private: |
| 364 friend class HistoryBackendTestDelegate; |
| 365 |
363 // testing::Test | 366 // testing::Test |
364 virtual void SetUp() { | 367 virtual void SetUp() { |
365 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("BackendTest"), | 368 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("BackendTest"), |
366 &test_dir_)) | 369 &test_dir_)) |
367 return; | 370 return; |
368 backend_ = new HistoryBackend(test_dir_, | 371 backend_ = new HistoryBackend(test_dir_, |
369 0, | 372 0, |
370 new HistoryBackendTestDelegate(this), | 373 new HistoryBackendTestDelegate(this), |
371 &bookmark_model_); | 374 &bookmark_model_); |
372 backend_->Init(std::string(), false); | 375 backend_->Init(std::string(), false); |
373 } | 376 } |
374 | 377 |
375 bool loaded_; | |
376 | |
377 private: | |
378 friend class HistoryBackendTestDelegate; | |
379 | |
380 virtual void TearDown() { | 378 virtual void TearDown() { |
381 if (backend_.get()) | 379 if (backend_.get()) |
382 backend_->Closing(); | 380 backend_->Closing(); |
383 backend_ = NULL; | 381 backend_ = NULL; |
384 mem_backend_.reset(); | 382 mem_backend_.reset(); |
385 base::DeleteFile(test_dir_, true); | 383 base::DeleteFile(test_dir_, true); |
386 base::RunLoop().RunUntilIdle(); | 384 base::RunLoop().RunUntilIdle(); |
387 } | 385 } |
388 | 386 |
389 void SetInMemoryBackend(int backend_id, InMemoryHistoryBackend* backend) { | 387 void SetInMemoryBackend(int backend_id, InMemoryHistoryBackend* backend) { |
(...skipping 2403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2793 EXPECT_FALSE(backend_->db()->GetURLRow(url2_id, &row)); | 2791 EXPECT_FALSE(backend_->db()->GetURLRow(url2_id, &row)); |
2794 EXPECT_FALSE(backend_->db()->GetURLRow(url3_id, &row)); | 2792 EXPECT_FALSE(backend_->db()->GetURLRow(url3_id, &row)); |
2795 | 2793 |
2796 // Test that corresponding keyword search terms are deleted for rows 2 & 3, | 2794 // Test that corresponding keyword search terms are deleted for rows 2 & 3, |
2797 // but not for row 1 | 2795 // but not for row 1 |
2798 EXPECT_TRUE(backend_->db()->GetKeywordSearchTermRow(url1_id, NULL)); | 2796 EXPECT_TRUE(backend_->db()->GetKeywordSearchTermRow(url1_id, NULL)); |
2799 EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url2_id, NULL)); | 2797 EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url2_id, NULL)); |
2800 EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url3_id, NULL)); | 2798 EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url3_id, NULL)); |
2801 } | 2799 } |
2802 | 2800 |
2803 class HistoryBackendSegmentDurationTest : public HistoryBackendTest { | |
2804 public: | |
2805 HistoryBackendSegmentDurationTest() {} | |
2806 | |
2807 virtual void SetUp() { | |
2808 CommandLine::ForCurrentProcess()->AppendSwitch( | |
2809 switches::kTrackActiveVisitTime); | |
2810 HistoryBackendTest::SetUp(); | |
2811 } | |
2812 | |
2813 private: | |
2814 DISALLOW_COPY_AND_ASSIGN(HistoryBackendSegmentDurationTest); | |
2815 }; | |
2816 | |
2817 // Assertions around segment durations. | |
2818 TEST_F(HistoryBackendSegmentDurationTest, SegmentDuration) { | |
2819 const GURL url1("http://www.google.com"); | |
2820 const GURL url2("http://www.foo.com/m"); | |
2821 const std::string segment1(VisitSegmentDatabase::ComputeSegmentName(url1)); | |
2822 const std::string segment2(VisitSegmentDatabase::ComputeSegmentName(url2)); | |
2823 | |
2824 Time segment_time(VisitSegmentDatabase::SegmentTime(Time::Now())); | |
2825 URLRow url_info1(url1); | |
2826 url_info1.set_visit_count(0); | |
2827 url_info1.set_typed_count(0); | |
2828 url_info1.set_last_visit(segment_time); | |
2829 url_info1.set_hidden(false); | |
2830 const URLID url1_id = backend_->db()->AddURL(url_info1); | |
2831 EXPECT_NE(0, url1_id); | |
2832 | |
2833 URLRow url_info2(url2); | |
2834 url_info2.set_visit_count(0); | |
2835 url_info2.set_typed_count(0); | |
2836 url_info2.set_last_visit(Time()); | |
2837 url_info2.set_hidden(false); | |
2838 const URLID url2_id = backend_->db()->AddURL(url_info2); | |
2839 EXPECT_NE(0, url2_id); | |
2840 EXPECT_NE(url1_id, url2_id); | |
2841 | |
2842 // Should not have any segments for the urls. | |
2843 EXPECT_EQ(0, backend_->db()->GetSegmentNamed(segment1)); | |
2844 EXPECT_EQ(0, backend_->db()->GetSegmentNamed(segment2)); | |
2845 | |
2846 // Update the duration, which should implicitly create the segments. | |
2847 const TimeDelta segment1_time_delta(TimeDelta::FromHours(1)); | |
2848 const TimeDelta segment2_time_delta(TimeDelta::FromHours(2)); | |
2849 backend_->IncreaseSegmentDuration(url1, segment_time, segment1_time_delta); | |
2850 backend_->IncreaseSegmentDuration(url2, segment_time, segment2_time_delta); | |
2851 | |
2852 // Get the ids of the segments that were created. | |
2853 const SegmentID segment1_id = backend_->db()->GetSegmentNamed(segment1); | |
2854 EXPECT_NE(0, segment1_id); | |
2855 const SegmentID segment2_id = backend_->db()->GetSegmentNamed(segment2); | |
2856 EXPECT_NE(0, segment2_id); | |
2857 EXPECT_NE(segment1_id, segment2_id); | |
2858 | |
2859 // Make sure the values made it to the db. | |
2860 SegmentDurationID segment1_duration_id; | |
2861 TimeDelta fetched_delta; | |
2862 EXPECT_TRUE(backend_->db()->GetSegmentDuration( | |
2863 segment1_id, segment_time, &segment1_duration_id, | |
2864 &fetched_delta)); | |
2865 EXPECT_NE(0, segment1_duration_id); | |
2866 EXPECT_EQ(segment1_time_delta.InHours(), fetched_delta.InHours()); | |
2867 | |
2868 SegmentDurationID segment2_duration_id; | |
2869 EXPECT_TRUE(backend_->db()->GetSegmentDuration( | |
2870 segment2_id, segment_time, &segment2_duration_id, | |
2871 &fetched_delta)); | |
2872 EXPECT_NE(0, segment2_duration_id); | |
2873 EXPECT_NE(segment1_duration_id, segment2_duration_id); | |
2874 EXPECT_EQ(segment2_time_delta.InHours(), fetched_delta.InHours()); | |
2875 | |
2876 // Query by duration. |url2| should be first as it has a longer view time. | |
2877 ScopedVector<PageUsageData> data; | |
2878 backend_->db()->QuerySegmentDuration(segment_time, 10, &data.get()); | |
2879 ASSERT_EQ(2u, data.size()); | |
2880 EXPECT_EQ(url2.spec(), data[0]->GetURL().spec()); | |
2881 EXPECT_EQ(url2_id, data[0]->GetID()); | |
2882 EXPECT_EQ(segment2_time_delta.InHours(), data[0]->duration().InHours()); | |
2883 | |
2884 EXPECT_EQ(url1.spec(), data[1]->GetURL().spec()); | |
2885 EXPECT_EQ(url1_id, data[1]->GetID()); | |
2886 EXPECT_EQ(segment1_time_delta.InHours(), data[1]->duration().InHours()); | |
2887 } | |
2888 | |
2889 // Simple test that removes a bookmark. This test exercises the code paths in | 2801 // Simple test that removes a bookmark. This test exercises the code paths in |
2890 // History that block till bookmark bar model is loaded. | 2802 // History that block till bookmark bar model is loaded. |
2891 TEST_F(HistoryBackendTest, RemoveNotification) { | 2803 TEST_F(HistoryBackendTest, RemoveNotification) { |
2892 scoped_ptr<TestingProfile> profile(new TestingProfile()); | 2804 scoped_ptr<TestingProfile> profile(new TestingProfile()); |
2893 | 2805 |
2894 ASSERT_TRUE(profile->CreateHistoryService(false, false)); | 2806 ASSERT_TRUE(profile->CreateHistoryService(false, false)); |
2895 profile->CreateBookmarkModel(true); | 2807 profile->CreateBookmarkModel(true); |
2896 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile.get()); | 2808 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile.get()); |
2897 test::WaitForBookmarkModelToLoad(model); | 2809 test::WaitForBookmarkModelToLoad(model); |
2898 | 2810 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2937 // Delete all DTS index databases. | 2849 // Delete all DTS index databases. |
2938 backend_->DeleteFTSIndexDatabases(); | 2850 backend_->DeleteFTSIndexDatabases(); |
2939 EXPECT_FALSE(base::PathExists(db1)); | 2851 EXPECT_FALSE(base::PathExists(db1)); |
2940 EXPECT_FALSE(base::PathExists(db1_wal)); | 2852 EXPECT_FALSE(base::PathExists(db1_wal)); |
2941 EXPECT_FALSE(base::PathExists(db1_journal)); | 2853 EXPECT_FALSE(base::PathExists(db1_journal)); |
2942 EXPECT_FALSE(base::PathExists(db2_symlink)); | 2854 EXPECT_FALSE(base::PathExists(db2_symlink)); |
2943 EXPECT_TRUE(base::PathExists(db2_actual)); // Symlinks shouldn't be followed. | 2855 EXPECT_TRUE(base::PathExists(db2_actual)); // Symlinks shouldn't be followed. |
2944 } | 2856 } |
2945 | 2857 |
2946 } // namespace history | 2858 } // namespace history |
OLD | NEW |