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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "chrome/browser/extensions/browser_action_test_util.h" | 11 #include "chrome/browser/extensions/browser_action_test_util.h" |
| 12 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
12 #include "chrome/browser/extensions/extension_apitest.h" | 13 #include "chrome/browser/extensions/extension_apitest.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/extensions/extension_tab_util.h" | 15 #include "chrome/browser/extensions/extension_tab_util.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_commands.h" | 18 #include "chrome/browser/ui/browser_commands.h" |
18 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
19 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/extensions/extension_action.h" | 22 #include "chrome/common/extensions/extension_action.h" |
22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
24 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
26 #include "content/public/test/browser_test_utils.h" | 27 #include "content/public/test/browser_test_utils.h" |
27 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
29 #include "ui/gfx/rect.h" | 31 #include "ui/gfx/rect.h" |
30 #include "ui/gfx/size.h" | 32 #include "ui/gfx/size.h" |
31 #include "ui/gfx/image/image_skia.h" | 33 #include "ui/gfx/image/image_skia.h" |
32 #include "ui/gfx/image/image_skia_operations.h" | 34 #include "ui/gfx/image/image_skia_operations.h" |
33 #include "ui/gfx/skia_util.h" | 35 #include "ui/gfx/skia_util.h" |
34 | 36 |
35 #if defined (OS_MACOSX) | 37 #if defined (OS_MACOSX) |
36 #include "ui/gfx/image/image_unittest_util.h" | 38 #include "ui/gfx/image/image_unittest_util.h" |
37 #endif | 39 #endif |
38 | 40 |
| 41 using content::BrowserThread; |
39 using content::WebContents; | 42 using content::WebContents; |
40 using extensions::Extension; | 43 using extensions::Extension; |
| 44 using ::testing::_; |
41 | 45 |
42 namespace { | 46 namespace { |
43 | 47 |
44 const char kEmptyImageDataError[] = | 48 const char kEmptyImageDataError[] = |
45 "The imageData property must contain an ImageData object or dictionary " | 49 "The imageData property must contain an ImageData object or dictionary " |
46 "of ImageData objects."; | 50 "of ImageData objects."; |
47 const char kEmptyPathError[] = "The path property must not be empty."; | 51 const char kEmptyPathError[] = "The path property must not be empty."; |
48 | 52 |
49 // Views implementation of browser action button will return icon whose | 53 // Views implementation of browser action button will return icon whose |
50 // background will be set. | 54 // background will be set. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; | 137 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; |
134 const Extension* extension = GetSingleLoadedExtension(); | 138 const Extension* extension = GetSingleLoadedExtension(); |
135 ASSERT_TRUE(extension) << message_; | 139 ASSERT_TRUE(extension) << message_; |
136 | 140 |
137 #if defined (OS_MACOSX) | 141 #if defined (OS_MACOSX) |
138 // We need this on mac so we don't loose 2x representations from browser icon | 142 // We need this on mac so we don't loose 2x representations from browser icon |
139 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia. | 143 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia. |
140 gfx::test::SetSupportedScaleFactorsTo1xAnd2x(); | 144 gfx::test::SetSupportedScaleFactorsTo1xAnd2x(); |
141 #endif | 145 #endif |
142 | 146 |
| 147 // We should not be creating icons asynchronoulsy, so we don't need an |
| 148 // observer. |
| 149 ExtensionActionIconFactory icon_factory(extension, NULL); |
| 150 |
143 // Test that there is a browser action in the toolbar. | 151 // Test that there is a browser action in the toolbar. |
144 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); | 152 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); |
145 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); | 153 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); |
146 | 154 |
147 gfx::Image action_icon = extension->browser_action()->GetIcon(0); | 155 gfx::Image action_icon = |
| 156 icon_factory.GetIcon(extension->browser_action(), 0); |
148 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID(); | 157 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID(); |
149 | 158 |
150 // Let's check that |GetIcon| doesn't always return bitmap with new id. | 159 // Let's check that |GetIcon| doesn't always return bitmap with new id. |
151 ASSERT_EQ(action_icon_last_id, | 160 ASSERT_EQ(action_icon_last_id, |
152 extension->browser_action()->GetIcon(0).ToSkBitmap()-> | 161 icon_factory.GetIcon(extension->browser_action(), 0).ToSkBitmap()-> |
153 getGenerationID()); | 162 getGenerationID()); |
154 | 163 |
155 uint32_t action_icon_current_id = 0; | 164 uint32_t action_icon_current_id = 0; |
156 | 165 |
157 ResultCatcher catcher; | 166 ResultCatcher catcher; |
158 | 167 |
159 // Tell the extension to update the icon using ImageData object. | 168 // Tell the extension to update the icon using ImageData object. |
160 GetBrowserActionsBar().Press(0); | 169 GetBrowserActionsBar().Press(0); |
161 ASSERT_TRUE(catcher.GetNextResult()); | 170 ASSERT_TRUE(catcher.GetNextResult()); |
162 | 171 |
163 action_icon = extension->browser_action()->GetIcon(0); | 172 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
164 | 173 |
165 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 174 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
166 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 175 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
167 action_icon_last_id = action_icon_current_id; | 176 action_icon_last_id = action_icon_current_id; |
168 | 177 |
169 EXPECT_FALSE( | 178 EXPECT_FALSE( |
170 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 179 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
171 | 180 |
172 EXPECT_TRUE(ImagesAreEqualAtScale( | 181 EXPECT_TRUE(ImagesAreEqualAtScale( |
173 AddBackgroundForViews(*action_icon.ToImageSkia()), | 182 AddBackgroundForViews(*action_icon.ToImageSkia()), |
174 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 183 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
175 ui::SCALE_FACTOR_100P)); | 184 ui::SCALE_FACTOR_100P)); |
176 | 185 |
177 // Tell the extension to update the icon using path. | 186 // Tell the extension to update the icon using path. |
178 GetBrowserActionsBar().Press(0); | 187 GetBrowserActionsBar().Press(0); |
179 ASSERT_TRUE(catcher.GetNextResult()); | 188 ASSERT_TRUE(catcher.GetNextResult()); |
180 | 189 |
181 action_icon = extension->browser_action()->GetIcon(0); | 190 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
182 | 191 |
183 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 192 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
184 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 193 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
185 action_icon_last_id = action_icon_current_id; | 194 action_icon_last_id = action_icon_current_id; |
186 | 195 |
187 EXPECT_FALSE( | 196 EXPECT_FALSE( |
188 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 197 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
189 | 198 |
190 EXPECT_TRUE(ImagesAreEqualAtScale( | 199 EXPECT_TRUE(ImagesAreEqualAtScale( |
191 AddBackgroundForViews(*action_icon.ToImageSkia()), | 200 AddBackgroundForViews(*action_icon.ToImageSkia()), |
192 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 201 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
193 ui::SCALE_FACTOR_100P)); | 202 ui::SCALE_FACTOR_100P)); |
194 | 203 |
195 // Tell the extension to update the icon using dictionary of ImageData | 204 // Tell the extension to update the icon using dictionary of ImageData |
196 // objects. | 205 // objects. |
197 GetBrowserActionsBar().Press(0); | 206 GetBrowserActionsBar().Press(0); |
198 ASSERT_TRUE(catcher.GetNextResult()); | 207 ASSERT_TRUE(catcher.GetNextResult()); |
199 | 208 |
200 action_icon = extension->browser_action()->GetIcon(0); | 209 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
201 | 210 |
202 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 211 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
203 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 212 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
204 action_icon_last_id = action_icon_current_id; | 213 action_icon_last_id = action_icon_current_id; |
205 | 214 |
206 EXPECT_TRUE( | 215 EXPECT_TRUE( |
207 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 216 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
208 | 217 |
209 EXPECT_TRUE(ImagesAreEqualAtScale( | 218 EXPECT_TRUE(ImagesAreEqualAtScale( |
210 AddBackgroundForViews(*action_icon.ToImageSkia()), | 219 AddBackgroundForViews(*action_icon.ToImageSkia()), |
211 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 220 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
212 ui::SCALE_FACTOR_100P)); | 221 ui::SCALE_FACTOR_100P)); |
213 | 222 |
214 // Tell the extension to update the icon using dictionary of paths. | 223 // Tell the extension to update the icon using dictionary of paths. |
215 GetBrowserActionsBar().Press(0); | 224 GetBrowserActionsBar().Press(0); |
216 ASSERT_TRUE(catcher.GetNextResult()); | 225 ASSERT_TRUE(catcher.GetNextResult()); |
217 | 226 |
218 action_icon = extension->browser_action()->GetIcon(0); | 227 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
219 | 228 |
220 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 229 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
221 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 230 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
222 action_icon_last_id = action_icon_current_id; | 231 action_icon_last_id = action_icon_current_id; |
223 | 232 |
224 EXPECT_TRUE( | 233 EXPECT_TRUE( |
225 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 234 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
226 | 235 |
227 EXPECT_TRUE(ImagesAreEqualAtScale( | 236 EXPECT_TRUE(ImagesAreEqualAtScale( |
228 AddBackgroundForViews(*action_icon.ToImageSkia()), | 237 AddBackgroundForViews(*action_icon.ToImageSkia()), |
229 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 238 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
230 ui::SCALE_FACTOR_100P)); | 239 ui::SCALE_FACTOR_100P)); |
231 | 240 |
232 // Tell the extension to update the icon using dictionary of ImageData | 241 // Tell the extension to update the icon using dictionary of ImageData |
233 // objects, but setting only size 19. | 242 // objects, but setting only size 19. |
234 GetBrowserActionsBar().Press(0); | 243 GetBrowserActionsBar().Press(0); |
235 ASSERT_TRUE(catcher.GetNextResult()); | 244 ASSERT_TRUE(catcher.GetNextResult()); |
236 | 245 |
237 action_icon = extension->browser_action()->GetIcon(0); | 246 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
238 | 247 |
239 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 248 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
240 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 249 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
241 action_icon_last_id = action_icon_current_id; | 250 action_icon_last_id = action_icon_current_id; |
242 | 251 |
243 EXPECT_FALSE( | 252 EXPECT_FALSE( |
244 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 253 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
245 | 254 |
246 EXPECT_TRUE(ImagesAreEqualAtScale( | 255 EXPECT_TRUE(ImagesAreEqualAtScale( |
247 AddBackgroundForViews(*action_icon.ToImageSkia()), | 256 AddBackgroundForViews(*action_icon.ToImageSkia()), |
248 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 257 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
249 ui::SCALE_FACTOR_100P)); | 258 ui::SCALE_FACTOR_100P)); |
250 | 259 |
251 // Tell the extension to update the icon using dictionary of paths, but | 260 // Tell the extension to update the icon using dictionary of paths, but |
252 // setting only size 19. | 261 // setting only size 19. |
253 GetBrowserActionsBar().Press(0); | 262 GetBrowserActionsBar().Press(0); |
254 ASSERT_TRUE(catcher.GetNextResult()); | 263 ASSERT_TRUE(catcher.GetNextResult()); |
255 | 264 |
256 action_icon = extension->browser_action()->GetIcon(0); | 265 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
257 | 266 |
258 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 267 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
259 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 268 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
260 action_icon_last_id = action_icon_current_id; | 269 action_icon_last_id = action_icon_current_id; |
261 | 270 |
262 EXPECT_FALSE( | 271 EXPECT_FALSE( |
263 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); | 272 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P)); |
264 | 273 |
265 EXPECT_TRUE(ImagesAreEqualAtScale( | 274 EXPECT_TRUE(ImagesAreEqualAtScale( |
266 AddBackgroundForViews(*action_icon.ToImageSkia()), | 275 AddBackgroundForViews(*action_icon.ToImageSkia()), |
267 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), | 276 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(), |
268 ui::SCALE_FACTOR_100P)); | 277 ui::SCALE_FACTOR_100P)); |
269 | 278 |
270 // Tell the extension to update the icon using dictionary of ImageData | 279 // Tell the extension to update the icon using dictionary of ImageData |
271 // objects, but setting only size 38. | 280 // objects, but setting only size 38. |
272 GetBrowserActionsBar().Press(0); | 281 GetBrowserActionsBar().Press(0); |
273 ASSERT_TRUE(catcher.GetNextResult()); | 282 ASSERT_TRUE(catcher.GetNextResult()); |
274 | 283 |
275 action_icon = extension->browser_action()->GetIcon(0); | 284 action_icon = icon_factory.GetIcon(extension->browser_action(), 0); |
276 | 285 |
277 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia(); | 286 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia(); |
278 | 287 |
279 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P)); | 288 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P)); |
280 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P)); | 289 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P)); |
281 | 290 |
282 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); | 291 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID(); |
283 EXPECT_GT(action_icon_current_id, action_icon_last_id); | 292 EXPECT_GT(action_icon_current_id, action_icon_last_id); |
284 action_icon_last_id = action_icon_current_id; | 293 action_icon_last_id = action_icon_current_id; |
285 | 294 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 GURL(extension->GetResourceURL("update.html"))); | 653 GURL(extension->GetResourceURL("update.html"))); |
645 ASSERT_TRUE(catcher.GetNextResult()); | 654 ASSERT_TRUE(catcher.GetNextResult()); |
646 | 655 |
647 // Test the getters for a specific tab. | 656 // Test the getters for a specific tab. |
648 ui_test_utils::NavigateToURL(browser(), | 657 ui_test_utils::NavigateToURL(browser(), |
649 GURL(extension->GetResourceURL("update2.html"))); | 658 GURL(extension->GetResourceURL("update2.html"))); |
650 ASSERT_TRUE(catcher.GetNextResult()); | 659 ASSERT_TRUE(catcher.GetNextResult()); |
651 } | 660 } |
652 | 661 |
653 } // namespace | 662 } // namespace |
OLD | NEW |