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

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

Issue 263253003: Allows arbitrary scale factor in ImageSkia. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 7 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
« no previous file with comments | « ui/gfx/image/image_skia.cc ('k') | ui/gfx/switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/command_line.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/threading/simple_thread.h" 9 #include "base/threading/simple_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 11 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "ui/gfx/image/image_skia_rep.h" 12 #include "ui/gfx/image/image_skia_rep.h"
12 #include "ui/gfx/image/image_skia_source.h" 13 #include "ui/gfx/image/image_skia_source.h"
13 #include "ui/gfx/size.h" 14 #include "ui/gfx/size.h"
15 #include "ui/gfx/switches.h"
14 16
15 // Duplicated from base/threading/non_thread_safe.h so that we can be 17 // Duplicated from base/threading/non_thread_safe.h so that we can be
16 // good citizens there and undef the macro. 18 // good citizens there and undef the macro.
17 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) 19 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
18 #define ENABLE_NON_THREAD_SAFE 1 20 #define ENABLE_NON_THREAD_SAFE 1
19 #else 21 #else
20 #define ENABLE_NON_THREAD_SAFE 0 22 #define ENABLE_NON_THREAD_SAFE 0
21 #endif 23 #endif
22 24
23 namespace gfx { 25 namespace gfx {
(...skipping 12 matching lines...) Expand all
36 } 38 }
37 39
38 private: 40 private:
39 ImageSkiaRep image_; 41 ImageSkiaRep image_;
40 42
41 DISALLOW_COPY_AND_ASSIGN(FixedSource); 43 DISALLOW_COPY_AND_ASSIGN(FixedSource);
42 }; 44 };
43 45
44 class DynamicSource : public ImageSkiaSource { 46 class DynamicSource : public ImageSkiaSource {
45 public: 47 public:
46 DynamicSource(const gfx::Size& size) : size_(size) {} 48 DynamicSource(const gfx::Size& size)
49 : size_(size),
50 last_requested_scale_(0.0f) {}
47 51
48 virtual ~DynamicSource() { 52 virtual ~DynamicSource() {
49 } 53 }
50 54
51 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { 55 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE {
56 last_requested_scale_ = scale;
52 return gfx::ImageSkiaRep(size_, scale); 57 return gfx::ImageSkiaRep(size_, scale);
53 } 58 }
54 59
60 float GetLastRequestedScaleAndReset() {
61 float result = last_requested_scale_;
62 last_requested_scale_ = 0.0f;
63 return result;
64 }
65
55 private: 66 private:
56 gfx::Size size_; 67 gfx::Size size_;
68 float last_requested_scale_;
57 69
58 DISALLOW_COPY_AND_ASSIGN(DynamicSource); 70 DISALLOW_COPY_AND_ASSIGN(DynamicSource);
59 }; 71 };
60 72
61 class NullSource: public ImageSkiaSource { 73 class NullSource: public ImageSkiaSource {
62 public: 74 public:
63 NullSource() { 75 NullSource() {
64 } 76 }
65 77
66 virtual ~NullSource() { 78 virtual ~NullSource() {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 ImageSkia* image_skia_; 118 ImageSkia* image_skia_;
107 119
108 bool can_read_; 120 bool can_read_;
109 bool can_modify_; 121 bool can_modify_;
110 122
111 DISALLOW_COPY_AND_ASSIGN(TestOnThread); 123 DISALLOW_COPY_AND_ASSIGN(TestOnThread);
112 }; 124 };
113 125
114 } // namespace test 126 } // namespace test
115 127
116 TEST(ImageSkiaTest, FixedSource) { 128 class ImageSkiaTest : public testing::Test {
129 public:
130 ImageSkiaTest() {
131 // In the test, we assume that we support 1.0f and 2.0f DSFs.
132 old_scales_ = ImageSkia::GetSupportedScales();
133
134 // Sets the list of scale factors supported by resource bundle.
135 std::vector<float> supported_scales;
136 supported_scales.push_back(1.0f);
137 supported_scales.push_back(2.0f);
138 ImageSkia::SetSupportedScales(supported_scales);
139 }
140 virtual ~ImageSkiaTest() {
141 ImageSkia::SetSupportedScales(old_scales_);
142 }
143
144 private:
145 std::vector<float> old_scales_;
146 DISALLOW_COPY_AND_ASSIGN(ImageSkiaTest);
147 };
148
149 TEST_F(ImageSkiaTest, FixedSource) {
117 ImageSkiaRep image(Size(100, 200), 1.0f); 150 ImageSkiaRep image(Size(100, 200), 1.0f);
118 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); 151 ImageSkia image_skia(new FixedSource(image), Size(100, 200));
119 EXPECT_EQ(0U, image_skia.image_reps().size()); 152 EXPECT_EQ(0U, image_skia.image_reps().size());
120 153
121 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f); 154 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
122 EXPECT_EQ(100, result_100p.GetWidth()); 155 EXPECT_EQ(100, result_100p.GetWidth());
123 EXPECT_EQ(200, result_100p.GetHeight()); 156 EXPECT_EQ(200, result_100p.GetHeight());
124 EXPECT_EQ(1.0f, result_100p.scale()); 157 EXPECT_EQ(1.0f, result_100p.scale());
125 EXPECT_EQ(1U, image_skia.image_reps().size()); 158 EXPECT_EQ(1U, image_skia.image_reps().size());
126 159
127 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f); 160 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f);
128 161
129 EXPECT_EQ(100, result_200p.GetWidth()); 162 EXPECT_EQ(100, result_200p.GetWidth());
130 EXPECT_EQ(200, result_200p.GetHeight()); 163 EXPECT_EQ(200, result_200p.GetHeight());
131 EXPECT_EQ(100, result_200p.pixel_width()); 164 EXPECT_EQ(100, result_200p.pixel_width());
132 EXPECT_EQ(200, result_200p.pixel_height()); 165 EXPECT_EQ(200, result_200p.pixel_height());
133 EXPECT_EQ(1.0f, result_200p.scale()); 166 EXPECT_EQ(1.0f, result_200p.scale());
134 EXPECT_EQ(1U, image_skia.image_reps().size()); 167 EXPECT_EQ(1U, image_skia.image_reps().size());
135 168
136 // Get the representation again and make sure it doesn't 169 // Get the representation again and make sure it doesn't
137 // generate new image skia rep. 170 // generate new image skia rep.
138 image_skia.GetRepresentation(1.0f); 171 image_skia.GetRepresentation(1.0f);
139 image_skia.GetRepresentation(2.0f); 172 image_skia.GetRepresentation(2.0f);
140 EXPECT_EQ(1U, image_skia.image_reps().size()); 173 EXPECT_EQ(1U, image_skia.image_reps().size());
141 } 174 }
142 175
143 TEST(ImageSkiaTest, DynamicSource) { 176 TEST_F(ImageSkiaTest, DynamicSource) {
144 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); 177 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
145 EXPECT_EQ(0U, image_skia.image_reps().size()); 178 EXPECT_EQ(0U, image_skia.image_reps().size());
146 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f); 179 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
147 EXPECT_EQ(100, result_100p.GetWidth()); 180 EXPECT_EQ(100, result_100p.GetWidth());
148 EXPECT_EQ(200, result_100p.GetHeight()); 181 EXPECT_EQ(200, result_100p.GetHeight());
149 EXPECT_EQ(1.0f, result_100p.scale()); 182 EXPECT_EQ(1.0f, result_100p.scale());
150 EXPECT_EQ(1U, image_skia.image_reps().size()); 183 EXPECT_EQ(1U, image_skia.image_reps().size());
151 184
152 const ImageSkiaRep& result_200p = 185 const ImageSkiaRep& result_200p =
153 image_skia.GetRepresentation(2.0f); 186 image_skia.GetRepresentation(2.0f);
154 EXPECT_EQ(100, result_200p.GetWidth()); 187 EXPECT_EQ(100, result_200p.GetWidth());
155 EXPECT_EQ(200, result_200p.GetHeight()); 188 EXPECT_EQ(200, result_200p.GetHeight());
156 EXPECT_EQ(200, result_200p.pixel_width()); 189 EXPECT_EQ(200, result_200p.pixel_width());
157 EXPECT_EQ(400, result_200p.pixel_height()); 190 EXPECT_EQ(400, result_200p.pixel_height());
158 EXPECT_EQ(2.0f, result_200p.scale()); 191 EXPECT_EQ(2.0f, result_200p.scale());
159 EXPECT_EQ(2U, image_skia.image_reps().size()); 192 EXPECT_EQ(2U, image_skia.image_reps().size());
160 193
161 // Get the representation again and make sure it doesn't 194 // Get the representation again and make sure it doesn't
162 // generate new image skia rep. 195 // generate new image skia rep.
163 image_skia.GetRepresentation(1.0f); 196 image_skia.GetRepresentation(1.0f);
164 EXPECT_EQ(2U, image_skia.image_reps().size()); 197 EXPECT_EQ(2U, image_skia.image_reps().size());
165 image_skia.GetRepresentation(2.0f); 198 image_skia.GetRepresentation(2.0f);
166 EXPECT_EQ(2U, image_skia.image_reps().size()); 199 EXPECT_EQ(2U, image_skia.image_reps().size());
167 } 200 }
168 201
169 // Tests that image_reps returns all of the representations in the 202 // Tests that image_reps returns all of the representations in the
170 // image when there are multiple representations for a scale factor. 203 // image when there are multiple representations for a scale factor.
171 // This currently is the case with ImageLoader::LoadImages. 204 // This currently is the case with ImageLoader::LoadImages.
172 TEST(ImageSkiaTest, ManyRepsPerScaleFactor) { 205 TEST_F(ImageSkiaTest, ManyRepsPerScaleFactor) {
173 const int kSmallIcon1x = 16; 206 const int kSmallIcon1x = 16;
174 const int kSmallIcon2x = 32; 207 const int kSmallIcon2x = 32;
175 const int kLargeIcon1x = 32; 208 const int kLargeIcon1x = 32;
176 209
177 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x)); 210 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x));
178 // Simulate a source which loads images on a delay. Upon 211 // Simulate a source which loads images on a delay. Upon
179 // GetImageForScaleFactor, it immediately returns null and starts loading 212 // GetImageForScaleFactor, it immediately returns null and starts loading
180 // image reps slowly. 213 // image reps slowly.
181 image.GetRepresentation(1.0f); 214 image.GetRepresentation(1.0f);
182 image.GetRepresentation(2.0f); 215 image.GetRepresentation(2.0f);
(...skipping 14 matching lines...) Expand all
197 for (size_t i = 0; i < image_reps.size(); ++i) { 230 for (size_t i = 0; i < image_reps.size(); ++i) {
198 if (image_reps[i].scale() == 1.0f) 231 if (image_reps[i].scale() == 1.0f)
199 num_1x++; 232 num_1x++;
200 else if (image_reps[i].scale() == 2.0f) 233 else if (image_reps[i].scale() == 2.0f)
201 num_2x++; 234 num_2x++;
202 } 235 }
203 EXPECT_EQ(2, num_1x); 236 EXPECT_EQ(2, num_1x);
204 EXPECT_EQ(1, num_2x); 237 EXPECT_EQ(1, num_2x);
205 } 238 }
206 239
207 TEST(ImageSkiaTest, GetBitmap) { 240 TEST_F(ImageSkiaTest, GetBitmap) {
208 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); 241 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
209 const SkBitmap* bitmap = image_skia.bitmap(); 242 const SkBitmap* bitmap = image_skia.bitmap();
210 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap); 243 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap);
211 EXPECT_FALSE(bitmap->isNull()); 244 EXPECT_FALSE(bitmap->isNull());
212 } 245 }
213 246
214 TEST(ImageSkiaTest, GetBitmapFromEmpty) { 247 TEST_F(ImageSkiaTest, GetBitmapFromEmpty) {
215 // Create an image with 1 representation and remove it so the ImageSkiaStorage 248 // Create an image with 1 representation and remove it so the ImageSkiaStorage
216 // is left with no representations. 249 // is left with no representations.
217 ImageSkia empty_image(ImageSkiaRep(Size(100, 200), 1.0f)); 250 ImageSkia empty_image(ImageSkiaRep(Size(100, 200), 1.0f));
218 ImageSkia empty_image_copy(empty_image); 251 ImageSkia empty_image_copy(empty_image);
219 empty_image.RemoveRepresentation(1.0f); 252 empty_image.RemoveRepresentation(1.0f);
220 253
221 // Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for 254 // Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for
222 // the image and all its copies. 255 // the image and all its copies.
223 const SkBitmap* bitmap = empty_image_copy.bitmap(); 256 const SkBitmap* bitmap = empty_image_copy.bitmap();
224 ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap); 257 ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap);
225 EXPECT_TRUE(bitmap->isNull()); 258 EXPECT_TRUE(bitmap->isNull());
226 EXPECT_TRUE(bitmap->empty()); 259 EXPECT_TRUE(bitmap->empty());
227 } 260 }
228 261
229 TEST(ImageSkiaTest, BackedBySameObjectAs) { 262 TEST_F(ImageSkiaTest, BackedBySameObjectAs) {
230 // Null images should all be backed by the same object (NULL). 263 // Null images should all be backed by the same object (NULL).
231 ImageSkia image; 264 ImageSkia image;
232 ImageSkia unrelated; 265 ImageSkia unrelated;
233 EXPECT_TRUE(image.BackedBySameObjectAs(unrelated)); 266 EXPECT_TRUE(image.BackedBySameObjectAs(unrelated));
234 267
235 image.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 268 image.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
236 1.0f)); 269 1.0f));
237 ImageSkia copy = image; 270 ImageSkia copy = image;
238 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 271 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
239 2.0f)); 272 2.0f));
240 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10), 273 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
241 1.0f)); 274 1.0f));
242 EXPECT_TRUE(image.BackedBySameObjectAs(copy)); 275 EXPECT_TRUE(image.BackedBySameObjectAs(copy));
243 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated)); 276 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated));
244 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated)); 277 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated));
245 } 278 }
246 279
247 #if ENABLE_NON_THREAD_SAFE 280 #if ENABLE_NON_THREAD_SAFE
248 TEST(ImageSkiaTest, EmptyOnThreadTest) { 281 TEST_F(ImageSkiaTest, EmptyOnThreadTest) {
249 ImageSkia empty; 282 ImageSkia empty;
250 test::TestOnThread empty_on_thread(&empty); 283 test::TestOnThread empty_on_thread(&empty);
251 empty_on_thread.Start(); 284 empty_on_thread.Start();
252 empty_on_thread.Join(); 285 empty_on_thread.Join();
253 EXPECT_TRUE(empty_on_thread.can_read()); 286 EXPECT_TRUE(empty_on_thread.can_read());
254 EXPECT_TRUE(empty_on_thread.can_modify()); 287 EXPECT_TRUE(empty_on_thread.can_modify());
255 } 288 }
256 289
257 TEST(ImageSkiaTest, StaticOnThreadTest) { 290 TEST_F(ImageSkiaTest, StaticOnThreadTest) {
258 ImageSkia image(ImageSkiaRep(Size(100, 200), 1.0f)); 291 ImageSkia image(ImageSkiaRep(Size(100, 200), 1.0f));
259 EXPECT_FALSE(image.IsThreadSafe()); 292 EXPECT_FALSE(image.IsThreadSafe());
260 293
261 test::TestOnThread image_on_thread(&image); 294 test::TestOnThread image_on_thread(&image);
262 // an image that was never accessed on this thread can be 295 // an image that was never accessed on this thread can be
263 // read by other thread. 296 // read by other thread.
264 image_on_thread.StartAndJoin(); 297 image_on_thread.StartAndJoin();
265 EXPECT_TRUE(image_on_thread.can_read()); 298 EXPECT_TRUE(image_on_thread.can_read());
266 EXPECT_TRUE(image_on_thread.can_modify()); 299 EXPECT_TRUE(image_on_thread.can_modify());
267 EXPECT_FALSE(image.CanRead()); 300 EXPECT_FALSE(image.CanRead());
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 image.MakeThreadSafe(); 349 image.MakeThreadSafe();
317 EXPECT_TRUE(image.IsThreadSafe()); 350 EXPECT_TRUE(image.IsThreadSafe());
318 test::TestOnThread threadsafe_on_thread(&image); 351 test::TestOnThread threadsafe_on_thread(&image);
319 threadsafe_on_thread.StartAndJoin(); 352 threadsafe_on_thread.StartAndJoin();
320 EXPECT_TRUE(threadsafe_on_thread.can_read()); 353 EXPECT_TRUE(threadsafe_on_thread.can_read());
321 EXPECT_FALSE(threadsafe_on_thread.can_modify()); 354 EXPECT_FALSE(threadsafe_on_thread.can_modify());
322 EXPECT_TRUE(image.CanRead()); 355 EXPECT_TRUE(image.CanRead());
323 EXPECT_FALSE(image.CanModify()); 356 EXPECT_FALSE(image.CanModify());
324 } 357 }
325 358
326 TEST(ImageSkiaTest, SourceOnThreadTest) { 359 TEST_F(ImageSkiaTest, SourceOnThreadTest) {
327 ImageSkia image(new DynamicSource(Size(100, 200)), Size(100, 200)); 360 ImageSkia image(new DynamicSource(Size(100, 200)), Size(100, 200));
328 EXPECT_FALSE(image.IsThreadSafe()); 361 EXPECT_FALSE(image.IsThreadSafe());
329 362
330 test::TestOnThread image_on_thread(&image); 363 test::TestOnThread image_on_thread(&image);
331 image_on_thread.StartAndJoin(); 364 image_on_thread.StartAndJoin();
332 // an image that was never accessed on this thread can be 365 // an image that was never accessed on this thread can be
333 // read by other thread. 366 // read by other thread.
334 EXPECT_TRUE(image_on_thread.can_read()); 367 EXPECT_TRUE(image_on_thread.can_read());
335 EXPECT_TRUE(image_on_thread.can_modify()); 368 EXPECT_TRUE(image_on_thread.can_modify());
336 EXPECT_FALSE(image.CanRead()); 369 EXPECT_FALSE(image.CanRead());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 EXPECT_TRUE(threadsafe_on_thread.can_read()); 401 EXPECT_TRUE(threadsafe_on_thread.can_read());
369 EXPECT_FALSE(threadsafe_on_thread.can_modify()); 402 EXPECT_FALSE(threadsafe_on_thread.can_modify());
370 EXPECT_TRUE(image.CanRead()); 403 EXPECT_TRUE(image.CanRead());
371 EXPECT_FALSE(image.CanModify()); 404 EXPECT_FALSE(image.CanModify());
372 } 405 }
373 #endif // ENABLE_NON_THREAD_SAFE 406 #endif // ENABLE_NON_THREAD_SAFE
374 407
375 // Just in case we ever get lumped together with other compilation units. 408 // Just in case we ever get lumped together with other compilation units.
376 #undef ENABLE_NON_THREAD_SAFE 409 #undef ENABLE_NON_THREAD_SAFE
377 410
378 TEST(ImageSkiaTest, Unscaled) { 411 TEST_F(ImageSkiaTest, Unscaled) {
379 SkBitmap bitmap; 412 SkBitmap bitmap;
380 413
381 // An ImageSkia created with 1x bitmap is unscaled. 414 // An ImageSkia created with 1x bitmap is unscaled.
382 ImageSkia image_skia = ImageSkia::CreateFrom1xBitmap(bitmap); 415 ImageSkia image_skia = ImageSkia::CreateFrom1xBitmap(bitmap);
383 EXPECT_TRUE(image_skia.GetRepresentation(1.0f).unscaled()); 416 EXPECT_TRUE(image_skia.GetRepresentation(1.0f).unscaled());
384 ImageSkiaRep rep_2x(Size(100, 100), 2.0f); 417 ImageSkiaRep rep_2x(Size(100, 100), 2.0f);
385 418
386 // When reps for other scales are added, the unscaled image 419 // When reps for other scales are added, the unscaled image
387 // becomes scaled. 420 // becomes scaled.
388 image_skia.AddRepresentation(rep_2x); 421 image_skia.AddRepresentation(rep_2x);
389 EXPECT_FALSE(image_skia.GetRepresentation(1.0f).unscaled()); 422 EXPECT_FALSE(image_skia.GetRepresentation(1.0f).unscaled());
390 EXPECT_FALSE(image_skia.GetRepresentation(2.0f).unscaled()); 423 EXPECT_FALSE(image_skia.GetRepresentation(2.0f).unscaled());
391 } 424 }
392 425
426 TEST_F(ImageSkiaTest, ArbitraryScaleFactor) {
427 base::CommandLine::ForCurrentProcess()->AppendSwitch(
428 switches::kAllowArbitraryScaleFactorInImageSkia);
429
430 // Do not test if the ImageSkia doesn't support arbitrary scale factors.
431 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled())
432 return;
433
434 // source is owned by |image|
435 DynamicSource* source = new DynamicSource(Size(100, 200));
436 ImageSkia image(source, gfx::Size(100, 200));
437
438 image.GetRepresentation(1.5f);
439 EXPECT_EQ(2.0f, source->GetLastRequestedScaleAndReset());
440 std::vector<ImageSkiaRep> image_reps = image.image_reps();
441 EXPECT_EQ(2u, image_reps.size());
442
443 std::vector<float> scale_factors;
444 for (size_t i = 0; i < image_reps.size(); ++i) {
445 scale_factors.push_back(image_reps[i].scale());
446 }
447 std::sort(scale_factors.begin(), scale_factors.end());
448 EXPECT_EQ(1.5f, scale_factors[0]);
449 EXPECT_EQ(2.0f, scale_factors[1]);
450
451 // Requesting 1.75 scale factor also falls back to 2.0f and rescale.
452 // However, the image already has the 2.0f data, so it won't fetch again.
453 image.GetRepresentation(1.75f);
454 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
455 image_reps = image.image_reps();
456 EXPECT_EQ(3u, image_reps.size());
457
458 scale_factors.clear();
459 for (size_t i = 0; i < image_reps.size(); ++i) {
460 scale_factors.push_back(image_reps[i].scale());
461 }
462 std::sort(scale_factors.begin(), scale_factors.end());
463 EXPECT_EQ(1.5f, scale_factors[0]);
464 EXPECT_EQ(1.75f, scale_factors[1]);
465 EXPECT_EQ(2.0f, scale_factors[2]);
466
467 // 1.25 is falled back to 1.0.
468 image.GetRepresentation(1.25f);
469 EXPECT_EQ(1.0f, source->GetLastRequestedScaleAndReset());
470 image_reps = image.image_reps();
471 EXPECT_EQ(5u, image_reps.size());
472
473 // Scale factor less than 1.0f will be falled back to 1.0f
474 image.GetRepresentation(0.75f);
475 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
476 image_reps = image.image_reps();
477 EXPECT_EQ(6u, image_reps.size());
478
479 scale_factors.clear();
480 for (size_t i = 0; i < image_reps.size(); ++i) {
481 scale_factors.push_back(image_reps[i].scale());
482 }
483 std::sort(scale_factors.begin(), scale_factors.end());
484 EXPECT_EQ(0.75f, scale_factors[0]);
485 EXPECT_EQ(1.0f, scale_factors[1]);
486 EXPECT_EQ(1.25f, scale_factors[2]);
487 EXPECT_EQ(1.5f, scale_factors[3]);
488 EXPECT_EQ(1.75f, scale_factors[4]);
489 EXPECT_EQ(2.0f, scale_factors[5]);
490
491 // Scale factor greater than 2.0f is falled back to 2.0f because it's not
492 // supported.
493 image.GetRepresentation(3.0f);
494 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
495 image_reps = image.image_reps();
496 EXPECT_EQ(7u, image_reps.size());
497 }
498
499 TEST_F(ImageSkiaTest, ArbitraryScaleFactorWithMissingResource) {
500 base::CommandLine::ForCurrentProcess()->AppendSwitch(
501 switches::kAllowArbitraryScaleFactorInImageSkia);
502
503 // Do not test if the ImageSkia doesn't support arbitrary scale factors.
504 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled())
505 return;
506
507 ImageSkia image(new FixedSource(
508 ImageSkiaRep(Size(100, 200), 1.0f)), Size(100, 200));
509
510 // Requesting 1.5f -- falls back to 2.0f, but couldn't find. It should
511 // look up 1.0f and then rescale it.
512 const ImageSkiaRep& rep = image.GetRepresentation(1.5f);
513 EXPECT_EQ(1.5f, rep.scale());
514 EXPECT_EQ(2U, image.image_reps().size());
515 EXPECT_EQ(1.0f, image.image_reps()[0].scale());
516 EXPECT_EQ(1.5f, image.image_reps()[1].scale());
517 }
518
519 TEST_F(ImageSkiaTest, UnscaledImageForArbitraryScaleFactor) {
520 base::CommandLine::ForCurrentProcess()->AppendSwitch(
521 switches::kAllowArbitraryScaleFactorInImageSkia);
522
523 // Do not test if the ImageSkia doesn't support arbitrary scale factors.
524 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled())
525 return;
526
527 // 0.0f means unscaled.
528 ImageSkia image(new FixedSource(
529 ImageSkiaRep(Size(100, 200), 0.0f)), Size(100, 200));
530
531 // Requesting 2.0f, which should return 1.0f unscaled image.
532 const ImageSkiaRep& rep = image.GetRepresentation(2.0f);
533 EXPECT_EQ(1.0f, rep.scale());
534 EXPECT_EQ("100x200", rep.pixel_size().ToString());
535 EXPECT_TRUE(rep.unscaled());
536 EXPECT_EQ(1U, image.image_reps().size());
537
538 // Same for any other scale factors.
539 const ImageSkiaRep& rep15 = image.GetRepresentation(1.5f);
540 EXPECT_EQ(1.0f, rep15.scale());
541 EXPECT_EQ("100x200", rep15.pixel_size().ToString());
542 EXPECT_TRUE(rep15.unscaled());
543 EXPECT_EQ(1U, image.image_reps().size());
544
545 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f);
546 EXPECT_EQ(1.0f, rep12.scale());
547 EXPECT_EQ("100x200", rep12.pixel_size().ToString());
548 EXPECT_TRUE(rep12.unscaled());
549 EXPECT_EQ(1U, image.image_reps().size());
550 }
551
393 } // namespace gfx 552 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/image/image_skia.cc ('k') | ui/gfx/switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698