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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
// Copyright 2014 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 "extensions/browser/image_loader.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/extensions_test.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_icon_set.h"
#include "extensions/common/extension_paths.h"
#include "extensions/common/extension_resource.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/image/image_skia.h"
using content::BrowserThread;
using content::NotificationService;
namespace extensions {
class ImageLoaderTest : public ExtensionsTest {
public:
ImageLoaderTest()
: image_loaded_count_(0),
quit_in_image_loaded_(false),
ui_thread_(BrowserThread::UI, &ui_loop_),
file_thread_(BrowserThread::FILE),
io_thread_(BrowserThread::IO),
notification_service_(NotificationService::Create()) {}
void OnImageLoaded(const gfx::Image& image) {
image_loaded_count_++;
if (quit_in_image_loaded_)
base::MessageLoop::current()->Quit();
image_ = image;
}
void OnImageFamilyLoaded(const gfx::ImageFamily& image_family) {
image_loaded_count_++;
if (quit_in_image_loaded_)
base::MessageLoop::current()->Quit();
image_family_ = image_family;
}
void WaitForImageLoad() {
quit_in_image_loaded_ = true;
base::MessageLoop::current()->Run();
quit_in_image_loaded_ = false;
}
int image_loaded_count() {
int result = image_loaded_count_;
image_loaded_count_ = 0;
return result;
}
scoped_refptr<Extension> CreateExtension(const char* dir_name,
Manifest::Location location) {
// Create and load an extension.
base::FilePath extension_dir;
if (!PathService::Get(DIR_TEST_DATA, &extension_dir)) {
EXPECT_FALSE(true);
return NULL;
}
extension_dir = extension_dir.AppendASCII(dir_name);
int error_code = 0;
std::string error;
JSONFileValueDeserializer deserializer(
extension_dir.AppendASCII("manifest.json"));
scoped_ptr<base::DictionaryValue> valid_value(
static_cast<base::DictionaryValue*>(
deserializer.Deserialize(&error_code, &error)));
EXPECT_EQ(0, error_code) << error;
if (error_code != 0)
return NULL;
EXPECT_TRUE(valid_value.get());
if (!valid_value)
return NULL;
return Extension::Create(
extension_dir, location, *valid_value, Extension::NO_FLAGS, &error);
}
gfx::Image image_;
gfx::ImageFamily image_family_;
private:
void SetUp() override {
testing::Test::SetUp();
file_thread_.Start();
io_thread_.Start();
}
int image_loaded_count_;
bool quit_in_image_loaded_;
base::MessageLoop ui_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
content::TestBrowserThread io_thread_;
scoped_ptr<NotificationService> notification_service_;
};
// Tests loading an image works correctly.
TEST_F(ImageLoaderTest, LoadImage) {
scoped_refptr<Extension> extension(
CreateExtension("image_loader", Manifest::INVALID_LOCATION));
ASSERT_TRUE(extension.get() != NULL);
ExtensionResource image_resource =
IconsInfo::GetIconResource(extension.get(),
extension_misc::EXTENSION_ICON_SMALLISH,
ExtensionIconSet::MATCH_EXACTLY);
gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
extension_misc::EXTENSION_ICON_SMALLISH);
ImageLoader loader;
loader.LoadImageAsync(extension.get(),
image_resource,
max_size,
base::Bind(&ImageLoaderTest::OnImageLoaded,
base::Unretained(this)));
// The image isn't cached, so we should not have received notification.
EXPECT_EQ(0, image_loaded_count());
WaitForImageLoad();
// We should have gotten the image.
EXPECT_FALSE(image_.IsEmpty());
EXPECT_EQ(1, image_loaded_count());
// Check that the image was loaded.
EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
image_.ToSkBitmap()->width());
}
// Tests deleting an extension while waiting for the image to load doesn't cause
// problems.
TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) {
scoped_refptr<Extension> extension(
CreateExtension("image_loader", Manifest::INVALID_LOCATION));
ASSERT_TRUE(extension.get() != NULL);
ExtensionResource image_resource =
IconsInfo::GetIconResource(extension.get(),
extension_misc::EXTENSION_ICON_SMALLISH,
ExtensionIconSet::MATCH_EXACTLY);
gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
extension_misc::EXTENSION_ICON_SMALLISH);
ImageLoader loader;
std::set<int> sizes;
sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH);
loader.LoadImageAsync(extension.get(),
image_resource,
max_size,
base::Bind(&ImageLoaderTest::OnImageLoaded,
base::Unretained(this)));
// The image isn't cached, so we should not have received notification.
EXPECT_EQ(0, image_loaded_count());
// Send out notification the extension was uninstalled.
ExtensionRegistry::Get(browser_context())->TriggerOnUnloaded(
extension.get(), UnloadedExtensionInfo::REASON_UNINSTALL);
// Chuck the extension, that way if anyone tries to access it we should crash
// or get valgrind errors.
extension = NULL;
WaitForImageLoad();
// Even though we deleted the extension, we should still get the image.
// We should still have gotten the image.
EXPECT_EQ(1, image_loaded_count());
// Check that the image was loaded.
EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
image_.ToSkBitmap()->width());
}
// Tests loading multiple dimensions of the same image.
TEST_F(ImageLoaderTest, MultipleImages) {
scoped_refptr<Extension> extension(
CreateExtension("image_loader", Manifest::INVALID_LOCATION));
ASSERT_TRUE(extension.get() != NULL);
std::vector<ImageLoader::ImageRepresentation> info_list;
int sizes[] = {extension_misc::EXTENSION_ICON_BITTY,
extension_misc::EXTENSION_ICON_SMALLISH, };
for (size_t i = 0; i < arraysize(sizes); ++i) {
ExtensionResource resource = IconsInfo::GetIconResource(
extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
info_list.push_back(ImageLoader::ImageRepresentation(
resource,
ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
gfx::Size(sizes[i], sizes[i]),
ui::SCALE_FACTOR_NONE));
}
ImageLoader loader;
loader.LoadImagesAsync(extension.get(), info_list,
base::Bind(&ImageLoaderTest::OnImageLoaded,
base::Unretained(this)));
// The image isn't cached, so we should not have received notification.
EXPECT_EQ(0, image_loaded_count());
WaitForImageLoad();
// We should have gotten the image.
EXPECT_EQ(1, image_loaded_count());
// Check that all images were loaded.
std::vector<gfx::ImageSkiaRep> image_reps =
image_.ToImageSkia()->image_reps();
ASSERT_EQ(2u, image_reps.size());
const gfx::ImageSkiaRep* img_rep1 = &image_reps[0];
const gfx::ImageSkiaRep* img_rep2 = &image_reps[1];
EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
img_rep1->pixel_width());
EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
img_rep2->pixel_width());
}
// Tests loading multiple dimensions of the same image into an image family.
TEST_F(ImageLoaderTest, LoadImageFamily) {
scoped_refptr<Extension> extension(
CreateExtension("image_loader", Manifest::INVALID_LOCATION));
ASSERT_TRUE(extension.get() != NULL);
std::vector<ImageLoader::ImageRepresentation> info_list;
int sizes[] = {extension_misc::EXTENSION_ICON_BITTY,
extension_misc::EXTENSION_ICON_SMALLISH, };
for (size_t i = 0; i < arraysize(sizes); ++i) {
ExtensionResource resource = IconsInfo::GetIconResource(
extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
info_list.push_back(ImageLoader::ImageRepresentation(
resource,
ImageLoader::ImageRepresentation::NEVER_RESIZE,
gfx::Size(sizes[i], sizes[i]),
ui::SCALE_FACTOR_100P));
}
// Add a second icon of 200P which should get grouped with the smaller icon's
// ImageSkia.
ExtensionResource resource =
IconsInfo::GetIconResource(extension.get(),
extension_misc::EXTENSION_ICON_SMALLISH,
ExtensionIconSet::MATCH_EXACTLY);
info_list.push_back(ImageLoader::ImageRepresentation(
resource,
ImageLoader::ImageRepresentation::NEVER_RESIZE,
gfx::Size(extension_misc::EXTENSION_ICON_BITTY,
extension_misc::EXTENSION_ICON_BITTY),
ui::SCALE_FACTOR_200P));
ImageLoader loader;
loader.LoadImageFamilyAsync(extension.get(),
info_list,
base::Bind(&ImageLoaderTest::OnImageFamilyLoaded,
base::Unretained(this)));
// The image isn't cached, so we should not have received notification.
EXPECT_EQ(0, image_loaded_count());
WaitForImageLoad();
// We should have gotten the image.
EXPECT_EQ(1, image_loaded_count());
// Check that all images were loaded.
for (size_t i = 0; i < arraysize(sizes); ++i) {
const gfx::Image* image = image_family_.GetBest(sizes[i], sizes[i]);
EXPECT_EQ(sizes[i], image->Width());
}
// Check the smaller image has 2 representations of different scale factors.
std::vector<gfx::ImageSkiaRep> image_reps =
image_family_.GetBest(extension_misc::EXTENSION_ICON_BITTY,
extension_misc::EXTENSION_ICON_BITTY)
->ToImageSkia()
->image_reps();
ASSERT_EQ(2u, image_reps.size());
const gfx::ImageSkiaRep* img_rep1 = &image_reps[0];
const gfx::ImageSkiaRep* img_rep2 = &image_reps[1];
EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, img_rep1->pixel_width());
EXPECT_EQ(1.0f, img_rep1->scale());
EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, img_rep2->pixel_width());
EXPECT_EQ(2.0f, img_rep2->scale());
}
} // namespace extensions
|