Chromium Code Reviews| Index: ui/gfx/image/image_skia_unittest.cc |
| diff --git a/ui/gfx/image/image_skia_unittest.cc b/ui/gfx/image/image_skia_unittest.cc |
| index 0047822122bb67ee954d7e21f940dd7c149ff275..c341eb3d6a6ab155e0b677cbb44e6a4bd8999653 100644 |
| --- a/ui/gfx/image/image_skia_unittest.cc |
| +++ b/ui/gfx/image/image_skia_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "ui/gfx/image/image_skia.h" |
| +#include "base/command_line.h" |
| #include "base/logging.h" |
| #include "base/threading/simple_thread.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -11,6 +12,7 @@ |
| #include "ui/gfx/image/image_skia_rep.h" |
| #include "ui/gfx/image/image_skia_source.h" |
| #include "ui/gfx/size.h" |
| +#include "ui/gfx/switches.h" |
| // Duplicated from base/threading/non_thread_safe.h so that we can be |
| // good citizens there and undef the macro. |
| @@ -43,17 +45,27 @@ class FixedSource : public ImageSkiaSource { |
| class DynamicSource : public ImageSkiaSource { |
| public: |
| - DynamicSource(const gfx::Size& size) : size_(size) {} |
| + DynamicSource(const gfx::Size& size) |
| + : size_(size), |
| + last_requested_scale_(0.0f) {} |
| virtual ~DynamicSource() { |
| } |
| virtual ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
| + last_requested_scale_ = scale; |
| return gfx::ImageSkiaRep(size_, scale); |
| } |
| + float GetLastRequestedScaleAndReset() { |
| + float result = last_requested_scale_; |
| + last_requested_scale_ = 0.0f; |
| + return result; |
| + } |
| + |
| private: |
| gfx::Size size_; |
| + float last_requested_scale_; |
| DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
| }; |
| @@ -74,6 +86,23 @@ class NullSource: public ImageSkiaSource { |
| DISALLOW_COPY_AND_ASSIGN(NullSource); |
| }; |
| +class ScopedScaleFactorSetter { |
|
oshima
2014/05/06 20:59:49
can you use ScopedSetSupportedScaleFactors in ui/b
Jun Mukai
2014/05/06 21:06:27
well, DEPS prohibit to include ui/base/layout.h fr
|
| + public: |
| + ScopedScaleFactorSetter(const std::vector<float>& scales) { |
| + old_scales_ = ImageSkia::GetSupportedScales(); |
| + ImageSkia::SetSupportedScales(scales); |
| + } |
| + |
| + ~ScopedScaleFactorSetter() { |
| + ImageSkia::SetSupportedScales(old_scales_); |
| + } |
| + |
| + private: |
| + std::vector<float> old_scales_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedScaleFactorSetter); |
| +}; |
| + |
| } // namespace |
| namespace test { |
| @@ -375,4 +404,76 @@ TEST(ImageSkiaTest, SourceOnThreadTest) { |
| // Just in case we ever get lumped together with other compilation units. |
| #undef ENABLE_NON_THREAD_SAFE |
| +TEST(ImageSkiaTest, ArbitraryScaleFactor) { |
| + base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kAllowArbitraryScaleFactorInImageSkia); |
| + |
| + // Do not test if the ImageSkia doesn't support arbitrary scale factors. |
| + if (!ImageSkia::IsDSFScalingInImageSkiaEnabled()) |
| + return; |
| + |
| + // Sets the list of scale factors supported by resource bundle. |
| + std::vector<float> supported_scale_factors; |
| + supported_scale_factors.push_back(1.0f); |
| + supported_scale_factors.push_back(2.0f); |
| + ScopedScaleFactorSetter scoped_setter(supported_scale_factors); |
| + |
| + // source is owned by |image| |
| + DynamicSource* source = new DynamicSource(Size(100, 200)); |
| + ImageSkia image(source, gfx::Size(100, 200)); |
| + |
| + image.GetRepresentation(1.5f); |
| + EXPECT_EQ(2.0f, source->GetLastRequestedScaleAndReset()); |
| + std::vector<ImageSkiaRep> image_reps = image.image_reps(); |
| + EXPECT_EQ(2u, image_reps.size()); |
| + |
| + std::vector<float> scale_factors; |
| + for (size_t i = 0; i < image_reps.size(); ++i) { |
| + scale_factors.push_back(image_reps[i].scale()); |
| + } |
| + std::sort(scale_factors.begin(), scale_factors.end()); |
| + EXPECT_EQ(1.5f, scale_factors[0]); |
| + EXPECT_EQ(2.0f, scale_factors[1]); |
| + |
| + // Requesting 1.75 scale factor also falls back to 2.0f and rescale. |
| + // However, the image already has the 2.0f data, so it won't fetch again. |
| + image.GetRepresentation(1.75f); |
| + EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| + image_reps = image.image_reps(); |
| + EXPECT_EQ(3u, image_reps.size()); |
| + |
| + scale_factors.clear(); |
| + for (size_t i = 0; i < image_reps.size(); ++i) { |
| + scale_factors.push_back(image_reps[i].scale()); |
| + } |
| + std::sort(scale_factors.begin(), scale_factors.end()); |
| + EXPECT_EQ(1.5f, scale_factors[0]); |
| + EXPECT_EQ(1.75f, scale_factors[1]); |
| + EXPECT_EQ(2.0f, scale_factors[2]); |
| + |
| + // Scale factor less than 1.0f will be falled back to 1.0f |
| + image.GetRepresentation(0.75f); |
| + EXPECT_EQ(1.0f, source->GetLastRequestedScaleAndReset()); |
| + image_reps = image.image_reps(); |
| + EXPECT_EQ(5u, image_reps.size()); |
| + |
| + scale_factors.clear(); |
| + for (size_t i = 0; i < image_reps.size(); ++i) { |
| + scale_factors.push_back(image_reps[i].scale()); |
| + } |
| + std::sort(scale_factors.begin(), scale_factors.end()); |
| + EXPECT_EQ(0.75f, scale_factors[0]); |
| + EXPECT_EQ(1.0f, scale_factors[1]); |
| + EXPECT_EQ(1.5f, scale_factors[2]); |
| + EXPECT_EQ(1.75f, scale_factors[3]); |
| + EXPECT_EQ(2.0f, scale_factors[4]); |
| + |
| + // Scale factor greater than 2.0f is falled back to 2.0f because it's not |
| + // supported. |
| + image.GetRepresentation(3.0f); |
| + EXPECT_EQ(0.0f, source->GetLastRequestedScaleAndReset()); |
| + image_reps = image.image_reps(); |
| + EXPECT_EQ(6u, image_reps.size()); |
| +} |
| + |
| } // namespace gfx |