Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / ui / gfx / image / image_skia_unittest.cc
blob49348e23c095f80ea3bcf99023f7cdad7a65876b
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
3 // found in the LICENSE file.
5 #include "ui/gfx/image/image_skia.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/threading/simple_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/gfx/geometry/size.h"
13 #include "ui/gfx/image/image_skia_rep.h"
14 #include "ui/gfx/image/image_skia_source.h"
15 #include "ui/gfx/switches.h"
17 // Duplicated from base/threading/non_thread_safe.h so that we can be
18 // good citizens there and undef the macro.
19 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
20 #define ENABLE_NON_THREAD_SAFE 1
21 #else
22 #define ENABLE_NON_THREAD_SAFE 0
23 #endif
25 namespace gfx {
27 namespace {
29 class FixedSource : public ImageSkiaSource {
30 public:
31 explicit FixedSource(const ImageSkiaRep& image) : image_(image) {}
33 ~FixedSource() override {}
35 ImageSkiaRep GetImageForScale(float scale) override { return image_; }
37 private:
38 ImageSkiaRep image_;
40 DISALLOW_COPY_AND_ASSIGN(FixedSource);
43 class FixedScaleSource : public ImageSkiaSource {
44 public:
45 explicit FixedScaleSource(const ImageSkiaRep& image) : image_(image) {}
47 ~FixedScaleSource() override {}
49 ImageSkiaRep GetImageForScale(float scale) override {
50 if (!image_.unscaled() && image_.scale() != scale)
51 return ImageSkiaRep();
52 return image_;
55 private:
56 ImageSkiaRep image_;
58 DISALLOW_COPY_AND_ASSIGN(FixedScaleSource);
61 class DynamicSource : public ImageSkiaSource {
62 public:
63 explicit DynamicSource(const gfx::Size& size)
64 : size_(size),
65 last_requested_scale_(0.0f) {}
67 ~DynamicSource() override {}
69 ImageSkiaRep GetImageForScale(float scale) override {
70 last_requested_scale_ = scale;
71 return gfx::ImageSkiaRep(size_, scale);
74 float GetLastRequestedScaleAndReset() {
75 float result = last_requested_scale_;
76 last_requested_scale_ = 0.0f;
77 return result;
80 private:
81 gfx::Size size_;
82 float last_requested_scale_;
84 DISALLOW_COPY_AND_ASSIGN(DynamicSource);
87 class NullSource: public ImageSkiaSource {
88 public:
89 NullSource() {
92 ~NullSource() override {}
94 ImageSkiaRep GetImageForScale(float scale) override {
95 return gfx::ImageSkiaRep();
98 private:
99 DISALLOW_COPY_AND_ASSIGN(NullSource);
102 } // namespace
104 namespace test {
105 class TestOnThread : public base::SimpleThread {
106 public:
107 explicit TestOnThread(ImageSkia* image_skia)
108 : SimpleThread("image_skia_on_thread"),
109 image_skia_(image_skia),
110 can_read_(false),
111 can_modify_(false) {
114 void Run() override {
115 can_read_ = image_skia_->CanRead();
116 can_modify_ = image_skia_->CanModify();
117 if (can_read_)
118 image_skia_->image_reps();
121 void StartAndJoin() {
122 Start();
123 Join();
126 bool can_read() const { return can_read_; }
128 bool can_modify() const { return can_modify_; }
130 private:
131 ImageSkia* image_skia_;
133 bool can_read_;
134 bool can_modify_;
136 DISALLOW_COPY_AND_ASSIGN(TestOnThread);
139 } // namespace test
141 class ImageSkiaTest : public testing::Test {
142 public:
143 ImageSkiaTest() {
144 // In the test, we assume that we support 1.0f and 2.0f DSFs.
145 old_scales_ = ImageSkia::GetSupportedScales();
147 // Sets the list of scale factors supported by resource bundle.
148 std::vector<float> supported_scales;
149 supported_scales.push_back(1.0f);
150 supported_scales.push_back(2.0f);
151 ImageSkia::SetSupportedScales(supported_scales);
153 ~ImageSkiaTest() override { ImageSkia::SetSupportedScales(old_scales_); }
155 private:
156 std::vector<float> old_scales_;
157 DISALLOW_COPY_AND_ASSIGN(ImageSkiaTest);
160 TEST_F(ImageSkiaTest, FixedSource) {
161 ImageSkiaRep image(Size(100, 200), 1.0f);
162 ImageSkia image_skia(new FixedSource(image), Size(100, 200));
163 EXPECT_EQ(0U, image_skia.image_reps().size());
165 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
166 EXPECT_EQ(100, result_100p.GetWidth());
167 EXPECT_EQ(200, result_100p.GetHeight());
168 EXPECT_EQ(1.0f, result_100p.scale());
169 EXPECT_EQ(1U, image_skia.image_reps().size());
171 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f);
173 EXPECT_EQ(100, result_200p.GetWidth());
174 EXPECT_EQ(200, result_200p.GetHeight());
175 EXPECT_EQ(100, result_200p.pixel_width());
176 EXPECT_EQ(200, result_200p.pixel_height());
177 EXPECT_EQ(1.0f, result_200p.scale());
178 EXPECT_EQ(1U, image_skia.image_reps().size());
180 // Get the representation again and make sure it doesn't
181 // generate new image skia rep.
182 image_skia.GetRepresentation(1.0f);
183 image_skia.GetRepresentation(2.0f);
184 EXPECT_EQ(1U, image_skia.image_reps().size());
187 TEST_F(ImageSkiaTest, FixedScaledSource) {
188 ImageSkiaRep image(Size(100, 200), 1.0f);
189 ImageSkia image_skia(new FixedScaleSource(image), Size(100, 200));
190 EXPECT_EQ(0U, image_skia.image_reps().size());
192 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
193 EXPECT_EQ(100, result_100p.GetWidth());
194 EXPECT_EQ(200, result_100p.GetHeight());
195 EXPECT_EQ(1.0f, result_100p.scale());
196 EXPECT_EQ(1U, image_skia.image_reps().size());
198 // 2.0f data doesn't exist, then it falls back to 1.0f and rescale it.
199 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f);
201 EXPECT_EQ(100, result_200p.GetWidth());
202 EXPECT_EQ(200, result_200p.GetHeight());
203 EXPECT_EQ(200, result_200p.pixel_width());
204 EXPECT_EQ(400, result_200p.pixel_height());
205 EXPECT_EQ(2.0f, result_200p.scale());
206 EXPECT_EQ(2U, image_skia.image_reps().size());
208 // Get the representation again and make sure it doesn't
209 // generate new image skia rep.
210 image_skia.GetRepresentation(1.0f);
211 image_skia.GetRepresentation(2.0f);
212 EXPECT_EQ(2U, image_skia.image_reps().size());
215 TEST_F(ImageSkiaTest, FixedUnscaledSource) {
216 ImageSkiaRep image(Size(100, 200), 0.0f);
217 ImageSkia image_skia(new FixedScaleSource(image), Size(100, 200));
218 EXPECT_EQ(0U, image_skia.image_reps().size());
220 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
221 EXPECT_EQ(100, result_100p.pixel_width());
222 EXPECT_EQ(200, result_100p.pixel_height());
223 EXPECT_TRUE(result_100p.unscaled());
224 EXPECT_EQ(1U, image_skia.image_reps().size());
226 // 2.0f data doesn't exist, but unscaled ImageSkiaRep shouldn't be rescaled.
227 const ImageSkiaRep& result_200p = image_skia.GetRepresentation(2.0f);
229 EXPECT_EQ(100, result_200p.pixel_width());
230 EXPECT_EQ(200, result_200p.pixel_height());
231 EXPECT_TRUE(result_200p.unscaled());
232 EXPECT_EQ(1U, image_skia.image_reps().size());
234 // Get the representation again and make sure it doesn't
235 // generate new image skia rep.
236 image_skia.GetRepresentation(1.0f);
237 image_skia.GetRepresentation(2.0f);
238 EXPECT_EQ(1U, image_skia.image_reps().size());
241 TEST_F(ImageSkiaTest, DynamicSource) {
242 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
243 EXPECT_EQ(0U, image_skia.image_reps().size());
244 const ImageSkiaRep& result_100p = image_skia.GetRepresentation(1.0f);
245 EXPECT_EQ(100, result_100p.GetWidth());
246 EXPECT_EQ(200, result_100p.GetHeight());
247 EXPECT_EQ(1.0f, result_100p.scale());
248 EXPECT_EQ(1U, image_skia.image_reps().size());
250 const ImageSkiaRep& result_200p =
251 image_skia.GetRepresentation(2.0f);
252 EXPECT_EQ(100, result_200p.GetWidth());
253 EXPECT_EQ(200, result_200p.GetHeight());
254 EXPECT_EQ(200, result_200p.pixel_width());
255 EXPECT_EQ(400, result_200p.pixel_height());
256 EXPECT_EQ(2.0f, result_200p.scale());
257 EXPECT_EQ(2U, image_skia.image_reps().size());
259 // Get the representation again and make sure it doesn't
260 // generate new image skia rep.
261 image_skia.GetRepresentation(1.0f);
262 EXPECT_EQ(2U, image_skia.image_reps().size());
263 image_skia.GetRepresentation(2.0f);
264 EXPECT_EQ(2U, image_skia.image_reps().size());
267 // Tests that image_reps returns all of the representations in the
268 // image when there are multiple representations for a scale factor.
269 // This currently is the case with ImageLoader::LoadImages.
270 TEST_F(ImageSkiaTest, ManyRepsPerScaleFactor) {
271 const int kSmallIcon1x = 16;
272 const int kSmallIcon2x = 32;
273 const int kLargeIcon1x = 32;
275 ImageSkia image(new NullSource(), gfx::Size(kSmallIcon1x, kSmallIcon1x));
276 // Simulate a source which loads images on a delay. Upon
277 // GetImageForScaleFactor, it immediately returns null and starts loading
278 // image reps slowly.
279 image.GetRepresentation(1.0f);
280 image.GetRepresentation(2.0f);
282 // After a lengthy amount of simulated time, finally loaded image reps.
283 image.AddRepresentation(ImageSkiaRep(
284 gfx::Size(kSmallIcon1x, kSmallIcon1x), 1.0f));
285 image.AddRepresentation(ImageSkiaRep(
286 gfx::Size(kSmallIcon2x, kSmallIcon2x), 2.0f));
287 image.AddRepresentation(ImageSkiaRep(
288 gfx::Size(kLargeIcon1x, kLargeIcon1x), 1.0f));
290 std::vector<ImageSkiaRep> image_reps = image.image_reps();
291 EXPECT_EQ(3u, image_reps.size());
293 int num_1x = 0;
294 int num_2x = 0;
295 for (size_t i = 0; i < image_reps.size(); ++i) {
296 if (image_reps[i].scale() == 1.0f)
297 num_1x++;
298 else if (image_reps[i].scale() == 2.0f)
299 num_2x++;
301 EXPECT_EQ(2, num_1x);
302 EXPECT_EQ(1, num_2x);
305 TEST_F(ImageSkiaTest, GetBitmap) {
306 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200));
307 const SkBitmap* bitmap = image_skia.bitmap();
308 EXPECT_NE(static_cast<SkBitmap*>(NULL), bitmap);
309 EXPECT_FALSE(bitmap->isNull());
312 TEST_F(ImageSkiaTest, GetBitmapFromEmpty) {
313 // Create an image with 1 representation and remove it so the ImageSkiaStorage
314 // is left with no representations.
315 ImageSkia empty_image(ImageSkiaRep(Size(100, 200), 1.0f));
316 ImageSkia empty_image_copy(empty_image);
317 empty_image.RemoveRepresentation(1.0f);
319 // Check that ImageSkia::bitmap() still returns a valid SkBitmap pointer for
320 // the image and all its copies.
321 const SkBitmap* bitmap = empty_image_copy.bitmap();
322 ASSERT_NE(static_cast<SkBitmap*>(NULL), bitmap);
323 EXPECT_TRUE(bitmap->isNull());
324 EXPECT_TRUE(bitmap->empty());
327 TEST_F(ImageSkiaTest, BackedBySameObjectAs) {
328 // Null images should all be backed by the same object (NULL).
329 ImageSkia image;
330 ImageSkia unrelated;
331 EXPECT_TRUE(image.BackedBySameObjectAs(unrelated));
333 image.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
334 1.0f));
335 ImageSkia copy = image;
336 copy.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
337 2.0f));
338 unrelated.AddRepresentation(gfx::ImageSkiaRep(gfx::Size(10, 10),
339 1.0f));
340 EXPECT_TRUE(image.BackedBySameObjectAs(copy));
341 EXPECT_FALSE(image.BackedBySameObjectAs(unrelated));
342 EXPECT_FALSE(copy.BackedBySameObjectAs(unrelated));
345 #if ENABLE_NON_THREAD_SAFE
346 TEST_F(ImageSkiaTest, EmptyOnThreadTest) {
347 ImageSkia empty;
348 test::TestOnThread empty_on_thread(&empty);
349 empty_on_thread.Start();
350 empty_on_thread.Join();
351 EXPECT_TRUE(empty_on_thread.can_read());
352 EXPECT_TRUE(empty_on_thread.can_modify());
355 TEST_F(ImageSkiaTest, StaticOnThreadTest) {
356 ImageSkia image(ImageSkiaRep(Size(100, 200), 1.0f));
357 EXPECT_FALSE(image.IsThreadSafe());
359 test::TestOnThread image_on_thread(&image);
360 // an image that was never accessed on this thread can be
361 // read by other thread.
362 image_on_thread.StartAndJoin();
363 EXPECT_TRUE(image_on_thread.can_read());
364 EXPECT_TRUE(image_on_thread.can_modify());
365 EXPECT_FALSE(image.CanRead());
366 EXPECT_FALSE(image.CanModify());
368 image.DetachStorageFromThread();
369 // An image is accessed by this thread,
370 // so other thread cannot read/modify it.
371 image.image_reps();
372 test::TestOnThread image_on_thread2(&image);
373 image_on_thread2.StartAndJoin();
374 EXPECT_FALSE(image_on_thread2.can_read());
375 EXPECT_FALSE(image_on_thread2.can_modify());
376 EXPECT_TRUE(image.CanRead());
377 EXPECT_TRUE(image.CanModify());
379 image.DetachStorageFromThread();
380 scoped_ptr<ImageSkia> deep_copy(image.DeepCopy());
381 EXPECT_FALSE(deep_copy->IsThreadSafe());
382 test::TestOnThread deepcopy_on_thread(deep_copy.get());
383 deepcopy_on_thread.StartAndJoin();
384 EXPECT_TRUE(deepcopy_on_thread.can_read());
385 EXPECT_TRUE(deepcopy_on_thread.can_modify());
386 EXPECT_FALSE(deep_copy->CanRead());
387 EXPECT_FALSE(deep_copy->CanModify());
389 scoped_ptr<ImageSkia> deep_copy2(image.DeepCopy());
390 EXPECT_EQ(1U, deep_copy2->image_reps().size());
391 // Access it from current thread so that it can't be
392 // accessed from another thread.
393 deep_copy2->image_reps();
394 EXPECT_FALSE(deep_copy2->IsThreadSafe());
395 test::TestOnThread deepcopy2_on_thread(deep_copy2.get());
396 deepcopy2_on_thread.StartAndJoin();
397 EXPECT_FALSE(deepcopy2_on_thread.can_read());
398 EXPECT_FALSE(deepcopy2_on_thread.can_modify());
399 EXPECT_TRUE(deep_copy2->CanRead());
400 EXPECT_TRUE(deep_copy2->CanModify());
402 image.DetachStorageFromThread();
403 image.SetReadOnly();
404 // A read-only ImageSkia with no source is thread safe.
405 EXPECT_TRUE(image.IsThreadSafe());
406 test::TestOnThread readonly_on_thread(&image);
407 readonly_on_thread.StartAndJoin();
408 EXPECT_TRUE(readonly_on_thread.can_read());
409 EXPECT_FALSE(readonly_on_thread.can_modify());
410 EXPECT_TRUE(image.CanRead());
411 EXPECT_FALSE(image.CanModify());
413 image.DetachStorageFromThread();
414 image.MakeThreadSafe();
415 EXPECT_TRUE(image.IsThreadSafe());
416 test::TestOnThread threadsafe_on_thread(&image);
417 threadsafe_on_thread.StartAndJoin();
418 EXPECT_TRUE(threadsafe_on_thread.can_read());
419 EXPECT_FALSE(threadsafe_on_thread.can_modify());
420 EXPECT_TRUE(image.CanRead());
421 EXPECT_FALSE(image.CanModify());
424 TEST_F(ImageSkiaTest, SourceOnThreadTest) {
425 ImageSkia image(new DynamicSource(Size(100, 200)), Size(100, 200));
426 EXPECT_FALSE(image.IsThreadSafe());
428 test::TestOnThread image_on_thread(&image);
429 image_on_thread.StartAndJoin();
430 // an image that was never accessed on this thread can be
431 // read by other thread.
432 EXPECT_TRUE(image_on_thread.can_read());
433 EXPECT_TRUE(image_on_thread.can_modify());
434 EXPECT_FALSE(image.CanRead());
435 EXPECT_FALSE(image.CanModify());
437 image.DetachStorageFromThread();
438 // An image is accessed by this thread,
439 // so other thread cannot read/modify it.
440 image.image_reps();
441 test::TestOnThread image_on_thread2(&image);
442 image_on_thread2.StartAndJoin();
443 EXPECT_FALSE(image_on_thread2.can_read());
444 EXPECT_FALSE(image_on_thread2.can_modify());
445 EXPECT_TRUE(image.CanRead());
446 EXPECT_TRUE(image.CanModify());
448 image.DetachStorageFromThread();
449 image.SetReadOnly();
450 EXPECT_FALSE(image.IsThreadSafe());
451 test::TestOnThread readonly_on_thread(&image);
452 readonly_on_thread.StartAndJoin();
453 EXPECT_TRUE(readonly_on_thread.can_read());
454 EXPECT_FALSE(readonly_on_thread.can_modify());
455 EXPECT_FALSE(image.CanRead());
456 EXPECT_FALSE(image.CanModify());
458 image.DetachStorageFromThread();
459 image.MakeThreadSafe();
460 EXPECT_TRUE(image.IsThreadSafe());
461 // Check if image reps are generated for supported scale factors.
462 EXPECT_EQ(ImageSkia::GetSupportedScales().size(),
463 image.image_reps().size());
464 test::TestOnThread threadsafe_on_thread(&image);
465 threadsafe_on_thread.StartAndJoin();
466 EXPECT_TRUE(threadsafe_on_thread.can_read());
467 EXPECT_FALSE(threadsafe_on_thread.can_modify());
468 EXPECT_TRUE(image.CanRead());
469 EXPECT_FALSE(image.CanModify());
471 #endif // ENABLE_NON_THREAD_SAFE
473 // Just in case we ever get lumped together with other compilation units.
474 #undef ENABLE_NON_THREAD_SAFE
476 TEST_F(ImageSkiaTest, Unscaled) {
477 SkBitmap bitmap;
479 // An ImageSkia created with 1x bitmap is unscaled.
480 ImageSkia image_skia = ImageSkia::CreateFrom1xBitmap(bitmap);
481 EXPECT_TRUE(image_skia.GetRepresentation(1.0f).unscaled());
482 ImageSkiaRep rep_2x(Size(100, 100), 2.0f);
484 // When reps for other scales are added, the unscaled image
485 // becomes scaled.
486 image_skia.AddRepresentation(rep_2x);
487 EXPECT_FALSE(image_skia.GetRepresentation(1.0f).unscaled());
488 EXPECT_FALSE(image_skia.GetRepresentation(2.0f).unscaled());
491 namespace {
493 std::vector<float> GetSortedScaleFactors(const gfx::ImageSkia& image) {
494 const std::vector<ImageSkiaRep>& image_reps = image.image_reps();
495 std::vector<float> scale_factors;
496 for (size_t i = 0; i < image_reps.size(); ++i) {
497 scale_factors.push_back(image_reps[i].scale());
499 std::sort(scale_factors.begin(), scale_factors.end());
500 return scale_factors;
503 } // namespace
505 TEST_F(ImageSkiaTest, ArbitraryScaleFactor) {
506 // source is owned by |image|
507 DynamicSource* source = new DynamicSource(Size(100, 200));
508 ImageSkia image(source, gfx::Size(100, 200));
510 image.GetRepresentation(1.5f);
511 EXPECT_EQ(2.0f, source->GetLastRequestedScaleAndReset());
512 std::vector<ImageSkiaRep> image_reps = image.image_reps();
513 EXPECT_EQ(2u, image_reps.size());
515 std::vector<float> scale_factors = GetSortedScaleFactors(image);
516 EXPECT_EQ(1.5f, scale_factors[0]);
517 EXPECT_EQ(2.0f, scale_factors[1]);
519 // Requesting 1.75 scale factor also falls back to 2.0f and rescale.
520 // However, the image already has the 2.0f data, so it won't fetch again.
521 image.GetRepresentation(1.75f);
522 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
523 image_reps = image.image_reps();
524 EXPECT_EQ(3u, image_reps.size());
526 scale_factors = GetSortedScaleFactors(image);
527 EXPECT_EQ(1.5f, scale_factors[0]);
528 EXPECT_EQ(1.75f, scale_factors[1]);
529 EXPECT_EQ(2.0f, scale_factors[2]);
531 // Requesting 1.25 scale factor also falls back to 2.0f and rescale.
532 // However, the image already has the 2.0f data, so it won't fetch again.
533 image.GetRepresentation(1.25f);
534 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
535 image_reps = image.image_reps();
536 EXPECT_EQ(4u, image_reps.size());
537 scale_factors = GetSortedScaleFactors(image);
538 EXPECT_EQ(1.25f, scale_factors[0]);
539 EXPECT_EQ(1.5f, scale_factors[1]);
540 EXPECT_EQ(1.75f, scale_factors[2]);
541 EXPECT_EQ(2.0f, scale_factors[3]);
543 // 1.20 is falled back to 1.0.
544 image.GetRepresentation(1.20f);
545 EXPECT_EQ(1.0f, source->GetLastRequestedScaleAndReset());
546 image_reps = image.image_reps();
547 EXPECT_EQ(6u, image_reps.size());
548 scale_factors = GetSortedScaleFactors(image);
549 EXPECT_EQ(1.0f, scale_factors[0]);
550 EXPECT_EQ(1.2f, scale_factors[1]);
551 EXPECT_EQ(1.25f, scale_factors[2]);
552 EXPECT_EQ(1.5f, scale_factors[3]);
553 EXPECT_EQ(1.75f, scale_factors[4]);
554 EXPECT_EQ(2.0f, scale_factors[5]);
556 // Scale factor less than 1.0f will be falled back to 1.0f
557 image.GetRepresentation(0.75f);
558 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
559 image_reps = image.image_reps();
560 EXPECT_EQ(7u, image_reps.size());
562 scale_factors = GetSortedScaleFactors(image);
563 EXPECT_EQ(0.75f, scale_factors[0]);
564 EXPECT_EQ(1.0f, scale_factors[1]);
565 EXPECT_EQ(1.2f, scale_factors[2]);
566 EXPECT_EQ(1.25f, scale_factors[3]);
567 EXPECT_EQ(1.5f, scale_factors[4]);
568 EXPECT_EQ(1.75f, scale_factors[5]);
569 EXPECT_EQ(2.0f, scale_factors[6]);
571 // Scale factor greater than 2.0f is falled back to 2.0f because it's not
572 // supported.
573 image.GetRepresentation(3.0f);
574 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset());
575 image_reps = image.image_reps();
576 EXPECT_EQ(8u, image_reps.size());
579 TEST_F(ImageSkiaTest, ArbitraryScaleFactorWithMissingResource) {
580 ImageSkia image(new FixedScaleSource(
581 ImageSkiaRep(Size(100, 200), 1.0f)), Size(100, 200));
583 // Requesting 1.5f -- falls back to 2.0f, but couldn't find. It should
584 // look up 1.0f and then rescale it. Note that the rescaled ImageSkiaRep will
585 // have 2.0f scale.
586 const ImageSkiaRep& rep = image.GetRepresentation(1.5f);
587 EXPECT_EQ(1.5f, rep.scale());
588 EXPECT_EQ(2U, image.image_reps().size());
589 EXPECT_EQ(2.0f, image.image_reps()[0].scale());
590 EXPECT_EQ(1.5f, image.image_reps()[1].scale());
593 TEST_F(ImageSkiaTest, UnscaledImageForArbitraryScaleFactor) {
594 // 0.0f means unscaled.
595 ImageSkia image(new FixedScaleSource(
596 ImageSkiaRep(Size(100, 200), 0.0f)), Size(100, 200));
598 // Requesting 2.0f, which should return 1.0f unscaled image.
599 const ImageSkiaRep& rep = image.GetRepresentation(2.0f);
600 EXPECT_EQ(1.0f, rep.scale());
601 EXPECT_EQ("100x200", rep.pixel_size().ToString());
602 EXPECT_TRUE(rep.unscaled());
603 EXPECT_EQ(1U, image.image_reps().size());
605 // Same for any other scale factors.
606 const ImageSkiaRep& rep15 = image.GetRepresentation(1.5f);
607 EXPECT_EQ(1.0f, rep15.scale());
608 EXPECT_EQ("100x200", rep15.pixel_size().ToString());
609 EXPECT_TRUE(rep15.unscaled());
610 EXPECT_EQ(1U, image.image_reps().size());
612 const ImageSkiaRep& rep12 = image.GetRepresentation(1.2f);
613 EXPECT_EQ(1.0f, rep12.scale());
614 EXPECT_EQ("100x200", rep12.pixel_size().ToString());
615 EXPECT_TRUE(rep12.unscaled());
616 EXPECT_EQ(1U, image.image_reps().size());
619 } // namespace gfx