OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/enhanced_bookmarks/image_store.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "components/enhanced_bookmarks/image_store_util.h" |
| 9 #include "components/enhanced_bookmarks/persistent_image_store.h" |
| 10 #include "components/enhanced_bookmarks/test_image_store.h" |
| 11 #include "testing/platform_test.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const SkBitmap CreateBitmap(int width, int height, int a, int r, int g, int b) { |
| 18 SkBitmap bitmap; |
| 19 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 20 bitmap.allocPixels(); |
| 21 bitmap.eraseARGB(a, r, g, b); |
| 22 return bitmap; |
| 23 } |
| 24 |
| 25 gfx::Image GenerateWhiteImage() { |
| 26 return gfx::Image::CreateFrom1xBitmap( |
| 27 CreateBitmap(42, 24, 255, 255, 255, 255)); |
| 28 } |
| 29 |
| 30 gfx::Image GenerateBlackImage(int width, int height) { |
| 31 return gfx::Image::CreateFrom1xBitmap( |
| 32 CreateBitmap(width, height, 255, 0, 0, 0)); |
| 33 } |
| 34 |
| 35 gfx::Image GenerateBlackImage() { |
| 36 return GenerateBlackImage(42, 24); |
| 37 } |
| 38 |
| 39 // Returns true if the two images are identical. |
| 40 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { |
| 41 if (image_1.IsEmpty() && image_2.IsEmpty()) |
| 42 return true; |
| 43 |
| 44 if (image_1.IsEmpty() || image_2.IsEmpty()) |
| 45 return false; |
| 46 |
| 47 scoped_refptr<base::RefCountedMemory> image_1_png = |
| 48 enhanced_bookmarks::BytesForImage(image_1); |
| 49 scoped_refptr<base::RefCountedMemory> image_2_png = |
| 50 enhanced_bookmarks::BytesForImage(image_2); |
| 51 |
| 52 if (image_1_png->size() != image_2_png->size()) |
| 53 return false; |
| 54 |
| 55 return !memcmp(image_1_png->front(), |
| 56 image_2_png->front(), |
| 57 image_1_png->size()); |
| 58 } |
| 59 |
| 60 // Factory functions for creating instances of the implementations. |
| 61 template <class T> |
| 62 scoped_refptr<ImageStore> CreateStore(base::ScopedTempDir& folder); |
| 63 |
| 64 template <> |
| 65 scoped_refptr<ImageStore> CreateStore<TestImageStore>( |
| 66 base::ScopedTempDir& folder) { |
| 67 return scoped_refptr<ImageStore>(new TestImageStore()); |
| 68 } |
| 69 |
| 70 template <> |
| 71 scoped_refptr<ImageStore> CreateStore<PersistentImageStore>( |
| 72 base::ScopedTempDir& folder) { |
| 73 return scoped_refptr<ImageStore>(new PersistentImageStore(folder.path())); |
| 74 } |
| 75 |
| 76 // Methods to check if persistence is on or not. |
| 77 template <class T> bool ShouldPersist(); |
| 78 template <> bool ShouldPersist<TestImageStore>() { return false; } |
| 79 template <> bool ShouldPersist<PersistentImageStore>() { return true; } |
| 80 |
| 81 // Test fixture class template for the abstract API. |
| 82 template <class T> |
| 83 class ImageStoreUnitTest : public PlatformTest { |
| 84 protected: |
| 85 ImageStoreUnitTest() {} |
| 86 virtual ~ImageStoreUnitTest() {} |
| 87 |
| 88 virtual void SetUp() OVERRIDE { |
| 89 bool success = tempDir_.CreateUniqueTempDir(); |
| 90 ASSERT_TRUE(success); |
| 91 store_ = CreateStore<T>(tempDir_); |
| 92 } |
| 93 |
| 94 virtual void TearDown() OVERRIDE { |
| 95 if (store_ && use_persistent_store()) |
| 96 store_->ClearAll(); |
| 97 } |
| 98 |
| 99 bool use_persistent_store() const { return ShouldPersist<T>(); } |
| 100 void ResetStore() { store_ = CreateStore<T>(tempDir_); } |
| 101 |
| 102 // The directory the database is saved into. |
| 103 base::ScopedTempDir tempDir_; |
| 104 // The object the fixture is testing, via its base interface. |
| 105 scoped_refptr<ImageStore> store_; |
| 106 |
| 107 private: |
| 108 DISALLOW_COPY_AND_ASSIGN(ImageStoreUnitTest); |
| 109 }; |
| 110 |
| 111 // The list of implementations of the abstract API that are going to be tested. |
| 112 typedef testing::Types<TestImageStore, |
| 113 PersistentImageStore> Implementations; |
| 114 |
| 115 TYPED_TEST_CASE(ImageStoreUnitTest, Implementations); |
| 116 |
| 117 // All those tests are run on all the implementations. |
| 118 TYPED_TEST(ImageStoreUnitTest, StartsEmpty) { |
| 119 std::set<GURL> all_urls; |
| 120 this->store_->GetAllPageUrls(&all_urls); |
| 121 EXPECT_EQ(0u, all_urls.size()); |
| 122 } |
| 123 |
| 124 TYPED_TEST(ImageStoreUnitTest, StoreOne) { |
| 125 this->store_->Insert(GURL("foo://bar"), GURL("a.jpg"), GenerateBlackImage()); |
| 126 |
| 127 std::set<GURL> all_urls; |
| 128 this->store_->GetAllPageUrls(&all_urls); |
| 129 EXPECT_EQ(1u, all_urls.size()); |
| 130 EXPECT_EQ(GURL("foo://bar"), *all_urls.begin()); |
| 131 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); |
| 132 } |
| 133 |
| 134 TYPED_TEST(ImageStoreUnitTest, Retrieve) { |
| 135 gfx::Image src_image = GenerateBlackImage(42, 24); |
| 136 const GURL url("foo://bar"); |
| 137 const GURL image_url("a.jpg"); |
| 138 this->store_->Insert(url, image_url, src_image); |
| 139 |
| 140 std::pair<gfx::Image, GURL> image_info = this->store_->Get(url); |
| 141 gfx::Size size = this->store_->GetSize(url); |
| 142 |
| 143 EXPECT_EQ(size.width(), 42); |
| 144 EXPECT_EQ(size.height(), 24); |
| 145 EXPECT_EQ(image_url, image_info.second); |
| 146 EXPECT_TRUE(CompareImages(src_image, image_info.first)); |
| 147 } |
| 148 |
| 149 TYPED_TEST(ImageStoreUnitTest, Erase) { |
| 150 gfx::Image src_image = GenerateBlackImage(); |
| 151 const GURL url("foo://bar"); |
| 152 const GURL image_url("a.jpg"); |
| 153 this->store_->Insert(url, image_url, src_image); |
| 154 this->store_->Erase(url); |
| 155 |
| 156 EXPECT_FALSE(this->store_->HasKey(url)); |
| 157 std::set<GURL> all_urls; |
| 158 this->store_->GetAllPageUrls(&all_urls); |
| 159 EXPECT_EQ(0u, all_urls.size()); |
| 160 } |
| 161 |
| 162 TYPED_TEST(ImageStoreUnitTest, ClearAll) { |
| 163 const GURL url_foo("http://foo"); |
| 164 this->store_->Insert(url_foo, GURL("foo.jpg"), GenerateBlackImage()); |
| 165 const GURL url_bar("http://bar"); |
| 166 this->store_->Insert(url_foo, GURL("bar.jpg"), GenerateWhiteImage()); |
| 167 |
| 168 this->store_->ClearAll(); |
| 169 |
| 170 EXPECT_FALSE(this->store_->HasKey(url_foo)); |
| 171 EXPECT_FALSE(this->store_->HasKey(url_bar)); |
| 172 std::set<GURL> all_urls; |
| 173 this->store_->GetAllPageUrls(&all_urls); |
| 174 EXPECT_EQ(0u, all_urls.size()); |
| 175 } |
| 176 |
| 177 TYPED_TEST(ImageStoreUnitTest, Update) { |
| 178 gfx::Image src_image1 = GenerateWhiteImage(); |
| 179 gfx::Image src_image2 = GenerateBlackImage(); |
| 180 const GURL url("foo://bar"); |
| 181 const GURL image_url1("1.jpg"); |
| 182 this->store_->Insert(url, image_url1, src_image1); |
| 183 |
| 184 const GURL image_url2("2.jpg"); |
| 185 this->store_->Insert(url, image_url2, src_image2); |
| 186 |
| 187 std::pair<gfx::Image, GURL> image_info = this->store_->Get(url); |
| 188 |
| 189 EXPECT_TRUE(this->store_->HasKey(url)); |
| 190 std::set<GURL> all_urls; |
| 191 this->store_->GetAllPageUrls(&all_urls); |
| 192 EXPECT_EQ(1u, all_urls.size()); |
| 193 EXPECT_EQ(image_url2, image_info.second); |
| 194 EXPECT_TRUE(CompareImages(src_image2, image_info.first)); |
| 195 } |
| 196 |
| 197 TYPED_TEST(ImageStoreUnitTest, Persistence) { |
| 198 gfx::Image src_image = GenerateBlackImage(); |
| 199 const GURL url("foo://bar"); |
| 200 const GURL image_url("a.jpg"); |
| 201 this->store_->Insert(url, image_url, src_image); |
| 202 |
| 203 this->ResetStore(); |
| 204 if (this->use_persistent_store()) { |
| 205 std::set<GURL> all_urls; |
| 206 this->store_->GetAllPageUrls(&all_urls); |
| 207 EXPECT_EQ(1u, all_urls.size()); |
| 208 EXPECT_EQ(GURL("foo://bar"), *all_urls.begin()); |
| 209 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); |
| 210 std::pair<gfx::Image, GURL> image_info = this->store_->Get(url); |
| 211 |
| 212 EXPECT_EQ(image_url, image_info.second); |
| 213 EXPECT_TRUE(CompareImages(src_image, image_info.first)); |
| 214 } else { |
| 215 std::set<GURL> all_urls; |
| 216 this->store_->GetAllPageUrls(&all_urls); |
| 217 EXPECT_EQ(0u, all_urls.size()); |
| 218 EXPECT_FALSE(this->store_->HasKey(GURL("foo://bar"))); |
| 219 } |
| 220 } |
| 221 |
| 222 } // namespace |
OLD | NEW |