OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/json/json_file_value_serializer.h" | 5 #include "base/json/json_file_value_serializer.h" |
6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "chrome/browser/extensions/image_loading_tracker.h" | 8 #include "chrome/browser/extensions/image_loading_tracker.h" |
9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
11 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_constants.h" |
12 #include "chrome/common/extensions/extension_icon_set.h" | 13 #include "chrome/common/extensions/extension_icon_set.h" |
13 #include "chrome/common/extensions/extension_resource.h" | 14 #include "chrome/common/extensions/extension_resource.h" |
14 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
16 #include "grit/component_extension_resources.h" | 17 #include "grit/component_extension_resources.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
20 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
21 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 content::TestBrowserThread io_thread_; | 100 content::TestBrowserThread io_thread_; |
100 }; | 101 }; |
101 | 102 |
102 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. | 103 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. |
103 TEST_F(ImageLoadingTrackerTest, Cache) { | 104 TEST_F(ImageLoadingTrackerTest, Cache) { |
104 scoped_refptr<Extension> extension(CreateExtension( | 105 scoped_refptr<Extension> extension(CreateExtension( |
105 "image_loading_tracker", Extension::INVALID)); | 106 "image_loading_tracker", Extension::INVALID)); |
106 ASSERT_TRUE(extension.get() != NULL); | 107 ASSERT_TRUE(extension.get() != NULL); |
107 | 108 |
108 ExtensionResource image_resource = | 109 ExtensionResource image_resource = |
109 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 110 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
110 ExtensionIconSet::MATCH_EXACTLY); | 111 ExtensionIconSet::MATCH_EXACTLY); |
111 gfx::Size max_size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 112 gfx::Size max_size(extension_misc::EXTENSION_ICON_SMALLISH, |
112 ExtensionIconSet::EXTENSION_ICON_SMALLISH); | 113 extension_misc::EXTENSION_ICON_SMALLISH); |
113 ImageLoadingTracker loader(this); | 114 ImageLoadingTracker loader(this); |
114 loader.LoadImage(extension.get(), | 115 loader.LoadImage(extension.get(), |
115 image_resource, | 116 image_resource, |
116 max_size, | 117 max_size, |
117 ImageLoadingTracker::CACHE); | 118 ImageLoadingTracker::CACHE); |
118 | 119 |
119 // The image isn't cached, so we should not have received notification. | 120 // The image isn't cached, so we should not have received notification. |
120 EXPECT_EQ(0, image_loaded_count()); | 121 EXPECT_EQ(0, image_loaded_count()); |
121 | 122 |
122 WaitForImageLoad(); | 123 WaitForImageLoad(); |
123 | 124 |
124 // We should have gotten the image. | 125 // We should have gotten the image. |
125 EXPECT_EQ(1, image_loaded_count()); | 126 EXPECT_EQ(1, image_loaded_count()); |
126 | 127 |
127 // Check that the image was loaded. | 128 // Check that the image was loaded. |
128 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 129 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
129 image_.ToSkBitmap()->width()); | 130 image_.ToSkBitmap()->width()); |
130 | 131 |
131 // The image should be cached in the Extension. | 132 // The image should be cached in the Extension. |
132 EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size)); | 133 EXPECT_TRUE(extension->HasCachedImage(image_resource, max_size)); |
133 | 134 |
134 // Make sure the image is in the extension. | 135 // Make sure the image is in the extension. |
135 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 136 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
136 extension->GetCachedImage(image_resource, max_size).width()); | 137 extension->GetCachedImage(image_resource, max_size).width()); |
137 | 138 |
138 // Ask the tracker for the image again, this should call us back immediately. | 139 // Ask the tracker for the image again, this should call us back immediately. |
139 loader.LoadImage(extension.get(), | 140 loader.LoadImage(extension.get(), |
140 image_resource, | 141 image_resource, |
141 max_size, | 142 max_size, |
142 ImageLoadingTracker::CACHE); | 143 ImageLoadingTracker::CACHE); |
143 // We should have gotten the image. | 144 // We should have gotten the image. |
144 EXPECT_EQ(1, image_loaded_count()); | 145 EXPECT_EQ(1, image_loaded_count()); |
145 | 146 |
146 // Check that the image was loaded. | 147 // Check that the image was loaded. |
147 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 148 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
148 image_.ToSkBitmap()->width()); | 149 image_.ToSkBitmap()->width()); |
149 } | 150 } |
150 | 151 |
151 // 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 |
152 // problems. | 153 // problems. |
153 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { | 154 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { |
154 scoped_refptr<Extension> extension(CreateExtension( | 155 scoped_refptr<Extension> extension(CreateExtension( |
155 "image_loading_tracker", Extension::INVALID)); | 156 "image_loading_tracker", Extension::INVALID)); |
156 ASSERT_TRUE(extension.get() != NULL); | 157 ASSERT_TRUE(extension.get() != NULL); |
157 | 158 |
158 ExtensionResource image_resource = | 159 ExtensionResource image_resource = |
159 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 160 extension->GetIconResource(extension_misc::EXTENSION_ICON_SMALLISH, |
160 ExtensionIconSet::MATCH_EXACTLY); | 161 ExtensionIconSet::MATCH_EXACTLY); |
161 ImageLoadingTracker loader(this); | 162 ImageLoadingTracker loader(this); |
162 loader.LoadImage(extension.get(), | 163 loader.LoadImage(extension.get(), |
163 image_resource, | 164 image_resource, |
164 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 165 gfx::Size(extension_misc::EXTENSION_ICON_SMALLISH, |
165 ExtensionIconSet::EXTENSION_ICON_SMALLISH), | 166 extension_misc::EXTENSION_ICON_SMALLISH), |
166 ImageLoadingTracker::CACHE); | 167 ImageLoadingTracker::CACHE); |
167 | 168 |
168 // The image isn't cached, so we should not have received notification. | 169 // The image isn't cached, so we should not have received notification. |
169 EXPECT_EQ(0, image_loaded_count()); | 170 EXPECT_EQ(0, image_loaded_count()); |
170 | 171 |
171 // Send out notification the extension was uninstalled. | 172 // Send out notification the extension was uninstalled. |
172 extensions::UnloadedExtensionInfo details(extension.get(), | 173 extensions::UnloadedExtensionInfo details(extension.get(), |
173 extension_misc::UNLOAD_REASON_UNINSTALL); | 174 extension_misc::UNLOAD_REASON_UNINSTALL); |
174 content::NotificationService::current()->Notify( | 175 content::NotificationService::current()->Notify( |
175 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 176 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
176 content::NotificationService::AllSources(), | 177 content::NotificationService::AllSources(), |
177 content::Details<extensions::UnloadedExtensionInfo>(&details)); | 178 content::Details<extensions::UnloadedExtensionInfo>(&details)); |
178 | 179 |
179 // Chuck the extension, that way if anyone tries to access it we should crash | 180 // Chuck the extension, that way if anyone tries to access it we should crash |
180 // or get valgrind errors. | 181 // or get valgrind errors. |
181 extension = NULL; | 182 extension = NULL; |
182 | 183 |
183 WaitForImageLoad(); | 184 WaitForImageLoad(); |
184 | 185 |
185 // Even though we deleted the extension, we should still get the image. | 186 // Even though we deleted the extension, we should still get the image. |
186 // We should still have gotten the image. | 187 // We should still have gotten the image. |
187 EXPECT_EQ(1, image_loaded_count()); | 188 EXPECT_EQ(1, image_loaded_count()); |
188 | 189 |
189 // Check that the image was loaded. | 190 // Check that the image was loaded. |
190 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 191 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
191 image_.ToSkBitmap()->width()); | 192 image_.ToSkBitmap()->width()); |
192 } | 193 } |
193 | 194 |
194 // Tests loading multiple dimensions of the same image. | 195 // Tests loading multiple dimensions of the same image. |
195 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 196 TEST_F(ImageLoadingTrackerTest, MultipleImages) { |
196 scoped_refptr<Extension> extension(CreateExtension( | 197 scoped_refptr<Extension> extension(CreateExtension( |
197 "image_loading_tracker", Extension::INVALID)); | 198 "image_loading_tracker", Extension::INVALID)); |
198 ASSERT_TRUE(extension.get() != NULL); | 199 ASSERT_TRUE(extension.get() != NULL); |
199 | 200 |
200 std::vector<ImageLoadingTracker::ImageInfo> info_list; | 201 std::vector<ImageLoadingTracker::ImageInfo> info_list; |
201 int sizes[] = {ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 202 int sizes[] = {extension_misc::EXTENSION_ICON_SMALLISH, |
202 ExtensionIconSet::EXTENSION_ICON_BITTY}; | 203 extension_misc::EXTENSION_ICON_BITTY}; |
203 for (size_t i = 0; i < arraysize(sizes); ++i) { | 204 for (size_t i = 0; i < arraysize(sizes); ++i) { |
204 ExtensionResource resource = | 205 ExtensionResource resource = |
205 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 206 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
206 info_list.push_back(ImageLoadingTracker::ImageInfo( | 207 info_list.push_back(ImageLoadingTracker::ImageInfo( |
207 resource, gfx::Size(sizes[i], sizes[i]))); | 208 resource, gfx::Size(sizes[i], sizes[i]))); |
208 } | 209 } |
209 | 210 |
210 ImageLoadingTracker loader(this); | 211 ImageLoadingTracker loader(this); |
211 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); | 212 loader.LoadImages(extension.get(), info_list, ImageLoadingTracker::CACHE); |
212 | 213 |
213 // The image isn't cached, so we should not have received notification. | 214 // The image isn't cached, so we should not have received notification. |
214 EXPECT_EQ(0, image_loaded_count()); | 215 EXPECT_EQ(0, image_loaded_count()); |
215 | 216 |
216 WaitForImageLoad(); | 217 WaitForImageLoad(); |
217 | 218 |
218 // We should have gotten the image. | 219 // We should have gotten the image. |
219 EXPECT_EQ(1, image_loaded_count()); | 220 EXPECT_EQ(1, image_loaded_count()); |
220 | 221 |
221 // Check that all images were loaded. | 222 // Check that all images were loaded. |
222 std::vector<gfx::ImageSkiaRep> image_reps = | 223 std::vector<gfx::ImageSkiaRep> image_reps = |
223 image_.ToImageSkia()->image_reps(); | 224 image_.ToImageSkia()->image_reps(); |
224 ASSERT_EQ(2u, image_reps.size()); | 225 ASSERT_EQ(2u, image_reps.size()); |
225 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; | 226 const gfx::ImageSkiaRep* img_rep1 = &image_reps[0]; |
226 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; | 227 const gfx::ImageSkiaRep* img_rep2 = &image_reps[1]; |
227 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { | 228 if (img_rep1->pixel_width() > img_rep2->pixel_width()) { |
228 std::swap(img_rep1, img_rep2); | 229 std::swap(img_rep1, img_rep2); |
229 } | 230 } |
230 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY, | 231 EXPECT_EQ(extension_misc::EXTENSION_ICON_BITTY, |
231 img_rep1->pixel_width()); | 232 img_rep1->pixel_width()); |
232 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 233 EXPECT_EQ(extension_misc::EXTENSION_ICON_SMALLISH, |
233 img_rep2->pixel_width()); | 234 img_rep2->pixel_width()); |
234 } | 235 } |
235 | 236 |
236 // Tests IsComponentExtensionResource function. | 237 // Tests IsComponentExtensionResource function. |
237 TEST_F(ImageLoadingTrackerTest, IsComponentExtensionResource) { | 238 TEST_F(ImageLoadingTrackerTest, IsComponentExtensionResource) { |
238 scoped_refptr<Extension> extension(CreateExtension( | 239 scoped_refptr<Extension> extension(CreateExtension( |
239 "file_manager", Extension::COMPONENT)); | 240 "file_manager", Extension::COMPONENT)); |
240 ASSERT_TRUE(extension.get() != NULL); | 241 ASSERT_TRUE(extension.get() != NULL); |
241 | 242 |
242 ExtensionResource resource = | 243 ExtensionResource resource = |
243 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_BITTY, | 244 extension->GetIconResource(extension_misc::EXTENSION_ICON_BITTY, |
244 ExtensionIconSet::MATCH_EXACTLY); | 245 ExtensionIconSet::MATCH_EXACTLY); |
245 | 246 |
246 #if defined(FILE_MANAGER_EXTENSION) | 247 #if defined(FILE_MANAGER_EXTENSION) |
247 ImageLoadingTracker loader(this); | 248 ImageLoadingTracker loader(this); |
248 int resource_id; | 249 int resource_id; |
249 ASSERT_EQ(true, | 250 ASSERT_EQ(true, |
250 loader.IsComponentExtensionResource(extension.get(), | 251 loader.IsComponentExtensionResource(extension.get(), |
251 resource, | 252 resource, |
252 resource_id)); | 253 resource_id)); |
253 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | 254 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); |
254 #endif | 255 #endif |
255 } | 256 } |
OLD | NEW |