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

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: Move the icon cache inside ExtensionAction. 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 gfx::Image puzzle_piece =
53 SkBitmap icon2 = LoadIcon("icon2.png"); 67 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
54 ASSERT_TRUE(action.GetIcon(1).isNull()); 68 IDR_EXTENSIONS_FAVICON);
55 action.SetIcon(ExtensionAction::kDefaultTabId, icon1); 69 gfx::Image icon1 = LoadIcon("icon1.png");
56 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(100))); 70 gfx::Image icon2 = LoadIcon("icon2.png");
57 action.SetIcon(100, icon2); 71 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1)));
58 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(1)));
59 ASSERT_TRUE(BitmapsAreEqual(icon2, action.GetIcon(100)));
60 72
61 // icon index 73 action.set_default_icon_path("the_default.png");
74 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1)))
75 << "Still returns the puzzle piece because the image isn't loaded yet.";
76 action.CacheIcon("the_default.png", icon2);
77 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1)));
78
79 action.SetIcon(ExtensionAction::kDefaultTabId, *icon1.ToSkBitmap());
80 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100)))
81 << "SetIcon(kDefaultTabId) overrides the default_icon_path.";
82
83 action.SetIcon(100, *icon2.ToSkBitmap());
84 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1)));
85 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100)));
86 }
87
88 TEST_F(ExtensionActionTest, IconIndex) {
89 gfx::Image icon1 = LoadIcon("icon1.png");
90 gfx::Image icon2 = LoadIcon("icon2.png");
91 gfx::Image puzzle_piece =
92 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
93 IDR_EXTENSIONS_FAVICON);
94
62 ASSERT_EQ(-1, action.GetIconIndex(1)); 95 ASSERT_EQ(-1, action.GetIconIndex(1));
63 action.icon_paths()->push_back("foo.png"); 96 action.icon_paths()->push_back("foo.png");
64 action.icon_paths()->push_back("bar.png"); 97 action.icon_paths()->push_back("bar.png");
65 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); 98 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1);
66 ASSERT_EQ(1, action.GetIconIndex(1)); 99 ASSERT_EQ(1, action.GetIconIndex(1));
67 ASSERT_EQ(1, action.GetIconIndex(100)); 100 ASSERT_EQ(1, action.GetIconIndex(100));
101 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100)))
102 << "Non-loaded icon gets the puzzle piece.";
103 action.CacheIcon("bar.png", icon1);
104 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100)));
105
68 action.SetIconIndex(100, 0); 106 action.SetIconIndex(100, 0);
69 ASSERT_EQ(0, action.GetIconIndex(100)); 107 ASSERT_EQ(0, action.GetIconIndex(100));
70 ASSERT_EQ(1, action.GetIconIndex(1)); 108 ASSERT_EQ(1, action.GetIconIndex(1));
71 action.ClearAllValuesForTab(100); 109 action.ClearAllValuesForTab(100);
72 ASSERT_EQ(1, action.GetIconIndex(100)); 110 ASSERT_EQ(1, action.GetIconIndex(100));
73 ASSERT_EQ(1, action.GetIconIndex(1)); 111 ASSERT_EQ(1, action.GetIconIndex(1));
112 }
74 113
75 // visibility 114 TEST_F(ExtensionActionTest, Visibility) {
76 ASSERT_FALSE(action.GetIsVisible(1)); 115 ASSERT_FALSE(action.GetIsVisible(1));
77 action.SetIsVisible(ExtensionAction::kDefaultTabId, true); 116 action.SetIsVisible(ExtensionAction::kDefaultTabId, true);
78 ASSERT_TRUE(action.GetIsVisible(1)); 117 ASSERT_TRUE(action.GetIsVisible(1));
79 ASSERT_TRUE(action.GetIsVisible(100)); 118 ASSERT_TRUE(action.GetIsVisible(100));
80 action.SetIsVisible(ExtensionAction::kDefaultTabId, false); 119 action.SetIsVisible(ExtensionAction::kDefaultTabId, false);
81 ASSERT_FALSE(action.GetIsVisible(1)); 120 ASSERT_FALSE(action.GetIsVisible(1));
82 ASSERT_FALSE(action.GetIsVisible(100)); 121 ASSERT_FALSE(action.GetIsVisible(100));
83 action.SetIsVisible(100, true); 122 action.SetIsVisible(100, true);
84 ASSERT_FALSE(action.GetIsVisible(1)); 123 ASSERT_FALSE(action.GetIsVisible(1));
85 ASSERT_TRUE(action.GetIsVisible(100)); 124 ASSERT_TRUE(action.GetIsVisible(100));
86 action.ClearAllValuesForTab(100); 125 action.ClearAllValuesForTab(100);
87 ASSERT_FALSE(action.GetIsVisible(1)); 126 ASSERT_FALSE(action.GetIsVisible(1));
88 ASSERT_FALSE(action.GetIsVisible(100)); 127 ASSERT_FALSE(action.GetIsVisible(100));
128 }
89 129
90 // badge text 130 TEST_F(ExtensionActionTest, Badge) {
91 ASSERT_EQ("", action.GetBadgeText(1)); 131 ASSERT_EQ("", action.GetBadgeText(1));
92 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); 132 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
93 ASSERT_EQ("foo", action.GetBadgeText(1)); 133 ASSERT_EQ("foo", action.GetBadgeText(1));
94 ASSERT_EQ("foo", action.GetBadgeText(100)); 134 ASSERT_EQ("foo", action.GetBadgeText(100));
95 action.SetBadgeText(100, "bar"); 135 action.SetBadgeText(100, "bar");
96 ASSERT_EQ("foo", action.GetBadgeText(1)); 136 ASSERT_EQ("foo", action.GetBadgeText(1));
97 ASSERT_EQ("bar", action.GetBadgeText(100)); 137 ASSERT_EQ("bar", action.GetBadgeText(100));
98 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); 138 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
99 ASSERT_EQ("baz", action.GetBadgeText(1)); 139 ASSERT_EQ("baz", action.GetBadgeText(1));
100 action.ClearAllValuesForTab(100); 140 action.ClearAllValuesForTab(100);
101 ASSERT_EQ("baz", action.GetBadgeText(100)); 141 ASSERT_EQ("baz", action.GetBadgeText(100));
142 }
102 143
103 // badge text color 144 TEST_F(ExtensionActionTest, BadgeTextColor) {
104 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); 145 ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1));
105 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); 146 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
106 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 147 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
107 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); 148 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100));
108 action.SetBadgeTextColor(100, 0xFF00FF00); 149 action.SetBadgeTextColor(100, 0xFF00FF00);
109 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); 150 ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1));
110 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); 151 ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100));
111 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); 152 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
112 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); 153 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1));
113 action.ClearAllValuesForTab(100); 154 action.ClearAllValuesForTab(100);
114 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); 155 ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100));
156 }
115 157
116 // badge background color 158 TEST_F(ExtensionActionTest, BadgeBackgroundColor) {
117 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); 159 ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1));
118 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 160 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
119 0xFFFF0000u); 161 0xFFFF0000u);
120 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 162 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
121 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); 163 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100));
122 action.SetBadgeBackgroundColor(100, 0xFF00FF00); 164 action.SetBadgeBackgroundColor(100, 0xFF00FF00);
123 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); 165 ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1));
124 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); 166 ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100));
125 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, 167 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
126 0xFF0000FFu); 168 0xFF0000FFu);
127 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); 169 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
128 action.ClearAllValuesForTab(100); 170 action.ClearAllValuesForTab(100);
129 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); 171 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
172 }
130 173
131 // popup url 174 TEST_F(ExtensionActionTest, PopupUrl) {
132 GURL url_unset; 175 GURL url_unset;
133 GURL url_foo("http://www.example.com/foo.html"); 176 GURL url_foo("http://www.example.com/foo.html");
134 GURL url_bar("http://www.example.com/bar.html"); 177 GURL url_bar("http://www.example.com/bar.html");
135 GURL url_baz("http://www.example.com/baz.html"); 178 GURL url_baz("http://www.example.com/baz.html");
136 179
137 ASSERT_EQ(url_unset, action.GetPopupUrl(1)); 180 ASSERT_EQ(url_unset, action.GetPopupUrl(1));
138 ASSERT_EQ(url_unset, action.GetPopupUrl(100)); 181 ASSERT_EQ(url_unset, action.GetPopupUrl(100));
139 ASSERT_FALSE(action.HasPopup(1)); 182 ASSERT_FALSE(action.HasPopup(1));
140 ASSERT_FALSE(action.HasPopup(100)); 183 ASSERT_FALSE(action.HasPopup(100));
141 184
142 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); 185 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo);
143 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 186 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
144 ASSERT_EQ(url_foo, action.GetPopupUrl(100)); 187 ASSERT_EQ(url_foo, action.GetPopupUrl(100));
145 188
146 action.SetPopupUrl(100, url_bar); 189 action.SetPopupUrl(100, url_bar);
147 ASSERT_EQ(url_foo, action.GetPopupUrl(1)); 190 ASSERT_EQ(url_foo, action.GetPopupUrl(1));
148 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 191 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
149 192
150 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); 193 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz);
151 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 194 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
152 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); 195 ASSERT_EQ(url_bar, action.GetPopupUrl(100));
153 196
154 action.ClearAllValuesForTab(100); 197 action.ClearAllValuesForTab(100);
155 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); 198 ASSERT_EQ(url_baz, action.GetPopupUrl(1));
156 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); 199 ASSERT_EQ(url_baz, action.GetPopupUrl(100));
157 } 200 }
201
202 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698