Chromium Code Reviews| 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 "ui/gfx/image/image_util.h" | 5 #include "ui/gfx/image/image_util.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "third_party/skia/include/core/SkRect.h" | 12 #include "third_party/skia/include/core/SkRect.h" |
| 13 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
| 14 #include "ui/gfx/image/image_unittest_util.h" | 14 #include "ui/gfx/image/image_unittest_util.h" |
| 15 | 15 |
| 16 TEST(ImageUtilTest, JPEGEncodeAndDecode) { | 16 TEST(ImageUtilTest, JPEGEncodeAndDecode) { |
| 17 gfx::Image original = gfx::test::CreateImage(100, 100); | 17 gfx::Image original = gfx::test::CreateImage(100, 100); |
| 18 | 18 |
| 19 std::vector<unsigned char> encoded; | 19 std::vector<unsigned char> encoded; |
| 20 ASSERT_TRUE(gfx::JPEG1xEncodedDataFromImage(original, 80, &encoded)); | 20 ASSERT_TRUE(gfx::JPEG1xEncodedDataFromImage(original, 80, &encoded)); |
| 21 | 21 |
| 22 gfx::Image decoded = | 22 gfx::Image decoded = |
| 23 gfx::ImageFrom1xJPEGEncodedData(&encoded.front(), encoded.size()); | 23 gfx::ImageFrom1xJPEGEncodedData(&encoded.front(), encoded.size()); |
| 24 | 24 |
| 25 // JPEG is lossy, so simply check that the image decoded successfully. | 25 // JPEG is lossy, so simply check that the image decoded successfully. |
| 26 EXPECT_FALSE(decoded.IsEmpty()); | 26 EXPECT_FALSE(decoded.IsEmpty()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 TEST(ImageUtilTest, TestVisibleMargins) { | 29 TEST(ImageUtilTest, GetVisibleMargins) { |
| 30 // Image with non-transparent piece should return margins at those | 30 int left, right; |
| 31 // columns. | |
| 32 SkBitmap bitmap1; | |
| 33 bitmap1.allocN32Pixels(16, 16); | |
| 34 bitmap1.eraseColor(SK_ColorTRANSPARENT); | |
| 35 bitmap1.eraseArea(SkIRect::MakeLTRB(3, 3, 14, 14), SK_ColorYELLOW); | |
| 36 gfx::ImageSkia img = gfx::ImageSkia::CreateFrom1xBitmap(bitmap1); | |
| 37 int x = 0; | |
| 38 int y = 0; | |
| 39 gfx::VisibleMargins(img, &x, &y); | |
| 40 EXPECT_EQ(3, x); | |
| 41 EXPECT_EQ(13, y); | |
| 42 EXPECT_EQ(16, img.width()); | |
| 43 | 31 |
| 44 // Full-width-transparent image should return margins in the center | 32 // Fully transparent image. |
| 45 // of the image. | 33 { |
| 46 SkBitmap bitmap2; | 34 SkBitmap bitmap; |
| 47 bitmap2.allocN32Pixels(16, 16); | 35 bitmap.allocN32Pixels(16, 16); |
|
danakj
2016/03/25 23:35:28
less symmetry would be nice, what if the code conf
Peter Kasting
2016/03/26 01:01:57
Sure, I can go through these and ensure that heigh
| |
| 48 bitmap2.eraseColor(SK_ColorTRANSPARENT); | 36 bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 49 gfx::ImageSkia img_transparent = gfx::ImageSkia::CreateFrom1xBitmap(bitmap2); | 37 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
| 50 x = 0; | 38 gfx::GetVisibleMargins(img, &left, &right); |
| 51 y = 0; | 39 EXPECT_EQ(8, left); |
| 52 gfx::VisibleMargins(img_transparent, &x, &y); | 40 EXPECT_EQ(8, right); |
| 53 EXPECT_EQ(8, x); | 41 } |
| 54 EXPECT_EQ(9, y); | |
| 55 EXPECT_EQ(16, img_transparent.width()); | |
| 56 | 42 |
| 57 // Image with non-transparent piece that is skewed to one side should | 43 // Fully non-transparent image. |
| 58 // return margins at those columns. | 44 { |
| 59 SkBitmap bitmap3; | 45 SkBitmap bitmap; |
| 60 bitmap3.allocN32Pixels(16, 16); | 46 bitmap.allocN32Pixels(16, 16); |
| 61 bitmap3.eraseColor(SK_ColorTRANSPARENT); | 47 bitmap.eraseColor(SK_ColorYELLOW); |
| 62 bitmap3.eraseArea(SkIRect::MakeLTRB(3, 3, 5, 5), SK_ColorYELLOW); | 48 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
| 63 gfx::ImageSkia img3 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap3); | 49 gfx::GetVisibleMargins(img, &left, &right); |
| 64 x = 0; | 50 EXPECT_EQ(0, left); |
| 65 y = 0; | 51 EXPECT_EQ(0, right); |
| 66 gfx::VisibleMargins(img3, &x, &y); | 52 } |
| 67 EXPECT_EQ(3, x); | |
| 68 EXPECT_EQ(4, y); | |
| 69 EXPECT_EQ(16, img3.width()); | |
| 70 | 53 |
| 71 // Image with non-transparent piece that is at one edge should | 54 // Image with non-transparent section in center. |
| 72 // return margins at those columns. | 55 { |
| 73 SkBitmap bitmap4; | 56 SkBitmap bitmap; |
| 74 bitmap4.allocN32Pixels(16, 16); | 57 bitmap.allocN32Pixels(16, 16); |
| 75 bitmap4.eraseColor(SK_ColorTRANSPARENT); | 58 bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 76 bitmap4.eraseArea(SkIRect::MakeLTRB(0, 3, 5, 5), SK_ColorYELLOW); | 59 bitmap.eraseArea(SkIRect::MakeLTRB(3, 3, 13, 13), SK_ColorYELLOW); |
| 77 gfx::ImageSkia img4 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap4); | 60 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
| 78 x = 0; | 61 gfx::GetVisibleMargins(img, &left, &right); |
| 79 y = 0; | 62 EXPECT_EQ(3, left); |
| 80 gfx::VisibleMargins(img4, &x, &y); | 63 EXPECT_EQ(3, right); |
| 81 EXPECT_EQ(0, x); | 64 } |
| 82 EXPECT_EQ(4, y); | |
| 83 EXPECT_EQ(16, img4.width()); | |
| 84 | 65 |
| 85 // Image with non-transparent piece that is at trailing edge should | 66 // Image with non-transparent section skewed to one side. |
| 86 // return margins at those columns. | 67 { |
| 87 SkBitmap bitmap5; | 68 SkBitmap bitmap; |
| 88 bitmap5.allocN32Pixels(16, 16); | 69 bitmap.allocN32Pixels(16, 16); |
| 89 bitmap5.eraseColor(SK_ColorTRANSPARENT); | 70 bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 90 bitmap5.eraseArea(SkIRect::MakeLTRB(4, 3, 16, 16), SK_ColorYELLOW); | 71 bitmap.eraseArea(SkIRect::MakeLTRB(3, 3, 5, 5), SK_ColorYELLOW); |
| 91 gfx::ImageSkia img5 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap5); | 72 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); |
| 92 x = 0; | 73 gfx::GetVisibleMargins(img, &left, &right); |
| 93 y = 0; | 74 EXPECT_EQ(3, left); |
| 94 gfx::VisibleMargins(img5, &x, &y); | 75 EXPECT_EQ(11, right); |
| 95 EXPECT_EQ(4, x); | 76 } |
| 96 EXPECT_EQ(15, y); | 77 |
| 97 EXPECT_EQ(16, img5.width()); | 78 // Image with non-transparent section at leading edge. |
| 79 { | |
| 80 SkBitmap bitmap; | |
| 81 bitmap.allocN32Pixels(16, 16); | |
| 82 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
| 83 bitmap.eraseArea(SkIRect::MakeLTRB(0, 3, 5, 5), SK_ColorYELLOW); | |
| 84 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 85 gfx::GetVisibleMargins(img, &left, &right); | |
| 86 EXPECT_EQ(0, left); | |
| 87 EXPECT_EQ(11, right); | |
| 88 } | |
| 89 | |
| 90 // Image with non-transparent section at trailing edge. | |
| 91 { | |
| 92 SkBitmap bitmap; | |
| 93 bitmap.allocN32Pixels(16, 16); | |
| 94 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
| 95 bitmap.eraseArea(SkIRect::MakeLTRB(4, 3, 16, 16), SK_ColorYELLOW); | |
| 96 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 97 gfx::GetVisibleMargins(img, &left, &right); | |
| 98 EXPECT_EQ(4, left); | |
| 99 EXPECT_EQ(0, right); | |
| 100 } | |
| 101 | |
| 102 // Image with narrow non-transparent section. | |
| 103 { | |
| 104 SkBitmap bitmap; | |
| 105 bitmap.allocN32Pixels(16, 16); | |
| 106 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
| 107 bitmap.eraseArea(SkIRect::MakeLTRB(8, 3, 9, 5), SK_ColorYELLOW); | |
| 108 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 109 gfx::GetVisibleMargins(img, &left, &right); | |
| 110 EXPECT_EQ(8, left); | |
| 111 EXPECT_EQ(7, right); | |
| 112 } | |
| 113 | |
| 114 // Image with faint pixels. | |
| 115 { | |
| 116 SkBitmap bitmap; | |
| 117 bitmap.allocN32Pixels(16, 16); | |
| 118 bitmap.eraseColor(SkColorSetA(SK_ColorYELLOW, 0x02)); | |
| 119 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 120 gfx::GetVisibleMargins(img, &left, &right); | |
| 121 EXPECT_EQ(8, left); | |
| 122 EXPECT_EQ(8, right); | |
| 123 } | |
| 124 | |
| 125 // Fully transparent image with odd width. | |
| 126 { | |
| 127 SkBitmap bitmap; | |
| 128 bitmap.allocN32Pixels(17, 16); | |
| 129 bitmap.eraseColor(SK_ColorTRANSPARENT); | |
| 130 gfx::ImageSkia img(gfx::ImageSkia::CreateFrom1xBitmap(bitmap)); | |
| 131 gfx::GetVisibleMargins(img, &left, &right); | |
| 132 EXPECT_EQ(9, left); | |
| 133 EXPECT_EQ(8, right); | |
| 134 } | |
| 98 } | 135 } |
| OLD | NEW |