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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c09b1cf7a198b0a852be8f925b47260918fabd07 |
--- /dev/null |
+++ b/ui/gfx/image/image_skia_unittest.cc |
@@ -0,0 +1,123 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/image/image_skia.h" |
+ |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/image/image_skia_rep.h" |
+#include "ui/gfx/image/image_skia_source.h" |
+#include "ui/gfx/size.h" |
+#include "ui/base/layout.h" |
+ |
+namespace gfx { |
+ |
+namespace { |
+ |
+ImageSkiaRep CreateImage(const gfx::Size& size, ui::ScaleFactor scale_factor) { |
+ SkBitmap bitmap; |
+ gfx::Size pixel_size = size.Scale(ui::GetScaleFactorScale(scale_factor)); |
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
+ pixel_size.width(), pixel_size.height()); |
+ bitmap.allocPixels(); |
+ return ImageSkiaRep(bitmap, scale_factor); |
+} |
+ |
+class FixedSource : public ImageSkiaSource { |
+ public: |
+ FixedSource(const ImageSkiaRep& image) : image_(image) {} |
+ |
+ virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
+ return image_; |
+ } |
+ |
+ private: |
+ ImageSkiaRep image_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(FixedSource); |
+}; |
+ |
+class DynamicSource : public ImageSkiaSource { |
+ public: |
+ DynamicSource(const gfx::Size& size) : size_(size) {} |
+ |
+ virtual ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) OVERRIDE { |
+ return CreateImage(size_, scale_factor); |
+ } |
+ |
+ private: |
+ gfx::Size size_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DynamicSource); |
+}; |
+ |
+} // namespace; |
+ |
+typedef testing::Test ImageSkiaTest; |
+ |
+TEST(ImageSkiaTest, NoSource) { |
+ ImageSkia image_skia(NULL, Size(100, 200)); |
+ EXPECT_FALSE(image_skia.empty()); |
+ EXPECT_FALSE(image_skia.isNull()); // Is this expected? |
oshima
2012/07/01 19:01:57
peter, is this correct?
pkotwicz
2012/07/01 22:22:23
This is correct. When an ImageSkia has an ImageSki
oshima
2012/07/02 16:43:34
Actually, the soruce should never be NULL (there i
pkotwicz
2012/07/02 17:13:53
You are correct that the source should never be NU
oshima
2012/07/02 17:26:47
Do you have real example you want to do this? That
oshima
2012/07/02 17:48:43
By the way, I'm thinking of introducing "primary s
pkotwicz
2012/07/02 18:05:12
I don't know of any case where you would want a so
|
+ EXPECT_EQ(100, image_skia.width()); |
+ EXPECT_EQ(200, image_skia.height()); |
+} |
+ |
+TEST(ImageSkiaTest, FixedSource) { |
+ ImageSkiaRep image(CreateImage(Size(100, 200), ui::SCALE_FACTOR_100P)); |
+ ImageSkia image_skia(new FixedSource(image), Size(100, 200)); |
+ EXPECT_EQ(0U, image_skia.image_reps().size()); |
+ |
+ const ImageSkiaRep& result_100p = |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
+ EXPECT_EQ(100, result_100p.GetWidth()); |
+ EXPECT_EQ(200, result_100p.GetHeight()); |
+ EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
+ EXPECT_EQ(1U, image_skia.image_reps().size()); |
+ |
+ const ImageSkiaRep& result_200p = |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
+ |
+ EXPECT_EQ(100, result_200p.GetWidth()); |
+ EXPECT_EQ(200, result_200p.GetHeight()); |
+ EXPECT_EQ(100, result_200p.pixel_width()); |
+ EXPECT_EQ(200, result_200p.pixel_height()); |
+ EXPECT_EQ(ui::SCALE_FACTOR_100P, result_200p.scale_factor()); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+ |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+ |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+} |
+ |
+TEST(ImageSkiaTest, DynamicSource) { |
+ ImageSkia image_skia(new DynamicSource(Size(100, 200)), Size(100, 200)); |
+ EXPECT_EQ(0U, image_skia.image_reps().size()); |
+ const ImageSkiaRep& result_100p = |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
+ EXPECT_EQ(100, result_100p.GetWidth()); |
+ EXPECT_EQ(200, result_100p.GetHeight()); |
+ EXPECT_EQ(ui::SCALE_FACTOR_100P, result_100p.scale_factor()); |
+ EXPECT_EQ(1U, image_skia.image_reps().size()); |
+ |
+ const ImageSkiaRep& result_200p = |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
+ EXPECT_EQ(100, result_200p.GetWidth()); |
+ EXPECT_EQ(200, result_200p.GetHeight()); |
+ EXPECT_EQ(200, result_200p.pixel_width()); |
+ EXPECT_EQ(400, result_200p.pixel_height()); |
+ EXPECT_EQ(ui::SCALE_FACTOR_200P, result_200p.scale_factor()); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+ |
+ // Get the representation again and make sure they |
+ // retuns the sam |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_100P); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+ image_skia.GetRepresentation(ui::SCALE_FACTOR_200P); |
+ EXPECT_EQ(2U, image_skia.image_reps().size()); |
+} |
+ |
+} // namespace gfx |