OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/gfx/image/image_skia.h" |
| 6 |
| 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/image/image_skia_rep.h" |
| 10 #include "ui/gfx/image/image_skia_source.h" |
| 11 #include "ui/gfx/size.h" |
| 12 #include "ui/base/layout.h" |
| 13 |
| 14 namespace gfx { |
| 15 |
| 16 namespace { |
| 17 |
| 18 ImageSkiaRep CreateImage(const gfx::Size& size, ui::ScaleFactor scale_factor) { |
| 19 SkBitmap bitmap; |
| 20 gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); |
| 21 bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
| 22 pixel_size.width(), pixel_size.height()); |
| 23 bitmap.allocPixels(); |
| 24 return ImageSkiaRep(bitmap, scale_factor); |
| 25 } |
| 26 |
| 27 class FixedSource : public ImageSkiaSource { |
| 28 public: |
| 29 FixedSource(const ImageSkiaRep& image) : image_(image) {} |
| 30 |
| 31 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 32 return image_; |
| 33 } |
| 34 |
| 35 private: |
| 36 ImageSkiaRep image_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(FixedSource); |
| 39 }; |
| 40 |
| 41 class DynamicSource : public ImageSkiaSource { |
| 42 public: |
| 43 DynamicSource(const gfx::Size& size) : size_(size) {} |
| 44 |
| 45 virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
| 46 return CreateImage(size_, scale_factor); |
| 47 } |
| 48 |
| 49 private: |
| 50 gfx::Size size_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
| 53 }; |
| 54 |
| 55 } // namespace; |
| 56 |
| 57 typedef testing::Test ImageSkiaTest; |
| 58 |
| 59 TEST(ImageSkiaTest, FixedSource) { |
| 60 ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P)); |
| 61 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
| 62 EXPECT_EQ(0U, image_skia.image_reps().size()); |
| 63 |
| 64 const ImageSkiaRep& result_100p = |
| 65 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 66 EXPECT_EQ(100, result_100p.GetWidth()); |
| 67 EXPECT_EQ(200, result_100p.GetHeight()); |
| 68 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
| 69 EXPECT_EQ(1U, image_skia.image_reps().size()); |
| 70 |
| 71 const ImageSkiaRep& result_200p = |
| 72 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 73 |
| 74 EXPECT_EQ(100, result_200p.GetWidth()); |
| 75 EXPECT_EQ(200, result_200p.GetHeight()); |
| 76 EXPECT_EQ(100, result_200p.pixel_width()); |
| 77 EXPECT_EQ(200, result_200p.pixel_height()); |
| 78 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_200p.scale_factor()); |
| 79 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 80 |
| 81 // Get the representation again and make sure it doesn't |
| 82 // generate new image skia rep. |
| 83 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 84 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 85 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 86 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 87 } |
| 88 |
| 89 TEST(ImageSkiaTest, DynamicSource) { |
| 90 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
| 91 EXPECT_EQ(0U, image_skia.image_reps().size()); |
| 92 const ImageSkiaRep& result_100p = |
| 93 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 94 EXPECT_EQ(100, result_100p.GetWidth()); |
| 95 EXPECT_EQ(200, result_100p.GetHeight()); |
| 96 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
| 97 EXPECT_EQ(1U, image_skia.image_reps().size()); |
| 98 |
| 99 const ImageSkiaRep& result_200p = |
| 100 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 101 EXPECT_EQ(100, result_200p.GetWidth()); |
| 102 EXPECT_EQ(200, result_200p.GetHeight()); |
| 103 EXPECT_EQ(200, result_200p.pixel_width()); |
| 104 EXPECT_EQ(400, result_200p.pixel_height()); |
| 105 EXPECT_EQ(ui::SCALE_FACTOR_200P, result_200p.scale_factor()); |
| 106 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 107 |
| 108 // Get the representation again and make sure it doesn't |
| 109 // generate new image skia rep. |
| 110 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 111 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 112 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 113 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 114 } |
| 115 |
| 116 } // namespace gfx |
OLD | NEW |