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

Side by Side Diff: chrome/browser/extensions/api/extension_action/browser_action_apitest.cc

Issue 10855154: Update browserAction.setIcon and pageAction.setIcon for hidpi (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/api/extension_action/extension_actions_api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_apitest.h" 12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_tab_util.h" 14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_commands.h" 17 #include "chrome/browser/ui/browser_commands.h"
18 #include "chrome/browser/ui/browser_tabstrip.h" 18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/common/extensions/extension_action.h" 21 #include "chrome/common/extensions/extension_action.h"
22 #include "chrome/common/url_constants.h" 22 #include "chrome/common/url_constants.h"
23 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
24 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
26 #include "content/public/test/browser_test_utils.h" 26 #include "content/public/test/browser_test_utils.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/rect.h" 29 #include "ui/gfx/rect.h"
28 #include "ui/gfx/size.h" 30 #include "ui/gfx/size.h"
31 #include "ui/gfx/image/image_skia.h"
32 #include "ui/gfx/image/image_skia_operations.h"
33 #include "ui/gfx/skia_util.h"
34
35 #if defined (OS_MACOSX)
36 #include "ui/gfx/image/image_unittest_util.h"
37 #endif
29 38
30 using content::WebContents; 39 using content::WebContents;
31 using extensions::Extension; 40 using extensions::Extension;
32 41
42 namespace {
43
44 const char kEmptyImageDataError[] =
45 "The imageData property must contain an ImageData object or dictionary "
46 "of ImageData objects.";
47 const char kEmptyPathError[] = "The path property must not be empty.";
48
49 // Views implementation of browser action button will return icon whose
50 // background will be set.
51 gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) {
52 #if defined(TOOLKIT_VIEWS)
53 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
54 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION);
55 return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon);
56 #endif
57
58 return icon;
59 }
60
61 bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
62 const gfx::ImageSkia& i2,
63 ui::ScaleFactor scale_factor) {
64 SkBitmap bitmap1 = i1.GetRepresentation(scale_factor).sk_bitmap();
65 SkBitmap bitmap2 = i2.GetRepresentation(scale_factor).sk_bitmap();
66 return gfx::BitmapsAreEqual(bitmap1, bitmap2);
67 }
68
33 class BrowserActionApiTest : public ExtensionApiTest { 69 class BrowserActionApiTest : public ExtensionApiTest {
34 public: 70 public:
35 BrowserActionApiTest() {} 71 BrowserActionApiTest() {}
36 virtual ~BrowserActionApiTest() {} 72 virtual ~BrowserActionApiTest() {}
37 73
38 protected: 74 protected:
39 BrowserActionTestUtil GetBrowserActionsBar() { 75 BrowserActionTestUtil GetBrowserActionsBar() {
40 return BrowserActionTestUtil(browser()); 76 return BrowserActionTestUtil(browser());
41 } 77 }
42 78
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 L" window.domAutomationController.send(true)}}, 100)", 127 L" window.domAutomationController.send(true)}}, 100)",
92 &result)); 128 &result));
93 ASSERT_TRUE(result); 129 ASSERT_TRUE(result);
94 } 130 }
95 131
96 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { 132 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
97 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; 133 ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_;
98 const Extension* extension = GetSingleLoadedExtension(); 134 const Extension* extension = GetSingleLoadedExtension();
99 ASSERT_TRUE(extension) << message_; 135 ASSERT_TRUE(extension) << message_;
100 136
137 #if defined (OS_MACOSX)
138 // We need this on mac so we don't loose 2x representations from browser icon
139 // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia.
140 gfx::test::SetSupportedScaleFactorsTo1xAnd2x();
141 #endif
142
101 // Test that there is a browser action in the toolbar. 143 // Test that there is a browser action in the toolbar.
102 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); 144 ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
103 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); 145 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
104 146
105 // Set prev_id which holds the id of the previous image, and use it in the 147 gfx::Image action_icon = extension->browser_action()->GetIcon(0);
106 // next test to see if the image changes. 148 uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID();
107 uint32_t prev_id = extension->browser_action()->GetIcon(0).
108 ToSkBitmap()->getGenerationID();
109 149
110 // Tell the extension to update the icon using setIcon({imageData:...}). 150 // Let's check that |GetIcon| doesn't always return bitmap with new id.
151 ASSERT_EQ(action_icon_last_id,
152 extension->browser_action()->GetIcon(0).ToSkBitmap()->
153 getGenerationID());
154
155 uint32_t action_icon_current_id = 0;
156
111 ResultCatcher catcher; 157 ResultCatcher catcher;
112 ui_test_utils::NavigateToURL(browser(), 158
113 GURL(extension->GetResourceURL("update.html"))); 159 // Tell the extension to update the icon using ImageData object.
160 GetBrowserActionsBar().Press(0);
114 ASSERT_TRUE(catcher.GetNextResult()); 161 ASSERT_TRUE(catcher.GetNextResult());
115 162
116 // Test that we received the changes. 163 action_icon = extension->browser_action()->GetIcon(0);
117 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
118 EXPECT_NE(prev_id,
119 extension->browser_action()->GetIcon(0).
120 ToSkBitmap()->getGenerationID());
121 prev_id = extension->browser_action()->GetIcon(0).
122 ToSkBitmap()->getGenerationID();
123 164
124 // Tell the extension to update the icon using setIcon({path:...}). 165 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
125 ui_test_utils::NavigateToURL(browser(), 166 EXPECT_GT(action_icon_current_id, action_icon_last_id);
126 GURL(extension->GetResourceURL("update2.html"))); 167 action_icon_last_id = action_icon_current_id;
168
169 EXPECT_FALSE(
170 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
171
172 EXPECT_TRUE(ImagesAreEqualAtScale(
173 AddBackgroundForViews(*action_icon.ToImageSkia()),
174 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
175 ui::SCALE_FACTOR_100P));
176
177 // Tell the extension to update the icon using path.
178 GetBrowserActionsBar().Press(0);
127 ASSERT_TRUE(catcher.GetNextResult()); 179 ASSERT_TRUE(catcher.GetNextResult());
128 EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0)); 180
129 EXPECT_NE(prev_id, 181 action_icon = extension->browser_action()->GetIcon(0);
130 extension->browser_action()->GetIcon(0). 182
131 ToSkBitmap()->getGenerationID()); 183 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
184 EXPECT_GT(action_icon_current_id, action_icon_last_id);
185 action_icon_last_id = action_icon_current_id;
186
187 EXPECT_FALSE(
188 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
189
190 EXPECT_TRUE(ImagesAreEqualAtScale(
191 AddBackgroundForViews(*action_icon.ToImageSkia()),
192 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
193 ui::SCALE_FACTOR_100P));
194
195 // Tell the extension to update the icon using dictionary of ImageData
196 // objects.
197 GetBrowserActionsBar().Press(0);
198 ASSERT_TRUE(catcher.GetNextResult());
199
200 action_icon = extension->browser_action()->GetIcon(0);
201
202 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
203 EXPECT_GT(action_icon_current_id, action_icon_last_id);
204 action_icon_last_id = action_icon_current_id;
205
206 EXPECT_TRUE(
207 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
208
209 EXPECT_TRUE(ImagesAreEqualAtScale(
210 AddBackgroundForViews(*action_icon.ToImageSkia()),
211 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
212 ui::SCALE_FACTOR_100P));
213
214 // Tell the extension to update the icon using dictionary of paths.
215 GetBrowserActionsBar().Press(0);
216 ASSERT_TRUE(catcher.GetNextResult());
217
218 action_icon = extension->browser_action()->GetIcon(0);
219
220 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
221 EXPECT_GT(action_icon_current_id, action_icon_last_id);
222 action_icon_last_id = action_icon_current_id;
223
224 EXPECT_TRUE(
225 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
226
227 EXPECT_TRUE(ImagesAreEqualAtScale(
228 AddBackgroundForViews(*action_icon.ToImageSkia()),
229 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
230 ui::SCALE_FACTOR_100P));
231
232 // Tell the extension to update the icon using dictionary of ImageData
233 // objects, but setting only size 19.
234 GetBrowserActionsBar().Press(0);
235 ASSERT_TRUE(catcher.GetNextResult());
236
237 action_icon = extension->browser_action()->GetIcon(0);
238
239 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
240 EXPECT_GT(action_icon_current_id, action_icon_last_id);
241 action_icon_last_id = action_icon_current_id;
242
243 EXPECT_FALSE(
244 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
245
246 EXPECT_TRUE(ImagesAreEqualAtScale(
247 AddBackgroundForViews(*action_icon.ToImageSkia()),
248 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
249 ui::SCALE_FACTOR_100P));
250
251 // Tell the extension to update the icon using dictionary of paths, but
252 // setting only size 19.
253 GetBrowserActionsBar().Press(0);
254 ASSERT_TRUE(catcher.GetNextResult());
255
256 action_icon = extension->browser_action()->GetIcon(0);
257
258 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
259 EXPECT_GT(action_icon_current_id, action_icon_last_id);
260 action_icon_last_id = action_icon_current_id;
261
262 EXPECT_FALSE(
263 action_icon.ToImageSkia()->HasRepresentation(ui::SCALE_FACTOR_200P));
264
265 EXPECT_TRUE(ImagesAreEqualAtScale(
266 AddBackgroundForViews(*action_icon.ToImageSkia()),
267 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
268 ui::SCALE_FACTOR_100P));
269
270 // Tell the extension to update the icon using dictionary of ImageData
271 // objects, but setting only size 38.
272 GetBrowserActionsBar().Press(0);
273 ASSERT_TRUE(catcher.GetNextResult());
274
275 action_icon = extension->browser_action()->GetIcon(0);
276
277 const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia();
278
279 EXPECT_FALSE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_100P));
280 EXPECT_TRUE(action_icon_skia->HasRepresentation(ui::SCALE_FACTOR_200P));
281
282 action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
283 EXPECT_GT(action_icon_current_id, action_icon_last_id);
284 action_icon_last_id = action_icon_current_id;
285
286 EXPECT_TRUE(gfx::BitmapsAreEqual(
287 *action_icon.ToSkBitmap(),
288 action_icon_skia->GetRepresentation(ui::SCALE_FACTOR_200P).sk_bitmap()));
289
290 EXPECT_TRUE(ImagesAreEqualAtScale(
291 AddBackgroundForViews(*action_icon_skia),
292 *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
293 ui::SCALE_FACTOR_200P));
294
295 // Try setting icon with empty dictionary of ImageData objects.
296 GetBrowserActionsBar().Press(0);
297 ASSERT_FALSE(catcher.GetNextResult());
298 EXPECT_EQ(kEmptyImageDataError, catcher.message());
299
300 // Try setting icon with empty dictionary of path objects.
301 GetBrowserActionsBar().Press(0);
302 ASSERT_FALSE(catcher.GetNextResult());
303 EXPECT_EQ(kEmptyPathError, catcher.message());
132 } 304 }
133 305
134 // This test is flaky as per http://crbug.com/74557. 306 // This test is flaky as per http://crbug.com/74557.
135 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, 307 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
136 DISABLED_TabSpecificBrowserActionState) { 308 DISABLED_TabSpecificBrowserActionState) {
137 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) << 309 ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) <<
138 message_; 310 message_;
139 const Extension* extension = GetSingleLoadedExtension(); 311 const Extension* extension = GetSingleLoadedExtension();
140 ASSERT_TRUE(extension) << message_; 312 ASSERT_TRUE(extension) << message_;
141 313
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 ResultCatcher catcher; 642 ResultCatcher catcher;
471 ui_test_utils::NavigateToURL(browser(), 643 ui_test_utils::NavigateToURL(browser(),
472 GURL(extension->GetResourceURL("update.html"))); 644 GURL(extension->GetResourceURL("update.html")));
473 ASSERT_TRUE(catcher.GetNextResult()); 645 ASSERT_TRUE(catcher.GetNextResult());
474 646
475 // Test the getters for a specific tab. 647 // Test the getters for a specific tab.
476 ui_test_utils::NavigateToURL(browser(), 648 ui_test_utils::NavigateToURL(browser(),
477 GURL(extension->GetResourceURL("update2.html"))); 649 GURL(extension->GetResourceURL("update2.html")));
478 ASSERT_TRUE(catcher.GetNextResult()); 650 ASSERT_TRUE(catcher.GetNextResult());
479 } 651 }
652
653 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/extension_action/extension_actions_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698