Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1128)

Side by Side Diff: ui/gfx/image/image_skia_unittest.cc

Issue 24175004: Remove dependency on ui::ScaleFactor from ui/gfx (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename methods and vars to make image_scale more clear Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/image/image_skia.h" 5 #include "ui/gfx/image/image_skia.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/threading/simple_thread.h" 8 #include "base/threading/simple_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/base/layout.h"
12 #include "ui/gfx/image/image_skia_rep.h" 11 #include "ui/gfx/image/image_skia_rep.h"
13 #include "ui/gfx/image/image_skia_source.h" 12 #include "ui/gfx/image/image_skia_source.h"
14 #include "ui/gfx/size.h" 13 #include "ui/gfx/size.h"
15 14
16 // Duplicated from base/threading/non_thread_safe.h so that we can be 15 // Duplicated from base/threading/non_thread_safe.h so that we can be
17 // good citizens there and undef the macro. 16 // good citizens there and undef the macro.
18 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) 17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
19 #define ENABLE_NON_THREAD_SAFE 1 18 #define ENABLE_NON_THREAD_SAFE 1
20 #else 19 #else
21 #define ENABLE_NON_THREAD_SAFE 0 20 #define ENABLE_NON_THREAD_SAFE 0
22 #endif 21 #endif
23 22
24 namespace gfx { 23 namespace gfx {
25 24
26 namespace { 25 namespace {
27 26
28 class FixedSource : public ImageSkiaSource { 27 class FixedSource : public ImageSkiaSource {
29 public: 28 public:
30 FixedSource(const ImageSkiaRep& image) : image_(image) {} 29 FixedSource(const ImageSkiaRep& image) : image_(image) {}
31 30
32 virtual ~FixedSource() { 31 virtual ~FixedSource() {
33 } 32 }
34 33
35 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 34 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
36 return image_; 35 return image_;
37 } 36 }
38 37
39 private: 38 private:
40 ImageSkiaRep image_; 39 ImageSkiaRep image_;
41 40
42 DISALLOW_COPY_AND_ASSIGN(FixedSource); 41 DISALLOW_COPY_AND_ASSIGN(FixedSource);
43 }; 42 };
44 43
45 class DynamicSource : public ImageSkiaSource { 44 class DynamicSource : public ImageSkiaSource {
46 public: 45 public:
47 DynamicSource(const gfx::Size& size) : size_(size) {} 46 DynamicSource(const gfx::Size& size) : size_(size) {}
48 47
49 virtual ~DynamicSource() { 48 virtual ~DynamicSource() {
50 } 49 }
51 50
52 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 51 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
53 return gfx::ImageSkiaRep(size_, scale_factor); 52 return gfx::ImageSkiaRep(size_, scale);
54 } 53 }
55 54
56 private: 55 private:
57 gfx::Size size_; 56 gfx::Size size_;
58 57
59 DISALLOW_COPY_AND_ASSIGN(DynamicSource); 58 DISALLOW_COPY_AND_ASSIGN(DynamicSource);
60 }; 59 };
61 60
62 class NullSource: public ImageSkiaSource { 61 class NullSource: public ImageSkiaSource {
63 public: 62 public:
64 NullSource() { 63 NullSource() {
65 } 64 }
66 65
67 virtual ~NullSource() { 66 virtual ~NullSource() {
68 } 67 }
69 68
70 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { 69 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
71 return gfx::ImageSkiaRep(); 70 return gfx::ImageSkiaRep();
72 } 71 }
73 72
74 private: 73 private:
75 DISALLOW_COPY_AND_ASSIGN(NullSource); 74 DISALLOW_COPY_AND_ASSIGN(NullSource);
76 }; 75 };
77 76
78 } // namespace 77 } // namespace
79 78
80 namespace test { 79 namespace test {
(...skipping 27 matching lines...) Expand all
108 107
109 bool can_read_; 108 bool can_read_;
110 bool can_modify_; 109 bool can_modify_;
111 110
112 DISALLOW_COPY_AND_ASSIGN(TestOnThread); 111 DISALLOW_COPY_AND_ASSIGN(TestOnThread);
113 }; 112 };
114 113
115 } // namespace test 114 } // namespace test
116 115
117 TEST(ImageSkiaTest, FixedSource) { 116 TEST(ImageSkiaTest, FixedSource) {
118 ImageSkiaRep image(Size(100, 200), ui::SCALE_FACTOR_100P); 117 ImageSkiaRep image(Size(100, 200), 1.0f);
119 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); 118 ImageSkia image_skia(new FixedSource(image), Size(100, 200));
120 EXPECT_EQ(0U, image_skia.image_reps().size()); 119 EXPECT_EQ(0U, image_skia.image_reps().size());
121 120
122 const ImageSkiaRep& result_100p = 121 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
123 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
124 EXPECT_EQ(100, result_100p.GetWidth()); 122 EXPECT_EQ(100, result_100p.GetWidth());
125 EXPECT_EQ(200, result_100p.GetHeight()); 123 EXPECT_EQ(200, result_100p.GetHeight());
126 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); 124 EXPECT_EQ(1.0f, result_100p.scale());
127 EXPECT_EQ(1U, image_skia.image_reps().size()); 125 EXPECT_EQ(1U, image_skia.image_reps().size());
128 126
129 const ImageSkiaRep& result_200p = 127 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f);
130 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P);
131 128
132 EXPECT_EQ(100, result_200p.GetWidth()); 129 EXPECT_EQ(100, result_200p.GetWidth());
133 EXPECT_EQ(200, result_200p.GetHeight()); 130 EXPECT_EQ(200, result_200p.GetHeight());
134 EXPECT_EQ(100, result_200p.pixel_width()); 131 EXPECT_EQ(100, result_200p.pixel_width());
135 EXPECT_EQ(200, result_200p.pixel_height()); 132 EXPECT_EQ(200, result_200p.pixel_height());
136 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_200p.scale_factor()); 133 EXPECT_EQ(1.0f, result_200p.scale());
137 EXPECT_EQ(1U, image_skia.image_reps().size()); 134 EXPECT_EQ(1U, image_skia.image_reps().size());
138 135
139 // Get the representation again and make sure it doesn't 136 // Get the representation again and make sure it doesn't
140 // generate new image skia rep. 137 // generate new image skia rep.
141 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); 138 image_skia.GetRepresentation(1.0f);
142 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); 139 image_skia.GetRepresentation(2.0f);
143 EXPECT_EQ(1U, image_skia.image_reps().size()); 140 EXPECT_EQ(1U, image_skia.image_reps().size());
144 } 141 }
145 142
146 TEST(ImageSkiaTest, DynamicSource) { 143 TEST(ImageSkiaTest, DynamicSource) {
147 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); 144 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
148 EXPECT_EQ(0U, image_skia.image_reps().size()); 145 EXPECT_EQ(0U, image_skia.image_reps().size());
149 const ImageSkiaRep& result_100p = 146 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
150 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P);
151 EXPECT_EQ(100, result_100p.GetWidth()); 147 EXPECT_EQ(100, result_100p.GetWidth());
152 EXPECT_EQ(200, result_100p.GetHeight()); 148 EXPECT_EQ(200, result_100p.GetHeight());
153 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); 149 EXPECT_EQ(1.0f, result_100p.scale());
154 EXPECT_EQ(1U, image_skia.image_reps().size()); 150 EXPECT_EQ(1U, image_skia.image_reps().size());
155 151
156 const ImageSkiaRep& result_200p = 152 const ImageSkiaRep& result_200p =
157 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); 153 image_skia.GetRepresentation(2.0f);
158 EXPECT_EQ(100, result_200p.GetWidth()); 154 EXPECT_EQ(100, result_200p.GetWidth());
159 EXPECT_EQ(200, result_200p.GetHeight()); 155 EXPECT_EQ(200, result_200p.GetHeight());
160 EXPECT_EQ(200, result_200p.pixel_width()); 156 EXPECT_EQ(200, result_200p.pixel_width());
161 EXPECT_EQ(400, result_200p.pixel_height()); 157 EXPECT_EQ(400, result_200p.pixel_height());
162 EXPECT_EQ(ui::SCALE_FACTOR_200P, result_200p.scale_factor()); 158 EXPECT_EQ(2.0f, result_200p.scale());
163 EXPECT_EQ(2U, image_skia.image_reps().size()); 159 EXPECT_EQ(2U, image_skia.image_reps().size());
164 160
165 // Get the representation again and make sure it doesn't 161 // Get the representation again and make sure it doesn't
166 // generate new image skia rep. 162 // generate new image skia rep.
167 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); 163 image_skia.GetRepresentation(1.0f);
168 EXPECT_EQ(2U, image_skia.image_reps().size()); 164 EXPECT_EQ(2U, image_skia.image_reps().size());
169 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); 165 image_skia.GetRepresentation(2.0f);
170 EXPECT_EQ(2U, image_skia.image_reps().size()); 166 EXPECT_EQ(2U, image_skia.image_reps().size());
171 } 167 }
172 168
173 // Tests that image_reps returns all of the representations in the 169 // Tests that image_reps returns all of the representations in the
174 // image when there are multiple representations for a scale factor. 170 // image when there are multiple representations for a scale factor.
175 // This currently is the case with ImageLoader::LoadImages. 171 // This currently is the case with ImageLoader::LoadImages.
176 TEST(ImageSkiaTest, ManyRepsPerScaleFactor) { 172 TEST(ImageSkiaTest, ManyRepsPerScaleFactor) {
177 const int kSmallIcon1x = 16; 173 const int kSmallIcon1x = 16;
178 const int kSmallIcon2x = 32; 174 const int kSmallIcon2x = 32;
179 const int kLargeIcon1x = 32; 175 const int kLargeIcon1x = 32;
180 176
181 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x)); 177 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x));
182 // Simulate a source which loads images on a delay. Upon 178 // Simulate a source which loads images on a delay. Upon
183 // GetImageForScaleFactor, it immediately returns null and starts loading 179 // GetImageForScaleFactor, it immediately returns null and starts loading
184 // image reps slowly. 180 // image reps slowly.
185 image.GetRepresentation(ui::SCALE_FACTOR_100P); 181 image.GetRepresentation(1.0f);
186 image.GetRepresentation(ui::SCALE_FACTOR_200P); 182 image.GetRepresentation(2.0f);
187 183
188 // After a lengthy amount of simulated time, finally loaded image reps. 184 // After a lengthy amount of simulated time, finally loaded image reps.
189 image.AddRepresentation(ImageSkiaRep( 185 image.AddRepresentation(ImageSkiaRep(
190 gfx::Size(kSmallIcon1x, kSmallIcon1x), ui::SCALE_FACTOR_100P)); 186 gfx::Size(kSmallIcon1x, kSmallIcon1x), 1.0f));
191 image.AddRepresentation(ImageSkiaRep( 187 image.AddRepresentation(ImageSkiaRep(
192 gfx::Size(kSmallIcon2x, kSmallIcon2x), ui::SCALE_FACTOR_200P)); 188 gfx::Size(kSmallIcon2x, kSmallIcon2x), 2.0f));
193 image.AddRepresentation(ImageSkiaRep( 189 image.AddRepresentation(ImageSkiaRep(
194 gfx::Size(kLargeIcon1x, kLargeIcon1x), ui::SCALE_FACTOR_100P)); 190 gfx::Size(kLargeIcon1x, kLargeIcon1x), 1.0f));
195 191
196 std::vector<ImageSkiaRep> image_reps = image.image_reps(); 192 std::vector<ImageSkiaRep> image_reps = image.image_reps();
197 EXPECT_EQ(3u, image_reps.size()); 193 EXPECT_EQ(3u, image_reps.size());
198 194
199 int num_1x = 0; 195 int num_1x = 0;
200 int num_2x = 0; 196 int num_2x = 0;
201 for (size_t i = 0; i < image_reps.size(); ++i) { 197 for (size_t i = 0; i < image_reps.size(); ++i) {
202 if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_100P) 198 if (image_reps[i].scale() == 1.0f)
203 num_1x++; 199 num_1x++;
204 else if (image_reps[i].scale_factor() == ui::SCALE_FACTOR_200P) 200 else if (image_reps[i].scale() == 2.0f)
205 num_2x++; 201 num_2x++;
206 } 202 }
207 EXPECT_EQ(2, num_1x); 203 EXPECT_EQ(2, num_1x);
208 EXPECT_EQ(1, num_2x); 204 EXPECT_EQ(1, num_2x);
209 } 205 }
210 206
211 TEST(ImageSkiaTest, GetBitmap) { 207 TEST(ImageSkiaTest, GetBitmap) {
212 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); 208 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
213 const SkBitmap* bitmap = image_skia.bitmap(); 209 const SkBitmap* bitmap = image_skia.bitmap();
214 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); 210 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap);
215 EXPECT_FALSE(bitmap->isNull()); 211 EXPECT_FALSE(bitmap->isNull());
216 } 212 }
217 213
218 TEST(ImageSkiaTest, GetBitmapFromEmpty) { 214 TEST(ImageSkiaTest, GetBitmapFromEmpty) {
219 // Create an image with 1 representation and remove it so the ImageSkiaStorage 215 // Create an image with 1 representation and remove it so the ImageSkiaStorage
220 // is left with no representations. 216 // is left with no representations.
221 ImageSkia empty_image(ImageSkiaRep(Size(100, 200), ui::SCALE_FACTOR_100P)); 217 ImageSkia empty_image(ImageSkiaRep(Size(100, 200), 1.0f));
222 ImageSkia empty_image_copy(empty_image); 218 ImageSkia empty_image_copy(empty_image);
223 empty_image.RemoveRepresentation(ui::SCALE_FACTOR_100P); 219 empty_image.RemoveRepresentation(1.0f);
224 220
225 // Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for 221 // Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for
226 // the image and all its copies. 222 // the image and all its copies.
227 const SkBitmap* bitmap = empty_image_copy.bitmap(); 223 const SkBitmap* bitmap = empty_image_copy.bitmap();
228 ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap); 224 ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap);
229 EXPECT_TRUE(bitmap->isNull()); 225 EXPECT_TRUE(bitmap->isNull());
230 EXPECT_TRUE(bitmap->empty()); 226 EXPECT_TRUE(bitmap->empty());
231 } 227 }
232 228
233 TEST(ImageSkiaTest, BackedBySameObjectAs) { 229 TEST(ImageSkiaTest, BackedBySameObjectAs) {
234 // Null images should all be backed by the same object (NULL). 230 // Null images should all be backed by the same object (NULL).
235 ImageSkia image; 231 ImageSkia image;
236 ImageSkia unrelated; 232 ImageSkia unrelated;
237 EXPECT_TRUE(image.BackedBySameObjectAs(unrelated)); 233 EXPECT_TRUE(image.BackedBySameObjectAs(unrelated));
238 234
239 image.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 235 image.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
240 ui::SCALE_FACTOR_100P)); 236 1.0f));
241 ImageSkia copy = image; 237 ImageSkia copy = image;
242 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 238 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
243 ui::SCALE_FACTOR_200P)); 239 2.0f));
244 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 240 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
245 ui::SCALE_FACTOR_100P)); 241 1.0f));
246 EXPECT_TRUE(image.BackedBySameObjectAs(copy)); 242 EXPECT_TRUE(image.BackedBySameObjectAs(copy));
247 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated)); 243 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated));
248 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated)); 244 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated));
249 } 245 }
250 246
251 #if ENABLE_NON_THREAD_SAFE 247 #if ENABLE_NON_THREAD_SAFE
252 TEST(ImageSkiaTest, EmptyOnThreadTest) { 248 TEST(ImageSkiaTest, EmptyOnThreadTest) {
253 ImageSkia empty; 249 ImageSkia empty;
254 test::TestOnThread empty_on_thread(&empty); 250 test::TestOnThread empty_on_thread(&empty);
255 empty_on_thread.Start(); 251 empty_on_thread.Start();
256 empty_on_thread.Join(); 252 empty_on_thread.Join();
257 EXPECT_TRUE(empty_on_thread.can_read()); 253 EXPECT_TRUE(empty_on_thread.can_read());
258 EXPECT_TRUE(empty_on_thread.can_modify()); 254 EXPECT_TRUE(empty_on_thread.can_modify());
259 } 255 }
260 256
261 TEST(ImageSkiaTest, StaticOnThreadTest) { 257 TEST(ImageSkiaTest, StaticOnThreadTest) {
262 ImageSkia image(ImageSkiaRep(Size(100, 200), ui::SCALE_FACTOR_100P)); 258 ImageSkia image(ImageSkiaRep(Size(100, 200), 1.0f));
263 EXPECT_FALSE(image.IsThreadSafe()); 259 EXPECT_FALSE(image.IsThreadSafe());
264 260
265 test::TestOnThread image_on_thread(&image); 261 test::TestOnThread image_on_thread(&image);
266 // an image that was never accessed on this thread can be 262 // an image that was never accessed on this thread can be
267 // read by other thread. 263 // read by other thread.
268 image_on_thread.StartAndJoin(); 264 image_on_thread.StartAndJoin();
269 EXPECT_TRUE(image_on_thread.can_read()); 265 EXPECT_TRUE(image_on_thread.can_read());
270 EXPECT_TRUE(image_on_thread.can_modify()); 266 EXPECT_TRUE(image_on_thread.can_modify());
271 EXPECT_FALSE(image.CanRead()); 267 EXPECT_FALSE(image.CanRead());
272 EXPECT_FALSE(image.CanModify()); 268 EXPECT_FALSE(image.CanModify());
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 readonly_on_thread.StartAndJoin(); 354 readonly_on_thread.StartAndJoin();
359 EXPECT_TRUE(readonly_on_thread.can_read()); 355 EXPECT_TRUE(readonly_on_thread.can_read());
360 EXPECT_FALSE(readonly_on_thread.can_modify()); 356 EXPECT_FALSE(readonly_on_thread.can_modify());
361 EXPECT_FALSE(image.CanRead()); 357 EXPECT_FALSE(image.CanRead());
362 EXPECT_FALSE(image.CanModify()); 358 EXPECT_FALSE(image.CanModify());
363 359
364 image.DetachStorageFromThread(); 360 image.DetachStorageFromThread();
365 image.MakeThreadSafe(); 361 image.MakeThreadSafe();
366 EXPECT_TRUE(image.IsThreadSafe()); 362 EXPECT_TRUE(image.IsThreadSafe());
367 // Check if image reps are generated for supported scale factors. 363 // Check if image reps are generated for supported scale factors.
368 EXPECT_EQ(ui::GetSupportedScaleFactors().size(), 364 EXPECT_EQ(ImageSkia::GetSupportedScales().size(),
369 image.image_reps().size()); 365 image.image_reps().size());
370 test::TestOnThread threadsafe_on_thread(&image); 366 test::TestOnThread threadsafe_on_thread(&image);
371 threadsafe_on_thread.StartAndJoin(); 367 threadsafe_on_thread.StartAndJoin();
372 EXPECT_TRUE(threadsafe_on_thread.can_read()); 368 EXPECT_TRUE(threadsafe_on_thread.can_read());
373 EXPECT_FALSE(threadsafe_on_thread.can_modify()); 369 EXPECT_FALSE(threadsafe_on_thread.can_modify());
374 EXPECT_TRUE(image.CanRead()); 370 EXPECT_TRUE(image.CanRead());
375 EXPECT_FALSE(image.CanModify()); 371 EXPECT_FALSE(image.CanModify());
376 } 372 }
377 #endif // ENABLE_NON_THREAD_SAFE 373 #endif // ENABLE_NON_THREAD_SAFE
378 374
379 // Just in case we ever get lumped together with other compilation units. 375 // Just in case we ever get lumped together with other compilation units.
380 #undef ENABLE_NON_THREAD_SAFE 376 #undef ENABLE_NON_THREAD_SAFE
381 377
382 } // namespace gfx 378 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698