OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/enhanced_bookmarks/image_store.h" | 5 #include "components/enhanced_bookmarks/image_store.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "components/enhanced_bookmarks/image_record.h" | 10 #include "components/enhanced_bookmarks/image_record.h" |
11 #include "components/enhanced_bookmarks/image_store_util.h" | 11 #include "components/enhanced_bookmarks/image_store_util.h" |
12 #include "components/enhanced_bookmarks/persistent_image_store.h" | 12 #include "components/enhanced_bookmarks/persistent_image_store.h" |
13 #include "components/enhanced_bookmarks/test_image_store.h" | 13 #include "components/enhanced_bookmarks/test_image_store.h" |
14 #include "sql/statement.h" | 14 #include "sql/statement.h" |
15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
17 #include "third_party/skia/include/core/SkColor.h" | 17 #include "third_party/skia/include/core/SkColor.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 gfx::Image CreateImage(int width, int height, int a, int r, int g, int b) { | 22 scoped_ptr<gfx::Image> CreateImage( |
| 23 int width, int height, int a, int r, int g, int b) { |
23 SkBitmap bitmap; | 24 SkBitmap bitmap; |
24 bitmap.allocN32Pixels(width, height); | 25 bitmap.allocN32Pixels(width, height); |
25 bitmap.eraseARGB(a, r, g, b); | 26 bitmap.eraseARGB(a, r, g, b); |
26 gfx::Image image(gfx::Image::CreateFrom1xBitmap(bitmap)); | 27 scoped_ptr<gfx::Image> image( |
| 28 new gfx::Image(gfx::Image::CreateFrom1xBitmap(bitmap))); |
27 | 29 |
28 #if defined(OS_IOS) | 30 #if defined(OS_IOS) |
29 // Make sure the image has a kImageRepCocoaTouch. | 31 // Make sure the image has a kImageRepCocoaTouch. |
30 image.ToUIImage(); | 32 image->ToUIImage(); |
31 #endif // defined(OS_IOS) | 33 #endif // defined(OS_IOS) |
32 | 34 |
33 return image; | 35 return image; |
34 } | 36 } |
35 | 37 |
36 gfx::Image GenerateWhiteImage() { | 38 scoped_ptr<gfx::Image> GenerateWhiteImage() { |
37 return CreateImage(42, 24, 255, 255, 255, 255); | 39 return CreateImage(42, 24, 255, 255, 255, 255); |
38 } | 40 } |
39 | 41 |
40 gfx::Image GenerateBlackImage(int width, int height) { | 42 scoped_ptr<gfx::Image> GenerateBlackImage(int width, int height) { |
41 return CreateImage(width, height, 255, 0, 0, 0); | 43 return CreateImage(width, height, 255, 0, 0, 0); |
42 } | 44 } |
43 | 45 |
44 gfx::Image GenerateBlackImage() { | 46 scoped_ptr<gfx::Image> GenerateBlackImage() { |
45 return GenerateBlackImage(42, 24); | 47 return GenerateBlackImage(42, 24); |
46 } | 48 } |
47 | 49 |
48 // Returns true if the two images are identical. | 50 // Returns true if the two images are identical. |
49 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { | 51 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { |
50 if (image_1.IsEmpty() && image_2.IsEmpty()) | 52 if (image_1.IsEmpty() && image_2.IsEmpty()) |
51 return true; | 53 return true; |
52 | 54 |
53 if (image_1.IsEmpty() || image_2.IsEmpty()) | 55 if (image_1.IsEmpty() || image_2.IsEmpty()) |
54 return false; | 56 return false; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 if (!db.Execute(kV1IndexSql)) | 92 if (!db.Execute(kV1IndexSql)) |
91 return false; | 93 return false; |
92 | 94 |
93 sql::Statement statement(db.GetUniqueStatement( | 95 sql::Statement statement(db.GetUniqueStatement( |
94 "INSERT INTO images_by_url " | 96 "INSERT INTO images_by_url " |
95 "(page_url, image_url, image_data, width, height) " | 97 "(page_url, image_url, image_data, width, height) " |
96 "VALUES (?, ?, ?, ?, ?)")); | 98 "VALUES (?, ?, ?, ?, ?)")); |
97 statement.BindString(0, "foo://bar"); | 99 statement.BindString(0, "foo://bar"); |
98 statement.BindString(1, "http://a.jpg"); | 100 statement.BindString(1, "http://a.jpg"); |
99 scoped_refptr<base::RefCountedMemory> image_bytes = | 101 scoped_refptr<base::RefCountedMemory> image_bytes = |
100 enhanced_bookmarks::BytesForImage(GenerateWhiteImage()); | 102 enhanced_bookmarks::BytesForImage(*GenerateWhiteImage()); |
101 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); | 103 statement.BindBlob(2, image_bytes->front(), (int)image_bytes->size()); |
102 statement.BindInt(3, 42); | 104 statement.BindInt(3, 42); |
103 statement.BindInt(4, 24); | 105 statement.BindInt(4, 24); |
104 | 106 |
105 return statement.Run(); | 107 return statement.Run(); |
106 } | 108 } |
107 | 109 |
108 // Factory functions for creating instances of the implementations. | 110 // Factory functions for creating instances of the implementations. |
109 template <class T> | 111 template <class T> |
110 ImageStore* CreateStore(base::ScopedTempDir& folder); | 112 ImageStore* CreateStore(base::ScopedTempDir& folder); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 TYPED_TEST_CASE(ImageStoreUnitTest, Implementations); | 165 TYPED_TEST_CASE(ImageStoreUnitTest, Implementations); |
164 | 166 |
165 // All those tests are run on all the implementations. | 167 // All those tests are run on all the implementations. |
166 TYPED_TEST(ImageStoreUnitTest, StartsEmpty) { | 168 TYPED_TEST(ImageStoreUnitTest, StartsEmpty) { |
167 std::set<GURL> all_urls; | 169 std::set<GURL> all_urls; |
168 this->store_->GetAllPageUrls(&all_urls); | 170 this->store_->GetAllPageUrls(&all_urls); |
169 EXPECT_EQ(0u, all_urls.size()); | 171 EXPECT_EQ(0u, all_urls.size()); |
170 } | 172 } |
171 | 173 |
172 TYPED_TEST(ImageStoreUnitTest, StoreOne) { | 174 TYPED_TEST(ImageStoreUnitTest, StoreOne) { |
173 const enhanced_bookmarks::ImageRecord image( | 175 scoped_refptr<enhanced_bookmarks::ImageRecord> image( |
174 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK); | 176 new enhanced_bookmarks::ImageRecord( |
| 177 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK)); |
175 this->store_->Insert(GURL("foo://bar"), image); | 178 this->store_->Insert(GURL("foo://bar"), image); |
176 | 179 |
177 std::set<GURL> all_urls; | 180 std::set<GURL> all_urls; |
178 this->store_->GetAllPageUrls(&all_urls); | 181 this->store_->GetAllPageUrls(&all_urls); |
179 EXPECT_EQ(1u, all_urls.size()); | 182 EXPECT_EQ(1u, all_urls.size()); |
180 EXPECT_EQ(GURL("foo://bar"), *all_urls.begin()); | 183 EXPECT_EQ(GURL("foo://bar"), *all_urls.begin()); |
181 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); | 184 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); |
182 } | 185 } |
183 | 186 |
184 TYPED_TEST(ImageStoreUnitTest, Retrieve) { | 187 TYPED_TEST(ImageStoreUnitTest, Retrieve) { |
185 const GURL url("foo://bar"); | 188 const GURL url("foo://bar"); |
186 const enhanced_bookmarks::ImageRecord image_in( | 189 scoped_refptr<enhanced_bookmarks::ImageRecord> image_in( |
187 CreateImage(42, 24, 1, 0, 0, 1), GURL("http://a.jpg"), SK_ColorBLUE); | 190 new enhanced_bookmarks::ImageRecord( |
| 191 CreateImage(42, 24, 1, 0, 0, 1), GURL("http://a.jpg"), SK_ColorBLUE)); |
188 this->store_->Insert(url, image_in); | 192 this->store_->Insert(url, image_in); |
189 | 193 |
190 const enhanced_bookmarks::ImageRecord image_out = this->store_->Get(url); | 194 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out = |
| 195 this->store_->Get(url); |
191 const gfx::Size size = this->store_->GetSize(url); | 196 const gfx::Size size = this->store_->GetSize(url); |
192 | 197 |
193 EXPECT_EQ(42, size.width()); | 198 EXPECT_EQ(42, size.width()); |
194 EXPECT_EQ(24, size.height()); | 199 EXPECT_EQ(24, size.height()); |
195 EXPECT_EQ(image_in.url, image_out.url); | 200 EXPECT_EQ(image_in->url, image_out->url); |
196 EXPECT_TRUE(CompareImages(image_in.image, image_out.image)); | 201 EXPECT_TRUE(CompareImages(*image_in->image, *image_out->image)); |
197 EXPECT_EQ(SK_ColorBLUE, image_out.dominant_color); | 202 EXPECT_EQ(SK_ColorBLUE, image_out->dominant_color); |
198 } | 203 } |
199 | 204 |
200 TYPED_TEST(ImageStoreUnitTest, Erase) { | 205 TYPED_TEST(ImageStoreUnitTest, Erase) { |
201 const GURL url("foo://bar"); | 206 const GURL url("foo://bar"); |
202 const enhanced_bookmarks::ImageRecord image( | 207 scoped_refptr<enhanced_bookmarks::ImageRecord> image( |
203 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK); | 208 new enhanced_bookmarks::ImageRecord( |
| 209 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK)); |
204 this->store_->Insert(url, image); | 210 this->store_->Insert(url, image); |
205 this->store_->Erase(url); | 211 this->store_->Erase(url); |
206 | 212 |
207 EXPECT_FALSE(this->store_->HasKey(url)); | 213 EXPECT_FALSE(this->store_->HasKey(url)); |
208 std::set<GURL> all_urls; | 214 std::set<GURL> all_urls; |
209 this->store_->GetAllPageUrls(&all_urls); | 215 this->store_->GetAllPageUrls(&all_urls); |
210 EXPECT_EQ(0u, all_urls.size()); | 216 EXPECT_EQ(0u, all_urls.size()); |
211 } | 217 } |
212 | 218 |
213 TYPED_TEST(ImageStoreUnitTest, ClearAll) { | 219 TYPED_TEST(ImageStoreUnitTest, ClearAll) { |
214 const GURL url_foo("http://foo"); | 220 const GURL url_foo("http://foo"); |
215 const enhanced_bookmarks::ImageRecord black_image( | 221 scoped_refptr<enhanced_bookmarks::ImageRecord> black_image( |
216 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK); | 222 new enhanced_bookmarks::ImageRecord( |
| 223 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK)); |
217 this->store_->Insert(url_foo, black_image); | 224 this->store_->Insert(url_foo, black_image); |
218 const GURL url_bar("http://bar"); | 225 const GURL url_bar("http://bar"); |
219 const enhanced_bookmarks::ImageRecord white_image( | 226 scoped_refptr<enhanced_bookmarks::ImageRecord> white_image( |
220 GenerateWhiteImage(), GURL("http://a.jpg"), SK_ColorWHITE); | 227 new enhanced_bookmarks::ImageRecord( |
| 228 GenerateWhiteImage(), GURL("http://a.jpg"), SK_ColorWHITE)); |
221 this->store_->Insert(url_bar, white_image); | 229 this->store_->Insert(url_bar, white_image); |
222 | 230 |
223 this->store_->ClearAll(); | 231 this->store_->ClearAll(); |
224 | 232 |
225 EXPECT_FALSE(this->store_->HasKey(url_foo)); | 233 EXPECT_FALSE(this->store_->HasKey(url_foo)); |
226 EXPECT_FALSE(this->store_->HasKey(url_bar)); | 234 EXPECT_FALSE(this->store_->HasKey(url_bar)); |
227 std::set<GURL> all_urls; | 235 std::set<GURL> all_urls; |
228 this->store_->GetAllPageUrls(&all_urls); | 236 this->store_->GetAllPageUrls(&all_urls); |
229 EXPECT_EQ(0u, all_urls.size()); | 237 EXPECT_EQ(0u, all_urls.size()); |
230 } | 238 } |
231 | 239 |
232 TYPED_TEST(ImageStoreUnitTest, Update) { | 240 TYPED_TEST(ImageStoreUnitTest, Update) { |
233 const GURL url("foo://bar"); | 241 const GURL url("foo://bar"); |
234 const enhanced_bookmarks::ImageRecord image1(GenerateWhiteImage(), | 242 |
235 GURL("1.jpg"), SK_ColorWHITE); | 243 scoped_refptr<enhanced_bookmarks::ImageRecord> image1( |
| 244 new enhanced_bookmarks::ImageRecord( |
| 245 GenerateWhiteImage(), GURL("1.jpg"), SK_ColorWHITE)); |
236 this->store_->Insert(url, image1); | 246 this->store_->Insert(url, image1); |
237 | 247 |
238 const enhanced_bookmarks::ImageRecord image2(GenerateBlackImage(), | 248 scoped_refptr<enhanced_bookmarks::ImageRecord> image2( |
239 GURL("2.jpg"), SK_ColorBLACK); | 249 new enhanced_bookmarks::ImageRecord( |
| 250 GenerateBlackImage(), GURL("2.jpg"), SK_ColorBLACK)); |
240 this->store_->Insert(url, image2); | 251 this->store_->Insert(url, image2); |
241 | 252 |
242 const enhanced_bookmarks::ImageRecord image_out = this->store_->Get(url); | 253 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out = |
| 254 this->store_->Get(url); |
243 | 255 |
244 EXPECT_TRUE(this->store_->HasKey(url)); | 256 EXPECT_TRUE(this->store_->HasKey(url)); |
245 std::set<GURL> all_urls; | 257 std::set<GURL> all_urls; |
246 this->store_->GetAllPageUrls(&all_urls); | 258 this->store_->GetAllPageUrls(&all_urls); |
247 EXPECT_EQ(1u, all_urls.size()); | 259 EXPECT_EQ(1u, all_urls.size()); |
248 EXPECT_EQ(image2.url, image_out.url); | 260 EXPECT_EQ(image2->url, image_out->url); |
249 EXPECT_TRUE(CompareImages(image2.image, image_out.image)); | 261 EXPECT_TRUE(CompareImages(*image2->image, *image_out->image)); |
250 EXPECT_EQ(SK_ColorBLACK, image_out.dominant_color); | 262 EXPECT_EQ(SK_ColorBLACK, image_out->dominant_color); |
251 } | 263 } |
252 | 264 |
253 TYPED_TEST(ImageStoreUnitTest, Persistence) { | 265 TYPED_TEST(ImageStoreUnitTest, Persistence) { |
254 const GURL url("foo://bar"); | 266 const GURL url("foo://bar"); |
255 const enhanced_bookmarks::ImageRecord image_in( | 267 scoped_refptr<enhanced_bookmarks::ImageRecord> image_in( |
256 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK); | 268 new enhanced_bookmarks::ImageRecord( |
| 269 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK)); |
257 this->store_->Insert(url, image_in); | 270 this->store_->Insert(url, image_in); |
258 | 271 |
259 this->ResetStore(); | 272 this->ResetStore(); |
260 if (this->use_persistent_store()) { | 273 if (this->use_persistent_store()) { |
261 std::set<GURL> all_urls; | 274 std::set<GURL> all_urls; |
262 this->store_->GetAllPageUrls(&all_urls); | 275 this->store_->GetAllPageUrls(&all_urls); |
263 EXPECT_EQ(1u, all_urls.size()); | 276 EXPECT_EQ(1u, all_urls.size()); |
264 EXPECT_EQ(url, *all_urls.begin()); | 277 EXPECT_EQ(url, *all_urls.begin()); |
265 EXPECT_TRUE(this->store_->HasKey(url)); | 278 EXPECT_TRUE(this->store_->HasKey(url)); |
266 const enhanced_bookmarks::ImageRecord image_out = this->store_->Get(url); | 279 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out = |
| 280 this->store_->Get(url); |
267 | 281 |
268 EXPECT_EQ(image_in.url, image_out.url); | 282 EXPECT_EQ(image_in->url, image_out->url); |
269 EXPECT_TRUE(CompareImages(image_in.image, image_out.image)); | 283 EXPECT_TRUE(CompareImages(*image_in->image, *image_out->image)); |
270 EXPECT_EQ(image_in.dominant_color, image_out.dominant_color); | 284 EXPECT_EQ(image_in->dominant_color, image_out->dominant_color); |
271 } else { | 285 } else { |
272 std::set<GURL> all_urls; | 286 std::set<GURL> all_urls; |
273 this->store_->GetAllPageUrls(&all_urls); | 287 this->store_->GetAllPageUrls(&all_urls); |
274 EXPECT_EQ(0u, all_urls.size()); | 288 EXPECT_EQ(0u, all_urls.size()); |
275 EXPECT_FALSE(this->store_->HasKey(url)); | 289 EXPECT_FALSE(this->store_->HasKey(url)); |
276 } | 290 } |
277 } | 291 } |
278 | 292 |
279 TYPED_TEST(ImageStoreUnitTest, MigrationToV2) { | 293 TYPED_TEST(ImageStoreUnitTest, MigrationToV2) { |
280 // Migration is available only with persistent stores. | 294 // Migration is available only with persistent stores. |
281 if (!this->use_persistent_store()) | 295 if (!this->use_persistent_store()) |
282 return; | 296 return; |
283 | 297 |
284 // Set up v1 DB. | 298 // Set up v1 DB. |
285 EXPECT_TRUE(CreateV1PersistentImageStoreDB(this->tempDir_.path().Append( | 299 EXPECT_TRUE(CreateV1PersistentImageStoreDB(this->tempDir_.path().Append( |
286 base::FilePath::FromUTF8Unsafe("BookmarkImageAndUrlStore.db")))); | 300 base::FilePath::FromUTF8Unsafe("BookmarkImageAndUrlStore.db")))); |
287 | 301 |
288 const enhanced_bookmarks::ImageRecord image_out = | 302 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out = |
289 this->store_->Get(GURL("foo://bar")); | 303 this->store_->Get(GURL("foo://bar")); |
290 EXPECT_EQ(SK_ColorWHITE, image_out.dominant_color); | 304 EXPECT_EQ(SK_ColorWHITE, image_out->dominant_color); |
291 } | 305 } |
292 | 306 |
293 TYPED_TEST(ImageStoreUnitTest, GetSize) { | 307 TYPED_TEST(ImageStoreUnitTest, GetSize) { |
294 const GURL url("foo://bar"); | 308 const GURL url("foo://bar"); |
295 const enhanced_bookmarks::ImageRecord image_in( | 309 scoped_refptr<enhanced_bookmarks::ImageRecord> image_in( |
296 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK); | 310 new enhanced_bookmarks::ImageRecord( |
| 311 GenerateBlackImage(), GURL("http://a.jpg"), SK_ColorBLACK)); |
297 | 312 |
298 int64 size = 0; | 313 int64 size = 0; |
299 if (this->use_persistent_store()) { | 314 if (this->use_persistent_store()) { |
300 // File shouldn't exist before we actually start using it since we do lazy | 315 // File shouldn't exist before we actually start using it since we do lazy |
301 // initialization. | 316 // initialization. |
302 EXPECT_EQ(this->store_->GetStoreSizeInBytes(), -1); | 317 EXPECT_EQ(this->store_->GetStoreSizeInBytes(), -1); |
303 } else { | 318 } else { |
304 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 1024); | 319 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 1024); |
305 } | 320 } |
306 for (int i = 0; i < 100; ++i) { | 321 for (int i = 0; i < 100; ++i) { |
307 this->store_->Insert(GURL(url.spec() + '/' + base::IntToString(i)), | 322 this->store_->Insert(GURL(url.spec() + '/' + base::IntToString(i)), |
308 image_in); | 323 image_in); |
309 EXPECT_GE(this->store_->GetStoreSizeInBytes(), size); | 324 EXPECT_GE(this->store_->GetStoreSizeInBytes(), size); |
310 size = this->store_->GetStoreSizeInBytes(); | 325 size = this->store_->GetStoreSizeInBytes(); |
311 } | 326 } |
312 | 327 |
313 if (this->use_persistent_store()) { | 328 if (this->use_persistent_store()) { |
314 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 80 * 1024); // 80kb | 329 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 80 * 1024); // 80kb |
315 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 200 * 1024); // 200kb | 330 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 200 * 1024); // 200kb |
316 } else { | 331 } else { |
317 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 400 * 1024); // 400kb | 332 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 400 * 1024); // 400kb |
318 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 500 * 1024); // 500kb | 333 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 500 * 1024); // 500kb |
319 } | 334 } |
320 } | 335 } |
321 | 336 |
322 } // namespace | 337 } // namespace |
OLD | NEW |