| 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" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "chrome/common/extensions/extension_action.h" | 10 #include "chrome/common/extensions/extension_action.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image_skia.h" |
| 17 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
| 18 #include "webkit/glue/image_decoder.h" | 18 #include "webkit/glue/image_decoder.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 bool ImagesAreEqual(const gfx::Image& i1, const gfx::Image& i2) { | 22 bool ImagesAreEqual(const gfx::ImageSkia& i1, const gfx::ImageSkia& i2) { |
| 23 return gfx::BitmapsAreEqual(*i1.ToSkBitmap(), *i2.ToSkBitmap()); | 23 return gfx::BitmapsAreEqual(*i1.bitmap(), *i2.bitmap()); |
| 24 } | 24 } |
| 25 | 25 |
| 26 gfx::Image LoadIcon(const std::string& filename) { | 26 gfx::ImageSkia LoadIcon(const std::string& filename) { |
| 27 FilePath path; | 27 FilePath path; |
| 28 PathService::Get(chrome::DIR_TEST_DATA, &path); | 28 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 29 path = path.AppendASCII("extensions").AppendASCII(filename); | 29 path = path.AppendASCII("extensions").AppendASCII(filename); |
| 30 | 30 |
| 31 std::string file_contents; | 31 std::string file_contents; |
| 32 file_util::ReadFileToString(path, &file_contents); | 32 file_util::ReadFileToString(path, &file_contents); |
| 33 const unsigned char* data = | 33 const unsigned char* data = |
| 34 reinterpret_cast<const unsigned char*>(file_contents.data()); | 34 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 35 | 35 |
| 36 SkBitmap bitmap; | 36 SkBitmap bitmap; |
| 37 webkit_glue::ImageDecoder decoder; | 37 webkit_glue::ImageDecoder decoder; |
| 38 bitmap = decoder.Decode(data, file_contents.length()); | 38 bitmap = decoder.Decode(data, file_contents.length()); |
| 39 | 39 |
| 40 return gfx::Image(bitmap); | 40 return gfx::ImageSkia(bitmap); |
| 41 } | 41 } |
| 42 | 42 |
| 43 class ExtensionActionTest : public testing::Test { | 43 class ExtensionActionTest : public testing::Test { |
| 44 public: | 44 public: |
| 45 ExtensionActionTest() | 45 ExtensionActionTest() |
| 46 : action("", ExtensionAction::TYPE_PAGE) { | 46 : action("", ExtensionAction::TYPE_PAGE) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 ExtensionAction action; | 49 ExtensionAction action; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 TEST_F(ExtensionActionTest, Title) { | 52 TEST_F(ExtensionActionTest, Title) { |
| 53 ASSERT_EQ("", action.GetTitle(1)); | 53 ASSERT_EQ("", action.GetTitle(1)); |
| 54 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); | 54 action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); |
| 55 ASSERT_EQ("foo", action.GetTitle(1)); | 55 ASSERT_EQ("foo", action.GetTitle(1)); |
| 56 ASSERT_EQ("foo", action.GetTitle(100)); | 56 ASSERT_EQ("foo", action.GetTitle(100)); |
| 57 action.SetTitle(100, "bar"); | 57 action.SetTitle(100, "bar"); |
| 58 ASSERT_EQ("foo", action.GetTitle(1)); | 58 ASSERT_EQ("foo", action.GetTitle(1)); |
| 59 ASSERT_EQ("bar", action.GetTitle(100)); | 59 ASSERT_EQ("bar", action.GetTitle(100)); |
| 60 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); | 60 action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); |
| 61 ASSERT_EQ("baz", action.GetTitle(1)); | 61 ASSERT_EQ("baz", action.GetTitle(1)); |
| 62 action.ClearAllValuesForTab(100); | 62 action.ClearAllValuesForTab(100); |
| 63 ASSERT_EQ("baz", action.GetTitle(100)); | 63 ASSERT_EQ("baz", action.GetTitle(100)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 TEST_F(ExtensionActionTest, Icon) { | 66 TEST_F(ExtensionActionTest, Icon) { |
| 67 gfx::Image puzzle_piece = | 67 gfx::ImageSkia puzzle_piece = |
| 68 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 68 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 69 IDR_EXTENSIONS_FAVICON); | 69 IDR_EXTENSIONS_FAVICON); |
| 70 gfx::Image icon1 = LoadIcon("icon1.png"); | 70 gfx::ImageSkia icon1 = LoadIcon("icon1.png"); |
| 71 gfx::Image icon2 = LoadIcon("icon2.png"); | 71 gfx::ImageSkia icon2 = LoadIcon("icon2.png"); |
| 72 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))); | 72 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))); |
| 73 | 73 |
| 74 action.set_default_icon_path("the_default.png"); | 74 action.set_default_icon_path("the_default.png"); |
| 75 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))) | 75 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(1))) |
| 76 << "Still returns the puzzle piece because the image isn't loaded yet."; | 76 << "Still returns the puzzle piece because the image isn't loaded yet."; |
| 77 action.CacheIcon("the_default.png", icon2); | 77 action.CacheIcon("the_default.png", icon2); |
| 78 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1))); | 78 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(1))); |
| 79 | 79 |
| 80 action.SetIcon(ExtensionAction::kDefaultTabId, *icon1.ToSkBitmap()); | 80 action.SetIcon(ExtensionAction::kDefaultTabId, icon1); |
| 81 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100))) | 81 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(100))) |
| 82 << "SetIcon(kDefaultTabId) overrides the default_icon_path."; | 82 << "SetIcon(kDefaultTabId) overrides the default_icon_path."; |
| 83 | 83 |
| 84 action.SetIcon(100, *icon2.ToSkBitmap()); | 84 action.SetIcon(100, *icon2.bitmap()); |
| 85 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1))); | 85 ASSERT_TRUE(ImagesAreEqual(icon1, action.GetIcon(1))); |
| 86 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100))); | 86 ASSERT_TRUE(ImagesAreEqual(icon2, action.GetIcon(100))); |
| 87 } | 87 } |
| 88 | 88 |
| 89 TEST_F(ExtensionActionTest, IconIndex) { | 89 TEST_F(ExtensionActionTest, IconIndex) { |
| 90 gfx::Image icon1 = LoadIcon("icon1.png"); | 90 gfx::ImageSkia icon1 = LoadIcon("icon1.png"); |
| 91 gfx::Image icon2 = LoadIcon("icon2.png"); | 91 gfx::ImageSkia icon2 = LoadIcon("icon2.png"); |
| 92 gfx::Image puzzle_piece = | 92 gfx::ImageSkia puzzle_piece = |
| 93 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 93 *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 94 IDR_EXTENSIONS_FAVICON); | 94 IDR_EXTENSIONS_FAVICON); |
| 95 | 95 |
| 96 ASSERT_EQ(-1, action.GetIconIndex(1)); | 96 ASSERT_EQ(-1, action.GetIconIndex(1)); |
| 97 action.icon_paths()->push_back("foo.png"); | 97 action.icon_paths()->push_back("foo.png"); |
| 98 action.icon_paths()->push_back("bar.png"); | 98 action.icon_paths()->push_back("bar.png"); |
| 99 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); | 99 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); |
| 100 ASSERT_EQ(1, action.GetIconIndex(1)); | 100 ASSERT_EQ(1, action.GetIconIndex(1)); |
| 101 ASSERT_EQ(1, action.GetIconIndex(100)); | 101 ASSERT_EQ(1, action.GetIconIndex(100)); |
| 102 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100))) | 102 ASSERT_TRUE(ImagesAreEqual(puzzle_piece, action.GetIcon(100))) |
| 103 << "Non-loaded icon gets the puzzle piece."; | 103 << "Non-loaded icon gets the puzzle piece."; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); | 228 action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); |
| 229 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | 229 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 230 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); | 230 ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 231 | 231 |
| 232 action.ClearAllValuesForTab(100); | 232 action.ClearAllValuesForTab(100); |
| 233 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); | 233 ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 234 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); | 234 ASSERT_EQ(url_baz, action.GetPopupUrl(100)); |
| 235 } | 235 } |
| 236 | 236 |
| 237 } // namespace | 237 } // namespace |
| OLD | NEW |