OLD | NEW |
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 Loading... |
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() { |
67 } | 79 } |
68 | 80 |
69 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 81 virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
70 return gfx::ImageSkiaRep(); | 82 return gfx::ImageSkiaRep(); |
71 } | 83 } |
72 | 84 |
73 private: | 85 private: |
74 DISALLOW_COPY_AND_ASSIGN(NullSource); | 86 DISALLOW_COPY_AND_ASSIGN(NullSource); |
75 }; | 87 }; |
76 | 88 |
| 89 class ScopedScaleFactorSetter { |
| 90 public: |
| 91 ScopedScaleFactorSetter(const std::vector<float>& scales) { |
| 92 old_scales_ = ImageSkia::GetSupportedScales(); |
| 93 ImageSkia::SetSupportedScales(scales); |
| 94 } |
| 95 |
| 96 ~ScopedScaleFactorSetter() { |
| 97 ImageSkia::SetSupportedScales(old_scales_); |
| 98 } |
| 99 |
| 100 private: |
| 101 std::vector<float> old_scales_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ScopedScaleFactorSetter); |
| 104 }; |
| 105 |
77 } // namespace | 106 } // namespace |
78 | 107 |
79 namespace test { | 108 namespace test { |
80 class TestOnThread : public base::SimpleThread { | 109 class TestOnThread : public base::SimpleThread { |
81 public: | 110 public: |
82 explicit TestOnThread(ImageSkia* image_skia) | 111 explicit TestOnThread(ImageSkia* image_skia) |
83 : SimpleThread("image_skia_on_thread"), | 112 : SimpleThread("image_skia_on_thread"), |
84 image_skia_(image_skia), | 113 image_skia_(image_skia), |
85 can_read_(false), | 114 can_read_(false), |
86 can_modify_(false) { | 115 can_modify_(false) { |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 EXPECT_TRUE(threadsafe_on_thread.can_read()); | 397 EXPECT_TRUE(threadsafe_on_thread.can_read()); |
369 EXPECT_FALSE(threadsafe_on_thread.can_modify()); | 398 EXPECT_FALSE(threadsafe_on_thread.can_modify()); |
370 EXPECT_TRUE(image.CanRead()); | 399 EXPECT_TRUE(image.CanRead()); |
371 EXPECT_FALSE(image.CanModify()); | 400 EXPECT_FALSE(image.CanModify()); |
372 } | 401 } |
373 #endif // ENABLE_NON_THREAD_SAFE | 402 #endif // ENABLE_NON_THREAD_SAFE |
374 | 403 |
375 // Just in case we ever get lumped together with other compilation units. | 404 // Just in case we ever get lumped together with other compilation units. |
376 #undef ENABLE_NON_THREAD_SAFE | 405 #undef ENABLE_NON_THREAD_SAFE |
377 | 406 |
| 407 TEST(ImageSkiaTest, ArbitraryScaleFactor) { |
| 408 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 409 switches::kAllowArbitraryScaleFactorInImageSkia); |
| 410 |
| 411 // Do not test if the ImageSkia doesn't support arbitrary scale factors. |
| 412 if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) |
| 413 return; |
| 414 |
| 415 // Sets the list of scale factors supported by resource bundle. |
| 416 std::vector<float> supported_scale_factors; |
| 417 supported_scale_factors.push_back(1.0f); |
| 418 supported_scale_factors.push_back(2.0f); |
| 419 ScopedScaleFactorSetter scoped_setter(supported_scale_factors); |
| 420 |
| 421 // source is owned by |image| |
| 422 DynamicSource* source = new DynamicSource(Size(100, 200)); |
| 423 ImageSkia image(source, gfx::Size(100, 200)); |
| 424 |
| 425 image.GetRepresentation(1.5f); |
| 426 EXPECT_EQ(2.0f, source->GetLastRequestedScaleAndReset()); |
| 427 std::vector<ImageSkiaRep> image_reps = image.image_reps(); |
| 428 EXPECT_EQ(2u, image_reps.size()); |
| 429 |
| 430 std::vector<float> scale_factors; |
| 431 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 432 scale_factors.push_back(image_reps[i].scale()); |
| 433 } |
| 434 std::sort(scale_factors.begin(), scale_factors.end()); |
| 435 EXPECT_EQ(1.5f, scale_factors[0]); |
| 436 EXPECT_EQ(2.0f, scale_factors[1]); |
| 437 |
| 438 // Requesting 1.75 scale factor also falls back to 2.0f and rescale. |
| 439 // However, the image already has the 2.0f data, so it won't fetch again. |
| 440 image.GetRepresentation(1.75f); |
| 441 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| 442 image_reps = image.image_reps(); |
| 443 EXPECT_EQ(3u, image_reps.size()); |
| 444 |
| 445 scale_factors.clear(); |
| 446 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 447 scale_factors.push_back(image_reps[i].scale()); |
| 448 } |
| 449 std::sort(scale_factors.begin(), scale_factors.end()); |
| 450 EXPECT_EQ(1.5f, scale_factors[0]); |
| 451 EXPECT_EQ(1.75f, scale_factors[1]); |
| 452 EXPECT_EQ(2.0f, scale_factors[2]); |
| 453 |
| 454 // 1.25 is falled back to 1.0. |
| 455 image.GetRepresentation(1.25f); |
| 456 EXPECT_EQ(1.0f, source->GetLastRequestedScaleAndReset()); |
| 457 image_reps = image.image_reps(); |
| 458 EXPECT_EQ(5u, image_reps.size()); |
| 459 |
| 460 // Scale factor less than 1.0f will be falled back to 1.0f |
| 461 image.GetRepresentation(0.75f); |
| 462 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| 463 image_reps = image.image_reps(); |
| 464 EXPECT_EQ(6u, image_reps.size()); |
| 465 |
| 466 scale_factors.clear(); |
| 467 for (size_t i = 0; i < image_reps.size(); ++i) { |
| 468 scale_factors.push_back(image_reps[i].scale()); |
| 469 } |
| 470 std::sort(scale_factors.begin(), scale_factors.end()); |
| 471 EXPECT_EQ(0.75f, scale_factors[0]); |
| 472 EXPECT_EQ(1.0f, scale_factors[1]); |
| 473 EXPECT_EQ(1.25f, scale_factors[2]); |
| 474 EXPECT_EQ(1.5f, scale_factors[3]); |
| 475 EXPECT_EQ(1.75f, scale_factors[4]); |
| 476 EXPECT_EQ(2.0f, scale_factors[5]); |
| 477 |
| 478 // Scale factor greater than 2.0f is falled back to 2.0f because it's not |
| 479 // supported. |
| 480 image.GetRepresentation(3.0f); |
| 481 EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| 482 image_reps = image.image_reps(); |
| 483 EXPECT_EQ(7u, image_reps.size()); |
| 484 } |
| 485 |
378 } // namespace gfx | 486 } // namespace gfx |
OLD | NEW |