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, NoSource) { |
| 60 ImageSkia image_skia(NULL, Size(100, 200)); |
| 61 EXPECT_FALSE(image_skia.empty()); |
| 62 EXPECT_FALSE(image_skia.isNull()); // Is this expected? |
| 63 EXPECT_EQ(100, image_skia.width()); |
| 64 EXPECT_EQ(200, image_skia.height()); |
| 65 } |
| 66 |
| 67 TEST(ImageSkiaTest, FixedSource) { |
| 68 ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P)); |
| 69 ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
| 70 EXPECT_EQ(0U, image_skia.image_reps().size()); |
| 71 |
| 72 const ImageSkiaRep& result_100p = |
| 73 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 74 EXPECT_EQ(100, result_100p.GetWidth()); |
| 75 EXPECT_EQ(200, result_100p.GetHeight()); |
| 76 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
| 77 EXPECT_EQ(1U, image_skia.image_reps().size()); |
| 78 |
| 79 const ImageSkiaRep& result_200p = |
| 80 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 81 |
| 82 EXPECT_EQ(100, result_200p.GetWidth()); |
| 83 EXPECT_EQ(200, result_200p.GetHeight()); |
| 84 EXPECT_EQ(100, result_200p.pixel_width()); |
| 85 EXPECT_EQ(200, result_200p.pixel_height()); |
| 86 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_200p.scale_factor()); |
| 87 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 88 |
| 89 // Get the representation again and make sure it doesn't |
| 90 // generate new image skia rep. |
| 91 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 92 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 93 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 94 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 95 } |
| 96 |
| 97 TEST(ImageSkiaTest, DynamicSource) { |
| 98 ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
| 99 EXPECT_EQ(0U, image_skia.image_reps().size()); |
| 100 const ImageSkiaRep& result_100p = |
| 101 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 102 EXPECT_EQ(100, result_100p.GetWidth()); |
| 103 EXPECT_EQ(200, result_100p.GetHeight()); |
| 104 EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
| 105 EXPECT_EQ(1U, image_skia.image_reps().size()); |
| 106 |
| 107 const ImageSkiaRep& result_200p = |
| 108 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 109 EXPECT_EQ(100, result_200p.GetWidth()); |
| 110 EXPECT_EQ(200, result_200p.GetHeight()); |
| 111 EXPECT_EQ(200, result_200p.pixel_width()); |
| 112 EXPECT_EQ(400, result_200p.pixel_height()); |
| 113 EXPECT_EQ(ui::SCALE_FACTOR_200P, result_200p.scale_factor()); |
| 114 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 115 |
| 116 // Get the representation again and make sure it doesn't |
| 117 // generate new image skia rep. |
| 118 image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
| 119 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 120 image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
| 121 EXPECT_EQ(2U, image_skia.image_reps().size()); |
| 122 } |
| 123 |
| 124 } // namespace gfx |
OLD | NEW |