| 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" |
| 16 #include "base/md5.h" | 15 #include "base/md5.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 44 file_util::Delete(file_path_.AppendASCII(url1_.host()), false); | 43 file_util::Delete(file_path_.AppendASCII(url1_.host()), false); |
| 45 file_util::Delete(file_path_.AppendASCII(url2_.host()), false); | 44 file_util::Delete(file_path_.AppendASCII(url2_.host()), false); |
| 46 } | 45 } |
| 47 | 46 |
| 48 // Compute the max difference over all pixels for each RGBA component. | 47 // Compute the max difference over all pixels for each RGBA component. |
| 49 void PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b); | 48 void PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b); |
| 50 | 49 |
| 51 // The directory where ThumbnailStore will store data. | 50 // The directory where ThumbnailStore will store data. |
| 52 FilePath file_path_; | 51 FilePath file_path_; |
| 53 | 52 |
| 53 scoped_refptr<ThumbnailStore> store_; |
| 54 scoped_ptr<SkBitmap> google_; | 54 scoped_ptr<SkBitmap> google_; |
| 55 scoped_ptr<SkBitmap> weewar_; | 55 scoped_ptr<SkBitmap> weewar_; |
| 56 scoped_refptr<RefCountedBytes> jpeg_google_; | 56 scoped_refptr<RefCountedBytes> jpeg_google_; |
| 57 scoped_refptr<RefCountedBytes> jpeg_weewar_; | 57 scoped_refptr<RefCountedBytes> jpeg_weewar_; |
| 58 ThumbnailScore score1_; | 58 ThumbnailScore score1_; |
| 59 GURL url1_, url2_; | 59 GURL url1_, url2_; |
| 60 base::Time time_; | 60 base::Time time_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 void ThumbnailStoreTest::SetUp() { | 63 void ThumbnailStoreTest::SetUp() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 &(jpeg_google_->data)); | 81 &(jpeg_google_->data)); |
| 82 | 82 |
| 83 SkAutoLockPixels lock2(*weewar_); | 83 SkAutoLockPixels lock2(*weewar_); |
| 84 jpeg_weewar_ = new RefCountedBytes; | 84 jpeg_weewar_ = new RefCountedBytes; |
| 85 JPEGCodec::Encode( | 85 JPEGCodec::Encode( |
| 86 reinterpret_cast<unsigned char*>(weewar_->getAddr32(0,0)), | 86 reinterpret_cast<unsigned char*>(weewar_->getAddr32(0,0)), |
| 87 JPEGCodec::FORMAT_BGRA, weewar_->width(), | 87 JPEGCodec::FORMAT_BGRA, weewar_->width(), |
| 88 weewar_->height(), | 88 weewar_->height(), |
| 89 static_cast<int>(weewar_->rowBytes()), 90, | 89 static_cast<int>(weewar_->rowBytes()), 90, |
| 90 &(jpeg_weewar_->data)); | 90 &(jpeg_weewar_->data)); |
| 91 |
| 92 store_ = new ThumbnailStore; |
| 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_); |
| 97 store_->cache_.reset(new ThumbnailStore::Cache); |
| 98 store_->redirect_urls_.reset(new history::RedirectMap); |
| 91 } | 99 } |
| 92 | 100 |
| 93 void ThumbnailStoreTest::PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b) { | 101 void ThumbnailStoreTest::PrintPixelDiff(SkBitmap* image_a, SkBitmap* image_b) { |
| 94 // 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 |
| 95 // pixels between the retrieved SkBitmap and the original. These | 103 // pixels between the retrieved SkBitmap and the original. These |
| 96 // differences should be small since encoding was done at 90% before | 104 // differences should be small since encoding was done at 90% before |
| 97 // writing to disk. | 105 // writing to disk. |
| 98 | 106 |
| 99 if (image_a->height() != image_b->height() || | 107 if (image_a->height() != image_b->height() || |
| 100 image_b->width() != image_b->width() || | 108 image_b->width() != image_b->width() || |
| (...skipping 21 matching lines...) Expand all Loading... |
| 122 } | 130 } |
| 123 | 131 |
| 124 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) = (" |
| 125 << maxv[0] << "," | 133 << maxv[0] << "," |
| 126 << maxv[1] << "," | 134 << maxv[1] << "," |
| 127 << maxv[2] << "," | 135 << maxv[2] << "," |
| 128 << maxv[3] << ")" << std::endl; | 136 << maxv[3] << ")" << std::endl; |
| 129 } | 137 } |
| 130 | 138 |
| 131 TEST_F(ThumbnailStoreTest, UpdateThumbnail) { | 139 TEST_F(ThumbnailStoreTest, UpdateThumbnail) { |
| 132 scoped_refptr<ThumbnailStore> store = new ThumbnailStore; | |
| 133 RefCountedBytes* read_image = NULL; | 140 RefCountedBytes* read_image = NULL; |
| 134 ThumbnailScore score2(0.1, true, true); | 141 ThumbnailScore score2(0.1, true, true); |
| 142 store_->cache_->clear(); |
| 143 store_->redirect_urls_->clear(); |
| 135 | 144 |
| 136 store->file_path_ = file_path_; | 145 // store_ google_ with a low score, then weewar_ with a higher score |
| 137 store->cache_.reset(new ThumbnailStore::Cache); | |
| 138 store->cache_initialized_ = true; | |
| 139 | |
| 140 // Store google_ with a low score, then weewar_ with a higher score | |
| 141 // and check that weewar_ overwrote google_. | 146 // and check that weewar_ overwrote google_. |
| 142 | 147 |
| 143 EXPECT_TRUE(store->SetPageThumbnail(url1_, *google_, score1_, false)); | 148 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 144 EXPECT_TRUE(store->SetPageThumbnail(url1_, *weewar_, score2, false)); | 149 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *weewar_, score2, false)); |
| 145 | 150 |
| 146 // Set fake redirects list. | 151 // Set fake redirects list. |
| 147 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); | 152 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 148 redirects->push_back(url1_); | 153 redirects->push_back(url1_); |
| 149 store->redirect_urls_[url1_] = new RefCountedVector<GURL>(*redirects); | 154 (*store_->redirect_urls_)[url1_] = new RefCountedVector<GURL>(*redirects); |
| 150 | 155 |
| 151 EXPECT_EQ(store->GetPageThumbnail(url1_, &read_image), | 156 EXPECT_TRUE(store_->GetPageThumbnail(url1_, &read_image)); |
| 152 ThumbnailStore::SUCCESS); | |
| 153 EXPECT_EQ(read_image->data.size(), jpeg_weewar_->data.size()); | 157 EXPECT_EQ(read_image->data.size(), jpeg_weewar_->data.size()); |
| 154 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], |
| 155 jpeg_weewar_->data.size())); | 159 jpeg_weewar_->data.size())); |
| 156 | 160 |
| 157 read_image->Release(); | 161 read_image->Release(); |
| 158 } | 162 } |
| 159 | 163 |
| 160 TEST_F(ThumbnailStoreTest, RetrieveFromCache) { | 164 TEST_F(ThumbnailStoreTest, RetrieveFromCache) { |
| 161 RefCountedBytes* read_image = NULL; | 165 RefCountedBytes* read_image = NULL; |
| 162 scoped_refptr<ThumbnailStore> store = new ThumbnailStore; | 166 store_->cache_->clear(); |
| 163 | 167 store_->redirect_urls_->clear(); |
| 164 store->file_path_ = file_path_; | |
| 165 store->cache_.reset(new ThumbnailStore::Cache); | |
| 166 store->cache_initialized_ = true; | |
| 167 | 168 |
| 168 // Retrieve a thumbnail/score for a page not in the cache. | 169 // Retrieve a thumbnail/score for a page not in the cache. |
| 169 | 170 |
| 170 EXPECT_EQ(store->GetPageThumbnail(url2_, &read_image), | 171 EXPECT_FALSE(store_->GetPageThumbnail(url2_, &read_image)); |
| 171 ThumbnailStore::ASYNC); | |
| 172 | 172 |
| 173 // Store a thumbnail into the cache and retrieve it. | 173 // store_ a thumbnail into the cache and retrieve it. |
| 174 | 174 |
| 175 EXPECT_TRUE(store->SetPageThumbnail(url1_, *google_, score1_, false)); | 175 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 176 | 176 |
| 177 // Set fake redirects list. | 177 // Set fake redirects list. |
| 178 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); | 178 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 179 redirects->push_back(url1_); | 179 redirects->push_back(url1_); |
| 180 store->redirect_urls_[url1_] = new RefCountedVector<GURL>(*redirects); | 180 (*store_->redirect_urls_)[url1_] = new RefCountedVector<GURL>(*redirects); |
| 181 | 181 |
| 182 EXPECT_EQ(store->GetPageThumbnail(url1_, &read_image), | 182 EXPECT_TRUE(store_->GetPageThumbnail(url1_, &read_image)); |
| 183 ThumbnailStore::SUCCESS); | 183 EXPECT_TRUE(score1_.Equals((*store_->cache_)[url1_].second)); |
| 184 EXPECT_TRUE(score1_.Equals((*store->cache_)[url1_].second)); | |
| 185 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); | 184 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); |
| 186 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], |
| 187 jpeg_google_->data.size())); | 186 jpeg_google_->data.size())); |
| 188 | 187 |
| 189 read_image->Release(); | 188 read_image->Release(); |
| 190 } | 189 } |
| 191 | 190 |
| 192 TEST_F(ThumbnailStoreTest, RetrieveFromDisk) { | 191 TEST_F(ThumbnailStoreTest, RetrieveFromDisk) { |
| 193 scoped_refptr<RefCountedBytes> read_image = new RefCountedBytes; | 192 scoped_refptr<RefCountedBytes> read_image = new RefCountedBytes; |
| 194 scoped_refptr<ThumbnailStore> store = new ThumbnailStore; | |
| 195 ThumbnailScore score2; | 193 ThumbnailScore score2; |
| 194 store_->cache_->clear(); |
| 195 store_->redirect_urls_->clear(); |
| 196 | 196 |
| 197 store->file_path_ = file_path_; | 197 // store_ a thumbnail onto the disk and retrieve it. |
| 198 store->cache_.reset(new ThumbnailStore::Cache); | |
| 199 store->cache_initialized_ = true; | |
| 200 | 198 |
| 201 // Store a thumbnail onto the disk and retrieve it. | 199 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 202 | 200 EXPECT_TRUE(store_->WriteThumbnailToDisk(url1_, jpeg_google_, score1_)); |
| 203 EXPECT_TRUE(store->SetPageThumbnail(url1_, *google_, score1_, false)); | 201 EXPECT_TRUE(store_->GetPageThumbnailFromDisk(file_path_.AppendASCII( |
| 204 | |
| 205 ThumbnailStore::Cache::iterator it = store->cache_->find(url1_); | |
| 206 DCHECK(it != store->cache_->end()); | |
| 207 | |
| 208 EXPECT_TRUE(store->WriteThumbnailToDisk(url1_, it->second.first, | |
| 209 it->second.second)); | |
| 210 EXPECT_TRUE(store->GetPageThumbnailFromDisk(file_path_.AppendASCII( | |
| 211 MD5String(url1_.spec())), &url2_, read_image, &score2)); | 202 MD5String(url1_.spec())), &url2_, read_image, &score2)); |
| 212 EXPECT_TRUE(url1_ == url2_); | 203 EXPECT_TRUE(url1_ == url2_); |
| 213 EXPECT_TRUE(score1_.Equals(score2)); | 204 EXPECT_TRUE(score1_.Equals(score2)); |
| 214 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); | 205 EXPECT_TRUE(read_image->data.size() == jpeg_google_->data.size()); |
| 215 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_google_->data[0], | 206 EXPECT_EQ(0, memcmp(&read_image->data[0], &jpeg_google_->data[0], |
| 216 jpeg_google_->data.size())); | 207 jpeg_google_->data.size())); |
| 217 } | 208 } |
| 218 | 209 |
| 219 TEST_F(ThumbnailStoreTest, FollowRedirects) { | 210 TEST_F(ThumbnailStoreTest, FollowRedirects) { |
| 220 RefCountedBytes* read_image = NULL; | 211 RefCountedBytes* read_image = NULL; |
| 221 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); | 212 scoped_ptr<std::vector<GURL> > redirects(new std::vector<GURL>); |
| 222 scoped_refptr<ThumbnailStore> store = new ThumbnailStore; | 213 store_->cache_->clear(); |
| 223 | 214 store_->redirect_urls_->clear(); |
| 224 store->file_path_ = file_path_; | |
| 225 store->cache_.reset(new ThumbnailStore::Cache); | |
| 226 store->cache_initialized_ = true; | |
| 227 | 215 |
| 228 GURL my_url("google"); | 216 GURL my_url("google"); |
| 229 redirects->push_back(GURL("google.com")); | 217 redirects->push_back(GURL("google.com")); |
| 230 redirects->push_back(GURL("www.google.com")); | 218 redirects->push_back(GURL("www.google.com")); |
| 231 redirects->push_back(url1_); // url1_ = http://www.google.com/ | 219 redirects->push_back(url1_); // url1_ = http://www.google.com/ |
| 232 | 220 |
| 233 store->redirect_urls_[my_url] = new RefCountedVector<GURL>(*redirects); | 221 store_->most_visited_urls_->push_back(my_url); |
| 234 EXPECT_TRUE(store->SetPageThumbnail(url1_, *google_, score1_, false)); | 222 |
| 235 EXPECT_EQ(store->GetPageThumbnail(my_url, &read_image), | 223 (*store_->redirect_urls_)[my_url] = new RefCountedVector<GURL>(*redirects); |
| 236 ThumbnailStore::SUCCESS); | 224 EXPECT_TRUE(store_->SetPageThumbnail(url1_, *google_, score1_, false)); |
| 225 EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); |
| 237 | 226 |
| 238 read_image->Release(); | 227 read_image->Release(); |
| 239 store->cache_->erase(store->cache_->find(url1_)); | 228 store_->cache_->erase(store_->cache_->find(url1_)); |
| 240 | 229 |
| 241 EXPECT_TRUE(store->SetPageThumbnail(GURL("google.com"), *google_, score1_, | 230 EXPECT_TRUE(store_->SetPageThumbnail(GURL("google.com"), *google_, score1_, |
| 242 false)); | 231 false)); |
| 243 EXPECT_EQ(store->GetPageThumbnail(my_url, &read_image), | 232 EXPECT_TRUE(store_->GetPageThumbnail(my_url, &read_image)); |
| 244 ThumbnailStore::SUCCESS); | |
| 245 | 233 |
| 246 read_image->Release(); | 234 read_image->Release(); |
| 247 } | 235 } |
| OLD | NEW |