| 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 <string.h> | 5 #include <string.h> |
| 6 #include <algorithm> | |
| 7 #include <iostream> | 6 #include <iostream> |
| 8 #include <vector> | 7 #include <vector> |
| 9 | 8 |
| 10 #include "chrome/browser/thumbnail_store.h" | 9 #include "chrome/browser/thumbnail_store.h" |
| 11 | 10 |
| 12 #include "base/time.h" | 11 #include "base/time.h" |
| 13 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 14 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 15 #include "base/gfx/jpeg_codec.h" | 14 #include "base/gfx/jpeg_codec.h" |
| 15 #include "base/md5.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
| 18 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/ref_counted_util.h" | 19 #include "chrome/common/ref_counted_util.h" |
| 20 #include "chrome/common/thumbnail_score.h" | 20 #include "chrome/common/thumbnail_score.h" |
| 21 #include "chrome/common/sqlite_compiled_statement.h" | |
| 22 #include "chrome/common/sqlite_utils.h" | |
| 23 #include "chrome/tools/profiles/thumbnail-inl.h" | 21 #include "chrome/tools/profiles/thumbnail-inl.h" |
| 24 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 #include "third_party/skia/include/core/SkPixelRef.h" | 25 #include "third_party/skia/include/core/SkPixelRef.h" |
| 28 | 26 |
| 29 inline unsigned int diff(unsigned int a, unsigned int b) { | 27 inline unsigned int diff(unsigned int a, unsigned int b) { |
| 30 return a>b ? a-b : b-a; | 28 return a>b ? a-b : b-a; |
| 31 } | 29 } |
| 32 | 30 |
| 33 class ThumbnailStoreTest : public testing::Test { | 31 class ThumbnailStoreTest : public testing::Test { |
| 34 public: | 32 public: |
| 35 ThumbnailStoreTest() : score_(.5, true, false), | 33 ThumbnailStoreTest() : score1_(.5, true, false), |
| 36 url_("http://www.google.com/") { | 34 url1_("http://www.google.com/"), url2_("http://www.elgoog.com") { |
| 37 } | 35 } |
| 38 | |
| 39 ~ThumbnailStoreTest() { | 36 ~ThumbnailStoreTest() { |
| 40 } | 37 } |
| 41 | 38 |
| 42 protected: | 39 protected: |
| 43 void SetUp(); | 40 void SetUp(); |
| 44 | 41 |
| 45 void TearDown() { | 42 void TearDown() { |
| 46 file_util::Delete(db_name_, false); | 43 file_util::Delete(file_path_.AppendASCII(url1_.host()), false); |
| 44 file_util::Delete(file_path_.AppendASCII(url2_.host()), false); |
| 47 } | 45 } |
| 48 | 46 |
| 49 // Compute the max difference over all pixels for each RGBA component. | 47 // Compute the max difference over all pixels for each RGBA component. |
| 50 void PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b); | 48 void PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b); |
| 51 | 49 |
| 52 // The directory where ThumbnailStore will store data. | 50 // The directory where ThumbnailStore will store data. |
| 53 FilePath db_name_; | 51 FilePath file_path_; |
| 54 | 52 |
| 55 scoped_refptr<ThumbnailStore> store_; | 53 scoped_refptr<ThumbnailStore> store_; |
| 56 scoped_ptr<SkBitmap> google_; | 54 scoped_ptr<SkBitmap> google_; |
| 57 scoped_ptr<SkBitmap> weewar_; | 55 scoped_ptr<SkBitmap> weewar_; |
| 58 scoped_refptr<RefCountedBytes> jpeg_google_; | 56 scoped_refptr<RefCountedBytes> jpeg_google_; |
| 59 scoped_refptr<RefCountedBytes> jpeg_weewar_; | 57 scoped_refptr<RefCountedBytes> jpeg_weewar_; |
| 60 ThumbnailScore score_; | 58 ThumbnailScore score1_; |
| 61 GURL url_; | 59 GURL url1_, url2_; |
| 62 base::Time time_; | 60 base::Time time_; |
| 63 }; | 61 }; |
| 64 | 62 |
| 65 void ThumbnailStoreTest::SetUp() { | 63 void ThumbnailStoreTest::SetUp() { |
| 66 if (!file_util::GetTempDir(&db_name_)) | 64 if (!file_util::GetTempDir(&file_path_)) |
| 67 FAIL(); | 65 FAIL(); |
| 68 | 66 |
| 69 // Delete any old thumbnail files if they exist. | 67 // Delete any old thumbnail files if they exist. |
| 70 db_name_ = db_name_.AppendASCII("ThumbnailDB"); | 68 file_util::Delete(file_path_.AppendASCII(url1_.host()), false); |
| 71 file_util::Delete(db_name_, false); | 69 file_util::Delete(file_path_.AppendASCII(url2_.host()), false); |
| 72 | 70 |
| 73 google_.reset(JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); | 71 google_.reset(JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); |
| 74 weewar_.reset(JPEGCodec::Decode(kWeewarThumbnail, sizeof(kWeewarThumbnail))); | 72 weewar_.reset(JPEGCodec::Decode(kWeewarThumbnail, sizeof(kWeewarThumbnail))); |
| 75 | 73 |
| 76 SkAutoLockPixels lock1(*google_); | 74 SkAutoLockPixels lock1(*google_); |
| 77 jpeg_google_ = new RefCountedBytes; | 75 jpeg_google_ = new RefCountedBytes; |
| 78 JPEGCodec::Encode( | 76 JPEGCodec::Encode( |
| 79 reinterpret_cast<unsigned char*>(google_->getAddr32(0, 0)), | 77 reinterpret_cast<unsigned char*>(google_->getAddr32(0, 0)), |
| 80 JPEGCodec::FORMAT_BGRA, google_->width(), | 78 JPEGCodec::FORMAT_BGRA, google_->width(), |
| 81 google_->height(), | 79 google_->height(), |
| 82 static_cast<int>(google_->rowBytes()), 90, | 80 static_cast<int>(google_->rowBytes()), 90, |
| 83 &(jpeg_google_->data)); | 81 &(jpeg_google_->data)); |
| 84 | 82 |
| 85 SkAutoLockPixels lock2(*weewar_); | 83 SkAutoLockPixels lock2(*weewar_); |
| 86 jpeg_weewar_ = new RefCountedBytes; | 84 jpeg_weewar_ = new RefCountedBytes; |
| 87 JPEGCodec::Encode( | 85 JPEGCodec::Encode( |
| 88 reinterpret_cast<unsigned char*>(weewar_->getAddr32(0,0)), | 86 reinterpret_cast<unsigned char*>(weewar_->getAddr32(0,0)), |
| 89 JPEGCodec::FORMAT_BGRA, weewar_->width(), | 87 JPEGCodec::FORMAT_BGRA, weewar_->width(), |
| 90 weewar_->height(), | 88 weewar_->height(), |
| 91 static_cast<int>(weewar_->rowBytes()), 90, | 89 static_cast<int>(weewar_->rowBytes()), 90, |
| 92 &(jpeg_weewar_->data)); | 90 &(jpeg_weewar_->data)); |
| 93 | 91 |
| 94 store_ = new ThumbnailStore; | 92 store_ = new ThumbnailStore; |
| 95 | 93 store_->cache_initialized_ = true; |
| 94 store_->file_path_ = file_path_; |
| 95 store_->most_visited_urls_.reset(new std::vector<GURL>); |
| 96 store_->most_visited_urls_->push_back(url1_); |
| 96 store_->cache_.reset(new ThumbnailStore::Cache); | 97 store_->cache_.reset(new ThumbnailStore::Cache); |
| 97 store_->redirect_urls_.reset(new history::RedirectMap); | 98 store_->redirect_urls_.reset(new history::RedirectMap); |
| 98 | |
| 99 store_->most_visited_urls_.reset(new std::vector<GURL>); | |
| 100 store_->most_visited_urls_->push_back(url_); | |
| 101 } | 99 } |
| 102 | 100 |
| 103 void ThumbnailStoreTest::PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b) { | 101 void ThumbnailStoreTest::PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b) { |
| 104 // Compute the maximum difference in each of the RGBA components across all | 102 // Compute the maximum difference in each of the RGBA components across all |
| 105 // pixels between the retrieved SkBitmap and the original. These | 103 // pixels between the retrieved SkBitmap and the original. These |
| 106 // differences should be small since encoding was done at 90% before | 104 // differences should be small since encoding was done at 90% before |
| 107 // writing to disk. | 105 // writing to disk. |
| 108 | 106 |
| 109 if (image_a->height() != image_b->height() || | 107 if (image_a->height() != image_b->height() || |
| 110 image_b->width() != image_b->width() || | 108 image_b->width() != image_b->width() || |
| (...skipping 23 matching lines...) Expand all Loading... |
| 134 std::cout << "Max diff btwn original and encoded image (b,g,r,a) = (" | 132 std::cout << "Max diff btwn original and encoded image (b,g,r,a) = (" |
| 135 << maxv[0] << "," | 133 << maxv[0] << "," |
| 136 << maxv[1] << "," | 134 << maxv[1] << "," |
| 137 << maxv[2] << "," | 135 << maxv[2] << "," |
| 138 << maxv[3] << ")" << std::endl; | 136 << maxv[3] << ")" << std::endl; |
| 139 } | 137 } |
| 140 | 138 |
| 141 TEST_F(ThumbnailStoreTest, UpdateThumbnail) { | 139 TEST_F(ThumbnailStoreTest, UpdateThumbnail) { |
| 142 RefCountedBytes* read_image = NULL; | 140 RefCountedBytes* read_image = NULL; |
| 143 ThumbnailScore score2(0.1, true, true); | 141 ThumbnailScore score2(0.1, true, true); |
| 142 store_->cache_->clear(); |
| 143 store_->redirect_urls_->clear(); |
| 144 | 144 |
| 145 // store_ google_ with a low score, then weewar_ with a higher score | 145 // store_ google_ with a low score, then weewar_ with a higher score |
| 146 // and check that weewar_ overwrote google_. | 146 // and check that weewar_ overwrote google_. |
| 147 | 147 |
| 148 EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); | 148 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 149 EXPECT_TRUE(store_->SetPageThumbnail(url_, *weewar_, score2)); | 149 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *weewar_, score2, false)); |
| 150 | 150 |
| 151 EXPECT_TRUE(store_->GetPageThumbnail(url_, &read_image)); | 151 // Set fake redirects list. |
| 152 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 153 redirects->push_back(url1_); |
| 154 (*store_->redirect_urls_)[url1_] = new RefCountedVector<GURL>(*redirects); |
| 155 |
| 156 EXPECT_TRUE(store_->GetPageThumbnail(url1_, &read_image)); |
| 152 EXPECT_EQ(read_image->data.size(), jpeg_weewar_->data.size()); | 157 EXPECT_EQ(read_image->data.size(), jpeg_weewar_->data.size()); |
| 153 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_weewar_->data[0], | 158 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_weewar_->data[0], |
| 154 jpeg_weewar_->data.size())); | 159 jpeg_weewar_->data.size())); |
| 155 | 160 |
| 156 read_image->Release(); | 161 read_image->Release(); |
| 157 } | 162 } |
| 158 | 163 |
| 159 TEST_F(ThumbnailStoreTest, RetrieveFromCache) { | 164 TEST_F(ThumbnailStoreTest, RetrieveFromCache) { |
| 160 RefCountedBytes* read_image = NULL; | 165 RefCountedBytes* read_image = NULL; |
| 166 store_->cache_->clear(); |
| 167 store_->redirect_urls_->clear(); |
| 161 | 168 |
| 162 // Retrieve a thumbnail/score for a page not in the cache. | 169 // Retrieve a thumbnail/score for a page not in the cache. |
| 163 | 170 |
| 164 EXPECT_FALSE(store_->GetPageThumbnail(GURL("nonexistent"), &read_image)); | 171 EXPECT_FALSE(store_->GetPageThumbnail(url2_, &read_image)); |
| 165 | 172 |
| 166 // Store a thumbnail into the cache and retrieve it. | 173 // store_ a thumbnail into the cache and retrieve it. |
| 167 | 174 |
| 168 EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); | 175 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 169 EXPECT_TRUE(store_->GetPageThumbnail(url_, &read_image)); | 176 |
| 170 EXPECT_TRUE(score_.Equals((*store_->cache_)[url_].score_)); | 177 // Set fake redirects list. |
| 178 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 179 redirects->push_back(url1_); |
| 180 (*store_->redirect_urls_)[url1_] = new RefCountedVector<GURL>(*redirects); |
| 181 |
| 182 EXPECT_TRUE(store_->GetPageThumbnail(url1_, &read_image)); |
| 183 EXPECT_TRUE(score1_.Equals((*store_->cache_)[url1_].second)); |
| 171 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); | 184 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); |
| 172 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_google_->data[0], | 185 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_google_->data[0], |
| 173 jpeg_google_->data.size())); | 186 jpeg_google_->data.size())); |
| 174 | 187 |
| 175 read_image->Release(); | 188 read_image->Release(); |
| 176 } | 189 } |
| 177 | 190 |
| 178 TEST_F(ThumbnailStoreTest, RetrieveFromDisk) { | 191 TEST_F(ThumbnailStoreTest, RetrieveFromDisk) { |
| 179 EXPECT_TRUE(store_->SetPageThumbnail(url_, *google_, score_)); | 192 scoped_refptr<RefCountedBytes> read_image = new RefCountedBytes; |
| 193 ThumbnailScore score2; |
| 194 store_->cache_->clear(); |
| 195 store_->redirect_urls_->clear(); |
| 180 | 196 |
| 181 // Write the thumbnail to disk and retrieve it. | 197 // store_ a thumbnail onto the disk and retrieve it. |
| 182 | 198 |
| 183 store_->InitializeFromDB(db_name_, NULL); | 199 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 184 store_->CommitCacheToDB(NULL); // Write to the DB (dirty bit sould be set) | 200 EXPECT_TRUE(store_->WriteThumbnailToDisk(url1_, jpeg_google_, score1_)); |
| 185 store_->cache_->clear(); // Clear it from the cache. | 201 EXPECT_TRUE(store_->GetPageThumbnailFromDisk(file_path_.AppendASCII( |
| 186 | 202 MD5String(url1_.spec())), &url2_, read_image, &score2)); |
| 187 // Read from the DB. | 203 EXPECT_TRUE(url1_ == url2_); |
| 188 SQLITE_UNIQUE_STATEMENT(statement, *store_->statement_cache_, | 204 EXPECT_TRUE(score1_.Equals(score2)); |
| 189 "SELECT * FROM thumbnails"); | 205 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); |
| 190 EXPECT_TRUE(statement->step() == SQLITE_ROW); | 206 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_google_->data[0], |
| 191 GURL url(statement->column_string(0)); | |
| 192 ThumbnailScore score(statement->column_double(1), | |
| 193 statement->column_bool(2), | |
| 194 statement->column_bool(3), | |
| 195 base::Time::FromInternalValue( | |
| 196 statement->column_int64(4))); | |
| 197 scoped_refptr<RefCountedBytes> data = new RefCountedBytes; | |
| 198 EXPECT_TRUE(statement->column_blob_as_vector(5, &data->data)); | |
| 199 | |
| 200 EXPECT_TRUE(url == url_); | |
| 201 EXPECT_TRUE(score.Equals(score_)); | |
| 202 EXPECT_TRUE(data->data.size() == jpeg_google_->data.size()); | |
| 203 EXPECT_EQ(0, memcmp(&data->data[0], &jpeg_google_->data[0], | |
| 204 jpeg_google_->data.size())); | 207 jpeg_google_->data.size())); |
| 205 } | 208 } |
| 206 | 209 |
| 207 TEST_F(ThumbnailStoreTest, FollowRedirects) { | 210 TEST_F(ThumbnailStoreTest, FollowRedirects) { |
| 208 RefCountedBytes* read_image = NULL; | 211 RefCountedBytes* read_image = NULL; |
| 209 std::vector<GURL> redirects; | 212 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 213 store_->cache_->clear(); |
| 214 store_->redirect_urls_->clear(); |
| 210 | 215 |
| 211 GURL my_url("google"); | 216 GURL my_url("google"); |
| 212 redirects.push_back(GURL("google.com")); | 217 redirects->push_back(GURL("google.com")); |
| 213 redirects.push_back(GURL("www.google.com")); | 218 redirects->push_back(GURL("www.google.com")); |
| 214 redirects.push_back(url_); // url_ = http://www.google.com/ | 219 redirects->push_back(url1_); // url1_ = http://www.google.com/ |
| 215 (*store_->redirect_urls_)[my_url] = new RefCountedVector<GURL>(redirects); | |
| 216 | 220 |
| 217 store_->most_visited_urls_->push_back(my_url); | 221 store_->most_visited_urls_->push_back(my_url); |
| 218 | 222 |
| 219 EXPECT_TRUE(store_->SetPageThumbnail(GURL("google.com"), *google_, score_)); | 223 (*store_->redirect_urls_)[my_url] = new RefCountedVector<GURL>(*redirects); |
| 224 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 225 EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); |
| 226 |
| 227 read_image->Release(); |
| 228 store_->cache_->erase(store_->cache_->find(url1_)); |
| 229 |
| 230 EXPECT_TRUE(store_->SetPageThumbnail(GURL("google.com"), *google_, score1_, |
| 231 false)); |
| 220 EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); | 232 EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); |
| 221 | 233 |
| 222 read_image->Release(); | 234 read_image->Release(); |
| 223 } | 235 } |
| OLD | NEW |