[email protected] | 3349b59 | 2012-04-26 12:35:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 5 | #include "base/file_path.h" |
| 6 | #include "base/file_util.h" |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 7 | #include "base/message_loop.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 8 | #include "base/path_service.h" |
| 9 | #include "chrome/common/chrome_paths.h" |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 10 | #include "chrome/common/extensions/extension_action.h" |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 11 | #include "googleurl/src/gurl.h" |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 12 | #include "grit/theme_resources.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 15 | #include "ui/base/resource/resource_bundle.h" |
| 16 | #include "ui/gfx/image/image.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 17 | #include "ui/gfx/skia_util.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 18 | #include "webkit/glue/image_decoder.h" |
| 19 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 20 | namespace { |
[email protected] | 18539ee | 2010-09-16 21:39:29 | [diff] [blame] | 21 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 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) { |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 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 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 40 | return gfx::Image(bitmap); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 43 | class ExtensionActionTest : public testing::Test { |
| 44 | public: |
| 45 | ExtensionActionTest() |
| 46 | : action("", ExtensionAction::TYPE_PAGE) { |
| 47 | } |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 48 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 49 | ExtensionAction action; |
| 50 | }; |
| 51 | |
| 52 | TEST_F(ExtensionActionTest, Title) { |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 53 | ASSERT_EQ("", action.GetTitle(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 54 | action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 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)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 60 | action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 61 | ASSERT_EQ("baz", action.GetTitle(1)); |
| 62 | action.ClearAllValuesForTab(100); |
| 63 | ASSERT_EQ("baz", action.GetTitle(100)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 64 | } |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 65 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 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))); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 73 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 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.ToSkBitmap()); |
| 81 | ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100))) |
| 82 | << "SetIcon(kDefaultTabId) overrides the default_icon_path."; |
| 83 | |
| 84 | action.SetIcon(100, *icon2.ToSkBitmap()); |
| 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 | |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 96 | ASSERT_EQ(-1, action.GetIconIndex(1)); |
| 97 | action.icon_paths()->push_back("foo.png"); |
| 98 | action.icon_paths()->push_back("bar.png"); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 99 | action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 100 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 101 | ASSERT_EQ(1, action.GetIconIndex(100)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 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 | |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 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)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 113 | } |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 114 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 115 | TEST_F(ExtensionActionTest, Visibility) { |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 116 | // Supports the icon animation. |
| 117 | MessageLoop message_loop; |
| 118 | |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 119 | ASSERT_FALSE(action.GetIsVisible(1)); |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 120 | EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); |
| 121 | action.SetAppearance(ExtensionAction::kDefaultTabId, ExtensionAction::ACTIVE); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 122 | ASSERT_TRUE(action.GetIsVisible(1)); |
| 123 | ASSERT_TRUE(action.GetIsVisible(100)); |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 124 | EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); |
| 125 | |
| 126 | action.SetAppearance(ExtensionAction::kDefaultTabId, |
| 127 | ExtensionAction::INVISIBLE); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 128 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 129 | ASSERT_FALSE(action.GetIsVisible(100)); |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 130 | EXPECT_FALSE(action.GetIconAnimation(100)); |
| 131 | action.SetAppearance(100, ExtensionAction::ACTIVE); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 132 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 133 | ASSERT_TRUE(action.GetIsVisible(100)); |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 134 | EXPECT_TRUE(action.GetIconAnimation(100)); |
| 135 | |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 136 | action.ClearAllValuesForTab(100); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 137 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 138 | ASSERT_FALSE(action.GetIsVisible(100)); |
[email protected] | 48a7a425b | 2012-08-03 12:52:58 | [diff] [blame^] | 139 | EXPECT_FALSE(action.GetIconAnimation(100)); |
| 140 | } |
| 141 | |
| 142 | TEST_F(ExtensionActionTest, GetAttention) { |
| 143 | // Supports the icon animation. |
| 144 | MessageLoop message_loop; |
| 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 | action.GetIconAnimation(1)->Stop(); |
| 154 | EXPECT_FALSE(action.GetIconAnimation(1)); // Sanity check. |
| 155 | |
| 156 | action.SetAppearance(1, ExtensionAction::ACTIVE); |
| 157 | EXPECT_FALSE(action.GetIconAnimation(1)) |
| 158 | << "The animation should not play again if the icon was already visible."; |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 159 | } |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 160 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 161 | TEST_F(ExtensionActionTest, Badge) { |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 162 | ASSERT_EQ("", action.GetBadgeText(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 163 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 164 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 165 | ASSERT_EQ("foo", action.GetBadgeText(100)); |
| 166 | action.SetBadgeText(100, "bar"); |
| 167 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 168 | ASSERT_EQ("bar", action.GetBadgeText(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 169 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 170 | ASSERT_EQ("baz", action.GetBadgeText(1)); |
| 171 | action.ClearAllValuesForTab(100); |
| 172 | ASSERT_EQ("baz", action.GetBadgeText(100)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 173 | } |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 174 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 175 | TEST_F(ExtensionActionTest, BadgeTextColor) { |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 176 | ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 177 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 178 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 179 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); |
| 180 | action.SetBadgeTextColor(100, 0xFF00FF00); |
| 181 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 182 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 183 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 184 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); |
| 185 | action.ClearAllValuesForTab(100); |
| 186 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 187 | } |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 188 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 189 | TEST_F(ExtensionActionTest, BadgeBackgroundColor) { |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 190 | ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 191 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 192 | 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 193 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 194 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); |
| 195 | action.SetBadgeBackgroundColor(100, 0xFF00FF00); |
| 196 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 197 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 198 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 199 | 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 200 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); |
| 201 | action.ClearAllValuesForTab(100); |
| 202 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 203 | } |
[email protected] | a388c484 | 2010-01-27 17:13:15 | [diff] [blame] | 204 | |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 205 | TEST_F(ExtensionActionTest, PopupUrl) { |
[email protected] | a388c484 | 2010-01-27 17:13:15 | [diff] [blame] | 206 | GURL url_unset; |
| 207 | GURL url_foo("http://www.example.com/foo.html"); |
| 208 | GURL url_bar("http://www.example.com/bar.html"); |
| 209 | GURL url_baz("http://www.example.com/baz.html"); |
| 210 | |
| 211 | ASSERT_EQ(url_unset, action.GetPopupUrl(1)); |
| 212 | ASSERT_EQ(url_unset, action.GetPopupUrl(100)); |
| 213 | ASSERT_FALSE(action.HasPopup(1)); |
| 214 | ASSERT_FALSE(action.HasPopup(100)); |
| 215 | |
| 216 | action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); |
| 217 | ASSERT_EQ(url_foo, action.GetPopupUrl(1)); |
| 218 | ASSERT_EQ(url_foo, action.GetPopupUrl(100)); |
| 219 | |
| 220 | action.SetPopupUrl(100, url_bar); |
| 221 | ASSERT_EQ(url_foo, action.GetPopupUrl(1)); |
| 222 | ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 223 | |
| 224 | action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); |
| 225 | ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 226 | ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 227 | |
| 228 | action.ClearAllValuesForTab(100); |
| 229 | ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 230 | ASSERT_EQ(url_baz, action.GetPopupUrl(100)); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 231 | } |
[email protected] | 39eae2b7 | 2012-08-01 07:22:32 | [diff] [blame] | 232 | |
| 233 | } // namespace |