Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / image / image_util_unittest.cc
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
16 TEST(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
29 TEST(ImageUtilTest, TestVisibleMargins) {
30   // Image with non-transparent piece should return margins at those
31   // columns.
32   SkBitmap bitmap1;
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);
38   int x = 0;
39   int y = 0;
40   gfx::VisibleMargins(img, &x, &y);
41   EXPECT_EQ(3, x);
42   EXPECT_EQ(13, y);
43   EXPECT_EQ(16, img.width());
44
45   // Full-width-transparent image should return margins in the center
46   // of the image.
47   SkBitmap bitmap2;
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);
52   x = 0;
53   y = 0;
54   gfx::VisibleMargins(img_transparent, &x, &y);
55   EXPECT_EQ(8, x);
56   EXPECT_EQ(9, y);
57   EXPECT_EQ(16, img_transparent.width());
58
59   // Image with non-transparent piece that is skewed to one side should
60   // return margins at those columns.
61   SkBitmap bitmap3;
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);
67   x = 0;
68   y = 0;
69   gfx::VisibleMargins(img3, &x, &y);
70   EXPECT_EQ(3, x);
71   EXPECT_EQ(4, y);
72   EXPECT_EQ(16, img3.width());
73
74   // Image with non-transparent piece that is at one edge should
75   // return margins at those columns.
76   SkBitmap bitmap4;
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);
82   x = 0;
83   y = 0;
84   gfx::VisibleMargins(img4, &x, &y);
85   EXPECT_EQ(0, x);
86   EXPECT_EQ(4, y);
87   EXPECT_EQ(16, img4.width());
88
89   // Image with non-transparent piece that is at trailing edge should
90   // return margins at those columns.
91   SkBitmap bitmap5;
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);
97   x = 0;
98   y = 0;
99   gfx::VisibleMargins(img5, &x, &y);
100   EXPECT_EQ(4, x);
101   EXPECT_EQ(15, y);
102   EXPECT_EQ(16, img5.width());
103 }