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) { | |
noyau (Ping after 24h)
2014/05/05 18:09:50
The images had randomness in them to make sure tha
Kibeom Kim (inactive)
2014/05/05 18:21:34
IIRC, I think the original code randomly chose one
| |
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() { | |
31 return gfx::Image::CreateFrom1xBitmap(CreateBitmap(42, 24, 255, 0, 0, 0)); | |
32 } | |
33 | |
34 // Returns true if the two images are identical. | |
35 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { | |
36 if (image_1.IsEmpty() && image_2.IsEmpty()) | |
37 return true; | |
38 | |
39 if (image_1.IsEmpty() || image_2.IsEmpty()) | |
40 return false; | |
41 | |
42 scoped_refptr<base::RefCountedMemory> image_1_png = | |
43 image_store_util::BytesForImage(image_1); | |
44 scoped_refptr<base::RefCountedMemory> image_2_png = | |
45 image_store_util::BytesForImage(image_2); | |
46 | |
47 if (image_1_png->size() != image_2_png->size()) | |
48 return false; | |
49 | |
50 return !memcmp(image_1_png->front(), | |
51 image_2_png->front(), | |
52 image_1_png->size()); | |
53 } | |
54 | |
55 // Factory functions for creating instances of the implementations. | |
56 template <class T> | |
57 scoped_refptr<ImageStore> createStore(base::ScopedTempDir& folder); | |
58 | |
59 template <> | |
60 scoped_refptr<ImageStore> createStore<MemoryImageStore>( | |
61 base::ScopedTempDir& folder) { | |
62 return scoped_refptr<ImageStore>(new MemoryImageStore()); | |
63 } | |
64 | |
65 template <> | |
66 scoped_refptr<ImageStore> createStore<PersistentImageStore>( | |
67 base::ScopedTempDir& folder) { | |
68 return scoped_refptr<ImageStore>(new PersistentImageStore(folder.path())); | |
69 } | |
70 | |
71 // Methods to check if persistence is on or not. | |
72 template <class T> bool shouldPersist(); | |
73 template <> bool shouldPersist<MemoryImageStore>() { return false; } | |
74 template <> bool shouldPersist<PersistentImageStore>() { return true; } | |
75 | |
76 // Test fixture class template for the abstract API. | |
77 template <class T> | |
78 class ImageStoreUnitTest : public PlatformTest { | |
79 protected: | |
80 ImageStoreUnitTest() { | |
81 bool success = tempDir_.CreateUniqueTempDir(); | |
82 EXPECT_TRUE(success); | |
sky
2014/05/06 16:14:20
Seems like this should be an ASSERT. And I would m
Kibeom Kim (inactive)
2014/05/07 00:09:46
Done.
| |
83 store_ = createStore<T>(tempDir_); | |
84 } | |
85 | |
86 void TearDown() OVERRIDE { | |
87 if (store_ && usePersistentStore()) | |
88 store_->ClearAll(); | |
89 } | |
90 | |
91 bool usePersistentStore() { return shouldPersist<T>(); } | |
92 void resetStore() { store_ = createStore<T>(tempDir_); } | |
93 virtual ~ImageStoreUnitTest() {} | |
sky
2014/05/06 16:14:20
destructor should be right after constructor.
Kibeom Kim (inactive)
2014/05/07 00:09:46
Done.
| |
94 // The directory the database is saved into. | |
95 base::ScopedTempDir tempDir_; | |
96 // The object the fixture is testing, via its base interface. | |
97 scoped_refptr<ImageStore> store_; | |
98 }; | |
sky
2014/05/06 16:14:20
DISALOW...
Kibeom Kim (inactive)
2014/05/07 00:09:46
Done.
| |
99 | |
100 // The list of implementations of the abstract API that are going to be tested. | |
101 typedef testing::Types<MemoryImageStore, | |
102 PersistentImageStore> Implementations; | |
103 | |
104 TYPED_TEST_CASE(ImageStoreUnitTest, Implementations); | |
105 | |
106 // All those tests are run on all the implementations. | |
107 TYPED_TEST(ImageStoreUnitTest, startsEmpty) { | |
108 std::vector<GURL> all_urls; | |
109 this->store_->GetAllPageUrls(&all_urls); | |
110 EXPECT_EQ(0ul, all_urls.size()); | |
sky
2014/05/06 16:14:20
You shouldn't need the 'l' in any of these, '0u' s
Kibeom Kim (inactive)
2014/05/07 00:09:46
Done.
| |
111 } | |
112 | |
113 TYPED_TEST(ImageStoreUnitTest, storeOne) { | |
114 this->store_->Insert(GURL("foo://bar"), GURL("a.jpg"), GenerateBlackImage()); | |
115 | |
116 std::vector<GURL> all_urls; | |
117 this->store_->GetAllPageUrls(&all_urls); | |
118 EXPECT_EQ(1ul, all_urls.size()); | |
119 EXPECT_EQ(GURL("foo://bar"), all_urls[0]); | |
120 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); | |
121 } | |
122 | |
123 TYPED_TEST(ImageStoreUnitTest, retrieve) { | |
124 gfx::Image src_image = GenerateBlackImage(); | |
125 const GURL url("foo://bar"); | |
126 const GURL image_url("a.jpg"); | |
127 this->store_->Insert(url, image_url, src_image); | |
128 GURL result; | |
129 gfx::Image dst_image = this->store_->Get(url, &result); | |
130 | |
131 EXPECT_EQ(image_url, result); | |
132 EXPECT_TRUE(CompareImages(src_image, dst_image)); | |
133 } | |
134 | |
135 TYPED_TEST(ImageStoreUnitTest, erase) { | |
136 gfx::Image src_image = GenerateBlackImage(); | |
137 const GURL url("foo://bar"); | |
138 const GURL image_url("a.jpg"); | |
139 this->store_->Insert(url, image_url, src_image); | |
140 this->store_->Erase(url); | |
141 | |
142 EXPECT_FALSE(this->store_->HasKey(url)); | |
143 std::vector<GURL> all_urls; | |
144 this->store_->GetAllPageUrls(&all_urls); | |
145 EXPECT_EQ(0ul, all_urls.size()); | |
146 } | |
147 | |
148 TYPED_TEST(ImageStoreUnitTest, update) { | |
149 gfx::Image src_image1 = GenerateWhiteImage(); | |
150 gfx::Image src_image2 = GenerateBlackImage(); | |
151 const GURL url("foo://bar"); | |
152 const GURL image_url1("1.jpg"); | |
153 this->store_->Insert(url, image_url1, src_image1); | |
154 | |
155 const GURL image_url2("2.jpg"); | |
156 this->store_->Insert(url, image_url2, src_image2); | |
157 | |
158 GURL result; | |
159 gfx::Image dst_image = this->store_->Get(url, &result); | |
160 | |
161 EXPECT_TRUE(this->store_->HasKey(url)); | |
162 std::vector<GURL> all_urls; | |
163 this->store_->GetAllPageUrls(&all_urls); | |
164 EXPECT_EQ(1ul, all_urls.size()); | |
165 EXPECT_EQ(image_url2, result); | |
166 EXPECT_TRUE(CompareImages(src_image2, dst_image)); | |
167 } | |
168 | |
169 TYPED_TEST(ImageStoreUnitTest, persistence) { | |
170 gfx::Image src_image = GenerateBlackImage(); | |
171 const GURL url("foo://bar"); | |
172 const GURL image_url("a.jpg"); | |
173 this->store_->Insert(url, image_url, src_image); | |
174 | |
175 this->resetStore(); | |
176 if (this->usePersistentStore()) { | |
177 std::vector<GURL> all_urls; | |
178 this->store_->GetAllPageUrls(&all_urls); | |
179 EXPECT_EQ(1ul, all_urls.size()); | |
180 EXPECT_EQ(GURL("foo://bar"), all_urls[0]); | |
181 EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar"))); | |
182 GURL result; | |
183 gfx::Image dst_image = this->store_->Get(url, &result); | |
184 | |
185 EXPECT_EQ(image_url, result); | |
186 EXPECT_TRUE(CompareImages(src_image, dst_image)); | |
187 } else { | |
188 std::vector<GURL> all_urls; | |
189 this->store_->GetAllPageUrls(&all_urls); | |
190 EXPECT_EQ(0ul, all_urls.size()); | |
191 EXPECT_FALSE(this->store_->HasKey(GURL("foo://bar"))); | |
192 } | |
193 } | |
194 | |
195 } // namespace | |
OLD | NEW |