Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_action_unittest.cc
blobc71e7b1562f0abc8c3105052da1e233f0a11dfbd
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.
5 #include "base/message_loop/message_loop.h"
6 #include "chrome/browser/extensions/extension_action.h"
7 #include "chrome/common/extensions/api/extension_action/action_info.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 namespace {
13 using extensions::ActionInfo;
15 TEST(ExtensionActionTest, Title) {
16 ActionInfo action_info;
17 action_info.default_title = "Initial Title";
18 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, action_info);
20 ASSERT_EQ("Initial Title", action.GetTitle(1));
21 action.SetTitle(ExtensionAction::kDefaultTabId, "foo");
22 ASSERT_EQ("foo", action.GetTitle(1));
23 ASSERT_EQ("foo", action.GetTitle(100));
24 action.SetTitle(100, "bar");
25 ASSERT_EQ("foo", action.GetTitle(1));
26 ASSERT_EQ("bar", action.GetTitle(100));
27 action.SetTitle(ExtensionAction::kDefaultTabId, "baz");
28 ASSERT_EQ("baz", action.GetTitle(1));
29 action.ClearAllValuesForTab(100);
30 ASSERT_EQ("baz", action.GetTitle(100));
33 TEST(ExtensionActionTest, Visibility) {
34 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, ActionInfo());
36 ASSERT_FALSE(action.GetIsVisible(1));
37 action.SetAppearance(ExtensionAction::kDefaultTabId, ExtensionAction::ACTIVE);
38 ASSERT_TRUE(action.GetIsVisible(1));
39 ASSERT_TRUE(action.GetIsVisible(100));
41 action.SetAppearance(ExtensionAction::kDefaultTabId,
42 ExtensionAction::INVISIBLE);
43 ASSERT_FALSE(action.GetIsVisible(1));
44 ASSERT_FALSE(action.GetIsVisible(100));
45 action.SetAppearance(100, ExtensionAction::ACTIVE);
46 ASSERT_FALSE(action.GetIsVisible(1));
47 ASSERT_TRUE(action.GetIsVisible(100));
48 EXPECT_FALSE(action.GetIconAnimation(100))
49 << "Page actions should not animate.";
51 action.ClearAllValuesForTab(100);
52 ASSERT_FALSE(action.GetIsVisible(1));
53 ASSERT_FALSE(action.GetIsVisible(100));
55 ExtensionAction browser_action(
56 std::string(), ActionInfo::TYPE_BROWSER, ActionInfo());
57 ASSERT_TRUE(browser_action.GetIsVisible(1));
60 TEST(ExtensionActionTest, ScriptBadgeAnimation) {
61 // Supports the icon animation.
62 base::MessageLoop message_loop;
64 ExtensionAction script_badge(
65 std::string(), ActionInfo::TYPE_SCRIPT_BADGE, ActionInfo());
66 EXPECT_FALSE(script_badge.GetIconAnimation(ExtensionAction::kDefaultTabId));
67 script_badge.SetAppearance(ExtensionAction::kDefaultTabId,
68 ExtensionAction::ACTIVE);
69 EXPECT_FALSE(script_badge.GetIconAnimation(ExtensionAction::kDefaultTabId))
70 << "Showing the default tab should not animate script badges.";
72 script_badge.SetAppearance(ExtensionAction::kDefaultTabId,
73 ExtensionAction::INVISIBLE);
74 EXPECT_FALSE(script_badge.GetIconAnimation(1))
75 << "Making a script badge invisible should not show its animation.";
76 script_badge.SetAppearance(1, ExtensionAction::ACTIVE);
77 EXPECT_TRUE(script_badge.GetIconAnimation(1))
78 << "Making a script badge visible should show its animation.";
80 script_badge.ClearAllValuesForTab(1);
81 EXPECT_FALSE(script_badge.GetIconAnimation(100));
84 TEST(ExtensionActionTest, GetAttention) {
85 // Supports the icon animation.
86 scoped_ptr<base::MessageLoop> message_loop(new base::MessageLoop);
88 ExtensionAction script_badge(
89 std::string(), ActionInfo::TYPE_SCRIPT_BADGE, ActionInfo());
90 EXPECT_FALSE(script_badge.GetIsVisible(1));
91 EXPECT_FALSE(script_badge.GetIconAnimation(1));
92 script_badge.SetAppearance(1, ExtensionAction::WANTS_ATTENTION);
93 EXPECT_TRUE(script_badge.GetIsVisible(1));
94 EXPECT_TRUE(script_badge.GetIconAnimation(1));
96 // Simulate waiting long enough for the animation to end.
97 message_loop.reset(); // Can't have 2 MessageLoops alive at once.
98 message_loop.reset(new base::MessageLoop);
99 EXPECT_FALSE(script_badge.GetIconAnimation(1)); // Sanity check.
101 script_badge.SetAppearance(1, ExtensionAction::ACTIVE);
102 EXPECT_FALSE(script_badge.GetIconAnimation(1))
103 << "The animation should not play again if the icon was already visible.";
106 TEST(ExtensionActionTest, Icon) {
107 ActionInfo action_info;
108 action_info.default_icon.Add(16, "icon16.png");
109 ExtensionAction page_action(
110 std::string(), ActionInfo::TYPE_PAGE, action_info);
111 ASSERT_TRUE(page_action.default_icon());
112 EXPECT_EQ("icon16.png",
113 page_action.default_icon()->Get(
114 16, ExtensionIconSet::MATCH_EXACTLY));
115 EXPECT_EQ("",
116 page_action.default_icon()->Get(
117 17, ExtensionIconSet::MATCH_BIGGER));
120 TEST(ExtensionActionTest, Badge) {
121 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, ActionInfo());
122 ASSERT_EQ("", action.GetBadgeText(1));
123 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
124 ASSERT_EQ("foo", action.GetBadgeText(1));
125 ASSERT_EQ("foo", action.GetBadgeText(100));
126 action.SetBadgeText(100, "bar");
127 ASSERT_EQ("foo", action.GetBadgeText(1));
128 ASSERT_EQ("bar", action.GetBadgeText(100));
129 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
130 ASSERT_EQ("baz", action.GetBadgeText(1));
131 action.ClearAllValuesForTab(100);
132 ASSERT_EQ("baz", action.GetBadgeText(100));
135 TEST(ExtensionActionTest, BadgeTextColor) {
136 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, ActionInfo());
137 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1));
138 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
139 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
140 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100));
141 action.SetBadgeTextColor(100, 0xFF00FF00);
142 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
143 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100));
144 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
145 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1));
146 action.ClearAllValuesForTab(100);
147 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100));
150 TEST(ExtensionActionTest, BadgeBackgroundColor) {
151 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, ActionInfo());
152 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1));
153 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
154 0xFFFF0000u);
155 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
156 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100));
157 action.SetBadgeBackgroundColor(100, 0xFF00FF00);
158 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
159 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100));
160 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
161 0xFF0000FFu);
162 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
163 action.ClearAllValuesForTab(100);
164 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
167 TEST(ExtensionActionTest, PopupUrl) {
168 GURL url_unset;
169 GURL url_foo("http://www.example.com/foo.html");
170 GURL url_bar("http://www.example.com/bar.html");
171 GURL url_baz("http://www.example.com/baz.html");
173 ActionInfo action_info;
174 action_info.default_popup_url = url_foo;
175 ExtensionAction action(std::string(), ActionInfo::TYPE_PAGE, action_info);
177 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
178 ASSERT_EQ(url_foo, action.GetPopupUrl(100));
179 ASSERT_TRUE(action.HasPopup(1));
180 ASSERT_TRUE(action.HasPopup(100));
182 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_unset);
183 ASSERT_EQ(url_unset, action.GetPopupUrl(1));
184 ASSERT_EQ(url_unset, action.GetPopupUrl(100));
185 ASSERT_FALSE(action.HasPopup(1));
186 ASSERT_FALSE(action.HasPopup(100));
188 action.SetPopupUrl(100, url_bar);
189 ASSERT_EQ(url_unset, action.GetPopupUrl(1));
190 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
192 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz);
193 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
194 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
196 action.ClearAllValuesForTab(100);
197 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
198 ASSERT_EQ(url_baz, action.GetPopupUrl(100));
201 } // namespace