Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: extensions/browser/image_loader_unittest.cc

Issue 439023002: Move ImageLoaderTest to extensions_unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: (image-loader) rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/image_loader.h" 5 #include "extensions/browser/image_loader.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/json/json_file_value_serializer.h" 8 #include "base/json/json_file_value_serializer.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "content/public/browser/notification_service.h" 12 #include "content/public/browser/notification_service.h"
14 #include "content/public/test/test_browser_thread.h" 13 #include "content/public/test/test_browser_thread.h"
15 #include "extensions/browser/component_extension_resource_manager.h"
16 #include "extensions/browser/extensions_browser_client.h" 14 #include "extensions/browser/extensions_browser_client.h"
17 #include "extensions/browser/notification_types.h" 15 #include "extensions/browser/notification_types.h"
18 #include "extensions/common/constants.h" 16 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_icon_set.h" 18 #include "extensions/common/extension_icon_set.h"
19 #include "extensions/common/extension_paths.h"
21 #include "extensions/common/extension_resource.h" 20 #include "extensions/common/extension_resource.h"
22 #include "extensions/common/manifest.h" 21 #include "extensions/common/manifest.h"
23 #include "extensions/common/manifest_handlers/icons_handler.h" 22 #include "extensions/common/manifest_handlers/icons_handler.h"
24 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkBitmap.h" 24 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/image/image_family.h" 26 #include "ui/gfx/image/image_family.h"
28 #include "ui/gfx/image/image_skia.h" 27 #include "ui/gfx/image/image_skia.h"
29 #include "ui/gfx/size.h" 28 #include "ui/gfx/size.h"
30 29
31 #if defined(OS_CHROMEOS)
32 #include "ui/file_manager/grit/file_manager_resources.h"
33 #endif
34
35 using content::BrowserThread; 30 using content::BrowserThread;
31 using content::NotificationService;
36 using extensions::Extension; 32 using extensions::Extension;
37 using extensions::ExtensionResource; 33 using extensions::ExtensionResource;
38 using extensions::ImageLoader; 34 using extensions::ImageLoader;
39 using extensions::Manifest; 35 using extensions::Manifest;
40 using extensions::UnloadedExtensionInfo; 36 using extensions::UnloadedExtensionInfo;
41 37
42 class ImageLoaderTest : public testing::Test { 38 class ImageLoaderTest : public testing::Test {
43 public: 39 public:
44 ImageLoaderTest() 40 ImageLoaderTest()
45 : image_loaded_count_(0), 41 : image_loaded_count_(0),
46 quit_in_image_loaded_(false), 42 quit_in_image_loaded_(false),
47 ui_thread_(BrowserThread::UI, &ui_loop_), 43 ui_thread_(BrowserThread::UI, &ui_loop_),
48 file_thread_(BrowserThread::FILE), 44 file_thread_(BrowserThread::FILE),
49 io_thread_(BrowserThread::IO) { 45 io_thread_(BrowserThread::IO),
50 } 46 notification_service_(NotificationService::Create()) {}
51 47
52 void OnImageLoaded(const gfx::Image& image) { 48 void OnImageLoaded(const gfx::Image& image) {
53 image_loaded_count_++; 49 image_loaded_count_++;
54 if (quit_in_image_loaded_) 50 if (quit_in_image_loaded_)
55 base::MessageLoop::current()->Quit(); 51 base::MessageLoop::current()->Quit();
56 image_ = image; 52 image_ = image;
57 } 53 }
58 54
59 void OnImageFamilyLoaded(const gfx::ImageFamily& image_family) { 55 void OnImageFamilyLoaded(const gfx::ImageFamily& image_family) {
60 image_loaded_count_++; 56 image_loaded_count_++;
(...skipping 11 matching lines...) Expand all
72 int image_loaded_count() { 68 int image_loaded_count() {
73 int result = image_loaded_count_; 69 int result = image_loaded_count_;
74 image_loaded_count_ = 0; 70 image_loaded_count_ = 0;
75 return result; 71 return result;
76 } 72 }
77 73
78 scoped_refptr<Extension> CreateExtension(const char* name, 74 scoped_refptr<Extension> CreateExtension(const char* name,
79 Manifest::Location location) { 75 Manifest::Location location) {
80 // Create and load an extension. 76 // Create and load an extension.
81 base::FilePath test_file; 77 base::FilePath test_file;
82 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { 78 if (!PathService::Get(extensions::DIR_TEST_DATA, &test_file)) {
83 EXPECT_FALSE(true); 79 EXPECT_FALSE(true);
84 return NULL; 80 return NULL;
85 } 81 }
86 test_file = test_file.AppendASCII("extensions") 82 test_file = test_file.AppendASCII(name);
87 .AppendASCII(name);
88 int error_code = 0; 83 int error_code = 0;
89 std::string error; 84 std::string error;
90 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); 85 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json"));
91 scoped_ptr<base::DictionaryValue> valid_value( 86 scoped_ptr<base::DictionaryValue> valid_value(
92 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code, 87 static_cast<base::DictionaryValue*>(serializer.Deserialize(&error_code,
93 &error))); 88 &error)));
94 EXPECT_EQ(0, error_code) << error; 89 EXPECT_EQ(0, error_code) << error;
95 if (error_code != 0) 90 if (error_code != 0)
96 return NULL; 91 return NULL;
97 92
98 EXPECT_TRUE(valid_value.get()); 93 EXPECT_TRUE(valid_value.get());
99 if (!valid_value) 94 if (!valid_value)
100 return NULL; 95 return NULL;
101 96
102 if (location == Manifest::COMPONENT) {
103 if (!PathService::Get(chrome::DIR_RESOURCES, &test_file)) {
104 EXPECT_FALSE(true);
105 return NULL;
106 }
107 test_file = test_file.AppendASCII(name);
108 }
109 return Extension::Create(test_file, location, *valid_value, 97 return Extension::Create(test_file, location, *valid_value,
110 Extension::NO_FLAGS, &error); 98 Extension::NO_FLAGS, &error);
111 } 99 }
112 100
113 gfx::Image image_; 101 gfx::Image image_;
114 gfx::ImageFamily image_family_; 102 gfx::ImageFamily image_family_;
115 103
116 private: 104 private:
117 virtual void SetUp() OVERRIDE { 105 virtual void SetUp() OVERRIDE {
118 testing::Test::SetUp(); 106 testing::Test::SetUp();
119 file_thread_.Start(); 107 file_thread_.Start();
120 io_thread_.Start(); 108 io_thread_.Start();
121 } 109 }
122 110
123 int image_loaded_count_; 111 int image_loaded_count_;
124 bool quit_in_image_loaded_; 112 bool quit_in_image_loaded_;
125 base::MessageLoop ui_loop_; 113 base::MessageLoop ui_loop_;
126 content::TestBrowserThread ui_thread_; 114 content::TestBrowserThread ui_thread_;
127 content::TestBrowserThread file_thread_; 115 content::TestBrowserThread file_thread_;
128 content::TestBrowserThread io_thread_; 116 content::TestBrowserThread io_thread_;
117 scoped_ptr<NotificationService> notification_service_;
129 }; 118 };
130 119
131 // Tests loading an image works correctly. 120 // Tests loading an image works correctly.
132 TEST_F(ImageLoaderTest, LoadImage) { 121 TEST_F(ImageLoaderTest, LoadImage) {
133 scoped_refptr<Extension> extension(CreateExtension( 122 scoped_refptr<Extension> extension(
134 "image_loading_tracker", Manifest::INVALID_LOCATION)); 123 CreateExtension("image_loader", Manifest::INVALID_LOCATION));
135 ASSERT_TRUE(extension.get() != NULL); 124 ASSERT_TRUE(extension.get() != NULL);
136 125
137 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( 126 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
138 extension.get(), 127 extension.get(),
139 extension_misc::EXTENSION_ICON_SMALLISH, 128 extension_misc::EXTENSION_ICON_SMALLISH,
140 ExtensionIconSet::MATCH_EXACTLY); 129 ExtensionIconSet::MATCH_EXACTLY);
141 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, 130 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
142 extension_misc::EXTENSION_ICON_SMALLISH); 131 extension_misc::EXTENSION_ICON_SMALLISH);
143 ImageLoader loader; 132 ImageLoader loader;
144 loader.LoadImageAsync(extension.get(), 133 loader.LoadImageAsync(extension.get(),
(...skipping 11 matching lines...) Expand all
156 EXPECT_EQ(1, image_loaded_count()); 145 EXPECT_EQ(1, image_loaded_count());
157 146
158 // Check that the image was loaded. 147 // Check that the image was loaded.
159 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, 148 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
160 image_.ToSkBitmap()->width()); 149 image_.ToSkBitmap()->width());
161 } 150 }
162 151
163 // Tests deleting an extension while waiting for the image to load doesn't cause 152 // Tests deleting an extension while waiting for the image to load doesn't cause
164 // problems. 153 // problems.
165 TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) { 154 TEST_F(ImageLoaderTest, DeleteExtensionWhileWaitingForCache) {
166 scoped_refptr<Extension> extension(CreateExtension( 155 scoped_refptr<Extension> extension(
167 "image_loading_tracker", Manifest::INVALID_LOCATION)); 156 CreateExtension("image_loader", Manifest::INVALID_LOCATION));
168 ASSERT_TRUE(extension.get() != NULL); 157 ASSERT_TRUE(extension.get() != NULL);
169 158
170 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource( 159 ExtensionResource image_resource = extensions::IconsInfo::GetIconResource(
171 extension.get(), 160 extension.get(),
172 extension_misc::EXTENSION_ICON_SMALLISH, 161 extension_misc::EXTENSION_ICON_SMALLISH,
173 ExtensionIconSet::MATCH_EXACTLY); 162 ExtensionIconSet::MATCH_EXACTLY);
174 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, 163 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH,
175 extension_misc::EXTENSION_ICON_SMALLISH); 164 extension_misc::EXTENSION_ICON_SMALLISH);
176 ImageLoader loader; 165 ImageLoader loader;
177 std::set<int> sizes; 166 std::set<int> sizes;
(...skipping 25 matching lines...) Expand all
203 // We should still have gotten the image. 192 // We should still have gotten the image.
204 EXPECT_EQ(1, image_loaded_count()); 193 EXPECT_EQ(1, image_loaded_count());
205 194
206 // Check that the image was loaded. 195 // Check that the image was loaded.
207 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, 196 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
208 image_.ToSkBitmap()->width()); 197 image_.ToSkBitmap()->width());
209 } 198 }
210 199
211 // Tests loading multiple dimensions of the same image. 200 // Tests loading multiple dimensions of the same image.
212 TEST_F(ImageLoaderTest, MultipleImages) { 201 TEST_F(ImageLoaderTest, MultipleImages) {
213 scoped_refptr<Extension> extension(CreateExtension( 202 scoped_refptr<Extension> extension(
214 "image_loading_tracker", Manifest::INVALID_LOCATION)); 203 CreateExtension("image_loader", Manifest::INVALID_LOCATION));
215 ASSERT_TRUE(extension.get() != NULL); 204 ASSERT_TRUE(extension.get() != NULL);
216 205
217 std::vector<ImageLoader::ImageRepresentation> info_list; 206 std::vector<ImageLoader::ImageRepresentation> info_list;
218 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, 207 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY,
219 extension_misc::EXTENSION_ICON_SMALLISH, }; 208 extension_misc::EXTENSION_ICON_SMALLISH, };
220 for (size_t i = 0; i < arraysize(sizes); ++i) { 209 for (size_t i = 0; i < arraysize(sizes); ++i) {
221 ExtensionResource resource = extensions::IconsInfo::GetIconResource( 210 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
222 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); 211 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
223 info_list.push_back(ImageLoader::ImageRepresentation( 212 info_list.push_back(ImageLoader::ImageRepresentation(
224 resource, 213 resource,
(...skipping 24 matching lines...) Expand all
249 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; 238 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1];
250 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, 239 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY,
251 img_rep1->pixel_width()); 240 img_rep1->pixel_width());
252 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, 241 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH,
253 img_rep2->pixel_width()); 242 img_rep2->pixel_width());
254 } 243 }
255 244
256 // Tests loading multiple dimensions of the same image into an image family. 245 // Tests loading multiple dimensions of the same image into an image family.
257 TEST_F(ImageLoaderTest, LoadImageFamily) { 246 TEST_F(ImageLoaderTest, LoadImageFamily) {
258 scoped_refptr<Extension> extension( 247 scoped_refptr<Extension> extension(
259 CreateExtension("image_loading_tracker", Manifest::INVALID_LOCATION)); 248 CreateExtension("image_loader", Manifest::INVALID_LOCATION));
260 ASSERT_TRUE(extension.get() != NULL); 249 ASSERT_TRUE(extension.get() != NULL);
261 250
262 std::vector<ImageLoader::ImageRepresentation> info_list; 251 std::vector<ImageLoader::ImageRepresentation> info_list;
263 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY, 252 int sizes[] = {extension_misc::EXTENSION_ICON_BITTY,
264 extension_misc::EXTENSION_ICON_SMALLISH, }; 253 extension_misc::EXTENSION_ICON_SMALLISH, };
265 for (size_t i = 0; i < arraysize(sizes); ++i) { 254 for (size_t i = 0; i < arraysize(sizes); ++i) {
266 ExtensionResource resource = extensions::IconsInfo::GetIconResource( 255 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
267 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY); 256 extension.get(), sizes[i], ExtensionIconSet::MATCH_EXACTLY);
268 info_list.push_back(ImageLoader::ImageRepresentation( 257 info_list.push_back(ImageLoader::ImageRepresentation(
269 resource, 258 resource,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 303
315 ASSERT_EQ(2u, image_reps.size()); 304 ASSERT_EQ(2u, image_reps.size());
316 305
317 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; 306 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0];
318 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; 307 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1];
319 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, img_rep1->pixel_width()); 308 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, img_rep1->pixel_width());
320 EXPECT_EQ(1.0f, img_rep1->scale()); 309 EXPECT_EQ(1.0f, img_rep1->scale());
321 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, img_rep2->pixel_width()); 310 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, img_rep2->pixel_width());
322 EXPECT_EQ(2.0f, img_rep2->scale()); 311 EXPECT_EQ(2.0f, img_rep2->scale());
323 } 312 }
324
325 // Tests IsComponentExtensionResource function.
326 // TODO(mukai): move this to ChromeComponentExtensionResourceManager's test.
327 TEST_F(ImageLoaderTest, IsComponentExtensionResource) {
328 extensions::ComponentExtensionResourceManager* resource_manager =
329 extensions::ExtensionsBrowserClient::Get()->
330 GetComponentExtensionResourceManager();
331 if (!resource_manager)
332 return;
333
334 scoped_refptr<Extension> extension(CreateExtension(
335 "file_manager", Manifest::COMPONENT));
336 ASSERT_TRUE(extension.get() != NULL);
337
338 ExtensionResource resource = extensions::IconsInfo::GetIconResource(
339 extension.get(),
340 extension_misc::EXTENSION_ICON_BITTY,
341 ExtensionIconSet::MATCH_EXACTLY);
342
343 #if defined(OS_CHROMEOS)
344 int resource_id;
345 ASSERT_TRUE(resource_manager->IsComponentExtensionResource(
346 extension->path(),
347 resource.relative_path(),
348 &resource_id));
349 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id);
350 #endif
351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698