Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/common/extensions/extension_action_unittest.cc

Issue 10806058: Move icon fallbacks into ExtensionAction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows support Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_path.h" 5 #include "base/file_path.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/path_service.h" 7 #include "base/path_service.h"
8 #include "chrome/common/chrome_paths.h" 8 #include "chrome/common/chrome_paths.h"
9 #include "chrome/common/extensions/extension_action.h" 9 #include "chrome/common/extensions/extension_action.h"
10 #include "googleurl/src/gurl.h" 10 #include "googleurl/src/gurl.h"
11 #include "grit/theme_resources.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/skia_util.h" 16 #include "ui/gfx/skia_util.h"
14 #include "webkit/glue/image_decoder.h" 17 #include "webkit/glue/image_decoder.h"
15 18
16 using gfx::BitmapsAreEqual; 19 static bool ImagesAreEqual(const gfx::Image& i1, const gfx::Image& i2) {
not at google - send to devlin 2012/07/26 02:29:44 ditto anonymous namespace, and next function. In
Jeffrey Yasskin 2012/07/26 21:11:47 Done.
20 return gfx::BitmapsAreEqual(*i1.ToSkBitmap(), *i2.ToSkBitmap());
21 }
17 22
18 static SkBitmap LoadIcon(const std::string& filename) { 23 static gfx::Image LoadIcon(const std::string& filename) {
19 FilePath path; 24 FilePath path;
20 PathService::Get(chrome::DIR_TEST_DATA, &path); 25 PathService::Get(chrome::DIR_TEST_DATA, &path);
21 path = path.AppendASCII("extensions").AppendASCII(filename); 26 path = path.AppendASCII("extensions").AppendASCII(filename);
22 27
23 std::string file_contents; 28 std::string file_contents;
24 file_util::ReadFileToString(path, &file_contents); 29 file_util::ReadFileToString(path, &file_contents);
25 const unsigned char* data = 30 const unsigned char* data =
26 reinterpret_cast<const unsigned char*>(file_contents.data()); 31 reinterpret_cast<const unsigned char*>(file_contents.data());
27 32
28 SkBitmap bitmap; 33 SkBitmap bitmap;
29 webkit_glue::ImageDecoder decoder; 34 webkit_glue::ImageDecoder decoder;
30 bitmap = decoder.Decode(data, file_contents.length()); 35 bitmap = decoder.Decode(data, file_contents.length());
31 36
32 return bitmap; 37 return gfx::Image(bitmap);
33 } 38 }
34 39
35 TEST(ExtensionActionTest, TabSpecificState) { 40 class ExtensionActionTest : public testing::Test {
36 ExtensionAction action("", ExtensionAction::TYPE_PAGE); 41 public:
42 ExtensionActionTest()
43 : action("", ExtensionAction::TYPE_PAGE) {
44 }
37 45
38 // title 46 ExtensionAction action;
Jeffrey Yasskin 2012/07/25 19:45:26 Breaks the naming rule in order to keep the diff s
47 };
48
49 TEST_F(ExtensionActionTest, Title) {
39 ASSERT_EQ("", action.GetTitle(1)); 50 ASSERT_EQ("", action.GetTitle(1));
40 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); 51 action.SetTitle(ExtensionAction::kDefaultTabId, "foo");
41 ASSERT_EQ("foo", action.GetTitle(1)); 52 ASSERT_EQ("foo", action.GetTitle(1));
42 ASSERT_EQ("foo", action.GetTitle(100)); 53 ASSERT_EQ("foo", action.GetTitle(100));
43 action.SetTitle(100, "bar"); 54 action.SetTitle(100, "bar");
44 ASSERT_EQ("foo", action.GetTitle(1)); 55 ASSERT_EQ("foo", action.GetTitle(1));
45 ASSERT_EQ("bar", action.GetTitle(100)); 56 ASSERT_EQ("bar", action.GetTitle(100));
46 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); 57 action.SetTitle(ExtensionAction::kDefaultTabId, "baz");
47 ASSERT_EQ("baz", action.GetTitle(1)); 58 ASSERT_EQ("baz", action.GetTitle(1));
48 action.ClearAllValuesForTab(100); 59 action.ClearAllValuesForTab(100);
49 ASSERT_EQ("baz", action.GetTitle(100)); 60 ASSERT_EQ("baz", action.GetTitle(100));
61 }
50 62
51 // icon 63 TEST_F(ExtensionActionTest, Icon) {
52 SkBitmap icon1 = LoadIcon("icon1.png"); 64 ExtensionAction::PathToIconCache cache;
53 SkBitmap icon2 = LoadIcon("icon2.png");
54 ASSERT_TRUE(action.GetIcon(1).isNull());
55 action.SetIcon(ExtensionAction::kDefaultTabId, icon1);
56 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(100)));
57 action.SetIcon(100, icon2);
58 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(1)));
59 ASSERT_TRUE(BitmapsAreEqual(icon2, action.GetIcon(100)));
60 65
61 // icon index 66 gfx::Image puzzle_piece =
67 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
68 IDR_EXTENSIONS_FAVICON);
69 gfx::Image icon1 = LoadIcon("icon1.png");
70 gfx::Image icon2 = LoadIcon("icon2.png");
71 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1, cache)));
72
73 action.set_default_icon_path("the_default.png");
74 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1, cache)))
75 << "Still returns the puzzle piece because the image isn't loaded yet.";
76 cache["the_default.png"] = icon2;
77 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1, cache)));
78
79 action.SetIcon(ExtensionAction::kDefaultTabId, *icon1.ToSkBitmap());
80 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100, cache)))
81 << "SetIcon(kDefaultTabId) overrides the default_icon_path.";
82
83 action.SetIcon(100, *icon2.ToSkBitmap());
84 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1, cache)));
85 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100, cache)));
86 }
87
88 TEST_F(ExtensionActionTest, IconIndex) {
89 ExtensionAction::PathToIconCache cache;
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
62 ASSERT_EQ(-1, action.GetIconIndex(1)); 96 ASSERT_EQ(-1, action.GetIconIndex(1));
63 action.icon_paths()->push_back("foo.png"); 97 action.icon_paths()->push_back("foo.png");
64 action.icon_paths()->push_back("bar.png"); 98 action.icon_paths()->push_back("bar.png");
65 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); 99 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1);
66 ASSERT_EQ(1, action.GetIconIndex(1)); 100 ASSERT_EQ(1, action.GetIconIndex(1));
67 ASSERT_EQ(1, action.GetIconIndex(100)); 101 ASSERT_EQ(1, action.GetIconIndex(100));
102 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100, cache)))
103 << "Non-loaded icon gets the puzzle piece.";
104 cache["bar.png"] = icon1;
105 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100, cache)));
106
68 action.SetIconIndex(100, 0); 107 action.SetIconIndex(100, 0);
69 ASSERT_EQ(0, action.GetIconIndex(100)); 108 ASSERT_EQ(0, action.GetIconIndex(100));
70 ASSERT_EQ(1, action.GetIconIndex(1)); 109 ASSERT_EQ(1, action.GetIconIndex(1));
71 action.ClearAllValuesForTab(100); 110 action.ClearAllValuesForTab(100);
72 ASSERT_EQ(1, action.GetIconIndex(100)); 111 ASSERT_EQ(1, action.GetIconIndex(100));
73 ASSERT_EQ(1, action.GetIconIndex(1)); 112 ASSERT_EQ(1, action.GetIconIndex(1));
113 }
74 114
75 // visibility 115 TEST_F(ExtensionActionTest, Visibility) {
76 ASSERT_FALSE(action.GetIsVisible(1)); 116 ASSERT_FALSE(action.GetIsVisible(1));
77 action.SetIsVisible(ExtensionAction::kDefaultTabId, true); 117 action.SetIsVisible(ExtensionAction::kDefaultTabId, true);
78 ASSERT_TRUE(action.GetIsVisible(1)); 118 ASSERT_TRUE(action.GetIsVisible(1));
79 ASSERT_TRUE(action.GetIsVisible(100)); 119 ASSERT_TRUE(action.GetIsVisible(100));
80 action.SetIsVisible(ExtensionAction::kDefaultTabId, false); 120 action.SetIsVisible(ExtensionAction::kDefaultTabId, false);
81 ASSERT_FALSE(action.GetIsVisible(1)); 121 ASSERT_FALSE(action.GetIsVisible(1));
82 ASSERT_FALSE(action.GetIsVisible(100)); 122 ASSERT_FALSE(action.GetIsVisible(100));
83 action.SetIsVisible(100, true); 123 action.SetIsVisible(100, true);
84 ASSERT_FALSE(action.GetIsVisible(1)); 124 ASSERT_FALSE(action.GetIsVisible(1));
85 ASSERT_TRUE(action.GetIsVisible(100)); 125 ASSERT_TRUE(action.GetIsVisible(100));
86 action.ClearAllValuesForTab(100); 126 action.ClearAllValuesForTab(100);
87 ASSERT_FALSE(action.GetIsVisible(1)); 127 ASSERT_FALSE(action.GetIsVisible(1));
88 ASSERT_FALSE(action.GetIsVisible(100)); 128 ASSERT_FALSE(action.GetIsVisible(100));
129 }
89 130
90 // badge text 131 TEST_F(ExtensionActionTest, Badge) {
91 ASSERT_EQ("", action.GetBadgeText(1)); 132 ASSERT_EQ("", action.GetBadgeText(1));
92 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); 133 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
93 ASSERT_EQ("foo", action.GetBadgeText(1)); 134 ASSERT_EQ("foo", action.GetBadgeText(1));
94 ASSERT_EQ("foo", action.GetBadgeText(100)); 135 ASSERT_EQ("foo", action.GetBadgeText(100));
95 action.SetBadgeText(100, "bar"); 136 action.SetBadgeText(100, "bar");
96 ASSERT_EQ("foo", action.GetBadgeText(1)); 137 ASSERT_EQ("foo", action.GetBadgeText(1));
97 ASSERT_EQ("bar", action.GetBadgeText(100)); 138 ASSERT_EQ("bar", action.GetBadgeText(100));
98 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); 139 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
99 ASSERT_EQ("baz", action.GetBadgeText(1)); 140 ASSERT_EQ("baz", action.GetBadgeText(1));
100 action.ClearAllValuesForTab(100); 141 action.ClearAllValuesForTab(100);
101 ASSERT_EQ("baz", action.GetBadgeText(100)); 142 ASSERT_EQ("baz", action.GetBadgeText(100));
143 }
102 144
103 // badge text color 145 TEST_F(ExtensionActionTest, BadgeTextColor) {
104 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); 146 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1));
105 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); 147 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
106 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 148 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
107 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); 149 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100));
108 action.SetBadgeTextColor(100, 0xFF00FF00); 150 action.SetBadgeTextColor(100, 0xFF00FF00);
109 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 151 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
110 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); 152 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100));
111 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); 153 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
112 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); 154 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1));
113 action.ClearAllValuesForTab(100); 155 action.ClearAllValuesForTab(100);
114 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); 156 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100));
157 }
115 158
116 // badge background color 159 TEST_F(ExtensionActionTest, BadgeBackgroundColor) {
117 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); 160 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1));
118 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 161 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
119 0xFFFF0000u); 162 0xFFFF0000u);
120 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 163 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
121 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); 164 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100));
122 action.SetBadgeBackgroundColor(100, 0xFF00FF00); 165 action.SetBadgeBackgroundColor(100, 0xFF00FF00);
123 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 166 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
124 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); 167 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100));
125 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 168 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
126 0xFF0000FFu); 169 0xFF0000FFu);
127 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); 170 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
128 action.ClearAllValuesForTab(100); 171 action.ClearAllValuesForTab(100);
129 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); 172 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
173 }
130 174
131 // popup url 175 TEST_F(ExtensionActionTest, PopupUrl) {
132 GURL url_unset; 176 GURL url_unset;
133 GURL url_foo("http://www.example.com/foo.html"); 177 GURL url_foo("http://www.example.com/foo.html");
134 GURL url_bar("http://www.example.com/bar.html"); 178 GURL url_bar("http://www.example.com/bar.html");
135 GURL url_baz("http://www.example.com/baz.html"); 179 GURL url_baz("http://www.example.com/baz.html");
136 180
137 ASSERT_EQ(url_unset, action.GetPopupUrl(1)); 181 ASSERT_EQ(url_unset, action.GetPopupUrl(1));
138 ASSERT_EQ(url_unset, action.GetPopupUrl(100)); 182 ASSERT_EQ(url_unset, action.GetPopupUrl(100));
139 ASSERT_FALSE(action.HasPopup(1)); 183 ASSERT_FALSE(action.HasPopup(1));
140 ASSERT_FALSE(action.HasPopup(100)); 184 ASSERT_FALSE(action.HasPopup(100));
141 185
142 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); 186 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo);
143 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 187 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
144 ASSERT_EQ(url_foo, action.GetPopupUrl(100)); 188 ASSERT_EQ(url_foo, action.GetPopupUrl(100));
145 189
146 action.SetPopupUrl(100, url_bar); 190 action.SetPopupUrl(100, url_bar);
147 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 191 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
148 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 192 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
149 193
150 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); 194 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz);
151 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 195 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
152 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 196 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
153 197
154 action.ClearAllValuesForTab(100); 198 action.ClearAllValuesForTab(100);
155 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 199 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
156 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); 200 ASSERT_EQ(url_baz, action.GetPopupUrl(100));
157 } 201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698