| 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/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "components/enhanced_bookmarks/image_store_util.h" | 9 #include "components/enhanced_bookmarks/image_store_util.h" |
| 10 #include "components/enhanced_bookmarks/persistent_image_store.h" | 10 #include "components/enhanced_bookmarks/persistent_image_store.h" |
| 11 #include "components/enhanced_bookmarks/test_image_store.h" | 11 #include "components/enhanced_bookmarks/test_image_store.h" |
| 12 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const SkBitmap CreateBitmap(int width, int height, int a, int r, int g, int b) { | 18 gfx::Image CreateImage(int width, int height, int a, int r, int g, int b) { |
| 19 SkBitmap bitmap; | 19 SkBitmap bitmap; |
| 20 bitmap.allocN32Pixels(width, height); | 20 bitmap.allocN32Pixels(width, height); |
| 21 bitmap.eraseARGB(a, r, g, b); | 21 bitmap.eraseARGB(a, r, g, b); |
| 22 return bitmap; | 22 gfx::Image image(gfx::Image::CreateFrom1xBitmap(bitmap)); |
| 23 |
| 24 #if defined(OS_IOS) |
| 25 // Make sure the image has a kImageRepCocoaTouch. |
| 26 image.ToUIImage(); |
| 27 #endif // defined(OS_IOS) |
| 28 |
| 29 return image; |
| 23 } | 30 } |
| 24 | 31 |
| 25 gfx::Image GenerateWhiteImage() { | 32 gfx::Image GenerateWhiteImage() { |
| 26 return gfx::Image::CreateFrom1xBitmap( | 33 return CreateImage(42, 24, 255, 255, 255, 255); |
| 27 CreateBitmap(42, 24, 255, 255, 255, 255)); | |
| 28 } | 34 } |
| 29 | 35 |
| 30 gfx::Image GenerateBlackImage(int width, int height) { | 36 gfx::Image GenerateBlackImage(int width, int height) { |
| 31 return gfx::Image::CreateFrom1xBitmap( | 37 return CreateImage(width, height, 255, 0, 0, 0); |
| 32 CreateBitmap(width, height, 255, 0, 0, 0)); | |
| 33 } | 38 } |
| 34 | 39 |
| 35 gfx::Image GenerateBlackImage() { | 40 gfx::Image GenerateBlackImage() { |
| 36 return GenerateBlackImage(42, 24); | 41 return GenerateBlackImage(42, 24); |
| 37 } | 42 } |
| 38 | 43 |
| 39 // Returns true if the two images are identical. | 44 // Returns true if the two images are identical. |
| 40 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { | 45 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { |
| 41 if (image_1.IsEmpty() && image_2.IsEmpty()) | 46 if (image_1.IsEmpty() && image_2.IsEmpty()) |
| 42 return true; | 47 return true; |
| 43 | 48 |
| 44 if (image_1.IsEmpty() || image_2.IsEmpty()) | 49 if (image_1.IsEmpty() || image_2.IsEmpty()) |
| 45 return false; | 50 return false; |
| 46 | 51 |
| 47 scoped_refptr<base::RefCountedMemory> image_1_png = | 52 scoped_refptr<base::RefCountedMemory> image_1_bytes = |
| 48 enhanced_bookmarks::BytesForImage(image_1); | 53 enhanced_bookmarks::BytesForImage(image_1); |
| 49 scoped_refptr<base::RefCountedMemory> image_2_png = | 54 scoped_refptr<base::RefCountedMemory> image_2_bytes = |
| 50 enhanced_bookmarks::BytesForImage(image_2); | 55 enhanced_bookmarks::BytesForImage(image_2); |
| 51 | 56 |
| 52 if (image_1_png->size() != image_2_png->size()) | 57 if (image_1_bytes->size() != image_2_bytes->size()) |
| 53 return false; | 58 return false; |
| 54 | 59 |
| 55 return !memcmp(image_1_png->front(), | 60 return !memcmp(image_1_bytes->front(), |
| 56 image_2_png->front(), | 61 image_2_bytes->front(), |
| 57 image_1_png->size()); | 62 image_1_bytes->size()); |
| 58 } | 63 } |
| 59 | 64 |
| 60 // Factory functions for creating instances of the implementations. | 65 // Factory functions for creating instances of the implementations. |
| 61 template <class T> | 66 template <class T> |
| 62 ImageStore* CreateStore(base::ScopedTempDir& folder); | 67 ImageStore* CreateStore(base::ScopedTempDir& folder); |
| 63 | 68 |
| 64 template <> | 69 template <> |
| 65 ImageStore* CreateStore<TestImageStore>( | 70 ImageStore* CreateStore<TestImageStore>( |
| 66 base::ScopedTempDir& folder) { | 71 base::ScopedTempDir& folder) { |
| 67 return new TestImageStore(); | 72 return new TestImageStore(); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 1024); | 238 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 1024); |
| 234 } | 239 } |
| 235 for (int i = 0; i < 100; ++i) { | 240 for (int i = 0; i < 100; ++i) { |
| 236 this->store_->Insert( | 241 this->store_->Insert( |
| 237 GURL(url.spec() + '/' + base::IntToString(i)), image_url, src_image); | 242 GURL(url.spec() + '/' + base::IntToString(i)), image_url, src_image); |
| 238 EXPECT_GE(this->store_->GetStoreSizeInBytes(), size); | 243 EXPECT_GE(this->store_->GetStoreSizeInBytes(), size); |
| 239 size = this->store_->GetStoreSizeInBytes(); | 244 size = this->store_->GetStoreSizeInBytes(); |
| 240 } | 245 } |
| 241 | 246 |
| 242 if (this->use_persistent_store()) { | 247 if (this->use_persistent_store()) { |
| 243 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 100 * 1024); // 100kb | 248 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 90 * 1024); // 90kb |
| 244 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 200 * 1024); // 200kb | 249 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 200 * 1024); // 200kb |
| 245 } else { | 250 } else { |
| 246 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 400 * 1024); // 400kb | 251 EXPECT_GE(this->store_->GetStoreSizeInBytes(), 400 * 1024); // 400kb |
| 247 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 500 * 1024); // 500kb | 252 EXPECT_LE(this->store_->GetStoreSizeInBytes(), 500 * 1024); // 500kb |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 | 255 |
| 251 } // namespace | 256 } // namespace |
| OLD | NEW |