Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / extension_action / browser_action_apitest.cc
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
3 // found in the LICENSE file.
4
5 #include "build/build_config.h"
6 #include "chrome/browser/extensions/browser_action_test_util.h"
7 #include "chrome/browser/extensions/extension_action.h"
8 #include "chrome/browser/extensions/extension_action_icon_factory.h"
9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_apitest.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_tab_util.h"
13 #include "chrome/browser/extensions/extension_toolbar_model.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_commands.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/notification_types.h"
26 #include "extensions/common/feature_switch.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_operations.h"
31 #include "ui/gfx/rect.h"
32 #include "ui/gfx/size.h"
33 #include "ui/gfx/skia_util.h"
34
35 using content::WebContents;
36
37 namespace extensions {
38 namespace {
39
40 const char kEmptyImageDataError[] =
41     "The imageData property must contain an ImageData object or dictionary "
42     "of ImageData objects.";
43 const char kEmptyPathError[] = "The path property must not be empty.";
44
45 // Views implementation of browser action button will return icon whose
46 // background will be set.
47 gfx::ImageSkia AddBackgroundForViews(const gfx::ImageSkia& icon) {
48 #if defined(TOOLKIT_VIEWS)
49   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
50   gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION);
51   return gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon);
52 #else
53   return icon;
54 #endif
55 }
56
57 bool ImagesAreEqualAtScale(const gfx::ImageSkia& i1,
58                            const gfx::ImageSkia& i2,
59                            float scale) {
60   SkBitmap bitmap1 = i1.GetRepresentation(scale).sk_bitmap();
61   SkBitmap bitmap2 = i2.GetRepresentation(scale).sk_bitmap();
62   return gfx::BitmapsAreEqual(bitmap1, bitmap2);
63 }
64
65 class BrowserActionApiTest : public ExtensionApiTest {
66  public:
67   BrowserActionApiTest() {}
68   virtual ~BrowserActionApiTest() {}
69
70  protected:
71   BrowserActionTestUtil GetBrowserActionsBar() {
72     return BrowserActionTestUtil(browser());
73   }
74
75   bool OpenPopup(int index) {
76     ResultCatcher catcher;
77     content::WindowedNotificationObserver popup_observer(
78         content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
79         content::NotificationService::AllSources());
80     GetBrowserActionsBar().Press(index);
81     popup_observer.Wait();
82     EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
83     return GetBrowserActionsBar().HasPopup();
84   }
85
86   ExtensionAction* GetBrowserAction(const Extension& extension) {
87     return ExtensionActionManager::Get(browser()->profile())->
88         GetBrowserAction(extension);
89   }
90 };
91
92 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) {
93   ASSERT_TRUE(test_server()->Start());
94   ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
95   const Extension* extension = GetSingleLoadedExtension();
96   ASSERT_TRUE(extension) << message_;
97
98   // Test that there is a browser action in the toolbar.
99   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
100
101   // Tell the extension to update the browser action state.
102   ResultCatcher catcher;
103   ui_test_utils::NavigateToURL(browser(),
104       GURL(extension->GetResourceURL("update.html")));
105   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
106
107   // Test that we received the changes.
108   ExtensionAction* action = GetBrowserAction(*extension);
109   ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId));
110   ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId));
111   ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
112             action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
113
114   // Simulate the browser action being clicked.
115   ui_test_utils::NavigateToURL(browser(),
116       test_server()->GetURL("files/extensions/test_file.txt"));
117
118   ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
119       browser()->profile());
120   toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
121
122   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
123 }
124
125 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) {
126   ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_;
127   const Extension* extension = GetSingleLoadedExtension();
128   ASSERT_TRUE(extension) << message_;
129
130 #if defined (OS_MACOSX)
131   // We need this on mac so we don't loose 2x representations from browser icon
132   // in transformations gfx::ImageSkia -> NSImage -> gfx::ImageSkia.
133   std::vector<ui::ScaleFactor> supported_scale_factors;
134   supported_scale_factors.push_back(ui::SCALE_FACTOR_100P);
135   supported_scale_factors.push_back(ui::SCALE_FACTOR_200P);
136   ui::SetSupportedScaleFactors(supported_scale_factors);
137 #endif
138
139   // We should not be creating icons asynchronously, so we don't need an
140   // observer.
141   ExtensionActionIconFactory icon_factory(
142       profile(),
143       extension,
144       GetBrowserAction(*extension),
145       NULL);
146   // Test that there is a browser action in the toolbar.
147   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
148   EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
149
150   gfx::Image action_icon = icon_factory.GetIcon(0);
151   uint32_t action_icon_last_id = action_icon.ToSkBitmap()->getGenerationID();
152
153   // Let's check that |GetIcon| doesn't always return bitmap with new id.
154   ASSERT_EQ(action_icon_last_id,
155             icon_factory.GetIcon(0).ToSkBitmap()->getGenerationID());
156
157   uint32_t action_icon_current_id = 0;
158
159   ResultCatcher catcher;
160
161   // Tell the extension to update the icon using ImageData object.
162   GetBrowserActionsBar().Press(0);
163   ASSERT_TRUE(catcher.GetNextResult());
164
165   action_icon = icon_factory.GetIcon(0);
166
167   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
168   EXPECT_GT(action_icon_current_id, action_icon_last_id);
169   action_icon_last_id = action_icon_current_id;
170
171   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
172
173   EXPECT_TRUE(
174       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
175                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
176                             1.0f));
177
178   // Tell the extension to update the icon using path.
179   GetBrowserActionsBar().Press(0);
180   ASSERT_TRUE(catcher.GetNextResult());
181
182   action_icon = icon_factory.GetIcon(0);
183
184   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
185   EXPECT_GT(action_icon_current_id, action_icon_last_id);
186   action_icon_last_id = action_icon_current_id;
187
188   EXPECT_FALSE(
189       action_icon.ToImageSkia()->HasRepresentation(2.0f));
190
191   EXPECT_TRUE(
192       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
193                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
194                             1.0f));
195
196   // Tell the extension to update the icon using dictionary of ImageData
197   // objects.
198   GetBrowserActionsBar().Press(0);
199   ASSERT_TRUE(catcher.GetNextResult());
200
201   action_icon = icon_factory.GetIcon(0);
202
203   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
204   EXPECT_GT(action_icon_current_id, action_icon_last_id);
205   action_icon_last_id = action_icon_current_id;
206
207   EXPECT_TRUE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
208
209   EXPECT_TRUE(
210       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
211                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
212                             1.0f));
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 = icon_factory.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(action_icon.ToImageSkia()->HasRepresentation(2.0f));
225
226   EXPECT_TRUE(
227       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
228                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
229                             1.0f));
230
231   // Tell the extension to update the icon using dictionary of ImageData
232   // objects, but setting only size 19.
233   GetBrowserActionsBar().Press(0);
234   ASSERT_TRUE(catcher.GetNextResult());
235
236   action_icon = icon_factory.GetIcon(0);
237
238   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
239   EXPECT_GT(action_icon_current_id, action_icon_last_id);
240   action_icon_last_id = action_icon_current_id;
241
242   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
243
244   EXPECT_TRUE(
245       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
246                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
247                             1.0f));
248
249   // Tell the extension to update the icon using dictionary of paths, but
250   // setting only size 19.
251   GetBrowserActionsBar().Press(0);
252   ASSERT_TRUE(catcher.GetNextResult());
253
254   action_icon = icon_factory.GetIcon(0);
255
256   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
257   EXPECT_GT(action_icon_current_id, action_icon_last_id);
258   action_icon_last_id = action_icon_current_id;
259
260   EXPECT_FALSE(action_icon.ToImageSkia()->HasRepresentation(2.0f));
261
262   EXPECT_TRUE(
263       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon.ToImageSkia()),
264                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
265                             1.0f));
266
267   // Tell the extension to update the icon using dictionary of ImageData
268   // objects, but setting only size 38.
269   GetBrowserActionsBar().Press(0);
270   ASSERT_TRUE(catcher.GetNextResult());
271
272   action_icon = icon_factory.GetIcon(0);
273
274   const gfx::ImageSkia* action_icon_skia = action_icon.ToImageSkia();
275
276   EXPECT_FALSE(action_icon_skia->HasRepresentation(1.0f));
277   EXPECT_TRUE(action_icon_skia->HasRepresentation(2.0f));
278
279   action_icon_current_id = action_icon.ToSkBitmap()->getGenerationID();
280   EXPECT_GT(action_icon_current_id, action_icon_last_id);
281   action_icon_last_id = action_icon_current_id;
282
283   EXPECT_TRUE(gfx::BitmapsAreEqual(
284       *action_icon.ToSkBitmap(),
285       action_icon_skia->GetRepresentation(2.0f).sk_bitmap()));
286
287   EXPECT_TRUE(
288       ImagesAreEqualAtScale(AddBackgroundForViews(*action_icon_skia),
289                             *GetBrowserActionsBar().GetIcon(0).ToImageSkia(),
290                             2.0f));
291
292   // Try setting icon with empty dictionary of ImageData objects.
293   GetBrowserActionsBar().Press(0);
294   ASSERT_FALSE(catcher.GetNextResult());
295   EXPECT_EQ(kEmptyImageDataError, catcher.message());
296
297   // Try setting icon with empty dictionary of path objects.
298   GetBrowserActionsBar().Press(0);
299   ASSERT_FALSE(catcher.GetNextResult());
300   EXPECT_EQ(kEmptyPathError, catcher.message());
301 }
302
303 // This test is flaky as per http://crbug.com/74557.
304 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest,
305                        DISABLED_TabSpecificBrowserActionState) {
306   ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) <<
307       message_;
308   const Extension* extension = GetSingleLoadedExtension();
309   ASSERT_TRUE(extension) << message_;
310
311   // Test that there is a browser action in the toolbar and that it has an icon.
312   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
313   EXPECT_TRUE(GetBrowserActionsBar().HasIcon(0));
314
315   // Execute the action, its title should change.
316   ResultCatcher catcher;
317   GetBrowserActionsBar().Press(0);
318   ASSERT_TRUE(catcher.GetNextResult());
319   EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
320
321   // Open a new tab, the title should go back.
322   chrome::NewTab(browser());
323   EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
324
325   // Go back to first tab, changed title should reappear.
326   browser()->tab_strip_model()->ActivateTabAt(0, true);
327   EXPECT_EQ("Showing icon 2", GetBrowserActionsBar().GetTooltip(0));
328
329   // Reload that tab, default title should come back.
330   ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
331   EXPECT_EQ("hi!", GetBrowserActionsBar().GetTooltip(0));
332 }
333
334 // http://code.google.com/p/chromium/issues/detail?id=70829
335 // Mac used to be ok, but then mac 10.5 started failing too. =(
336 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_BrowserActionPopup) {
337   ASSERT_TRUE(
338       LoadExtension(test_data_dir_.AppendASCII("browser_action/popup")));
339   BrowserActionTestUtil actions_bar = GetBrowserActionsBar();
340   const Extension* extension = GetSingleLoadedExtension();
341   ASSERT_TRUE(extension) << message_;
342
343   // The extension's popup's size grows by |growFactor| each click.
344   const int growFactor = 500;
345   gfx::Size minSize = BrowserActionTestUtil::GetMinPopupSize();
346   gfx::Size middleSize = gfx::Size(growFactor, growFactor);
347   gfx::Size maxSize = BrowserActionTestUtil::GetMaxPopupSize();
348
349   // Ensure that two clicks will exceed the maximum allowed size.
350   ASSERT_GT(minSize.height() + growFactor * 2, maxSize.height());
351   ASSERT_GT(minSize.width() + growFactor * 2, maxSize.width());
352
353   // Simulate a click on the browser action and verify the size of the resulting
354   // popup.  The first one tries to be 0x0, so it should be the min values.
355   ASSERT_TRUE(OpenPopup(0));
356   EXPECT_EQ(minSize, actions_bar.GetPopupBounds().size());
357   EXPECT_TRUE(actions_bar.HidePopup());
358
359   ASSERT_TRUE(OpenPopup(0));
360   EXPECT_EQ(middleSize, actions_bar.GetPopupBounds().size());
361   EXPECT_TRUE(actions_bar.HidePopup());
362
363   // One more time, but this time it should be constrained by the max values.
364   ASSERT_TRUE(OpenPopup(0));
365   EXPECT_EQ(maxSize, actions_bar.GetPopupBounds().size());
366   EXPECT_TRUE(actions_bar.HidePopup());
367 }
368
369 // Test that calling chrome.browserAction.setPopup() can enable and change
370 // a popup.
371 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionAddPopup) {
372   ASSERT_TRUE(RunExtensionTest("browser_action/add_popup")) << message_;
373   const Extension* extension = GetSingleLoadedExtension();
374   ASSERT_TRUE(extension) << message_;
375
376   int tab_id = ExtensionTabUtil::GetTabId(
377       browser()->tab_strip_model()->GetActiveWebContents());
378
379   ExtensionAction* browser_action = GetBrowserAction(*extension);
380   ASSERT_TRUE(browser_action)
381       << "Browser action test extension should have a browser action.";
382
383   ASSERT_FALSE(browser_action->HasPopup(tab_id));
384   ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
385
386   // Simulate a click on the browser action icon.  The onClicked handler
387   // will add a popup.
388   {
389     ResultCatcher catcher;
390     GetBrowserActionsBar().Press(0);
391     ASSERT_TRUE(catcher.GetNextResult());
392   }
393
394   // The call to setPopup in background.html set a tab id, so the
395   // current tab's setting should have changed, but the default setting
396   // should not have changed.
397   ASSERT_TRUE(browser_action->HasPopup(tab_id))
398       << "Clicking on the browser action should have caused a popup to "
399       << "be added.";
400   ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
401       << "Clicking on the browser action should not have set a default "
402       << "popup.";
403
404   ASSERT_STREQ("/a_popup.html",
405                browser_action->GetPopupUrl(tab_id).path().c_str());
406
407   // Now change the popup from a_popup.html to another_popup.html by loading
408   // a page which removes the popup using chrome.browserAction.setPopup().
409   {
410     ResultCatcher catcher;
411     ui_test_utils::NavigateToURL(
412         browser(),
413         GURL(extension->GetResourceURL("change_popup.html")));
414     ASSERT_TRUE(catcher.GetNextResult());
415   }
416
417   // The call to setPopup in change_popup.html did not use a tab id,
418   // so the default setting should have changed as well as the current tab.
419   ASSERT_TRUE(browser_action->HasPopup(tab_id));
420   ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId));
421   ASSERT_STREQ("/another_popup.html",
422                browser_action->GetPopupUrl(tab_id).path().c_str());
423 }
424
425 // Test that calling chrome.browserAction.setPopup() can remove a popup.
426 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) {
427   // Load the extension, which has a browser action with a default popup.
428   ASSERT_TRUE(RunExtensionTest("browser_action/remove_popup")) << message_;
429   const Extension* extension = GetSingleLoadedExtension();
430   ASSERT_TRUE(extension) << message_;
431
432   int tab_id = ExtensionTabUtil::GetTabId(
433       browser()->tab_strip_model()->GetActiveWebContents());
434
435   ExtensionAction* browser_action = GetBrowserAction(*extension);
436   ASSERT_TRUE(browser_action)
437       << "Browser action test extension should have a browser action.";
438
439   ASSERT_TRUE(browser_action->HasPopup(tab_id))
440       << "Expect a browser action popup before the test removes it.";
441   ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
442       << "Expect a browser action popup is the default for all tabs.";
443
444   // Load a page which removes the popup using chrome.browserAction.setPopup().
445   {
446     ResultCatcher catcher;
447     ui_test_utils::NavigateToURL(
448         browser(),
449         GURL(extension->GetResourceURL("remove_popup.html")));
450     ASSERT_TRUE(catcher.GetNextResult());
451   }
452
453   ASSERT_FALSE(browser_action->HasPopup(tab_id))
454       << "Browser action popup should have been removed.";
455   ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId))
456       << "Browser action popup default should not be changed by setting "
457       << "a specific tab id.";
458 }
459
460 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoBasic) {
461   ASSERT_TRUE(test_server()->Start());
462
463   ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_;
464   const Extension* extension = GetSingleLoadedExtension();
465   ASSERT_TRUE(extension) << message_;
466
467   // Test that there is a browser action in the toolbar.
468   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
469
470   // Open an incognito window and test that the browser action isn't there by
471   // default.
472   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
473   Browser* incognito_browser =
474       new Browser(Browser::CreateParams(incognito_profile,
475                                         browser()->host_desktop_type()));
476
477   ASSERT_EQ(0,
478             BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
479
480   // Now enable the extension in incognito mode, and test that the browser
481   // action shows up. Note that we don't update the existing window at the
482   // moment, so we just create a new one.
483   extensions::ExtensionPrefs::Get(browser()->profile())
484       ->SetIsIncognitoEnabled(extension->id(), true);
485
486   chrome::CloseWindow(incognito_browser);
487   incognito_browser =
488       new Browser(Browser::CreateParams(incognito_profile,
489                                         browser()->host_desktop_type()));
490   ASSERT_EQ(1,
491             BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
492
493   // TODO(mpcomplete): simulate a click and have it do the right thing in
494   // incognito.
495 }
496
497 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoDragging) {
498   ExtensionService* service = extensions::ExtensionSystem::Get(
499       browser()->profile())->extension_service();
500
501   // The tooltips for each respective browser action.
502   const char kTooltipA[] = "Alpha";
503   const char kTooltipB[] = "Beta";
504   const char kTooltipC[] = "Gamma";
505
506   const size_t size_before = service->extensions()->size();
507
508   base::FilePath test_dir = test_data_dir_.AppendASCII("browser_action");
509   const Extension* extension_a = InstallExtension(
510       test_dir.AppendASCII("empty_browser_action_alpha.crx"), 1);
511   const Extension* extension_b = InstallExtension(
512       test_dir.AppendASCII("empty_browser_action_beta.crx"), 1);
513   const Extension* extension_c = InstallExtension(
514       test_dir.AppendASCII("empty_browser_action_gamma.crx"), 1);
515   ASSERT_TRUE(extension_a);
516   ASSERT_TRUE(extension_b);
517   ASSERT_TRUE(extension_c);
518
519   // Test that there are 3 browser actions in the toolbar.
520   ASSERT_EQ(size_before + 3, service->extensions()->size());
521   ASSERT_EQ(3, GetBrowserActionsBar().NumberOfBrowserActions());
522
523   // Now enable 2 of the extensions in incognito mode, and test that the browser
524   // actions show up.
525   extensions::ExtensionPrefs* prefs =
526       extensions::ExtensionPrefs::Get(browser()->profile());
527   prefs->SetIsIncognitoEnabled(extension_a->id(), true);
528   prefs->SetIsIncognitoEnabled(extension_c->id(), true);
529
530   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
531   Browser* incognito_browser =
532       new Browser(Browser::CreateParams(incognito_profile,
533                                         browser()->host_desktop_type()));
534   BrowserActionTestUtil incognito_bar(incognito_browser);
535
536   // Navigate just to have a tab in this window, otherwise wonky things happen.
537   ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
538
539   ASSERT_EQ(2, incognito_bar.NumberOfBrowserActions());
540
541   // Ensure that the browser actions are in the right order (ABC).
542   EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(0));
543   EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
544   EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(2));
545
546   EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(0));
547   EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(1));
548
549   // Now rearrange them and ensure that they are rearranged correctly in both
550   // regular and incognito mode.
551
552   // ABC -> CAB
553   ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
554       browser()->profile());
555   toolbar_model->MoveBrowserAction(extension_c, 0);
556
557   EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
558   EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(1));
559   EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(2));
560
561   EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
562   EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
563
564   // CAB -> CBA
565   toolbar_model->MoveBrowserAction(extension_b, 1);
566
567   EXPECT_EQ(kTooltipC, GetBrowserActionsBar().GetTooltip(0));
568   EXPECT_EQ(kTooltipB, GetBrowserActionsBar().GetTooltip(1));
569   EXPECT_EQ(kTooltipA, GetBrowserActionsBar().GetTooltip(2));
570
571   EXPECT_EQ(kTooltipC, incognito_bar.GetTooltip(0));
572   EXPECT_EQ(kTooltipA, incognito_bar.GetTooltip(1));
573 }
574
575 // Tests that events are dispatched to the correct profile for split mode
576 // extensions.
577 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, IncognitoSplit) {
578   ResultCatcher catcher;
579   const Extension* extension = LoadExtensionWithFlags(
580       test_data_dir_.AppendASCII("browser_action/split_mode"),
581       kFlagEnableIncognito);
582   ASSERT_TRUE(extension) << message_;
583
584   // Open an incognito window.
585   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
586   Browser* incognito_browser =
587       new Browser(Browser::CreateParams(incognito_profile,
588                                         browser()->host_desktop_type()));
589   // Navigate just to have a tab in this window, otherwise wonky things happen.
590   ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
591   ASSERT_EQ(1,
592             BrowserActionTestUtil(incognito_browser).NumberOfBrowserActions());
593
594   // A click in the regular profile should open a tab in the regular profile.
595   ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
596       browser()->profile());
597   toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
598   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
599
600   // A click in the incognito profile should open a tab in the
601   // incognito profile.
602   toolbar_model->ExecuteBrowserAction(extension, incognito_browser, NULL, true);
603   ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
604 }
605
606 // Disabled because of failures (crashes) on ASAN bot.
607 // See http://crbug.com/98861.
608 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DISABLED_CloseBackgroundPage) {
609   ASSERT_TRUE(LoadExtension(
610       test_data_dir_.AppendASCII("browser_action/close_background")));
611   const Extension* extension = GetSingleLoadedExtension();
612
613   // There is a background page and a browser action with no badge text.
614   extensions::ProcessManager* manager =
615       extensions::ExtensionSystem::Get(browser()->profile())->process_manager();
616   ASSERT_TRUE(manager->GetBackgroundHostForExtension(extension->id()));
617   ExtensionAction* action = GetBrowserAction(*extension);
618   ASSERT_EQ("", action->GetBadgeText(ExtensionAction::kDefaultTabId));
619
620   content::WindowedNotificationObserver host_destroyed_observer(
621       extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
622       content::NotificationService::AllSources());
623
624   // Click the browser action.
625   ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(
626       browser()->profile());
627   toolbar_model->ExecuteBrowserAction(extension, browser(), NULL, true);
628
629   // It can take a moment for the background page to actually get destroyed
630   // so we wait for the notification before checking that it's really gone
631   // and the badge text has been set.
632   host_destroyed_observer.Wait();
633   ASSERT_FALSE(manager->GetBackgroundHostForExtension(extension->id()));
634   ASSERT_EQ("X", action->GetBadgeText(ExtensionAction::kDefaultTabId));
635 }
636
637 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BadgeBackgroundColor) {
638   ASSERT_TRUE(test_server()->Start());
639   ASSERT_TRUE(RunExtensionTest("browser_action/color")) << message_;
640   const Extension* extension = GetSingleLoadedExtension();
641   ASSERT_TRUE(extension) << message_;
642
643   // Test that there is a browser action in the toolbar.
644   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
645
646   // Test that CSS values (#FF0000) set color correctly.
647   ExtensionAction* action = GetBrowserAction(*extension);
648   ASSERT_EQ(SkColorSetARGB(255, 255, 0, 0),
649             action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
650
651   // Tell the extension to update the browser action state.
652   ResultCatcher catcher;
653   ui_test_utils::NavigateToURL(browser(),
654       GURL(extension->GetResourceURL("update.html")));
655   ASSERT_TRUE(catcher.GetNextResult());
656
657   // Test that CSS values (#0F0) set color correctly.
658   ASSERT_EQ(SkColorSetARGB(255, 0, 255, 0),
659             action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
660
661   ui_test_utils::NavigateToURL(browser(),
662       GURL(extension->GetResourceURL("update2.html")));
663   ASSERT_TRUE(catcher.GetNextResult());
664
665   // Test that array values set color correctly.
666   ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255),
667             action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId));
668 }
669
670 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) {
671   ASSERT_TRUE(RunExtensionTest("browser_action/getters")) << message_;
672   const Extension* extension = GetSingleLoadedExtension();
673   ASSERT_TRUE(extension) << message_;
674
675   // Test that there is a browser action in the toolbar.
676   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
677
678   // Test the getters for defaults.
679   ResultCatcher catcher;
680   ui_test_utils::NavigateToURL(browser(),
681       GURL(extension->GetResourceURL("update.html")));
682   ASSERT_TRUE(catcher.GetNextResult());
683
684   // Test the getters for a specific tab.
685   ui_test_utils::NavigateToURL(browser(),
686       GURL(extension->GetResourceURL("update2.html")));
687   ASSERT_TRUE(catcher.GetNextResult());
688 }
689
690 // Verify triggering browser action.
691 IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) {
692   ASSERT_TRUE(test_server()->Start());
693
694   ASSERT_TRUE(RunExtensionTest("trigger_actions/browser_action")) << message_;
695   const Extension* extension = GetSingleLoadedExtension();
696   ASSERT_TRUE(extension) << message_;
697
698   // Test that there is a browser action in the toolbar.
699   ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions());
700
701   ui_test_utils::NavigateToURL(
702      browser(),
703      test_server()->GetURL("files/simple.html"));
704
705   ExtensionAction* browser_action = GetBrowserAction(*extension);
706   EXPECT_TRUE(browser_action != NULL);
707
708   // Simulate a click on the browser action icon.
709   {
710     ResultCatcher catcher;
711     GetBrowserActionsBar().Press(0);
712     EXPECT_TRUE(catcher.GetNextResult());
713   }
714
715   WebContents* tab =
716       browser()->tab_strip_model()->GetActiveWebContents();
717   EXPECT_TRUE(tab != NULL);
718
719   // Verify that the browser action turned the background color red.
720   const std::string script =
721       "window.domAutomationController.send(document.body.style."
722       "backgroundColor);";
723   std::string result;
724   EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result));
725   EXPECT_EQ(result, "red");
726 }
727
728 }  // namespace
729 }  // namespace extensions