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