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.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
34 bitmap1.allocPixels();
35 bitmap1.eraseColor(SK_ColorTRANSPARENT);
36 bitmap1.eraseArea(SkIRect::MakeLTRB(3, 3, 14, 14), SK_ColorYELLOW);
37 gfx::ImageSkia img = gfx::ImageSkia::CreateFrom1xBitmap(bitmap1);
40 gfx::VisibleMargins(img, &x, &y);
43 EXPECT_EQ(16, img.width());
45 // Full-width-transparent image should return margins in the center
48 bitmap2.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
49 bitmap2.allocPixels();
50 bitmap2.eraseColor(SK_ColorTRANSPARENT);
51 gfx::ImageSkia img_transparent = gfx::ImageSkia::CreateFrom1xBitmap(bitmap2);
54 gfx::VisibleMargins(img_transparent, &x, &y);
57 EXPECT_EQ(16, img_transparent.width());
59 // Image with non-transparent piece that is skewed to one side should
60 // return margins at those columns.
62 bitmap3.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
63 bitmap3.allocPixels();
64 bitmap3.eraseColor(SK_ColorTRANSPARENT);
65 bitmap3.eraseArea(SkIRect::MakeLTRB(3, 3, 5, 5), SK_ColorYELLOW);
66 gfx::ImageSkia img3 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap3);
69 gfx::VisibleMargins(img3, &x, &y);
72 EXPECT_EQ(16, img3.width());
74 // Image with non-transparent piece that is at one edge should
75 // return margins at those columns.
77 bitmap4.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
78 bitmap4.allocPixels();
79 bitmap4.eraseColor(SK_ColorTRANSPARENT);
80 bitmap4.eraseArea(SkIRect::MakeLTRB(0, 3, 5, 5), SK_ColorYELLOW);
81 gfx::ImageSkia img4 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap4);
84 gfx::VisibleMargins(img4, &x, &y);
87 EXPECT_EQ(16, img4.width());
89 // Image with non-transparent piece that is at trailing edge should
90 // return margins at those columns.
92 bitmap5.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
93 bitmap5.allocPixels();
94 bitmap5.eraseColor(SK_ColorTRANSPARENT);
95 bitmap5.eraseArea(SkIRect::MakeLTRB(4, 3, 16, 16), SK_ColorYELLOW);
96 gfx::ImageSkia img5 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap5);
99 gfx::VisibleMargins(img5, &x, &y);
102 EXPECT_EQ(16, img5.width());