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/bookmarks_enhanced/image_store.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "components/bookmarks_enhanced/image_store_util.h" |
| 9 #include "components/bookmarks_enhanced/memory_image_store.h" |
| 10 #include "components/bookmarks_enhanced/persistent_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 bookmarks_enhanced::BytesForImage(image_1); |
| 49 scoped_refptr<base::RefCountedMemory> image_2_png = |
| 50 bookmarks_enhanced::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<MemoryImageStore>( |
| 66 base::ScopedTempDir& folder) { |
| 67 return scoped_refptr<ImageStore>(new MemoryImageStore()); |
| 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<MemoryImageStore>() { 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() { |
| 89 bool success = tempDir_.CreateUniqueTempDir(); |
| 90 ASSERT_TRUE(success); |
| 91 store_ = createStore<T>(tempDir_); |
| 92 } |
| 93 |
| 94 void TearDown() OVERRIDE { |
| 95 if (store_ && usePersistentStore()) |
| 96 store_->ClearAll(); |
| 97 } |
| 98 |
| 99 bool usePersistentStore() { 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<MemoryImageStore, |
| 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, update) { |
| 163 gfx::Image src_image1 = GenerateWhiteImage(); |
| 164 gfx::Image src_image2 = GenerateBlackImage(); |
| 165 const GURL url("foo://bar"); |
| 166 const GURL image_url1("1.jpg"); |
| 167 this->store_->Insert(url, image_url1, src_image1); |
| 168 |
| 169 const GURL image_url2("2.jpg"); |
| 170 this->store_->Insert(url, image_url2, src_image2); |
| 171 |
| 172 std::pair<gfx::Image, GURL> image_info = this->store_->Get(url); |
| 173 |
| 174 EXPECT_TRUE(this->store_->HasKey(url)); |
| 175 std::set<GURL> all_urls; |
| 176 this->store_->GetAllPageUrls(&all_urls); |
| 177 EXPECT_EQ(1u, all_urls.size()); |
| 178 EXPECT_EQ(image_url2, image_info.second); |
| 179 EXPECT_TRUE(CompareImages(src_image2, image_info.first)); |
| 180 } |
| 181 |
| 182 TYPED_TEST(ImageStoreUnitTest, persistence) { |
| 183 gfx::Image src_image = GenerateBlackImage(); |
| 184 const GURL url("foo://bar"); |
| 185 const GURL image_url("a.jpg"); |
| 186 this->store_->Insert(url, image_url, src_image); |
| 187 |
| 188 this->resetStore(); |
| 189 if (this->usePersistentStore()) { |
| 190 std::set<GURL> all_urls; |
| 191 this->store_->GetAllPageUrls(&all_urls); |
| 192 EXPECT_EQ(1u, all_urls.size()); |
| 193 EXPECT_EQ(GURL("foo://bar"), *all_urls.begin()); |
| 194 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); |
| 195 std::pair<gfx::Image, GURL> image_info = this->store_->Get(url); |
| 196 |
| 197 EXPECT_EQ(image_url, image_info.second); |
| 198 EXPECT_TRUE(CompareImages(src_image, image_info.first)); |
| 199 } else { |
| 200 std::set<GURL> all_urls; |
| 201 this->store_->GetAllPageUrls(&all_urls); |
| 202 EXPECT_EQ(0u, all_urls.size()); |
| 203 EXPECT_FALSE(this->store_->HasKey(GURL("foo://bar"))); |
| 204 } |
| 205 } |
| 206 |
| 207 } // namespace |
OLD | NEW |