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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "ui/gfx/image/image.h" | 6 #include "ui/gfx/image/image.h" |
7 #include "ui/gfx/image/image_skia.h" | 7 #include "ui/gfx/image/image_skia.h" |
8 #include "ui/gfx/image/image_unittest_util.h" | 8 #include "ui/gfx/image/image_unittest_util.h" |
9 | 9 |
10 #if defined(TOOLKIT_GTK) | 10 #if defined(TOOLKIT_GTK) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 const SkBitmap* bitmap1 = image.ToSkBitmap(); | 87 const SkBitmap* bitmap1 = image.ToSkBitmap(); |
88 EXPECT_FALSE(bitmap1->isNull()); | 88 EXPECT_FALSE(bitmap1->isNull()); |
89 EXPECT_EQ(1U, image.RepresentationCount()); | 89 EXPECT_EQ(1U, image.RepresentationCount()); |
90 | 90 |
91 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); | 91 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); |
92 if (!kUsesSkiaNatively) | 92 if (!kUsesSkiaNatively) |
93 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); | 93 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); |
94 } | 94 } |
95 | 95 |
| 96 TEST_F(ImageTest, SkiaToPNGEncodeAndDecode) { |
| 97 gfx::Image image(gt::CreateBitmap(25, 25)); |
| 98 const std::vector<unsigned char>* png = image.ToImagePNG(); |
| 99 EXPECT_TRUE(png); |
| 100 EXPECT_FALSE(png->empty()); |
| 101 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
| 102 |
| 103 gfx::Image from_png(&png->front(), png->size()); |
| 104 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
| 105 EXPECT_TRUE(gt::IsEqual(from_png, image)); |
| 106 } |
| 107 |
| 108 TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) { |
| 109 gfx::Image image(gt::CreatePlatformImage()); |
| 110 const std::vector<unsigned char>* png = image.ToImagePNG(); |
| 111 EXPECT_TRUE(png); |
| 112 EXPECT_FALSE(png->empty()); |
| 113 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
| 114 |
| 115 gfx::Image from_png(&png->front(), png->size()); |
| 116 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); |
| 117 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); |
| 118 } |
| 119 |
| 120 // The platform types use the platform provided encoding/decoding of PNGs. Make |
| 121 // sure these work with the Skia Encode/Decode. |
| 122 TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { |
| 123 // Force the conversion sequence skia to png to platform_type. |
| 124 gfx::Image from_skia(gt::CreateBitmap(25, 25)); |
| 125 const std::vector<unsigned char>* png = from_skia.ToImagePNG(); |
| 126 gfx::Image from_png(&png->front(), png->size()); |
| 127 gfx::Image from_platform(gt::CopyPlatformType(from_png)); |
| 128 |
| 129 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(from_platform))); |
| 130 EXPECT_TRUE(gt::IsEqual(from_skia, from_platform)); |
| 131 } |
| 132 |
| 133 TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) { |
| 134 // Force the conversion sequence platform_type to png to skia. |
| 135 gfx::Image from_platform(gt::CreatePlatformImage()); |
| 136 const std::vector<unsigned char>* png = from_platform.ToImagePNG(); |
| 137 gfx::Image from_png(&png->front(), png->size()); |
| 138 gfx::Image from_skia(*from_png.ToImageSkia()); |
| 139 |
| 140 EXPECT_TRUE(gt::IsEqual(from_skia, from_platform)); |
| 141 } |
| 142 |
| 143 TEST_F(ImageTest, PNGDecodeToSkiaFailure) { |
| 144 std::vector<unsigned char> png(100, 0); |
| 145 gfx::Image image(&png.front(), png.size()); |
| 146 const SkBitmap* bitmap = image.ToSkBitmap(); |
| 147 |
| 148 SkAutoLockPixels auto_lock(*bitmap); |
| 149 gt::CheckColor(bitmap->getColor(10, 10), true); |
| 150 } |
| 151 |
| 152 TEST_F(ImageTest, PNGDecodeToPlatformFailure) { |
| 153 std::vector<unsigned char> png(100, 0); |
| 154 gfx::Image image(&png.front(), png.size()); |
| 155 gt::CheckColor(gt::GetPlatformImageColor(gt::ToPlatformType(image)), true); |
| 156 } |
| 157 |
96 TEST_F(ImageTest, SkiaToPlatform) { | 158 TEST_F(ImageTest, SkiaToPlatform) { |
97 gfx::Image image(gt::CreateBitmap(25, 25)); | 159 gfx::Image image(gt::CreateBitmap(25, 25)); |
98 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; | 160 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
99 | 161 |
100 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); | 162 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); |
101 if (!kUsesSkiaNatively) | 163 if (!kUsesSkiaNatively) |
102 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); | 164 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); |
103 | 165 |
104 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); | 166 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); |
105 EXPECT_EQ(kRepCount, image.RepresentationCount()); | 167 EXPECT_EQ(kRepCount, image.RepresentationCount()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 ns_image = image.CopyNSImage(); | 251 ns_image = image.CopyNSImage(); |
190 } | 252 } |
191 | 253 |
192 EXPECT_TRUE(ns_image); | 254 EXPECT_TRUE(ns_image); |
193 base::mac::NSObjectRelease(ns_image); | 255 base::mac::NSObjectRelease(ns_image); |
194 } | 256 } |
195 #endif | 257 #endif |
196 | 258 |
197 TEST_F(ImageTest, CheckSkiaColor) { | 259 TEST_F(ImageTest, CheckSkiaColor) { |
198 gfx::Image image(gt::CreatePlatformImage()); | 260 gfx::Image image(gt::CreatePlatformImage()); |
| 261 |
199 const SkBitmap* bitmap = image.ToSkBitmap(); | 262 const SkBitmap* bitmap = image.ToSkBitmap(); |
200 | |
201 SkAutoLockPixels auto_lock(*bitmap); | 263 SkAutoLockPixels auto_lock(*bitmap); |
202 uint32_t* pixel = bitmap->getAddr32(10, 10); | 264 gt::CheckColor(bitmap->getColor(10, 10), false); |
203 EXPECT_EQ(SK_ColorRED, *pixel); | |
204 } | 265 } |
205 | 266 |
206 TEST_F(ImageTest, SwapRepresentations) { | 267 TEST_F(ImageTest, SwapRepresentations) { |
207 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; | 268 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
208 | 269 |
209 gfx::Image image1(gt::CreateBitmap(25, 25)); | 270 gfx::Image image1(gt::CreateBitmap(25, 25)); |
210 const SkBitmap* bitmap1 = image1.ToSkBitmap(); | 271 const SkBitmap* bitmap1 = image1.ToSkBitmap(); |
211 EXPECT_EQ(1U, image1.RepresentationCount()); | 272 EXPECT_EQ(1U, image1.RepresentationCount()); |
212 | 273 |
213 gfx::Image image2(gt::CreatePlatformImage()); | 274 gfx::Image image2(gt::CreatePlatformImage()); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 image = gfx::Image(gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P)); | 376 image = gfx::Image(gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P)); |
316 } | 377 } |
317 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); | 378 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); |
318 } | 379 } |
319 | 380 |
320 // Integration tests with UI toolkit frameworks require linking against the | 381 // Integration tests with UI toolkit frameworks require linking against the |
321 // Views library and cannot be here (ui_unittests doesn't include it). They | 382 // Views library and cannot be here (ui_unittests doesn't include it). They |
322 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. | 383 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. |
323 | 384 |
324 } // namespace | 385 } // namespace |
OLD | NEW |