1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/image.h"
#include "base/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image_unittest.h"
#if defined(OS_LINUX)
#include <gtk/gtk.h>
#include "ui/gfx/gtk_util.h"
#elif defined(OS_MACOSX)
#include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h"
#endif
namespace {
using namespace gfx::test;
#if defined(TOOLKIT_VIEWS)
const bool kUsesSkiaNatively = true;
#else
const bool kUsesSkiaNatively = false;
#endif
class ImageTest : public testing::Test {
public:
size_t GetRepCount(const gfx::Image& image) {
return image.representations_.size();
}
};
TEST_F(ImageTest, SkiaToSkia) {
gfx::Image image(CreateBitmap());
const SkBitmap* bitmap = static_cast<const SkBitmap*>(image);
EXPECT_TRUE(bitmap);
EXPECT_FALSE(bitmap->isNull());
EXPECT_EQ(1U, GetRepCount(image));
// Make sure double conversion doesn't happen.
bitmap = static_cast<const SkBitmap*>(image);
EXPECT_TRUE(bitmap);
EXPECT_FALSE(bitmap->isNull());
EXPECT_EQ(1U, GetRepCount(image));
EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
if (!kUsesSkiaNatively)
EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
}
TEST_F(ImageTest, SkiaToSkiaRef) {
gfx::Image image(CreateBitmap());
const SkBitmap& bitmap = static_cast<const SkBitmap&>(image);
EXPECT_FALSE(bitmap.isNull());
EXPECT_EQ(1U, GetRepCount(image));
const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image);
EXPECT_FALSE(bitmap1->isNull());
EXPECT_EQ(1U, GetRepCount(image));
EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
if (!kUsesSkiaNatively)
EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
}
TEST_F(ImageTest, SkiaToPlatform) {
gfx::Image image(CreateBitmap());
const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
if (!kUsesSkiaNatively)
EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType()));
EXPECT_TRUE(static_cast<PlatformImage>(image));
EXPECT_EQ(kRepCount, GetRepCount(image));
const SkBitmap& bitmap = static_cast<const SkBitmap&>(image);
EXPECT_FALSE(bitmap.isNull());
EXPECT_EQ(kRepCount, GetRepCount(image));
EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
}
TEST_F(ImageTest, PlatformToSkia) {
gfx::Image image(CreatePlatformImage());
const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
if (!kUsesSkiaNatively)
EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
const SkBitmap* bitmap = static_cast<const SkBitmap*>(image);
EXPECT_TRUE(bitmap);
EXPECT_FALSE(bitmap->isNull());
EXPECT_EQ(kRepCount, GetRepCount(image));
EXPECT_TRUE(static_cast<PlatformImage>(image));
EXPECT_EQ(kRepCount, GetRepCount(image));
EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
}
TEST_F(ImageTest, PlatformToPlatform) {
gfx::Image image(CreatePlatformImage());
EXPECT_TRUE(static_cast<PlatformImage>(image));
EXPECT_EQ(1U, GetRepCount(image));
// Make sure double conversion doesn't happen.
EXPECT_TRUE(static_cast<PlatformImage>(image));
EXPECT_EQ(1U, GetRepCount(image));
EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType()));
if (!kUsesSkiaNatively)
EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep));
}
TEST_F(ImageTest, CheckSkiaColor) {
gfx::Image image(CreatePlatformImage());
const SkBitmap& bitmap(image);
SkAutoLockPixels auto_lock(bitmap);
uint32_t* pixel = bitmap.getAddr32(10, 10);
EXPECT_EQ(SK_ColorRED, *pixel);
}
TEST_F(ImageTest, SwapRepresentations) {
const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U;
gfx::Image image1(CreateBitmap());
const SkBitmap* bitmap1 = image1;
EXPECT_EQ(1U, GetRepCount(image1));
gfx::Image image2(CreatePlatformImage());
const SkBitmap* bitmap2 = image2;
PlatformImage platform_image = static_cast<PlatformImage>(image2);
EXPECT_EQ(kRepCount, GetRepCount(image2));
image1.SwapRepresentations(&image2);
EXPECT_EQ(bitmap2, static_cast<const SkBitmap*>(image1));
EXPECT_EQ(platform_image, static_cast<PlatformImage>(image1));
EXPECT_EQ(bitmap1, static_cast<const SkBitmap*>(image2));
EXPECT_EQ(kRepCount, GetRepCount(image1));
EXPECT_EQ(1U, GetRepCount(image2));
}
// Integration tests with UI toolkit frameworks require linking against the
// Views library and cannot be here (gfx_unittests doesn't include it). They
// instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc.
} // namespace
|