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.
4
5#include "ui/gfx/image/image_util.h"
6
7#include <vector>
8
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"
15
16TEST(ImageUtilTest, JPEGEncodeAndDecode) {
17  gfx::Image original = gfx::test::CreateImage(100, 100);
18
19  std::vector<unsigned char> encoded;
20  ASSERT_TRUE(gfx::JPEG1xEncodedDataFromImage(original, 80, &encoded));
21
22  gfx::Image decoded =
23      gfx::ImageFrom1xJPEGEncodedData(&encoded.front(), encoded.size());
24
25  // JPEG is lossy, so simply check that the image decoded successfully.
26  EXPECT_FALSE(decoded.IsEmpty());
27}
28
29TEST(ImageUtilTest, TestVisibleMargins) {
30  // Image with non-transparent piece should return margins at those
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
44  // Full-width-transparent image should return margins in the center
45  // of the image.
46  SkBitmap bitmap2;
47  bitmap2.allocN32Pixels(16, 16);
48  bitmap2.eraseColor(SK_ColorTRANSPARENT);
49  gfx::ImageSkia img_transparent = gfx::ImageSkia::CreateFrom1xBitmap(bitmap2);
50  x = 0;
51  y = 0;
52  gfx::VisibleMargins(img_transparent, &x, &y);
53  EXPECT_EQ(8, x);
54  EXPECT_EQ(9, y);
55  EXPECT_EQ(16, img_transparent.width());
56
57  // Image with non-transparent piece that is skewed to one side should
58  // return margins at those columns.
59  SkBitmap bitmap3;
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);
64  x = 0;
65  y = 0;
66  gfx::VisibleMargins(img3, &x, &y);
67  EXPECT_EQ(3, x);
68  EXPECT_EQ(4, y);
69  EXPECT_EQ(16, img3.width());
70
71  // Image with non-transparent piece that is at one edge should
72  // return margins at those columns.
73  SkBitmap bitmap4;
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);
78  x = 0;
79  y = 0;
80  gfx::VisibleMargins(img4, &x, &y);
81  EXPECT_EQ(0, x);
82  EXPECT_EQ(4, y);
83  EXPECT_EQ(16, img4.width());
84
85  // Image with non-transparent piece that is at trailing edge should
86  // return margins at those columns.
87  SkBitmap bitmap5;
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);
92  x = 0;
93  y = 0;
94  gfx::VisibleMargins(img5, &x, &y);
95  EXPECT_EQ(4, x);
96  EXPECT_EQ(15, y);
97  EXPECT_EQ(16, img5.width());
98}
99