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.
5 #include "ui/gfx/image/image_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkRect.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/image/image_unittest_util.h"
16 TEST(ImageUtilTest
, JPEGEncodeAndDecode
) {
17 gfx::Image original
= gfx::test::CreateImage(100, 100);
19 std::vector
<unsigned char> encoded
;
20 ASSERT_TRUE(gfx::JPEG1xEncodedDataFromImage(original
, 80, &encoded
));
23 gfx::ImageFrom1xJPEGEncodedData(&encoded
.front(), encoded
.size());
25 // JPEG is lossy, so simply check that the image decoded successfully.
26 EXPECT_FALSE(decoded
.IsEmpty());
29 TEST(ImageUtilTest
, TestVisibleMargins
) {
30 // Image with non-transparent piece should return margins at those
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
);
39 gfx::VisibleMargins(img
, &x
, &y
);
42 EXPECT_EQ(16, img
.width());
44 // Full-width-transparent image should return margins in the center
47 bitmap2
.allocN32Pixels(16, 16);
48 bitmap2
.eraseColor(SK_ColorTRANSPARENT
);
49 gfx::ImageSkia img_transparent
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap2
);
52 gfx::VisibleMargins(img_transparent
, &x
, &y
);
55 EXPECT_EQ(16, img_transparent
.width());
57 // Image with non-transparent piece that is skewed to one side should
58 // return margins at those columns.
60 bitmap3
.allocN32Pixels(16, 16);
61 bitmap3
.eraseColor(SK_ColorTRANSPARENT
);
62 bitmap3
.eraseArea(SkIRect::MakeLTRB(3, 3, 5, 5), SK_ColorYELLOW
);
63 gfx::ImageSkia img3
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap3
);
66 gfx::VisibleMargins(img3
, &x
, &y
);
69 EXPECT_EQ(16, img3
.width());
71 // Image with non-transparent piece that is at one edge should
72 // return margins at those columns.
74 bitmap4
.allocN32Pixels(16, 16);
75 bitmap4
.eraseColor(SK_ColorTRANSPARENT
);
76 bitmap4
.eraseArea(SkIRect::MakeLTRB(0, 3, 5, 5), SK_ColorYELLOW
);
77 gfx::ImageSkia img4
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap4
);
80 gfx::VisibleMargins(img4
, &x
, &y
);
83 EXPECT_EQ(16, img4
.width());
85 // Image with non-transparent piece that is at trailing edge should
86 // return margins at those columns.
88 bitmap5
.allocN32Pixels(16, 16);
89 bitmap5
.eraseColor(SK_ColorTRANSPARENT
);
90 bitmap5
.eraseArea(SkIRect::MakeLTRB(4, 3, 16, 16), SK_ColorYELLOW
);
91 gfx::ImageSkia img5
= gfx::ImageSkia::CreateFrom1xBitmap(bitmap5
);
94 gfx::VisibleMargins(img5
, &x
, &y
);
97 EXPECT_EQ(16, img5
.width());