Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/files/scoped_temp_dir.h" | |
| 6 #include "chrome/browser/bookmarks/enhanced/image_store.h" | |
|
sky
2014/04/30 18:09:13
nit: this should be your first include (use same o
Kibeom Kim (inactive)
2014/05/01 19:02:26
Done.
| |
| 7 #include "testing/platform_test.h" | |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | |
| 9 | |
| 10 #if defined(OS_IOS) | |
| 11 #include "chrome/browser/bookmarks/enhanced/image_store_ios.h" | |
| 12 #endif // defined(OS_IOS) | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const SkBitmap CreateBitmap(int width, int height) { | |
| 17 SkBitmap bitmap; | |
| 18 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 19 bitmap.allocPixels(); | |
| 20 bitmap.eraseARGB(rand()%255, rand()%255, rand()%255, rand()%255); | |
|
sky
2014/04/30 18:09:13
Why the random here? Also, use spaces, eg rand() %
Kibeom Kim (inactive)
2014/05/01 19:02:26
Done.
| |
| 21 return bitmap; | |
| 22 } | |
| 23 | |
| 24 gfx::Image GenerateRandomImage() { | |
| 25 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(42, 24)); | |
| 26 } | |
| 27 | |
| 28 // Returns true if the two images are identical. | |
| 29 bool CompareImages(gfx::Image image_1, gfx::Image image_2) { | |
|
sky
2014/04/30 18:09:13
const gfx::Image&?
Kibeom Kim (inactive)
2014/05/01 19:02:26
Done.
| |
| 30 if (image_1.IsEmpty() && image_2.IsEmpty()) | |
| 31 return true; | |
| 32 | |
| 33 if (image_1.IsEmpty() || image_2.IsEmpty()) | |
| 34 return false; | |
| 35 | |
| 36 #if defined(OS_IOS) | |
| 37 // Forces the two images to have a UIImage representation. | |
| 38 image_1.ToUIImage(); | |
| 39 image_2.ToUIImage(); | |
| 40 scoped_refptr<base::RefCountedMemory> image_1_png( | |
| 41 image_store_ios::bytesForImage(image_1)); | |
| 42 scoped_refptr<base::RefCountedMemory> image_2_png( | |
| 43 image_store_ios::bytesForImage(image_2)); | |
| 44 #else | |
| 45 scoped_refptr<base::RefCountedMemory> image_1_png(image_1.As1xPNGBytes()); | |
| 46 scoped_refptr<base::RefCountedMemory> image_2_png(image_2.As1xPNGBytes()); | |
| 47 #endif // defined(OS_IOS) | |
| 48 | |
| 49 if (image_1_png->size() != image_2_png->size()) | |
| 50 return false; | |
| 51 | |
| 52 return !memcmp(image_1_png->front(), | |
| 53 image_2_png->front(), | |
| 54 image_1_png->size()); | |
| 55 } | |
| 56 | |
| 57 // Factory functions for creating instances of the implementations. | |
| 58 template <class T> | |
| 59 scoped_refptr<ImageStore> createStore(base::ScopedTempDir& folder); | |
| 60 | |
| 61 template <> | |
| 62 scoped_refptr<ImageStore> createStore<MemoryImageStore>( | |
| 63 base::ScopedTempDir& folder) { | |
| 64 return scoped_refptr<ImageStore>(new MemoryImageStore()); | |
| 65 } | |
| 66 | |
| 67 template <> | |
| 68 scoped_refptr<ImageStore> createStore<PersistentImageStore>( | |
| 69 base::ScopedTempDir& folder) { | |
| 70 return scoped_refptr<ImageStore>(new PersistentImageStore(folder.path())); | |
| 71 } | |
| 72 | |
| 73 // Methods to check if persistence is on or not. | |
| 74 template <class T> bool shouldPersist(); | |
| 75 template <> bool shouldPersist<MemoryImageStore>() { return false; } | |
| 76 template <> bool shouldPersist<PersistentImageStore>() { return true; } | |
| 77 | |
| 78 // Test fixture class template for the abstract API. | |
| 79 template <class T> | |
| 80 class ImageStoreUnitTest : public PlatformTest { | |
| 81 protected: | |
| 82 ImageStoreUnitTest() { | |
| 83 bool success = tempDir_.CreateUniqueTempDir(); | |
| 84 EXPECT_TRUE(success); | |
| 85 store_ = createStore<T>(tempDir_); | |
| 86 } | |
| 87 | |
| 88 void TearDown() OVERRIDE { | |
| 89 if (store_ && usePersistentStore()) | |
| 90 store_->ClearAll(); | |
| 91 } | |
| 92 | |
| 93 bool usePersistentStore() { return shouldPersist<T>(); } | |
| 94 void resetStore() { store_ = createStore<T>(tempDir_); } | |
| 95 virtual ~ImageStoreUnitTest() {} | |
| 96 // The directory the database is saved into. | |
| 97 base::ScopedTempDir tempDir_; | |
| 98 // The object the fixture is testing, via its base interface. | |
| 99 scoped_refptr<ImageStore> store_; | |
| 100 }; | |
| 101 | |
| 102 // The list of implementations of the abstract API that are going to be tested. | |
| 103 typedef testing::Types<MemoryImageStore, | |
| 104 PersistentImageStore> Implementations; | |
| 105 | |
| 106 TYPED_TEST_CASE(ImageStoreUnitTest, Implementations); | |
| 107 | |
| 108 // All those tests are run on all the implementations. | |
| 109 TYPED_TEST(ImageStoreUnitTest, startsEmpty) { | |
| 110 EXPECT_EQ(0ul, this->store_->AllKeys().size()); | |
| 111 } | |
| 112 | |
| 113 TYPED_TEST(ImageStoreUnitTest, storeOne) { | |
| 114 this->store_->Insert(GURL("foo://bar"), GURL("a.jpg"), GenerateRandomImage()); | |
| 115 EXPECT_EQ(1ul, this->store_->AllKeys().size()); | |
| 116 EXPECT_EQ(GURL("foo://bar"), this->store_->AllKeys()[0]); | |
| 117 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); | |
| 118 } | |
| 119 | |
| 120 TYPED_TEST(ImageStoreUnitTest, retrieve) { | |
| 121 gfx::Image src_image = GenerateRandomImage(); | |
| 122 const GURL url("foo://bar"); | |
| 123 const GURL image_url("a.jpg"); | |
| 124 this->store_->Insert(url, image_url, src_image); | |
| 125 GURL result; | |
| 126 gfx::Image dst_image = this->store_->Get(url, result); | |
| 127 | |
| 128 EXPECT_EQ(image_url, result); | |
| 129 EXPECT_TRUE(CompareImages(src_image, dst_image)); | |
| 130 } | |
| 131 | |
| 132 TYPED_TEST(ImageStoreUnitTest, erase) { | |
| 133 gfx::Image src_image = GenerateRandomImage(); | |
| 134 const GURL url("foo://bar"); | |
| 135 const GURL image_url("a.jpg"); | |
| 136 this->store_->Insert(url, image_url, src_image); | |
| 137 | |
| 138 this->store_->Erase(url); | |
| 139 EXPECT_FALSE(this->store_->HasKey(url)); | |
| 140 EXPECT_EQ(0ul, this->store_->AllKeys().size()); | |
| 141 } | |
| 142 | |
| 143 TYPED_TEST(ImageStoreUnitTest, update) { | |
| 144 gfx::Image src_image1 = GenerateRandomImage(); | |
| 145 gfx::Image src_image2 = GenerateRandomImage(); | |
| 146 const GURL url("foo://bar"); | |
| 147 const GURL image_url1("1.jpg"); | |
| 148 this->store_->Insert(url, image_url1, src_image1); | |
| 149 | |
| 150 const GURL image_url2("2.jpg"); | |
| 151 this->store_->Insert(url, image_url2, src_image2); | |
| 152 | |
| 153 GURL result; | |
| 154 gfx::Image dst_image = this->store_->Get(url, result); | |
| 155 | |
| 156 EXPECT_TRUE(this->store_->HasKey(url)); | |
| 157 EXPECT_EQ(1ul, this->store_->AllKeys().size()); | |
| 158 EXPECT_EQ(image_url2, result); | |
| 159 EXPECT_TRUE(CompareImages(src_image2, dst_image)); | |
| 160 } | |
| 161 | |
| 162 TYPED_TEST(ImageStoreUnitTest, persistence) { | |
| 163 gfx::Image src_image = GenerateRandomImage(); | |
| 164 const GURL url("foo://bar"); | |
| 165 const GURL image_url("a.jpg"); | |
| 166 this->store_->Insert(url, image_url, src_image); | |
| 167 | |
| 168 this->resetStore(); | |
| 169 if (this->usePersistentStore()) { | |
| 170 EXPECT_EQ(1ul, this->store_->AllKeys().size()); | |
| 171 EXPECT_EQ(GURL("foo://bar"), this->store_->AllKeys()[0]); | |
| 172 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); | |
| 173 GURL result; | |
| 174 gfx::Image dst_image = this->store_->Get(url, result); | |
| 175 | |
| 176 EXPECT_EQ(image_url, result); | |
| 177 EXPECT_TRUE(CompareImages(src_image, dst_image)); | |
| 178 } else { | |
| 179 EXPECT_EQ(0ul, this->store_->AllKeys().size()); | |
| 180 EXPECT_FALSE(this->store_->HasKey(GURL("foo://bar"))); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 #if defined(OS_IOS) | |
| 185 TYPED_TEST(ImageStoreUnitTest, retrieveUIImageWithScale) { | |
| 186 CGFloat scales[] = { 0.0, 1.0, 2.0 }; | |
| 187 gfx::Size image_size(42, 24); | |
| 188 for (unsigned long i = 0; i < arraysize(scales); i++) { | |
| 189 gfx::Image src_image(image_store_ios::generateRandomUIImage( | |
| 190 image_size, scales[i])); | |
| 191 const GURL url("foo://bar"); | |
| 192 const GURL image_url("a.jpg"); | |
| 193 this->store_->Insert(url, image_url, src_image); | |
| 194 GURL result; | |
| 195 gfx::Image dst_image = this->store_->Get(url, result); | |
| 196 | |
| 197 EXPECT_EQ(image_url, result); | |
| 198 EXPECT_TRUE(CompareImages(src_image, dst_image)); | |
| 199 } | |
| 200 } | |
| 201 #endif // defined(OS_IOS) | |
| 202 | |
| 203 } // namespace | |
| OLD | NEW |