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

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: Fix kalman's comments. Created 8 years, 4 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 namespace {
17 20
18 static SkBitmap LoadIcon(const std::string& filename) { 21 bool ImagesAreEqual(const gfx::Image& i1, const gfx::Image& i2) {
22 return gfx::BitmapsAreEqual(*i1.ToSkBitmap(), *i2.ToSkBitmap());
23 }
24
25 gfx::Image LoadIcon(const std::string& filename) {
19 FilePath path; 26 FilePath path;
20 PathService::Get(chrome::DIR_TEST_DATA, &path); 27 PathService::Get(chrome::DIR_TEST_DATA, &path);
21 path = path.AppendASCII("extensions").AppendASCII(filename); 28 path = path.AppendASCII("extensions").AppendASCII(filename);
22 29
23 std::string file_contents; 30 std::string file_contents;
24 file_util::ReadFileToString(path, &file_contents); 31 file_util::ReadFileToString(path, &file_contents);
25 const unsigned char* data = 32 const unsigned char* data =
26 reinterpret_cast<const unsigned char*>(file_contents.data()); 33 reinterpret_cast<const unsigned char*>(file_contents.data());
27 34
28 SkBitmap bitmap; 35 SkBitmap bitmap;
29 webkit_glue::ImageDecoder decoder; 36 webkit_glue::ImageDecoder decoder;
30 bitmap = decoder.Decode(data, file_contents.length()); 37 bitmap = decoder.Decode(data, file_contents.length());
31 38
32 return bitmap; 39 return gfx::Image(bitmap);
33 } 40 }
34 41
35 TEST(ExtensionActionTest, TabSpecificState) { 42 class ExtensionActionTest : public testing::Test {
36 ExtensionAction action("", ExtensionAction::TYPE_PAGE); 43 public:
44 ExtensionActionTest()
45 : action("", ExtensionAction::TYPE_PAGE) {
46 }
37 47
38 // title 48 ExtensionAction action;
49 };
50
51 TEST_F(ExtensionActionTest, Title) {
39 ASSERT_EQ("", action.GetTitle(1)); 52 ASSERT_EQ("", action.GetTitle(1));
40 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); 53 action.SetTitle(ExtensionAction::kDefaultTabId, "foo");
41 ASSERT_EQ("foo", action.GetTitle(1)); 54 ASSERT_EQ("foo", action.GetTitle(1));
42 ASSERT_EQ("foo", action.GetTitle(100)); 55 ASSERT_EQ("foo", action.GetTitle(100));
43 action.SetTitle(100, "bar"); 56 action.SetTitle(100, "bar");
44 ASSERT_EQ("foo", action.GetTitle(1)); 57 ASSERT_EQ("foo", action.GetTitle(1));
45 ASSERT_EQ("bar", action.GetTitle(100)); 58 ASSERT_EQ("bar", action.GetTitle(100));
46 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); 59 action.SetTitle(ExtensionAction::kDefaultTabId, "baz");
47 ASSERT_EQ("baz", action.GetTitle(1)); 60 ASSERT_EQ("baz", action.GetTitle(1));
48 action.ClearAllValuesForTab(100); 61 action.ClearAllValuesForTab(100);
49 ASSERT_EQ("baz", action.GetTitle(100)); 62 ASSERT_EQ("baz", action.GetTitle(100));
63 }
50 64
51 // icon 65 TEST_F(ExtensionActionTest, Icon) {
52 SkBitmap icon1 = LoadIcon("icon1.png"); 66 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 67
61 // icon index 68 gfx::Image puzzle_piece =
69 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
70 IDR_EXTENSIONS_FAVICON);
71 gfx::Image icon1 = LoadIcon("icon1.png");
72 gfx::Image icon2 = LoadIcon("icon2.png");
73 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1, cache)));
74
75 action.set_default_icon_path("the_default.png");
76 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1, cache)))
77 << "Still returns the puzzle piece because the image isn't loaded yet.";
78 cache["the_default.png"] = icon2;
79 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1, cache)));
80
81 action.SetIcon(ExtensionAction::kDefaultTabId, *icon1.ToSkBitmap());
82 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100, cache)))
83 << "SetIcon(kDefaultTabId) overrides the default_icon_path.";
84
85 action.SetIcon(100, *icon2.ToSkBitmap());
86 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1, cache)));
87 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100, cache)));
88 }
89
90 TEST_F(ExtensionActionTest, IconIndex) {
91 ExtensionAction::PathToIconCache cache;
92 gfx::Image icon1 = LoadIcon("icon1.png");
93 gfx::Image icon2 = LoadIcon("icon2.png");
94 gfx::Image puzzle_piece =
95 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
96 IDR_EXTENSIONS_FAVICON);
97
62 ASSERT_EQ(-1, action.GetIconIndex(1)); 98 ASSERT_EQ(-1, action.GetIconIndex(1));
63 action.icon_paths()->push_back("foo.png"); 99 action.icon_paths()->push_back("foo.png");
64 action.icon_paths()->push_back("bar.png"); 100 action.icon_paths()->push_back("bar.png");
65 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); 101 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1);
66 ASSERT_EQ(1, action.GetIconIndex(1)); 102 ASSERT_EQ(1, action.GetIconIndex(1));
67 ASSERT_EQ(1, action.GetIconIndex(100)); 103 ASSERT_EQ(1, action.GetIconIndex(100));
104 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100, cache)))
105 << "Non-loaded icon gets the puzzle piece.";
106 cache["bar.png"] = icon1;
107 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100, cache)));
108
68 action.SetIconIndex(100, 0); 109 action.SetIconIndex(100, 0);
69 ASSERT_EQ(0, action.GetIconIndex(100)); 110 ASSERT_EQ(0, action.GetIconIndex(100));
70 ASSERT_EQ(1, action.GetIconIndex(1)); 111 ASSERT_EQ(1, action.GetIconIndex(1));
71 action.ClearAllValuesForTab(100); 112 action.ClearAllValuesForTab(100);
72 ASSERT_EQ(1, action.GetIconIndex(100)); 113 ASSERT_EQ(1, action.GetIconIndex(100));
73 ASSERT_EQ(1, action.GetIconIndex(1)); 114 ASSERT_EQ(1, action.GetIconIndex(1));
115 }
74 116
75 // visibility 117 TEST_F(ExtensionActionTest, Visibility) {
76 ASSERT_FALSE(action.GetIsVisible(1)); 118 ASSERT_FALSE(action.GetIsVisible(1));
77 action.SetIsVisible(ExtensionAction::kDefaultTabId, true); 119 action.SetIsVisible(ExtensionAction::kDefaultTabId, true);
78 ASSERT_TRUE(action.GetIsVisible(1)); 120 ASSERT_TRUE(action.GetIsVisible(1));
79 ASSERT_TRUE(action.GetIsVisible(100)); 121 ASSERT_TRUE(action.GetIsVisible(100));
80 action.SetIsVisible(ExtensionAction::kDefaultTabId, false); 122 action.SetIsVisible(ExtensionAction::kDefaultTabId, false);
81 ASSERT_FALSE(action.GetIsVisible(1)); 123 ASSERT_FALSE(action.GetIsVisible(1));
82 ASSERT_FALSE(action.GetIsVisible(100)); 124 ASSERT_FALSE(action.GetIsVisible(100));
83 action.SetIsVisible(100, true); 125 action.SetIsVisible(100, true);
84 ASSERT_FALSE(action.GetIsVisible(1)); 126 ASSERT_FALSE(action.GetIsVisible(1));
85 ASSERT_TRUE(action.GetIsVisible(100)); 127 ASSERT_TRUE(action.GetIsVisible(100));
86 action.ClearAllValuesForTab(100); 128 action.ClearAllValuesForTab(100);
87 ASSERT_FALSE(action.GetIsVisible(1)); 129 ASSERT_FALSE(action.GetIsVisible(1));
88 ASSERT_FALSE(action.GetIsVisible(100)); 130 ASSERT_FALSE(action.GetIsVisible(100));
131 }
89 132
90 // badge text 133 TEST_F(ExtensionActionTest, Badge) {
91 ASSERT_EQ("", action.GetBadgeText(1)); 134 ASSERT_EQ("", action.GetBadgeText(1));
92 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); 135 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
93 ASSERT_EQ("foo", action.GetBadgeText(1)); 136 ASSERT_EQ("foo", action.GetBadgeText(1));
94 ASSERT_EQ("foo", action.GetBadgeText(100)); 137 ASSERT_EQ("foo", action.GetBadgeText(100));
95 action.SetBadgeText(100, "bar"); 138 action.SetBadgeText(100, "bar");
96 ASSERT_EQ("foo", action.GetBadgeText(1)); 139 ASSERT_EQ("foo", action.GetBadgeText(1));
97 ASSERT_EQ("bar", action.GetBadgeText(100)); 140 ASSERT_EQ("bar", action.GetBadgeText(100));
98 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); 141 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
99 ASSERT_EQ("baz", action.GetBadgeText(1)); 142 ASSERT_EQ("baz", action.GetBadgeText(1));
100 action.ClearAllValuesForTab(100); 143 action.ClearAllValuesForTab(100);
101 ASSERT_EQ("baz", action.GetBadgeText(100)); 144 ASSERT_EQ("baz", action.GetBadgeText(100));
145 }
102 146
103 // badge text color 147 TEST_F(ExtensionActionTest, BadgeTextColor) {
104 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); 148 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1));
105 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); 149 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
106 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 150 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
107 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); 151 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100));
108 action.SetBadgeTextColor(100, 0xFF00FF00); 152 action.SetBadgeTextColor(100, 0xFF00FF00);
109 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 153 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
110 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); 154 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100));
111 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); 155 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
112 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); 156 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1));
113 action.ClearAllValuesForTab(100); 157 action.ClearAllValuesForTab(100);
114 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); 158 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100));
159 }
115 160
116 // badge background color 161 TEST_F(ExtensionActionTest, BadgeBackgroundColor) {
117 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); 162 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1));
118 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 163 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
119 0xFFFF0000u); 164 0xFFFF0000u);
120 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 165 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
121 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); 166 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100));
122 action.SetBadgeBackgroundColor(100, 0xFF00FF00); 167 action.SetBadgeBackgroundColor(100, 0xFF00FF00);
123 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 168 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
124 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); 169 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100));
125 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 170 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
126 0xFF0000FFu); 171 0xFF0000FFu);
127 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); 172 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
128 action.ClearAllValuesForTab(100); 173 action.ClearAllValuesForTab(100);
129 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); 174 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
175 }
130 176
131 // popup url 177 TEST_F(ExtensionActionTest, PopupUrl) {
132 GURL url_unset; 178 GURL url_unset;
133 GURL url_foo("http://www.example.com/foo.html"); 179 GURL url_foo("http://www.example.com/foo.html");
134 GURL url_bar("http://www.example.com/bar.html"); 180 GURL url_bar("http://www.example.com/bar.html");
135 GURL url_baz("http://www.example.com/baz.html"); 181 GURL url_baz("http://www.example.com/baz.html");
136 182
137 ASSERT_EQ(url_unset, action.GetPopupUrl(1)); 183 ASSERT_EQ(url_unset, action.GetPopupUrl(1));
138 ASSERT_EQ(url_unset, action.GetPopupUrl(100)); 184 ASSERT_EQ(url_unset, action.GetPopupUrl(100));
139 ASSERT_FALSE(action.HasPopup(1)); 185 ASSERT_FALSE(action.HasPopup(1));
140 ASSERT_FALSE(action.HasPopup(100)); 186 ASSERT_FALSE(action.HasPopup(100));
141 187
142 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); 188 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo);
143 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 189 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
144 ASSERT_EQ(url_foo, action.GetPopupUrl(100)); 190 ASSERT_EQ(url_foo, action.GetPopupUrl(100));
145 191
146 action.SetPopupUrl(100, url_bar); 192 action.SetPopupUrl(100, url_bar);
147 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 193 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
148 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 194 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
149 195
150 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); 196 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz);
151 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 197 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
152 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 198 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
153 199
154 action.ClearAllValuesForTab(100); 200 action.ClearAllValuesForTab(100);
155 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 201 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
156 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); 202 ASSERT_EQ(url_baz, action.GetPopupUrl(100));
157 } 203 }
204
205 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698