1 // Copyright 2015 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.
5 #include "base/run_loop.h"
6 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
7 #include "chrome/browser/ui/tabs/tab_strip_model.h"
8 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
9 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_unittest.h"
10 #include "chrome/common/icon_with_badge_image_source.h"
11 #include "chrome/grit/chromium_strings.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
15 // Tests the icon appearance of extension actions without the toolbar redesign.
16 // In this case, the action should never be grayscaled or decorated to indicate
17 // whether or not it wants to run.
18 TEST_F(ToolbarActionsBarUnitTest
, ExtensionActionNormalAppearance
) {
19 CreateAndAddExtension("extension",
20 extensions::extension_action_test_util::BROWSER_ACTION
);
21 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount());
23 AddTab(browser(), GURL("chrome://newtab"));
25 gfx::Size
size(ToolbarActionsBar::IconWidth(false),
26 ToolbarActionsBar::IconHeight());
27 content::WebContents
* web_contents
=
28 browser()->tab_strip_model()->GetActiveWebContents();
29 ExtensionActionViewController
* action
=
30 static_cast<ExtensionActionViewController
*>(
31 toolbar_actions_bar()->GetActions()[0]);
32 scoped_ptr
<IconWithBadgeImageSource
> image_source
=
33 action
->GetIconImageSourceForTesting(web_contents
, size
);
34 EXPECT_FALSE(image_source
->grayscale());
35 EXPECT_FALSE(image_source
->paint_decoration());
37 SetActionWantsToRunOnTab(action
->extension_action(), web_contents
, false);
38 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
39 EXPECT_FALSE(image_source
->grayscale());
40 EXPECT_FALSE(image_source
->paint_decoration());
42 toolbar_model()->SetVisibleIconCount(0u);
43 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
44 EXPECT_FALSE(image_source
->grayscale());
45 EXPECT_FALSE(image_source
->paint_decoration());
47 SetActionWantsToRunOnTab(action
->extension_action(), web_contents
, true);
48 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
49 EXPECT_FALSE(image_source
->grayscale());
50 EXPECT_FALSE(image_source
->paint_decoration());
53 // Tests the icon appearance of extension actions with the toolbar redesign.
54 // Extensions that don't want to run should have their icons grayscaled.
55 // Overflowed extensions that want to run should have an additional decoration.
56 TEST_F(ToolbarActionsBarRedesignUnitTest
, ExtensionActionWantsToRunAppearance
) {
57 CreateAndAddExtension("extension",
58 extensions::extension_action_test_util::PAGE_ACTION
);
59 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount());
60 EXPECT_EQ(0u, overflow_bar()->GetIconCount());
62 AddTab(browser(), GURL("chrome://newtab"));
64 gfx::Size
size(ToolbarActionsBar::IconWidth(false),
65 ToolbarActionsBar::IconHeight());
66 content::WebContents
* web_contents
=
67 browser()->tab_strip_model()->GetActiveWebContents();
68 ExtensionActionViewController
* action
=
69 static_cast<ExtensionActionViewController
*>(
70 toolbar_actions_bar()->GetActions()[0]);
71 scoped_ptr
<IconWithBadgeImageSource
> image_source
=
72 action
->GetIconImageSourceForTesting(web_contents
, size
);
73 EXPECT_TRUE(image_source
->grayscale());
74 EXPECT_FALSE(image_source
->paint_decoration());
76 SetActionWantsToRunOnTab(action
->extension_action(), web_contents
, true);
77 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
78 EXPECT_FALSE(image_source
->grayscale());
79 EXPECT_FALSE(image_source
->paint_decoration());
81 toolbar_model()->SetVisibleIconCount(0u);
82 EXPECT_EQ(0u, toolbar_actions_bar()->GetIconCount());
83 EXPECT_EQ(1u, overflow_bar()->GetIconCount());
85 action
= static_cast<ExtensionActionViewController
*>(
86 overflow_bar()->GetActions()[0]);
87 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
88 EXPECT_FALSE(image_source
->grayscale());
89 EXPECT_TRUE(image_source
->paint_decoration());
91 SetActionWantsToRunOnTab(action
->extension_action(), web_contents
, false);
92 image_source
= action
->GetIconImageSourceForTesting(web_contents
, size
);
93 EXPECT_TRUE(image_source
->grayscale());
94 EXPECT_FALSE(image_source
->paint_decoration());
97 TEST_F(ToolbarActionsBarRedesignUnitTest
, ExtensionActionContextMenu
) {
98 CreateAndAddExtension("extension",
99 extensions::extension_action_test_util::BROWSER_ACTION
);
100 EXPECT_EQ(1u, toolbar_actions_bar()->GetIconCount());
102 // Check that the context menu has the proper string for the action's position
103 // (in the main toolbar, in the overflow container, or temporarily popped
105 auto check_visibility_string
= [](ToolbarActionViewController
* action
,
106 int expected_visibility_string
) {
107 ui::SimpleMenuModel
* context_menu
=
108 static_cast<ui::SimpleMenuModel
*>(action
->GetContextMenu());
109 int visibility_index
= context_menu
->GetIndexOfCommandId(
110 extensions::ExtensionContextMenuModel::TOGGLE_VISIBILITY
);
111 ASSERT_GE(visibility_index
, 0);
112 base::string16 visibility_label
=
113 context_menu
->GetLabelAt(visibility_index
);
114 EXPECT_EQ(l10n_util::GetStringUTF16(expected_visibility_string
),
118 check_visibility_string(toolbar_actions_bar()->GetActions()[0],
119 IDS_EXTENSIONS_HIDE_BUTTON_IN_MENU
);
120 toolbar_model()->SetVisibleIconCount(0u);
121 check_visibility_string(overflow_bar()->GetActions()[0],
122 IDS_EXTENSIONS_SHOW_BUTTON_IN_TOOLBAR
);
123 base::RunLoop run_loop
;
124 toolbar_actions_bar()->PopOutAction(toolbar_actions_bar()->GetActions()[0],
125 run_loop
.QuitClosure());
127 check_visibility_string(toolbar_actions_bar()->GetActions()[0],
128 IDS_EXTENSIONS_KEEP_BUTTON_IN_TOOLBAR
);