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.
5 #include "chrome/browser/extensions/image_loader.h"
7 #include "base/json/json_file_value_serializer.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/extension_icon_set.h"
15 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "extensions/common/extension_resource.h"
19 #include "extensions/common/manifest.h"
20 #include "grit/component_extension_resources.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/size.h"
27 using content::BrowserThread;
28 using extensions::Extension;
29 using extensions::ExtensionResource;
30 using extensions::ImageLoader;
31 using extensions::Manifest;
32 using extensions::UnloadedExtensionInfo;
34 class ImageLoaderTest : public testing::Test {
37 : image_loaded_count_(0),
38 quit_in_image_loaded_(false),
39 ui_thread_(BrowserThread::UI, &ui_loop_),
40 file_thread_(BrowserThread::FILE),
41 io_thread_(BrowserThread::IO) {
44 void OnImageLoaded(const gfx::Image& image) {
45 image_loaded_count_++;
46 if (quit_in_image_loaded_)
47 base::MessageLoop::current()->Quit();
51 void WaitForImageLoad() {
52 quit_in_image_loaded_ = true;
53 base::MessageLoop::current()->Run();
54 quit_in_image_loaded_ = false;
57 int image_loaded_count() {
58 int result = image_loaded_count_;
59 image_loaded_count_ = 0;
63 scoped_refptr<Extension> CreateExtension(const char* name,
64 Manifest::Location location) {
65 // Create and load an extension.
66 base::FilePath test_file;
67 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) {
71 test_file = test_file.AppendASCII("extensions")
75 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json"));
76 scoped_ptr<DictionaryValue> valid_value(
77 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code,
79 EXPECT_EQ(0, error_code) << error;
83 EXPECT_TRUE(valid_value.get());
87 if (location == Manifest::COMPONENT) {
88 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) {
92 test_file = test_file.AppendASCII(name);
94 return Extension::Create(test_file, location, *valid_value,
95 Extension::NO_FLAGS, &error);
101 virtual void SetUp() OVERRIDE {
102 testing::Test::SetUp();
103 file_thread_.Start();
107 int image_loaded_count_;
108 bool quit_in_image_loaded_;
109 base::MessageLoop ui_loop_;
110 content::TestBrowserThread ui_thread_;
111 content::TestBrowserThread file_thread_;
112 content::TestBrowserThread io_thread_;
115 // Tests loading an image works correctly.
116 TEST_F(ImageLoaderTest, LoadImage) {
117 scoped_refptr<Extension> extension(CreateExtension(
118 "image_loading_tracker", Manifest::INVALID_LOCATION));
119 ASSERT_TRUE(extension.get() != NULL);
121 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
123 extension_misc::EXTENSION_ICON_SMALLISH,
124 ExtensionIconSet::MATCH_EXACTLY);
125 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
126 extension_misc::EXTENSION_ICON_SMALLISH);
128 loader.LoadImageAsync(extension.get(),
131 base::Bind(&ImageLoaderTest::OnImageLoaded,
132 base::Unretained(this)));
134 // The image isn't cached, so we should not have received notification.
135 EXPECT_EQ(0, image_loaded_count());
139 // We should have gotten the image.
140 EXPECT_EQ(1, image_loaded_count());
142 // Check that the image was loaded.
143 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
144 image_.ToSkBitmap()->width());
147 // Tests deleting an extension while waiting for the image to load doesn't cause
149 TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) {
150 scoped_refptr<Extension> extension(CreateExtension(
151 "image_loading_tracker", Manifest::INVALID_LOCATION));
152 ASSERT_TRUE(extension.get() != NULL);
154 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
156 extension_misc::EXTENSION_ICON_SMALLISH,
157 ExtensionIconSet::MATCH_EXACTLY);
158 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
159 extension_misc::EXTENSION_ICON_SMALLISH);
162 sizes.insert(extension_misc::EXTENSION_ICON_SMALLISH);
163 loader.LoadImageAsync(extension.get(),
166 base::Bind(&ImageLoaderTest::OnImageLoaded,
167 base::Unretained(this)));
169 // The image isn't cached, so we should not have received notification.
170 EXPECT_EQ(0, image_loaded_count());
172 // Send out notification the extension was uninstalled.
173 UnloadedExtensionInfo details(extension.get(),
174 UnloadedExtensionInfo::REASON_UNINSTALL);
175 content::NotificationService::current()->Notify(
176 chrome::NOTIFICATION_EXTENSION_UNLOADED,
177 content::NotificationService::AllSources(),
178 content::Details<UnloadedExtensionInfo>(&details));
180 // Chuck the extension, that way if anyone tries to access it we should crash
181 // or get valgrind errors.
186 // Even though we deleted the extension, we should still get the image.
187 // We should still have gotten the image.
188 EXPECT_EQ(1, image_loaded_count());
190 // Check that the image was loaded.
191 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
192 image_.ToSkBitmap()->width());
195 // Tests loading multiple dimensions of the same image.
196 TEST_F(ImageLoaderTest, MultipleImages) {
197 scoped_refptr<Extension> extension(CreateExtension(
198 "image_loading_tracker", Manifest::INVALID_LOCATION));
199 ASSERT_TRUE(extension.get() != NULL);
201 std::vector<ImageLoader::ImageRepresentation> info_list;
202 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH,
203 extension_misc::EXTENSION_ICON_BITTY};
204 for (size_t i = 0; i < arraysize(sizes); ++i) {
205 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
206 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
207 info_list.push_back(ImageLoader::ImageRepresentation(
209 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
210 gfx::Size(sizes[i], sizes[i]),
211 ui::SCALE_FACTOR_NONE));
215 loader.LoadImagesAsync(extension.get(), info_list,
216 base::Bind(&ImageLoaderTest::OnImageLoaded,
217 base::Unretained(this)));
219 // The image isn't cached, so we should not have received notification.
220 EXPECT_EQ(0, image_loaded_count());
224 // We should have gotten the image.
225 EXPECT_EQ(1, image_loaded_count());
227 // Check that all images were loaded.
228 std::vector<gfx::ImageSkiaRep> image_reps =
229 image_.ToImageSkia()->image_reps();
230 ASSERT_EQ(2u, image_reps.size());
231 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0];
232 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1];
233 if (img_rep1->pixel_width() > img_rep2->pixel_width()) {
234 std::swap(img_rep1, img_rep2);
236 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
237 img_rep1->pixel_width());
238 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
239 img_rep2->pixel_width());
242 // Tests IsComponentExtensionResource function.
243 TEST_F(ImageLoaderTest, IsComponentExtensionResource) {
244 scoped_refptr<Extension> extension(CreateExtension(
245 "file_manager", Manifest::COMPONENT));
246 ASSERT_TRUE(extension.get() != NULL);
248 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
250 extension_misc::EXTENSION_ICON_BITTY,
251 ExtensionIconSet::MATCH_EXACTLY);
253 #if defined(FILE_MANAGER_EXTENSION)
256 ImageLoader::IsComponentExtensionResource(extension->path(),
257 resource.relative_path(),
259 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id);