OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/scoped_temp_dir.h" | 5 #include "base/scoped_temp_dir.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "chrome/browser/history/top_sites.h" | 7 #include "chrome/browser/history/top_sites.h" |
8 #include "chrome/common/chrome_paths.h" | 8 #include "chrome/common/chrome_paths.h" |
9 #include "chrome/browser/history/top_sites_database.h" | 9 #include "chrome/browser/history/top_sites_database.h" |
| 10 #include "chrome/browser/history/history_notifications.h" |
10 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
11 #include "chrome/tools/profiles/thumbnail-inl.h" | 12 #include "chrome/tools/profiles/thumbnail-inl.h" |
12 #include "gfx/codec/jpeg_codec.h" | 13 #include "gfx/codec/jpeg_codec.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 17 |
17 | 18 |
18 namespace history { | 19 namespace history { |
19 | 20 |
20 static const unsigned char kBlob[] = | 21 static const unsigned char kBlob[] = |
21 "12346102356120394751634516591348710478123649165419234519234512349134"; | 22 "12346102356120394751634516591348710478123649165419234519234512349134"; |
22 | 23 |
23 class TopSitesTest : public testing::Test { | 24 class TopSitesTest : public testing::Test { |
24 public: | 25 public: |
25 TopSitesTest() { | 26 TopSitesTest() { |
26 } | 27 } |
27 ~TopSitesTest() { | 28 ~TopSitesTest() { |
28 } | 29 } |
29 | 30 |
30 TopSites& top_sites() { return *top_sites_; } | 31 TopSites& top_sites() { return *top_sites_; } |
| 32 Profile& profile() {return *profile_;} |
31 FilePath& file_name() { return file_name_; } | 33 FilePath& file_name() { return file_name_; } |
32 RefCountedBytes* google_thumbnail() { return google_thumbnail_; } | 34 RefCountedBytes* google_thumbnail() { return google_thumbnail_; } |
33 RefCountedBytes* random_thumbnail() { return random_thumbnail_; } | 35 RefCountedBytes* random_thumbnail() { return random_thumbnail_; } |
34 RefCountedBytes* weewar_thumbnail() { return weewar_thumbnail_; } | 36 RefCountedBytes* weewar_thumbnail() { return weewar_thumbnail_; } |
35 | 37 |
36 virtual void SetUp() { | 38 virtual void SetUp() { |
37 profile_.reset(new TestingProfile); | 39 profile_.reset(new TestingProfile); |
38 top_sites_ = new TopSites(profile_.get()); | 40 top_sites_ = new TopSites(profile_.get()); |
39 | 41 |
40 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 83 } |
82 | 84 |
83 private: | 85 private: |
84 scoped_refptr<TopSites> top_sites_; | 86 scoped_refptr<TopSites> top_sites_; |
85 scoped_ptr<TestingProfile> profile_; | 87 scoped_ptr<TestingProfile> profile_; |
86 ScopedTempDir temp_dir_; | 88 ScopedTempDir temp_dir_; |
87 FilePath file_name_; // Database filename. | 89 FilePath file_name_; // Database filename. |
88 scoped_refptr<RefCountedBytes> google_thumbnail_; | 90 scoped_refptr<RefCountedBytes> google_thumbnail_; |
89 scoped_refptr<RefCountedBytes> random_thumbnail_; | 91 scoped_refptr<RefCountedBytes> random_thumbnail_; |
90 scoped_refptr<RefCountedBytes> weewar_thumbnail_; | 92 scoped_refptr<RefCountedBytes> weewar_thumbnail_; |
| 93 MessageLoop message_loop_; |
91 | 94 |
92 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); | 95 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); |
93 }; | 96 }; |
94 | 97 |
95 // A mockup of a HistoryService used for testing TopSites. | 98 // A mockup of a HistoryService used for testing TopSites. |
96 class MockHistoryServiceImpl : public TopSites::MockHistoryService { | 99 class MockHistoryServiceImpl : public TopSites::MockHistoryService { |
97 public: | 100 public: |
98 // Calls the callback directly with the results. | 101 // Calls the callback directly with the results. |
99 HistoryService::Handle QueryMostVisitedURLs( | 102 HistoryService::Handle QueryMostVisitedURLs( |
100 int result_count, int days_back, | 103 int result_count, int days_back, |
101 CancelableRequestConsumerBase* consumer, | 104 CancelableRequestConsumerBase* consumer, |
102 HistoryService::QueryMostVisitedURLsCallback* callback) { | 105 HistoryService::QueryMostVisitedURLsCallback* callback) { |
103 callback->Run(CancelableRequestProvider::Handle(0), // Handle is unused. | 106 callback->Run(CancelableRequestProvider::Handle(0), // Handle is unused. |
104 most_visited_urls_); | 107 most_visited_urls_); |
105 delete callback; | 108 delete callback; |
106 return 0; | 109 return 0; |
107 } | 110 } |
108 | 111 |
109 // Add a page to the end of the pages list. | 112 // Add a page to the end of the pages list. |
110 void AppendMockPage(const GURL& url, | 113 void AppendMockPage(const GURL& url, |
111 const string16& title) { | 114 const string16& title) { |
112 MostVisitedURL page; | 115 MostVisitedURL page; |
113 page.url = url; | 116 page.url = url; |
114 page.title = title; | 117 page.title = title; |
115 page.redirects = RedirectList(); | 118 page.redirects = RedirectList(); |
116 page.redirects.push_back(url); | 119 page.redirects.push_back(url); |
117 most_visited_urls_.push_back(page); | 120 most_visited_urls_.push_back(page); |
118 } | 121 } |
119 | 122 |
| 123 // Removes the last URL in the list. |
| 124 void RemoveMostVisitedURL() { |
| 125 most_visited_urls_.pop_back(); |
| 126 } |
| 127 |
120 private: | 128 private: |
121 MostVisitedURLList most_visited_urls_; | 129 MostVisitedURLList most_visited_urls_; |
122 }; | 130 }; |
123 | 131 |
124 | 132 |
125 // A mockup of a TopSitesDatabase used for testing TopSites. | 133 // A mockup of a TopSitesDatabase used for testing TopSites. |
126 class MockTopSitesDatabaseImpl : public TopSitesDatabase { | 134 class MockTopSitesDatabaseImpl : public TopSitesDatabase { |
127 public: | 135 public: |
128 virtual MostVisitedURLList GetTopURLs() { | 136 virtual MostVisitedURLList GetTopURLs() { |
129 // Return a copy of the vector. | 137 // Return a copy of the vector. |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 high_score)); | 608 high_score)); |
601 // Check that the thumbnail was updated. | 609 // Check that the thumbnail was updated. |
602 top_sites().GetPageThumbnail(google1_url, &out_1); | 610 top_sites().GetPageThumbnail(google1_url, &out_1); |
603 EXPECT_FALSE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); | 611 EXPECT_FALSE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); |
604 // Read the new thumbnail from the DB - should match what's in TopSites. | 612 // Read the new thumbnail from the DB - should match what's in TopSites. |
605 db->GetPageThumbnail(url2, &out_2); | 613 db->GetPageThumbnail(url2, &out_2); |
606 EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); | 614 EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); |
607 EXPECT_TRUE(high_score.Equals(out_2.thumbnail_score)); | 615 EXPECT_TRUE(high_score.Equals(out_2.thumbnail_score)); |
608 } | 616 } |
609 | 617 |
| 618 TEST_F(TopSitesTest, DeleteNotifications) { |
| 619 GURL google1_url("http://google.com"); |
| 620 GURL google2_url("http://google.com/redirect"); |
| 621 GURL google3_url("http://www.google.com"); |
| 622 string16 google_title(ASCIIToUTF16("Google")); |
| 623 GURL news_url("http://news.google.com"); |
| 624 string16 news_title(ASCIIToUTF16("Google News")); |
| 625 |
| 626 MockHistoryServiceImpl hs; |
| 627 |
| 628 top_sites().Init(file_name()); |
| 629 |
| 630 hs.AppendMockPage(google1_url, google_title); |
| 631 hs.AppendMockPage(news_url, news_title); |
| 632 top_sites().SetMockHistoryService(&hs); |
| 633 |
| 634 top_sites().StartQueryForMostVisited(); |
| 635 |
| 636 MostVisitedURLList result = top_sites().GetMostVisitedURLs(); |
| 637 ASSERT_EQ(2u, result.size()); |
| 638 |
| 639 hs.RemoveMostVisitedURL(); |
| 640 |
| 641 history::URLsDeletedDetails details; |
| 642 details.all_history = false; |
| 643 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 644 Source<Profile> (&profile()), |
| 645 (const NotificationDetails&)details); |
| 646 |
| 647 result = top_sites().GetMostVisitedURLs(); |
| 648 ASSERT_EQ(1u, result.size()); |
| 649 EXPECT_EQ(google_title, result[0].title); |
| 650 |
| 651 hs.RemoveMostVisitedURL(); |
| 652 details.all_history = true; |
| 653 top_sites().Observe(NotificationType::HISTORY_URLS_DELETED, |
| 654 Source<Profile> (&profile()), |
| 655 (const NotificationDetails&)details); |
| 656 result = top_sites().GetMostVisitedURLs(); |
| 657 ASSERT_EQ(0u, result.size()); |
| 658 } |
| 659 |
| 660 TEST_F(TopSitesTest, GetUpdateDelay) { |
| 661 top_sites().last_num_urls_changed_ = 0; |
| 662 EXPECT_EQ(60, top_sites().GetUpdateDelay().InMinutes()); |
| 663 |
| 664 top_sites().last_num_urls_changed_ = 3; |
| 665 EXPECT_EQ(52, top_sites().GetUpdateDelay().InMinutes()); |
| 666 |
| 667 top_sites().last_num_urls_changed_ = 20; |
| 668 EXPECT_EQ(1, top_sites().GetUpdateDelay().InMinutes()); |
| 669 } |
| 670 |
610 } // namespace history | 671 } // namespace history |
OLD | NEW |