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 "extensions/common/test_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions
{
16 scoped_ptr
<ExtensionAction
> CreateAction(ActionInfo::Type type
,
17 const ActionInfo
& action_info
) {
18 scoped_refptr
<const Extension
> extension
= test_util::CreateEmptyExtension();
19 return make_scoped_ptr(new ExtensionAction(*extension
, type
, action_info
));
24 TEST(ExtensionActionTest
, Title
) {
25 ActionInfo action_info
;
26 action_info
.default_title
= "Initial Title";
27 scoped_ptr
<ExtensionAction
> action
=
28 CreateAction(ActionInfo::TYPE_PAGE
, action_info
);
30 ASSERT_EQ("Initial Title", action
->GetTitle(1));
31 action
->SetTitle(ExtensionAction::kDefaultTabId
, "foo");
32 ASSERT_EQ("foo", action
->GetTitle(1));
33 ASSERT_EQ("foo", action
->GetTitle(100));
34 action
->SetTitle(100, "bar");
35 ASSERT_EQ("foo", action
->GetTitle(1));
36 ASSERT_EQ("bar", action
->GetTitle(100));
37 action
->SetTitle(ExtensionAction::kDefaultTabId
, "baz");
38 ASSERT_EQ("baz", action
->GetTitle(1));
39 action
->ClearAllValuesForTab(100);
40 ASSERT_EQ("baz", action
->GetTitle(100));
43 TEST(ExtensionActionTest
, Visibility
) {
44 scoped_ptr
<ExtensionAction
> action
=
45 CreateAction(ActionInfo::TYPE_PAGE
, ActionInfo());
47 ASSERT_FALSE(action
->GetIsVisible(1));
48 action
->SetIsVisible(ExtensionAction::kDefaultTabId
, true);
49 ASSERT_TRUE(action
->GetIsVisible(1));
50 ASSERT_TRUE(action
->GetIsVisible(100));
52 action
->SetIsVisible(ExtensionAction::kDefaultTabId
, false);
53 ASSERT_FALSE(action
->GetIsVisible(1));
54 ASSERT_FALSE(action
->GetIsVisible(100));
55 action
->SetIsVisible(100, true);
56 ASSERT_FALSE(action
->GetIsVisible(1));
57 ASSERT_TRUE(action
->GetIsVisible(100));
59 action
->ClearAllValuesForTab(100);
60 ASSERT_FALSE(action
->GetIsVisible(1));
61 ASSERT_FALSE(action
->GetIsVisible(100));
63 scoped_ptr
<ExtensionAction
> browser_action
=
64 CreateAction(ActionInfo::TYPE_BROWSER
, ActionInfo());
65 ASSERT_TRUE(browser_action
->GetIsVisible(1));
68 TEST(ExtensionActionTest
, Icon
) {
69 ActionInfo action_info
;
70 action_info
.default_icon
.Add(16, "icon16.png");
71 scoped_ptr
<ExtensionAction
> page_action
=
72 CreateAction(ActionInfo::TYPE_PAGE
, action_info
);
73 ASSERT_TRUE(page_action
->default_icon());
74 EXPECT_EQ("icon16.png",
75 page_action
->default_icon()->Get(
76 16, ExtensionIconSet::MATCH_EXACTLY
));
78 page_action
->default_icon()->Get(
79 17, ExtensionIconSet::MATCH_BIGGER
));
82 TEST(ExtensionActionTest
, Badge
) {
83 scoped_ptr
<ExtensionAction
> action
=
84 CreateAction(ActionInfo::TYPE_PAGE
, ActionInfo());
85 ASSERT_EQ("", action
->GetBadgeText(1));
86 action
->SetBadgeText(ExtensionAction::kDefaultTabId
, "foo");
87 ASSERT_EQ("foo", action
->GetBadgeText(1));
88 ASSERT_EQ("foo", action
->GetBadgeText(100));
89 action
->SetBadgeText(100, "bar");
90 ASSERT_EQ("foo", action
->GetBadgeText(1));
91 ASSERT_EQ("bar", action
->GetBadgeText(100));
92 action
->SetBadgeText(ExtensionAction::kDefaultTabId
, "baz");
93 ASSERT_EQ("baz", action
->GetBadgeText(1));
94 action
->ClearAllValuesForTab(100);
95 ASSERT_EQ("baz", action
->GetBadgeText(100));
98 TEST(ExtensionActionTest
, BadgeTextColor
) {
99 scoped_ptr
<ExtensionAction
> action
=
100 CreateAction(ActionInfo::TYPE_PAGE
, ActionInfo());
101 ASSERT_EQ(0x00000000u
, action
->GetBadgeTextColor(1));
102 action
->SetBadgeTextColor(ExtensionAction::kDefaultTabId
, 0xFFFF0000u
);
103 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeTextColor(1));
104 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeTextColor(100));
105 action
->SetBadgeTextColor(100, 0xFF00FF00);
106 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeTextColor(1));
107 ASSERT_EQ(0xFF00FF00u
, action
->GetBadgeTextColor(100));
108 action
->SetBadgeTextColor(ExtensionAction::kDefaultTabId
, 0xFF0000FFu
);
109 ASSERT_EQ(0xFF0000FFu
, action
->GetBadgeTextColor(1));
110 action
->ClearAllValuesForTab(100);
111 ASSERT_EQ(0xFF0000FFu
, action
->GetBadgeTextColor(100));
114 TEST(ExtensionActionTest
, BadgeBackgroundColor
) {
115 scoped_ptr
<ExtensionAction
> action
=
116 CreateAction(ActionInfo::TYPE_PAGE
, ActionInfo());
117 ASSERT_EQ(0x00000000u
, action
->GetBadgeBackgroundColor(1));
118 action
->SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
,
120 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeBackgroundColor(1));
121 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeBackgroundColor(100));
122 action
->SetBadgeBackgroundColor(100, 0xFF00FF00);
123 ASSERT_EQ(0xFFFF0000u
, action
->GetBadgeBackgroundColor(1));
124 ASSERT_EQ(0xFF00FF00u
, action
->GetBadgeBackgroundColor(100));
125 action
->SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId
,
127 ASSERT_EQ(0xFF0000FFu
, action
->GetBadgeBackgroundColor(1));
128 action
->ClearAllValuesForTab(100);
129 ASSERT_EQ(0xFF0000FFu
, action
->GetBadgeBackgroundColor(100));
132 TEST(ExtensionActionTest
, PopupUrl
) {
134 GURL
url_foo("http://www.example.com/foo.html");
135 GURL
url_bar("http://www.example.com/bar.html");
136 GURL
url_baz("http://www.example.com/baz.html");
138 ActionInfo action_info
;
139 action_info
.default_popup_url
= url_foo
;
140 scoped_ptr
<ExtensionAction
> action
=
141 CreateAction(ActionInfo::TYPE_PAGE
, action_info
);
143 ASSERT_EQ(url_foo
, action
->GetPopupUrl(1));
144 ASSERT_EQ(url_foo
, action
->GetPopupUrl(100));
145 ASSERT_TRUE(action
->HasPopup(1));
146 ASSERT_TRUE(action
->HasPopup(100));
148 action
->SetPopupUrl(ExtensionAction::kDefaultTabId
, url_unset
);
149 ASSERT_EQ(url_unset
, action
->GetPopupUrl(1));
150 ASSERT_EQ(url_unset
, action
->GetPopupUrl(100));
151 ASSERT_FALSE(action
->HasPopup(1));
152 ASSERT_FALSE(action
->HasPopup(100));
154 action
->SetPopupUrl(100, url_bar
);
155 ASSERT_EQ(url_unset
, action
->GetPopupUrl(1));
156 ASSERT_EQ(url_bar
, action
->GetPopupUrl(100));
158 action
->SetPopupUrl(ExtensionAction::kDefaultTabId
, url_baz
);
159 ASSERT_EQ(url_baz
, action
->GetPopupUrl(1));
160 ASSERT_EQ(url_bar
, action
->GetPopupUrl(100));
162 action
->ClearAllValuesForTab(100);
163 ASSERT_EQ(url_baz
, action
->GetPopupUrl(1));
164 ASSERT_EQ(url_baz
, action
->GetPopupUrl(100));
167 } // namespace extensions