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_icon_set.h" | 12 #include "chrome/common/extensions/extension_icon_set.h" |
13 #include "chrome/common/extensions/extension_resource.h" | 13 #include "chrome/common/extensions/extension_resource.h" |
14 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
15 #include "content/test/test_browser_thread.h" | 15 #include "content/test/test_browser_thread.h" |
16 #include "grit/component_extension_resources.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
19 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 | 23 |
23 class ImageLoadingTrackerTest : public testing::Test, | 24 class ImageLoadingTrackerTest : public testing::Test, |
24 public ImageLoadingTracker::Observer { | 25 public ImageLoadingTracker::Observer { |
25 public: | 26 public: |
(...skipping 19 matching lines...) Expand all Loading... | |
45 MessageLoop::current()->Run(); | 46 MessageLoop::current()->Run(); |
46 quit_in_image_loaded_ = false; | 47 quit_in_image_loaded_ = false; |
47 } | 48 } |
48 | 49 |
49 int image_loaded_count() { | 50 int image_loaded_count() { |
50 int result = image_loaded_count_; | 51 int result = image_loaded_count_; |
51 image_loaded_count_ = 0; | 52 image_loaded_count_ = 0; |
52 return result; | 53 return result; |
53 } | 54 } |
54 | 55 |
55 scoped_refptr<Extension> CreateExtension() { | 56 scoped_refptr<Extension> CreateExtension(const char* name, |
57 Extension::Location location) { | |
Finnur
2012/04/11 17:38:13
nit: Indentation.
dgozman
2012/04/12 11:47:24
Done.
| |
56 // Create and load an extension. | 58 // Create and load an extension. |
57 FilePath test_file; | 59 FilePath test_file; |
58 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { | 60 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_file)) { |
59 EXPECT_FALSE(true); | 61 EXPECT_FALSE(true); |
60 return NULL; | 62 return NULL; |
61 } | 63 } |
62 test_file = test_file.AppendASCII("extensions") | 64 test_file = test_file.AppendASCII("extensions") |
63 .AppendASCII("image_loading_tracker"); | 65 .AppendASCII(name); |
64 int error_code = 0; | 66 int error_code = 0; |
65 std::string error; | 67 std::string error; |
66 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); | 68 JSONFileValueSerializer serializer(test_file.AppendASCII("app.json")); |
67 scoped_ptr<DictionaryValue> valid_value( | 69 scoped_ptr<DictionaryValue> valid_value( |
68 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, | 70 static_cast<DictionaryValue*>(serializer.Deserialize(&error_code, |
69 &error))); | 71 &error))); |
70 EXPECT_EQ(0, error_code) << error; | 72 EXPECT_EQ(0, error_code) << error; |
71 if (error_code != 0) | 73 if (error_code != 0) |
72 return NULL; | 74 return NULL; |
73 | 75 |
74 EXPECT_TRUE(valid_value.get()); | 76 EXPECT_TRUE(valid_value.get()); |
75 if (!valid_value.get()) | 77 if (!valid_value.get()) |
76 return NULL; | 78 return NULL; |
77 | 79 |
78 return Extension::Create(test_file, Extension::INVALID, *valid_value, | 80 return Extension::Create(test_file, location, *valid_value, |
79 Extension::STRICT_ERROR_CHECKS, &error); | 81 Extension::STRICT_ERROR_CHECKS, &error); |
80 } | 82 } |
81 | 83 |
82 gfx::Image image_; | 84 gfx::Image image_; |
83 | 85 |
86 bool IsComponentExtensionResource(const Extension* extension, | |
87 const ExtensionResource& resource, | |
88 int& resource_id, | |
89 const ImageLoadingTracker* tracker) { | |
90 return | |
91 tracker->IsComponentExtensionResource(extension, resource, resource_id); | |
92 } | |
93 | |
84 private: | 94 private: |
85 virtual void SetUp() { | 95 virtual void SetUp() { |
86 file_thread_.Start(); | 96 file_thread_.Start(); |
87 io_thread_.Start(); | 97 io_thread_.Start(); |
88 } | 98 } |
89 | 99 |
90 int image_loaded_count_; | 100 int image_loaded_count_; |
91 bool quit_in_image_loaded_; | 101 bool quit_in_image_loaded_; |
92 MessageLoop ui_loop_; | 102 MessageLoop ui_loop_; |
93 content::TestBrowserThread ui_thread_; | 103 content::TestBrowserThread ui_thread_; |
94 content::TestBrowserThread file_thread_; | 104 content::TestBrowserThread file_thread_; |
95 content::TestBrowserThread io_thread_; | 105 content::TestBrowserThread io_thread_; |
96 }; | 106 }; |
97 | 107 |
98 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. | 108 // Tests asking ImageLoadingTracker to cache pushes the result to the Extension. |
99 TEST_F(ImageLoadingTrackerTest, Cache) { | 109 TEST_F(ImageLoadingTrackerTest, Cache) { |
100 scoped_refptr<Extension> extension(CreateExtension()); | 110 scoped_refptr<Extension> extension(CreateExtension( |
111 "image_loading_tracker", Extension::INVALID)); | |
101 ASSERT_TRUE(extension.get() != NULL); | 112 ASSERT_TRUE(extension.get() != NULL); |
102 | 113 |
103 ExtensionResource image_resource = | 114 ExtensionResource image_resource = |
104 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 115 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
105 ExtensionIconSet::MATCH_EXACTLY); | 116 ExtensionIconSet::MATCH_EXACTLY); |
106 gfx::Size max_size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 117 gfx::Size max_size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
107 ExtensionIconSet::EXTENSION_ICON_SMALLISH); | 118 ExtensionIconSet::EXTENSION_ICON_SMALLISH); |
108 ImageLoadingTracker loader(this); | 119 ImageLoadingTracker loader(this); |
109 loader.LoadImage(extension.get(), | 120 loader.LoadImage(extension.get(), |
110 image_resource, | 121 image_resource, |
(...skipping 28 matching lines...) Expand all Loading... | |
139 EXPECT_EQ(1, image_loaded_count()); | 150 EXPECT_EQ(1, image_loaded_count()); |
140 | 151 |
141 // Check that the image was loaded. | 152 // Check that the image was loaded. |
142 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 153 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
143 image_.ToSkBitmap()->width()); | 154 image_.ToSkBitmap()->width()); |
144 } | 155 } |
145 | 156 |
146 // Tests deleting an extension while waiting for the image to load doesn't cause | 157 // Tests deleting an extension while waiting for the image to load doesn't cause |
147 // problems. | 158 // problems. |
148 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { | 159 TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) { |
149 scoped_refptr<Extension> extension(CreateExtension()); | 160 scoped_refptr<Extension> extension(CreateExtension( |
161 "image_loading_tracker", Extension::INVALID)); | |
150 ASSERT_TRUE(extension.get() != NULL); | 162 ASSERT_TRUE(extension.get() != NULL); |
151 | 163 |
152 ExtensionResource image_resource = | 164 ExtensionResource image_resource = |
153 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 165 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
154 ExtensionIconSet::MATCH_EXACTLY); | 166 ExtensionIconSet::MATCH_EXACTLY); |
155 ImageLoadingTracker loader(this); | 167 ImageLoadingTracker loader(this); |
156 loader.LoadImage(extension.get(), | 168 loader.LoadImage(extension.get(), |
157 image_resource, | 169 image_resource, |
158 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 170 gfx::Size(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
159 ExtensionIconSet::EXTENSION_ICON_SMALLISH), | 171 ExtensionIconSet::EXTENSION_ICON_SMALLISH), |
(...skipping 20 matching lines...) Expand all Loading... | |
180 // We should still have gotten the image. | 192 // We should still have gotten the image. |
181 EXPECT_EQ(1, image_loaded_count()); | 193 EXPECT_EQ(1, image_loaded_count()); |
182 | 194 |
183 // Check that the image was loaded. | 195 // Check that the image was loaded. |
184 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 196 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
185 image_.ToSkBitmap()->width()); | 197 image_.ToSkBitmap()->width()); |
186 } | 198 } |
187 | 199 |
188 // Tests loading multiple dimensions of the same image. | 200 // Tests loading multiple dimensions of the same image. |
189 TEST_F(ImageLoadingTrackerTest, MultipleImages) { | 201 TEST_F(ImageLoadingTrackerTest, MultipleImages) { |
190 scoped_refptr<Extension> extension(CreateExtension()); | 202 scoped_refptr<Extension> extension(CreateExtension( |
203 "image_loading_tracker", Extension::INVALID)); | |
191 ASSERT_TRUE(extension.get() != NULL); | 204 ASSERT_TRUE(extension.get() != NULL); |
192 | 205 |
193 std::vector<ImageLoadingTracker::ImageInfo> info_list; | 206 std::vector<ImageLoadingTracker::ImageInfo> info_list; |
194 int sizes[] = {ExtensionIconSet::EXTENSION_ICON_SMALLISH, | 207 int sizes[] = {ExtensionIconSet::EXTENSION_ICON_SMALLISH, |
195 ExtensionIconSet::EXTENSION_ICON_BITTY}; | 208 ExtensionIconSet::EXTENSION_ICON_BITTY}; |
196 for (size_t i = 0; i < arraysize(sizes); ++i) { | 209 for (size_t i = 0; i < arraysize(sizes); ++i) { |
197 ExtensionResource resource = | 210 ExtensionResource resource = |
198 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); | 211 extension->GetIconResource(sizes[i], ExtensionIconSet::MATCH_EXACTLY); |
199 info_list.push_back(ImageLoadingTracker::ImageInfo( | 212 info_list.push_back(ImageLoadingTracker::ImageInfo( |
200 resource, gfx::Size(sizes[i], sizes[i]))); | 213 resource, gfx::Size(sizes[i], sizes[i]))); |
(...skipping 13 matching lines...) Expand all Loading... | |
214 // Check that all images were loaded. | 227 // Check that all images were loaded. |
215 ASSERT_EQ(2u, image_.GetNumberOfSkBitmaps()); | 228 ASSERT_EQ(2u, image_.GetNumberOfSkBitmaps()); |
216 const SkBitmap* bmp1 = image_.GetSkBitmapAtIndex(0); | 229 const SkBitmap* bmp1 = image_.GetSkBitmapAtIndex(0); |
217 const SkBitmap* bmp2 = image_.GetSkBitmapAtIndex(1); | 230 const SkBitmap* bmp2 = image_.GetSkBitmapAtIndex(1); |
218 if (bmp1->width() > bmp2->width()) { | 231 if (bmp1->width() > bmp2->width()) { |
219 std::swap(bmp1, bmp2); | 232 std::swap(bmp1, bmp2); |
220 } | 233 } |
221 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY, bmp1->width()); | 234 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_BITTY, bmp1->width()); |
222 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, bmp2->width()); | 235 EXPECT_EQ(ExtensionIconSet::EXTENSION_ICON_SMALLISH, bmp2->width()); |
223 } | 236 } |
237 | |
238 // Tests loading multiple dimensions of the same image. | |
239 TEST_F(ImageLoadingTrackerTest, IsComponentExtensionResource) { | |
240 scoped_refptr<Extension> extension(CreateExtension( | |
241 "file_manager", Extension::COMPONENT)); | |
242 ASSERT_TRUE(extension.get() != NULL); | |
243 | |
244 ExtensionResource resource = | |
245 extension->GetIconResource(ExtensionIconSet::EXTENSION_ICON_BITTY, | |
246 ExtensionIconSet::MATCH_EXACTLY); | |
247 | |
248 ImageLoadingTracker loader(this); | |
249 int resource_id; | |
250 ASSERT_EQ(true, | |
251 IsComponentExtensionResource(extension.get(), | |
252 resource, | |
253 resource_id, | |
254 &loader)); | |
255 ASSERT_EQ(IDR_FILE_MANAGER_ICON_16, resource_id); | |
256 } | |
OLD | NEW |