Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(790)

Side by Side Diff: ui/gfx/image/image_unittest.cc

Issue 10963064: Properly flip the CGContext during UIImage->SkBitmap conversions. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Review. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "third_party/skia/include/core/SkCanvas.h" 6 #include "third_party/skia/include/core/SkCanvas.h"
7 #include "third_party/skia/include/core/SkPaint.h" 7 #include "third_party/skia/include/core/SkPaint.h"
8 #include "ui/gfx/image/image.h" 8 #include "ui/gfx/image/image.h"
9 #include "ui/gfx/image/image_skia.h" 9 #include "ui/gfx/image/image_skia.h"
10 #include "ui/gfx/image/image_unittest_util.h" 10 #include "ui/gfx/image/image_unittest_util.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 169
170 TEST_F(ImageTest, PNGDecodeToSkiaFailure) { 170 TEST_F(ImageTest, PNGDecodeToSkiaFailure) {
171 std::vector<unsigned char> png(100, 0); 171 std::vector<unsigned char> png(100, 0);
172 gfx::Image image(&png.front(), png.size()); 172 gfx::Image image(&png.front(), png.size());
173 const SkBitmap* bitmap = image.ToSkBitmap(); 173 const SkBitmap* bitmap = image.ToSkBitmap();
174 174
175 SkAutoLockPixels auto_lock(*bitmap); 175 SkAutoLockPixels auto_lock(*bitmap);
176 gt::CheckColor(bitmap->getColor(10, 10), true); 176 gt::CheckColor(bitmap->getColor(10, 10), true);
177 } 177 }
178 178
179 // TODO(rohitrao): This test needs an iOS implementation of
180 // GetPlatformImageColor().
181 #if !defined(OS_IOS)
182 TEST_F(ImageTest, PNGDecodeToPlatformFailure) { 179 TEST_F(ImageTest, PNGDecodeToPlatformFailure) {
183 std::vector<unsigned char> png(100, 0); 180 std::vector<unsigned char> png(100, 0);
184 gfx::Image image(&png.front(), png.size()); 181 gfx::Image image(&png.front(), png.size());
185 gt::CheckColor(gt::GetPlatformImageColor(gt::ToPlatformType(image), 10, 10), 182 gt::CheckColor(gt::GetPlatformImageColor(gt::ToPlatformType(image), 10, 10),
186 true); 183 true);
187 } 184 }
188 #endif
189 185
190 TEST_F(ImageTest, SkiaToPlatform) { 186 TEST_F(ImageTest, SkiaToPlatform) {
191 gfx::Image image(gt::CreateBitmap(25, 25)); 187 gfx::Image image(gt::CreateBitmap(25, 25));
192 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 188 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
193 189
194 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia)); 190 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepSkia));
195 if (!kUsesSkiaNatively) 191 if (!kUsesSkiaNatively)
196 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); 192 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType()));
197 193
198 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image))); 194 EXPECT_TRUE(gt::IsPlatformImageValid(gt::ToPlatformType(image)));
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 #endif 297 #endif
302 298
303 TEST_F(ImageTest, CheckSkiaColor) { 299 TEST_F(ImageTest, CheckSkiaColor) {
304 gfx::Image image(gt::CreatePlatformImage()); 300 gfx::Image image(gt::CreatePlatformImage());
305 301
306 const SkBitmap* bitmap = image.ToSkBitmap(); 302 const SkBitmap* bitmap = image.ToSkBitmap();
307 SkAutoLockPixels auto_lock(*bitmap); 303 SkAutoLockPixels auto_lock(*bitmap);
308 gt::CheckColor(bitmap->getColor(10, 10), false); 304 gt::CheckColor(bitmap->getColor(10, 10), false);
309 } 305 }
310 306
311 // TODO(rohitrao): This test needs an iOS implementation of
312 // GetPlatformImageColor().
313 #if !defined(OS_IOS)
314 TEST_F(ImageTest, SkBitmapConversionPreservesOrientation) { 307 TEST_F(ImageTest, SkBitmapConversionPreservesOrientation) {
315 const int width = 50; 308 const int width = 50;
316 const int height = 50; 309 const int height = 50;
317 SkBitmap bitmap; 310 SkBitmap bitmap;
318 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); 311 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
319 bitmap.allocPixels(); 312 bitmap.allocPixels();
320 bitmap.eraseRGB(0, 255, 0); 313 bitmap.eraseRGB(0, 255, 0);
321 314
322 // Paint the upper half of the image in red (lower half is in green). 315 // Paint the upper half of the image in red (lower half is in green).
323 SkCanvas canvas(bitmap); 316 SkCanvas canvas(bitmap);
324 SkPaint red; 317 SkPaint red;
325 red.setColor(SK_ColorRED); 318 red.setColor(SK_ColorRED);
326 canvas.drawRect(SkRect::MakeWH(width, height / 2), red); 319 canvas.drawRect(SkRect::MakeWH(width, height / 2), red);
327 gt::CheckColor(bitmap.getColor(10, 10), true); 320 {
328 gt::CheckColor(bitmap.getColor(10, 40), false); 321 SCOPED_TRACE("Checking color of the initial SkBitmap");
322 gt::CheckColor(bitmap.getColor(10, 10), true);
323 gt::CheckColor(bitmap.getColor(10, 40), false);
324 }
329 325
330 // Convert from SkBitmap to a platform representation, then check the upper 326 // Convert from SkBitmap to a platform representation, then check the upper
331 // half of the platform image to make sure it is red, not green. 327 // half of the platform image to make sure it is red, not green.
332 gfx::Image from_skbitmap(bitmap); 328 gfx::Image from_skbitmap(bitmap);
333 gt::CheckColor( 329 {
334 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10), 330 SCOPED_TRACE("Checking color of the platform image");
335 true); 331 gt::CheckColor(
336 gt::CheckColor( 332 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 10),
337 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40), 333 true);
338 false); 334 gt::CheckColor(
335 gt::GetPlatformImageColor(gt::ToPlatformType(from_skbitmap), 10, 40),
336 false);
337 }
339 338
340 // Force a conversion back to SkBitmap and check that the upper half is red. 339 // Force a conversion back to SkBitmap and check that the upper half is red.
341 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap)); 340 gfx::Image from_platform(gt::CopyPlatformType(from_skbitmap));
342 const SkBitmap* bitmap2 = from_platform.ToSkBitmap(); 341 const SkBitmap* bitmap2 = from_platform.ToSkBitmap();
343 SkAutoLockPixels auto_lock(*bitmap2); 342 SkAutoLockPixels auto_lock(*bitmap2);
344 gt::CheckColor(bitmap2->getColor(10, 10), true); 343 {
345 gt::CheckColor(bitmap2->getColor(10, 40), false); 344 SCOPED_TRACE("Checking color after conversion back to SkBitmap");
345 gt::CheckColor(bitmap2->getColor(10, 10), true);
346 gt::CheckColor(bitmap2->getColor(10, 40), false);
347 }
346 } 348 }
347 #endif // !defined(OS_IOS)
348 349
349 TEST_F(ImageTest, SwapRepresentations) { 350 TEST_F(ImageTest, SwapRepresentations) {
350 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; 351 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
351 352
352 gfx::Image image1(gt::CreateBitmap(25, 25)); 353 gfx::Image image1(gt::CreateBitmap(25, 25));
353 const SkBitmap* bitmap1 = image1.ToSkBitmap(); 354 const SkBitmap* bitmap1 = image1.ToSkBitmap();
354 EXPECT_EQ(1U, image1.RepresentationCount()); 355 EXPECT_EQ(1U, image1.RepresentationCount());
355 356
356 gfx::Image image2(gt::CreatePlatformImage()); 357 gfx::Image image2(gt::CreatePlatformImage());
357 const SkBitmap* bitmap2 = image2.ToSkBitmap(); 358 const SkBitmap* bitmap2 = image2.ToSkBitmap();
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 image = gfx::Image(gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P)); 459 image = gfx::Image(gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P));
459 } 460 }
460 EXPECT_TRUE(!image.ToSkBitmap()->isNull()); 461 EXPECT_TRUE(!image.ToSkBitmap()->isNull());
461 } 462 }
462 463
463 // Integration tests with UI toolkit frameworks require linking against the 464 // Integration tests with UI toolkit frameworks require linking against the
464 // Views library and cannot be here (ui_unittests doesn't include it). They 465 // Views library and cannot be here (ui_unittests doesn't include it). They
465 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. 466 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.
466 467
467 } // namespace 468 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698