| 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/test/testing_profile.h" | 10 #include "chrome/test/testing_profile.h" |
| 11 #include "chrome/tools/profiles/thumbnail-inl.h" | 11 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 12 #include "gfx/codec/jpeg_codec.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 | 16 |
| 16 | 17 |
| 17 namespace history { | 18 namespace history { |
| 18 | 19 |
| 19 static const unsigned char kBlob[] = | 20 static const unsigned char kBlob[] = |
| 20 "12346102356120394751634516591348710478123649165419234519234512349134"; | 21 "12346102356120394751634516591348710478123649165419234519234512349134"; |
| 21 | 22 |
| 22 class TopSitesTest : public testing::Test { | 23 class TopSitesTest : public testing::Test { |
| 23 public: | 24 public: |
| 24 TopSitesTest() { | 25 TopSitesTest() { |
| 25 } | 26 } |
| 26 ~TopSitesTest() { | 27 ~TopSitesTest() { |
| 27 } | 28 } |
| 28 | 29 |
| 29 TopSites& top_sites() { return *top_sites_; } | 30 TopSites& top_sites() { return *top_sites_; } |
| 30 FilePath& file_name() { return file_name_; } | 31 FilePath& file_name() { return file_name_; } |
| 31 RefCountedBytes* google_thumbnail() { return google_thumbnail_; } | 32 RefCountedBytes* google_thumbnail() { return google_thumbnail_; } |
| 32 RefCountedBytes* random_thumbnail() { return random_thumbnail_; } | 33 RefCountedBytes* random_thumbnail() { return random_thumbnail_; } |
| 34 RefCountedBytes* weewar_thumbnail() { return weewar_thumbnail_; } |
| 33 | 35 |
| 34 virtual void SetUp() { | 36 virtual void SetUp() { |
| 35 profile_.reset(new TestingProfile); | 37 profile_.reset(new TestingProfile); |
| 36 top_sites_ = new TopSites(profile_.get()); | 38 top_sites_ = new TopSites(profile_.get()); |
| 37 | 39 |
| 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 40 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 39 file_name_ = temp_dir_.path().AppendASCII("TopSites.db"); | 41 file_name_ = temp_dir_.path().AppendASCII("TopSites.db"); |
| 40 EXPECT_TRUE(file_util::Delete(file_name_, false)); | 42 EXPECT_TRUE(file_util::Delete(file_name_, false)); |
| 41 | 43 |
| 42 std::vector<unsigned char> random_data(kBlob, kBlob + sizeof(kBlob)); | 44 std::vector<unsigned char> random_data(kBlob, kBlob + sizeof(kBlob)); |
| 43 std::vector<unsigned char> google_data(kGoogleThumbnail, | 45 std::vector<unsigned char> google_data(kGoogleThumbnail, |
| 44 kGoogleThumbnail + | 46 kGoogleThumbnail + |
| 45 sizeof(kGoogleThumbnail)); | 47 sizeof(kGoogleThumbnail)); |
| 48 std::vector<unsigned char> weewar_data(kWeewarThumbnail, |
| 49 kWeewarThumbnail + |
| 50 sizeof(kWeewarThumbnail)); |
| 46 random_thumbnail_ = new RefCountedBytes(random_data); | 51 random_thumbnail_ = new RefCountedBytes(random_data); |
| 47 google_thumbnail_ = new RefCountedBytes(google_data); | 52 google_thumbnail_ = new RefCountedBytes(google_data); |
| 53 weewar_thumbnail_ = new RefCountedBytes(weewar_data); |
| 48 } | 54 } |
| 49 | 55 |
| 50 virtual void TearDown() { | 56 virtual void TearDown() { |
| 51 profile_.reset(); | 57 profile_.reset(); |
| 52 top_sites_ = NULL; | 58 top_sites_ = NULL; |
| 53 EXPECT_TRUE(file_util::Delete(file_name_, false)); | 59 EXPECT_TRUE(file_util::Delete(file_name_, false)); |
| 54 } | 60 } |
| 55 | 61 |
| 56 // Wrappers that allow private TopSites functions to be called from the | 62 // Wrappers that allow private TopSites functions to be called from the |
| 57 // individual tests without making them all be friends. | 63 // individual tests without making them all be friends. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 74 added_urls, deleted_urls, moved_urls); | 80 added_urls, deleted_urls, moved_urls); |
| 75 } | 81 } |
| 76 | 82 |
| 77 private: | 83 private: |
| 78 scoped_refptr<TopSites> top_sites_; | 84 scoped_refptr<TopSites> top_sites_; |
| 79 scoped_ptr<TestingProfile> profile_; | 85 scoped_ptr<TestingProfile> profile_; |
| 80 ScopedTempDir temp_dir_; | 86 ScopedTempDir temp_dir_; |
| 81 FilePath file_name_; // Database filename. | 87 FilePath file_name_; // Database filename. |
| 82 scoped_refptr<RefCountedBytes> google_thumbnail_; | 88 scoped_refptr<RefCountedBytes> google_thumbnail_; |
| 83 scoped_refptr<RefCountedBytes> random_thumbnail_; | 89 scoped_refptr<RefCountedBytes> random_thumbnail_; |
| 90 scoped_refptr<RefCountedBytes> weewar_thumbnail_; |
| 84 | 91 |
| 85 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); | 92 DISALLOW_COPY_AND_ASSIGN(TopSitesTest); |
| 86 }; | 93 }; |
| 87 | 94 |
| 88 // A mockup of a HistoryService used for testing TopSites. | 95 // A mockup of a HistoryService used for testing TopSites. |
| 89 class MockHistoryServiceImpl : public TopSites::MockHistoryService { | 96 class MockHistoryServiceImpl : public TopSites::MockHistoryService { |
| 90 public: | 97 public: |
| 91 // Calls the callback directly with the results. | 98 // Calls the callback directly with the results. |
| 92 HistoryService::Handle QueryMostVisitedURLs( | 99 HistoryService::Handle QueryMostVisitedURLs( |
| 93 int result_count, int days_back, | 100 int result_count, int days_back, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 mv.url = url; | 193 mv.url = url; |
| 187 mv.redirects.push_back(url); | 194 mv.redirects.push_back(url); |
| 188 list->push_back(mv); | 195 list->push_back(mv); |
| 189 } | 196 } |
| 190 | 197 |
| 191 // Returns true if t1 and t2 contain the same data. | 198 // Returns true if t1 and t2 contain the same data. |
| 192 static bool ThumbnailsAreEqual(RefCountedBytes* t1, | 199 static bool ThumbnailsAreEqual(RefCountedBytes* t1, |
| 193 RefCountedBytes* t2) { | 200 RefCountedBytes* t2) { |
| 194 if (!t1 || !t2) | 201 if (!t1 || !t2) |
| 195 return false; | 202 return false; |
| 203 if (t1->data.size() != t2->data.size()) |
| 204 return false; |
| 196 return std::equal(t1->data.begin(), | 205 return std::equal(t1->data.begin(), |
| 197 t1->data.end(), | 206 t1->data.end(), |
| 198 t2->data.begin()); | 207 t2->data.begin()); |
| 199 } | 208 } |
| 200 | 209 |
| 201 // Same as AppendMostVisitedURL except that it adds a redirect from the first | 210 // Same as AppendMostVisitedURL except that it adds a redirect from the first |
| 202 // URL to the second. | 211 // URL to the second. |
| 203 static void AppendMostVisitedURLWithRedirect( | 212 static void AppendMostVisitedURLWithRedirect( |
| 204 std::vector<MostVisitedURL>* list, | 213 std::vector<MostVisitedURL>* list, |
| 205 const GURL& redirect_source, const GURL& redirect_dest) { | 214 const GURL& redirect_source, const GURL& redirect_dest) { |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 551 |
| 543 EXPECT_EQ(asdf_url, result[1].url); | 552 EXPECT_EQ(asdf_url, result[1].url); |
| 544 EXPECT_EQ(asdf_title, result[1].title); | 553 EXPECT_EQ(asdf_title, result[1].title); |
| 545 | 554 |
| 546 MockHistoryServiceImpl hs; | 555 MockHistoryServiceImpl hs; |
| 547 // Add one old, one new URL to the history. | 556 // Add one old, one new URL to the history. |
| 548 hs.AppendMockPage(google1_url, google_title); | 557 hs.AppendMockPage(google1_url, google_title); |
| 549 hs.AppendMockPage(news_url, news_title); | 558 hs.AppendMockPage(news_url, news_title); |
| 550 top_sites().SetMockHistoryService(&hs); | 559 top_sites().SetMockHistoryService(&hs); |
| 551 | 560 |
| 552 // This writes the new data to the DB. | 561 // This requests data from History Service and writes it to the DB. |
| 553 top_sites().StartQueryForMostVisited(); | 562 top_sites().StartQueryForMostVisited(); |
| 554 | 563 |
| 555 result = db->GetTopURLs(); | 564 result = db->GetTopURLs(); |
| 556 ASSERT_EQ(2u, result.size()); | 565 ASSERT_EQ(2u, result.size()); |
| 557 EXPECT_EQ(google_title, result[0].title); | 566 EXPECT_EQ(google_title, result[0].title); |
| 558 EXPECT_EQ(news_title, result[1].title); | 567 EXPECT_EQ(news_title, result[1].title); |
| 568 |
| 569 scoped_ptr<SkBitmap> weewar_bitmap( |
| 570 gfx::JPEGCodec::Decode(weewar_thumbnail()->front(), |
| 571 weewar_thumbnail()->size())); |
| 572 |
| 573 base::Time now = base::Time::Now(); |
| 574 ThumbnailScore low_score(1.0, true, true, now); |
| 575 ThumbnailScore medium_score(0.5, true, true, now); |
| 576 ThumbnailScore high_score(0.0, true, true, now); |
| 577 |
| 578 // 1. Set to weewar. (Writes the thumbnail to the DB.) |
| 579 EXPECT_TRUE(top_sites().SetPageThumbnail(google1_url, |
| 580 *weewar_bitmap, |
| 581 medium_score)); |
| 582 RefCountedBytes* out_1; |
| 583 TopSites::Images out_2; |
| 584 top_sites().GetPageThumbnail(google1_url, &out_1); |
| 585 db->GetPageThumbnail(url2, &out_2); |
| 586 EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); |
| 587 |
| 588 scoped_ptr<SkBitmap> google_bitmap( |
| 589 gfx::JPEGCodec::Decode(google_thumbnail()->front(), |
| 590 google_thumbnail()->size())); |
| 591 |
| 592 // 2. Set to google - low score. |
| 593 EXPECT_FALSE(top_sites().SetPageThumbnail(google1_url, |
| 594 *google_bitmap, |
| 595 low_score)); |
| 596 |
| 597 // 3. Set to google - high score. |
| 598 EXPECT_TRUE(top_sites().SetPageThumbnail(google1_url, |
| 599 *google_bitmap, |
| 600 high_score)); |
| 601 // Check that the thumbnail was updated. |
| 602 top_sites().GetPageThumbnail(google1_url, &out_1); |
| 603 EXPECT_FALSE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); |
| 604 // Read the new thumbnail from the DB - should match what's in TopSites. |
| 605 db->GetPageThumbnail(url2, &out_2); |
| 606 EXPECT_TRUE(ThumbnailsAreEqual(out_1, out_2.thumbnail)); |
| 607 EXPECT_TRUE(high_score.Equals(out_2.thumbnail_score)); |
| 559 } | 608 } |
| 560 | 609 |
| 561 } // namespace history | 610 } // namespace history |
| OLD | NEW |