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