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