| OLD | NEW |
| 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" | |
| 6 #include "base/file_util.h" | |
| 7 #include "base/message_loop.h" | 5 #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" | 6 #include "chrome/common/extensions/extension_action.h" |
| 11 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
| 12 #include "grit/theme_resources.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 8 #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 | 9 |
| 20 namespace { | 10 namespace { |
| 21 | 11 |
| 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 { | 12 class ExtensionActionTest : public testing::Test { |
| 44 public: | 13 public: |
| 45 ExtensionActionTest() | 14 ExtensionActionTest() |
| 46 : action("", ExtensionAction::TYPE_PAGE) { | 15 : action("", ExtensionAction::TYPE_PAGE) { |
| 47 } | 16 } |
| 48 | 17 |
| 49 ExtensionAction action; | 18 ExtensionAction action; |
| 50 }; | 19 }; |
| 51 | 20 |
| 52 TEST_F(ExtensionActionTest, Title) { | 21 TEST_F(ExtensionActionTest, Title) { |
| 53 ASSERT_EQ("", action.GetTitle(1)); | 22 ASSERT_EQ("", action.GetTitle(1)); |
| 54 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); | 23 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); |
| 55 ASSERT_EQ("foo", action.GetTitle(1)); | 24 ASSERT_EQ("foo", action.GetTitle(1)); |
| 56 ASSERT_EQ("foo", action.GetTitle(100)); | 25 ASSERT_EQ("foo", action.GetTitle(100)); |
| 57 action.SetTitle(100, "bar"); | 26 action.SetTitle(100, "bar"); |
| 58 ASSERT_EQ("foo", action.GetTitle(1)); | 27 ASSERT_EQ("foo", action.GetTitle(1)); |
| 59 ASSERT_EQ("bar", action.GetTitle(100)); | 28 ASSERT_EQ("bar", action.GetTitle(100)); |
| 60 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); | 29 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); |
| 61 ASSERT_EQ("baz", action.GetTitle(1)); | 30 ASSERT_EQ("baz", action.GetTitle(1)); |
| 62 action.ClearAllValuesForTab(100); | 31 action.ClearAllValuesForTab(100); |
| 63 ASSERT_EQ("baz", action.GetTitle(100)); | 32 ASSERT_EQ("baz", action.GetTitle(100)); |
| 64 } | 33 } |
| 65 | 34 |
| 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(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, Visibility) { | 35 TEST_F(ExtensionActionTest, Visibility) { |
| 90 // Supports the icon animation. | 36 // Supports the icon animation. |
| 91 MessageLoop message_loop; | 37 MessageLoop message_loop; |
| 92 | 38 |
| 93 ASSERT_FALSE(action.GetIsVisible(1)); | 39 ASSERT_FALSE(action.GetIsVisible(1)); |
| 94 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); | 40 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); |
| 95 action.SetAppearance(ExtensionAction::kDefaultTabId, ExtensionAction::ACTIVE); | 41 action.SetAppearance(ExtensionAction::kDefaultTabId, ExtensionAction::ACTIVE); |
| 96 ASSERT_TRUE(action.GetIsVisible(1)); | 42 ASSERT_TRUE(action.GetIsVisible(1)); |
| 97 ASSERT_TRUE(action.GetIsVisible(100)); | 43 ASSERT_TRUE(action.GetIsVisible(100)); |
| 98 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); | 44 EXPECT_FALSE(action.GetIconAnimation(ExtensionAction::kDefaultTabId)); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); | 145 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); |
| 200 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | 146 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 201 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); | 147 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 202 | 148 |
| 203 action.ClearAllValuesForTab(100); | 149 action.ClearAllValuesForTab(100); |
| 204 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | 150 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 205 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); | 151 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); |
| 206 } | 152 } |
| 207 | 153 |
| 208 } // namespace | 154 } // namespace |
| OLD | NEW |