OLD | NEW |
| (Empty) |
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 "base/file_path.h" | |
6 #include "base/file_util.h" | |
7 #include "base/message_loop.h" | |
8 #include "base/path_service.h" | |
9 #include "chrome/common/chrome_paths.h" | |
10 #include "chrome/common/extensions/extension_action.h" | |
11 #include "googleurl/src/gurl.h" | |
12 #include "grit/theme_resources.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/image/image_skia.h" | |
17 #include "ui/gfx/skia_util.h" | |
18 #include "webkit/glue/image_decoder.h" | |
19 | |
20 namespace { | |
21 | |
22 bool ImagesAreEqual(const gfx::Image& i1, const gfx::Image& i2) { | |
23 return gfx::BitmapsAreEqual(*i1.ToSkBitmap(), *i2.ToSkBitmap()); | |
24 } | |
25 | |
26 gfx::Image LoadIcon(const std::string& filename) { | |
27 FilePath path; | |
28 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
29 path = path.AppendASCII("extensions").AppendASCII(filename); | |
30 | |
31 std::string file_contents; | |
32 file_util::ReadFileToString(path, &file_contents); | |
33 const unsigned char* data = | |
34 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
35 | |
36 SkBitmap bitmap; | |
37 webkit_glue::ImageDecoder decoder; | |
38 bitmap = decoder.Decode(data, file_contents.length()); | |
39 | |
40 return gfx::Image(bitmap); | |
41 } | |
42 | |
43 class ExtensionActionTest : public testing::Test { | |
44 public: | |
45 ExtensionActionTest() | |
46 : action("", ExtensionAction::TYPE_PAGE) { | |
47 } | |
48 | |
49 ExtensionAction action; | |
50 }; | |
51 | |
52 TEST_F(ExtensionActionTest, Title) { | |
53 ASSERT_EQ("", action.GetTitle(1)); | |
54 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); | |
55 ASSERT_EQ("foo", action.GetTitle(1)); | |
56 ASSERT_EQ("foo", action.GetTitle(100)); | |
57 action.SetTitle(100, "bar"); | |
58 ASSERT_EQ("foo", action.GetTitle(1)); | |
59 ASSERT_EQ("bar", action.GetTitle(100)); | |
60 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); | |
61 ASSERT_EQ("baz", action.GetTitle(1)); | |
62 action.ClearAllValuesForTab(100); | |
63 ASSERT_EQ("baz", action.GetTitle(100)); | |
64 } | |
65 | |
66 TEST_F(ExtensionActionTest, Icon) { | |
67 gfx::Image puzzle_piece = | |
68 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
69 IDR_EXTENSIONS_FAVICON); | |
70 gfx::Image icon1 = LoadIcon("icon1.png"); | |
71 gfx::Image icon2 = LoadIcon("icon2.png"); | |
72 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))); | |
73 | |
74 action.set_default_icon_path("the_default.png"); | |
75 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))) | |
76 << "Still returns the puzzle piece because the image isn't loaded yet."; | |
77 action.CacheIcon("the_default.png", icon2); | |
78 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1))); | |
79 | |
80 action.SetIcon(ExtensionAction::kDefaultTabId, icon1); | |
81 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100))) | |
82 << "SetIcon(kDefaultTabId) overrides the default_icon_path."; | |
83 | |
84 action.SetIcon(100, icon2); | |
85 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1))); | |
86 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100))); | |
87 } | |
88 | |
89 TEST_F(ExtensionActionTest, IconIndex) { | |
90 gfx::Image icon1 = LoadIcon("icon1.png"); | |
91 gfx::Image icon2 = LoadIcon("icon2.png"); | |
92 gfx::Image puzzle_piece = | |
93 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
94 IDR_EXTENSIONS_FAVICON); | |
95 | |
96 ASSERT_EQ(-1, action.GetIconIndex(1)); | |
97 action.icon_paths()->push_back("foo.png"); | |
98 action.icon_paths()->push_back("bar.png"); | |
99 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); | |
100 ASSERT_EQ(1, action.GetIconIndex(1)); | |
101 ASSERT_EQ(1, action.GetIconIndex(100)); | |
102 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100))) | |
103 << "Non-loaded icon gets the puzzle piece."; | |
104 action.CacheIcon("bar.png", icon1); | |
105 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100))); | |
106 | |
107 action.SetIconIndex(100, 0); | |
108 ASSERT_EQ(0, action.GetIconIndex(100)); | |
109 ASSERT_EQ(1, action.GetIconIndex(1)); | |
110 action.ClearAllValuesForTab(100); | |
111 ASSERT_EQ(1, action.GetIconIndex(100)); | |
112 ASSERT_EQ(1, action.GetIconIndex(1)); | |
113 } | |
114 | |
115 TEST_F(ExtensionActionTest, Visibility) { | |
116 // Supports the icon animation. | |
117 MessageLoop message_loop; | |
118 | |
119 ASSERT_FALSE(action.GetIsVisible(1)); | |
120 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); | |
121 action.SetAppearance(ExtensionAction::kDefaultTabId, ExtensionAction::ACTIVE); | |
122 ASSERT_TRUE(action.GetIsVisible(1)); | |
123 ASSERT_TRUE(action.GetIsVisible(100)); | |
124 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); | |
125 | |
126 action.SetAppearance(ExtensionAction::kDefaultTabId, | |
127 ExtensionAction::INVISIBLE); | |
128 ASSERT_FALSE(action.GetIsVisible(1)); | |
129 ASSERT_FALSE(action.GetIsVisible(100)); | |
130 EXPECT_FALSE(action.GetIconAnimation(100)); | |
131 action.SetAppearance(100, ExtensionAction::ACTIVE); | |
132 ASSERT_FALSE(action.GetIsVisible(1)); | |
133 ASSERT_TRUE(action.GetIsVisible(100)); | |
134 EXPECT_TRUE(action.GetIconAnimation(100)); | |
135 | |
136 action.ClearAllValuesForTab(100); | |
137 ASSERT_FALSE(action.GetIsVisible(1)); | |
138 ASSERT_FALSE(action.GetIsVisible(100)); | |
139 EXPECT_FALSE(action.GetIconAnimation(100)); | |
140 } | |
141 | |
142 TEST_F(ExtensionActionTest, GetAttention) { | |
143 // Supports the icon animation. | |
144 scoped_ptr<MessageLoop> message_loop(new MessageLoop); | |
145 | |
146 EXPECT_FALSE(action.GetIsVisible(1)); | |
147 EXPECT_FALSE(action.GetIconAnimation(1)); | |
148 action.SetAppearance(1, ExtensionAction::WANTS_ATTENTION); | |
149 EXPECT_TRUE(action.GetIsVisible(1)); | |
150 EXPECT_TRUE(action.GetIconAnimation(1)); | |
151 | |
152 // Simulate waiting long enough for the animation to end. | |
153 message_loop.reset(); // Can't have 2 MessageLoops alive at once. | |
154 message_loop.reset(new MessageLoop); | |
155 EXPECT_FALSE(action.GetIconAnimation(1)); // Sanity check. | |
156 | |
157 action.SetAppearance(1, ExtensionAction::ACTIVE); | |
158 EXPECT_FALSE(action.GetIconAnimation(1)) | |
159 << "The animation should not play again if the icon was already visible."; | |
160 } | |
161 | |
162 TEST_F(ExtensionActionTest, Badge) { | |
163 ASSERT_EQ("", action.GetBadgeText(1)); | |
164 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); | |
165 ASSERT_EQ("foo", action.GetBadgeText(1)); | |
166 ASSERT_EQ("foo", action.GetBadgeText(100)); | |
167 action.SetBadgeText(100, "bar"); | |
168 ASSERT_EQ("foo", action.GetBadgeText(1)); | |
169 ASSERT_EQ("bar", action.GetBadgeText(100)); | |
170 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); | |
171 ASSERT_EQ("baz", action.GetBadgeText(1)); | |
172 action.ClearAllValuesForTab(100); | |
173 ASSERT_EQ("baz", action.GetBadgeText(100)); | |
174 } | |
175 | |
176 TEST_F(ExtensionActionTest, BadgeTextColor) { | |
177 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); | |
178 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); | |
179 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); | |
180 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); | |
181 action.SetBadgeTextColor(100, 0xFF00FF00); | |
182 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); | |
183 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); | |
184 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); | |
185 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); | |
186 action.ClearAllValuesForTab(100); | |
187 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); | |
188 } | |
189 | |
190 TEST_F(ExtensionActionTest, BadgeBackgroundColor) { | |
191 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); | |
192 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, | |
193 0xFFFF0000u); | |
194 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); | |
195 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); | |
196 action.SetBadgeBackgroundColor(100, 0xFF00FF00); | |
197 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); | |
198 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); | |
199 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, | |
200 0xFF0000FFu); | |
201 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); | |
202 action.ClearAllValuesForTab(100); | |
203 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); | |
204 } | |
205 | |
206 TEST_F(ExtensionActionTest, PopupUrl) { | |
207 GURL url_unset; | |
208 GURL url_foo("http://www.example.com/foo.html"); | |
209 GURL url_bar("http://www.example.com/bar.html"); | |
210 GURL url_baz("http://www.example.com/baz.html"); | |
211 | |
212 ASSERT_EQ(url_unset, action.GetPopupUrl(1)); | |
213 ASSERT_EQ(url_unset, action.GetPopupUrl(100)); | |
214 ASSERT_FALSE(action.HasPopup(1)); | |
215 ASSERT_FALSE(action.HasPopup(100)); | |
216 | |
217 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); | |
218 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); | |
219 ASSERT_EQ(url_foo, action.GetPopupUrl(100)); | |
220 | |
221 action.SetPopupUrl(100, url_bar); | |
222 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); | |
223 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); | |
224 | |
225 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); | |
226 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | |
227 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); | |
228 | |
229 action.ClearAllValuesForTab(100); | |
230 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | |
231 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); | |
232 } | |
233 | |
234 } // namespace | |
OLD | NEW |