OLD | NEW |
(Empty) | |
| 1 #include "base/file_path.h" |
| 2 #include "base/file_util.h" |
| 3 #include "base/path_service.h" |
| 4 #include "chrome/common/chrome_paths.h" |
| 5 #include "chrome/common/extensions/extension_action2.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "webkit/glue/image_decoder.h" |
| 9 |
| 10 static SkBitmap LoadIcon(const std::string& filename) { |
| 11 FilePath path; |
| 12 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 13 path = path.AppendASCII("extensions").AppendASCII(filename); |
| 14 |
| 15 std::string file_contents; |
| 16 file_util::ReadFileToString(path, &file_contents); |
| 17 const unsigned char* data = |
| 18 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 19 |
| 20 SkBitmap bitmap; |
| 21 webkit_glue::ImageDecoder decoder; |
| 22 bitmap = decoder.Decode(data, file_contents.length()); |
| 23 |
| 24 return bitmap; |
| 25 } |
| 26 |
| 27 static bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { |
| 28 void* addr1 = NULL; |
| 29 void* addr2 = NULL; |
| 30 |
| 31 bitmap1.lockPixels(); |
| 32 addr1 = bitmap1.getAddr32(0, 0); |
| 33 bitmap1.unlockPixels(); |
| 34 |
| 35 bitmap2.lockPixels(); |
| 36 addr2 = bitmap2.getAddr32(0, 0); |
| 37 bitmap2.unlockPixels(); |
| 38 |
| 39 return 0 == memcmp(addr1, addr2, bitmap1.getSize()); |
| 40 } |
| 41 |
| 42 TEST(ExtensionAction2Test, TabSpecificState) { |
| 43 ExtensionAction2 action; |
| 44 |
| 45 // title |
| 46 ASSERT_EQ("", action.GetTitle(1)); |
| 47 action.SetTitle(ExtensionAction2::kDefaultTabId, "foo"); |
| 48 ASSERT_EQ("foo", action.GetTitle(1)); |
| 49 ASSERT_EQ("foo", action.GetTitle(100)); |
| 50 action.SetTitle(100, "bar"); |
| 51 ASSERT_EQ("foo", action.GetTitle(1)); |
| 52 ASSERT_EQ("bar", action.GetTitle(100)); |
| 53 action.SetTitle(ExtensionAction2::kDefaultTabId, "baz"); |
| 54 ASSERT_EQ("baz", action.GetTitle(1)); |
| 55 action.ClearAllValuesForTab(100); |
| 56 ASSERT_EQ("baz", action.GetTitle(100)); |
| 57 |
| 58 // icon |
| 59 SkBitmap icon1 = LoadIcon("icon1.png"); |
| 60 SkBitmap icon2 = LoadIcon("icon2.png"); |
| 61 ASSERT_TRUE(action.GetIcon(1).isNull()); |
| 62 action.SetIcon(ExtensionAction2::kDefaultTabId, icon1); |
| 63 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(100))); |
| 64 action.SetIcon(100, icon2); |
| 65 ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(1))); |
| 66 ASSERT_TRUE(BitmapsAreEqual(icon2, action.GetIcon(100))); |
| 67 |
| 68 // badge text |
| 69 ASSERT_EQ("", action.GetBadgeText(1)); |
| 70 action.SetBadgeText(ExtensionAction2::kDefaultTabId, "foo"); |
| 71 ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 72 ASSERT_EQ("foo", action.GetBadgeText(100)); |
| 73 action.SetBadgeText(100, "bar"); |
| 74 ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 75 ASSERT_EQ("bar", action.GetBadgeText(100)); |
| 76 action.SetBadgeText(ExtensionAction2::kDefaultTabId, "baz"); |
| 77 ASSERT_EQ("baz", action.GetBadgeText(1)); |
| 78 action.ClearAllValuesForTab(100); |
| 79 ASSERT_EQ("baz", action.GetBadgeText(100)); |
| 80 |
| 81 // badge text color |
| 82 ASSERT_EQ(0x00000000, action.GetBadgeTextColor(1)); |
| 83 action.SetBadgeTextColor(ExtensionAction2::kDefaultTabId, 0xFFFF0000); |
| 84 ASSERT_EQ(0xFFFF0000, action.GetBadgeTextColor(1)); |
| 85 ASSERT_EQ(0xFFFF0000, action.GetBadgeTextColor(100)); |
| 86 action.SetBadgeTextColor(100, 0xFF00FF00); |
| 87 ASSERT_EQ(0xFFFF0000, action.GetBadgeTextColor(1)); |
| 88 ASSERT_EQ(0xFF00FF00, action.GetBadgeTextColor(100)); |
| 89 action.SetBadgeTextColor(ExtensionAction2::kDefaultTabId, 0xFF0000FF); |
| 90 ASSERT_EQ(0xFF0000FF, action.GetBadgeTextColor(1)); |
| 91 action.ClearAllValuesForTab(100); |
| 92 ASSERT_EQ(0xFF0000FF, action.GetBadgeTextColor(100)); |
| 93 |
| 94 // badge background color |
| 95 ASSERT_EQ(0x00000000, action.GetBadgeBackgroundColor(1)); |
| 96 action.SetBadgeBackgroundColor(ExtensionAction2::kDefaultTabId, 0xFFFF0000); |
| 97 ASSERT_EQ(0xFFFF0000, action.GetBadgeBackgroundColor(1)); |
| 98 ASSERT_EQ(0xFFFF0000, action.GetBadgeBackgroundColor(100)); |
| 99 action.SetBadgeBackgroundColor(100, 0xFF00FF00); |
| 100 ASSERT_EQ(0xFFFF0000, action.GetBadgeBackgroundColor(1)); |
| 101 ASSERT_EQ(0xFF00FF00, action.GetBadgeBackgroundColor(100)); |
| 102 action.SetBadgeBackgroundColor(ExtensionAction2::kDefaultTabId, 0xFF0000FF); |
| 103 ASSERT_EQ(0xFF0000FF, action.GetBadgeBackgroundColor(1)); |
| 104 action.ClearAllValuesForTab(100); |
| 105 ASSERT_EQ(0xFF0000FF, action.GetBadgeBackgroundColor(100)); |
| 106 } |
| 107 |
| 108 TEST(ExtensionAction2Test, IconOddCases) { |
| 109 ExtensionAction2 action; |
| 110 |
| 111 action.SetIcon(ExtensionAction2::kDefaultTabId, LoadIcon("icon1.png")); |
| 112 action.SetDefaultIcon("foo.png"); |
| 113 ASSERT_TRUE(action.GetIcon(1).isNull()); |
| 114 ASSERT_EQ("foo.png", action.GetDefaultIconPath()); |
| 115 |
| 116 action.icon_paths()->push_back("a.png"); |
| 117 action.icon_paths()->push_back("b.png"); |
| 118 action.SetDefaultIcon(1); |
| 119 ASSERT_TRUE(action.GetIcon(1).isNull()); |
| 120 ASSERT_EQ("b.png", action.GetDefaultIconPath()); |
| 121 |
| 122 action.SetIcon(100, LoadIcon("icon1.png")); |
| 123 ASSERT_TRUE(!action.GetIcon(100).isNull()); |
| 124 action.SetIcon(ExtensionAction2::kDefaultTabId, LoadIcon("icon1.png")); |
| 125 ASSERT_TRUE(!action.GetIcon(1).isNull()); |
| 126 ASSERT_EQ("", action.GetDefaultIconPath()); |
| 127 } |
OLD | NEW |