forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_util_unittest.cc
238 lines (218 loc) · 10.3 KB
/
image_util_unittest.cc
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_split.h"
#include "base/test/metrics/histogram_tester.h"
#include "extensions/common/extension_paths.h"
#include "extensions/common/image_util.h"
#include "extensions/test/logging_timer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_utils.h"
#include "url/gurl.h"
namespace extensions {
TEST(ImageUtilTest, IsIconSufficientlyVisible) {
base::FilePath test_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &test_dir));
base::FilePath icon_path;
const std::string metric_name =
"Extensions.IsRenderedIconSufficientlyVisibleTime";
{
base::HistogramTester histogram_tester;
// This icon has all transparent pixels, so it will fail.
icon_path = test_dir.AppendASCII("transparent_icon.png");
SkBitmap transparent_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &transparent_icon));
EXPECT_FALSE(image_util::IsIconSufficientlyVisible(transparent_icon));
EXPECT_FALSE(image_util::IsRenderedIconSufficientlyVisible(transparent_icon,
SK_ColorWHITE));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with an icon that has one opaque pixel.
icon_path = test_dir.AppendASCII("one_pixel_opaque_icon.png");
SkBitmap visible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &visible_icon));
EXPECT_FALSE(image_util::IsIconSufficientlyVisible(visible_icon));
EXPECT_FALSE(image_util::IsRenderedIconSufficientlyVisible(visible_icon,
SK_ColorWHITE));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with an icon that has one transparent pixel.
icon_path = test_dir.AppendASCII("one_pixel_transparent_icon.png");
SkBitmap visible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &visible_icon));
EXPECT_TRUE(image_util::IsIconSufficientlyVisible(visible_icon));
EXPECT_TRUE(image_util::IsRenderedIconSufficientlyVisible(visible_icon,
SK_ColorWHITE));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with an icon that is completely opaque.
icon_path = test_dir.AppendASCII("opaque_icon.png");
SkBitmap visible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &visible_icon));
EXPECT_TRUE(image_util::IsIconSufficientlyVisible(visible_icon));
EXPECT_TRUE(image_util::IsRenderedIconSufficientlyVisible(visible_icon,
SK_ColorWHITE));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with an icon that is rectangular.
icon_path = test_dir.AppendASCII("rectangle.png");
SkBitmap visible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &visible_icon));
EXPECT_TRUE(image_util::IsIconSufficientlyVisible(visible_icon));
EXPECT_TRUE(image_util::IsRenderedIconSufficientlyVisible(visible_icon,
SK_ColorWHITE));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with a solid color icon that is completely opaque. Use the icon's
// color as the background color in the call to analyze its visibility.
// It should be invisible in this case.
icon_path = test_dir.AppendASCII("grey_21x21.png");
SkBitmap solid_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &solid_icon));
const SkColor pixel_color = solid_icon.getColor(0, 0);
EXPECT_FALSE(
image_util::IsRenderedIconSufficientlyVisible(solid_icon, pixel_color));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
{
base::HistogramTester histogram_tester;
// Test with a two-color icon that is completely opaque. Use one of the
// icon's colors as the background color in the call to analyze its
// visibility. It should be visible in this case.
icon_path = test_dir.AppendASCII("two_color_21x21.png");
SkBitmap two_color_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &two_color_icon));
const SkColor pixel_color = two_color_icon.getColor(0, 0);
EXPECT_TRUE(image_util::IsRenderedIconSufficientlyVisible(two_color_icon,
pixel_color));
histogram_tester.ExpectTotalCount(metric_name, 1);
}
}
TEST(ImageUtilTest, IconTooLargeForAnalysis) {
base::FilePath test_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &test_dir));
// This is a large icon which is entirely black, so it would be
// visible. However, it exceeds the max allowed size for analysis,
// so it will fail.
base::FilePath icon_path = test_dir.AppendASCII("3000x3000.png");
SkBitmap large_icon;
SkBitmap rendered_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &large_icon));
EXPECT_FALSE(image_util::RenderIconForVisibilityAnalysis(
large_icon, SK_ColorWHITE, &rendered_icon));
// Shrink the icon so it's under the limit. It should be visible.
const SkImageInfo& image_info = large_icon.info();
SkImageInfo new_image_info = SkImageInfo::Make(
128, 128, image_info.colorType(), image_info.alphaType());
ASSERT_TRUE(large_icon.setInfo(new_image_info));
EXPECT_TRUE(image_util::RenderIconForVisibilityAnalysis(
large_icon, SK_ColorWHITE, &rendered_icon));
EXPECT_FALSE(rendered_icon.empty());
}
TEST(ImageUtilTest, MANUAL_IsIconSufficientlyVisiblePerfTest) {
base::FilePath test_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &test_dir));
base::FilePath icon_path;
// This icon has all transparent pixels.
icon_path = test_dir.AppendASCII("transparent_icon.png");
SkBitmap invisible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &invisible_icon));
// This icon is completely opaque.
icon_path = test_dir.AppendASCII("opaque_icon.png");
SkBitmap visible_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, &visible_icon));
static constexpr char kInvisibleTimerId[] = "InvisibleIcon";
static constexpr char kVisibleTimerId[] = "VisibleIcon";
static constexpr char kInvisibleRenderedTimerId[] = "InvisibleRenderedIcon";
static constexpr char kVisibleRenderedTimerId[] = "VisibleRenderedIcon";
constexpr int kIterations = 100000;
for (int i = 0; i < kIterations; ++i) {
LoggingTimer timer(kInvisibleTimerId);
EXPECT_FALSE(image_util::IsIconSufficientlyVisible(invisible_icon));
}
for (int i = 0; i < kIterations; ++i) {
LoggingTimer timer(kVisibleTimerId);
EXPECT_TRUE(image_util::IsIconSufficientlyVisible(visible_icon));
}
for (int i = 0; i < kIterations; ++i) {
LoggingTimer timer(kInvisibleRenderedTimerId);
EXPECT_FALSE(image_util::IsRenderedIconSufficientlyVisible(invisible_icon,
SK_ColorWHITE));
}
for (int i = 0; i < kIterations; ++i) {
LoggingTimer timer(kVisibleRenderedTimerId);
EXPECT_TRUE(image_util::IsRenderedIconSufficientlyVisible(visible_icon,
SK_ColorWHITE));
}
LoggingTimer::Print();
}
namespace {
void WriteRenderedIcon(const SkBitmap& icon,
SkColor background_color,
const base::FilePath& rendered_icon_path) {
SkBitmap bitmap;
DCHECK(image_util::RenderIconForVisibilityAnalysis(icon, background_color,
&bitmap));
std::vector<unsigned char> output_data;
ASSERT_TRUE(gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output_data));
const int bytes_to_write = output_data.size();
ASSERT_EQ(bytes_to_write,
base::WriteFile(rendered_icon_path,
reinterpret_cast<const char*>(&output_data[0]),
bytes_to_write));
}
} // namespace
TEST(ImageUtilTest, DISABLED_AnalyzeAllDownloadedIcons) {
// See the README in extensions/test/data/icon_visibility for more details
// on running this test.
// TODO(crbug.com/805600): Remove this test when the bug is closed.
base::FilePath test_dir;
ASSERT_TRUE(base::PathService::Get(DIR_TEST_DATA, &test_dir));
test_dir = test_dir.AppendASCII("icon_visibility");
base::FilePath icons_file_path = test_dir.AppendASCII("source_urls.txt");
std::string file_data;
ASSERT_TRUE(base::ReadFileToString(icons_file_path, &file_data));
base::FilePath output_file_path =
test_dir.AppendASCII("invisible_source_urls.txt");
base::File output_file(output_file_path, base::File::FLAG_CREATE_ALWAYS |
base::File::FLAG_WRITE);
ASSERT_TRUE(output_file.IsValid());
base::FilePath rendered_icon_path = test_dir.AppendASCII("rendered_pngs");
ASSERT_TRUE(base::CreateDirectory(rendered_icon_path));
base::FilePath downloaded_icons_path = test_dir.AppendASCII("pngs");
ASSERT_TRUE(base::DirectoryExists(downloaded_icons_path));
const std::vector<base::StringPiece> urls = base::SplitStringPiece(
file_data, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const base::StringPiece url : urls) {
const std::string file_name = GURL(url).ExtractFileName();
base::FilePath icon_path = downloaded_icons_path.AppendASCII(file_name);
SkBitmap current_icon;
ASSERT_TRUE(image_util::LoadPngFromFile(icon_path, ¤t_icon));
if (!image_util::IsRenderedIconSufficientlyVisible(current_icon,
SK_ColorWHITE)) {
output_file.WriteAtCurrentPos(url.data(), url.length());
output_file.WriteAtCurrentPos("\n", 1);
WriteRenderedIcon(current_icon, SK_ColorWHITE,
rendered_icon_path.AppendASCII(file_name + ".png"));
}
}
}
} // namespace extensions