blob: 37bfa98a018245083f5ff2839f3525c7735183f6 [file] [log] [blame]
[email protected]40a080e2011-02-11 17:42:281// Copyright (c) 2011 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.h"
6
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/scoped_ptr.h"
[email protected]40a080e2011-02-11 17:42:288#include "testing/gtest/include/gtest/gtest.h"
9#include "third_party/skia/include/core/SkBitmap.h"
10#include "ui/gfx/image_unittest.h"
11
12#if defined(OS_LINUX)
13#include <gtk/gtk.h>
[email protected]00ed9452011-02-13 00:13:4414#include "ui/gfx/gtk_util.h"
[email protected]40a080e2011-02-11 17:42:2815#elif defined(OS_MACOSX)
16#include "base/mac/mac_util.h"
17#include "skia/ext/skia_utils_mac.h"
18#endif
19
20namespace {
21
[email protected]06ec9a22011-02-17 17:24:0722using namespace gfx::test;
[email protected]40a080e2011-02-11 17:42:2823
24#if defined(TOOLKIT_VIEWS)
25const bool kUsesSkiaNatively = true;
26#else
27const bool kUsesSkiaNatively = false;
28#endif
29
30class ImageTest : public testing::Test {
31 public:
[email protected]06ec9a22011-02-17 17:24:0732 size_t GetRepCount(const gfx::Image& image) {
[email protected]40a080e2011-02-11 17:42:2833 return image.representations_.size();
34 }
35};
36
37TEST_F(ImageTest, SkiaToSkia) {
[email protected]06ec9a22011-02-17 17:24:0738 gfx::Image image(CreateBitmap());
[email protected]40a080e2011-02-11 17:42:2839 const SkBitmap* bitmap = static_cast<const SkBitmap*>(image);
40 EXPECT_TRUE(bitmap);
41 EXPECT_FALSE(bitmap->isNull());
42 EXPECT_EQ(1U, GetRepCount(image));
43
44 // Make sure double conversion doesn't happen.
45 bitmap = static_cast<const SkBitmap*>(image);
46 EXPECT_TRUE(bitmap);
47 EXPECT_FALSE(bitmap->isNull());
48 EXPECT_EQ(1U, GetRepCount(image));
[email protected]c41b06f2011-02-24 15:53:2649
50 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
51 if (!kUsesSkiaNatively)
52 EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
[email protected]40a080e2011-02-11 17:42:2853}
54
55TEST_F(ImageTest, SkiaToSkiaRef) {
[email protected]06ec9a22011-02-17 17:24:0756 gfx::Image image(CreateBitmap());
[email protected]40a080e2011-02-11 17:42:2857
58 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image);
59 EXPECT_FALSE(bitmap.isNull());
60 EXPECT_EQ(1U, GetRepCount(image));
61
62 const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image);
63 EXPECT_FALSE(bitmap1->isNull());
64 EXPECT_EQ(1U, GetRepCount(image));
[email protected]c41b06f2011-02-24 15:53:2665
66 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
67 if (!kUsesSkiaNatively)
68 EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
[email protected]40a080e2011-02-11 17:42:2869}
70
71TEST_F(ImageTest, SkiaToPlatform) {
[email protected]06ec9a22011-02-17 17:24:0772 gfx::Image image(CreateBitmap());
[email protected]40a080e2011-02-11 17:42:2873 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
74
[email protected]c41b06f2011-02-24 15:53:2675 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
76 if (!kUsesSkiaNatively)
77 EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
78
[email protected]40a080e2011-02-11 17:42:2879 EXPECT_TRUE(static_cast<PlatformImage>(image));
80 EXPECT_EQ(kRepCount, GetRepCount(image));
81
82 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image);
83 EXPECT_FALSE(bitmap.isNull());
84 EXPECT_EQ(kRepCount, GetRepCount(image));
[email protected]c41b06f2011-02-24 15:53:2685
86 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
87 EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
[email protected]40a080e2011-02-11 17:42:2888}
89
90TEST_F(ImageTest, PlatformToSkia) {
[email protected]06ec9a22011-02-17 17:24:0791 gfx::Image image(CreatePlatformImage());
[email protected]40a080e2011-02-11 17:42:2892 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
93
[email protected]c41b06f2011-02-24 15:53:2694 EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
95 if (!kUsesSkiaNatively)
96 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
97
[email protected]40a080e2011-02-11 17:42:2898 const SkBitmap* bitmap = static_cast<const SkBitmap*>(image);
99 EXPECT_TRUE(bitmap);
100 EXPECT_FALSE(bitmap->isNull());
101 EXPECT_EQ(kRepCount, GetRepCount(image));
102
103 EXPECT_TRUE(static_cast<PlatformImage>(image));
104 EXPECT_EQ(kRepCount, GetRepCount(image));
[email protected]c41b06f2011-02-24 15:53:26105
106 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
[email protected]40a080e2011-02-11 17:42:28107}
108
109TEST_F(ImageTest, PlatformToPlatform) {
[email protected]06ec9a22011-02-17 17:24:07110 gfx::Image image(CreatePlatformImage());
[email protected]40a080e2011-02-11 17:42:28111 EXPECT_TRUE(static_cast<PlatformImage>(image));
112 EXPECT_EQ(1U, GetRepCount(image));
113
114 // Make sure double conversion doesn't happen.
115 EXPECT_TRUE(static_cast<PlatformImage>(image));
116 EXPECT_EQ(1U, GetRepCount(image));
[email protected]c41b06f2011-02-24 15:53:26117
118 EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
119 if (!kUsesSkiaNatively)
120 EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
[email protected]40a080e2011-02-11 17:42:28121}
122
123TEST_F(ImageTest, CheckSkiaColor) {
[email protected]06ec9a22011-02-17 17:24:07124 gfx::Image image(CreatePlatformImage());
[email protected]40a080e2011-02-11 17:42:28125 const SkBitmap& bitmap(image);
[email protected]5d29a5ce2011-02-11 18:37:00126
127 SkAutoLockPixels auto_lock(bitmap);
[email protected]40a080e2011-02-11 17:42:28128 uint32_t* pixel = bitmap.getAddr32(10, 10);
129 EXPECT_EQ(SK_ColorRED, *pixel);
130}
131
[email protected]66171ab2011-03-03 15:50:07132TEST_F(ImageTest, SwapRepresentations) {
133 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
134
135 gfx::Image image1(CreateBitmap());
136 const SkBitmap* bitmap1 = image1;
137 EXPECT_EQ(1U, GetRepCount(image1));
138
139 gfx::Image image2(CreatePlatformImage());
140 const SkBitmap* bitmap2 = image2;
141 PlatformImage platform_image = static_cast<PlatformImage>(image2);
142 EXPECT_EQ(kRepCount, GetRepCount(image2));
143
144 image1.SwapRepresentations(&image2);
145
146 EXPECT_EQ(bitmap2, static_cast<const SkBitmap*>(image1));
147 EXPECT_EQ(platform_image, static_cast<PlatformImage>(image1));
148 EXPECT_EQ(bitmap1, static_cast<const SkBitmap*>(image2));
149 EXPECT_EQ(kRepCount, GetRepCount(image1));
150 EXPECT_EQ(1U, GetRepCount(image2));
151}
152
[email protected]40a080e2011-02-11 17:42:28153// Integration tests with UI toolkit frameworks require linking against the
154// Views library and cannot be here (gfx_unittests doesn't include it). They
155// instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.
156
157} // namespace