[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame^] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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" |
| 7 | #include "base/path_service.h" |
| 8 | #include "chrome/common/chrome_paths.h" |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 9 | #include "chrome/common/extensions/extension_action.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "third_party/skia/include/core/SkBitmap.h" |
| 12 | #include "webkit/glue/image_decoder.h" |
| 13 | |
| 14 | static SkBitmap LoadIcon(const std::string& filename) { |
| 15 | FilePath path; |
| 16 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 17 | path = path.AppendASCII("extensions").AppendASCII(filename); |
| 18 | |
| 19 | std::string file_contents; |
| 20 | file_util::ReadFileToString(path, &file_contents); |
| 21 | const unsigned char* data = |
| 22 | reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 23 | |
| 24 | SkBitmap bitmap; |
| 25 | webkit_glue::ImageDecoder decoder; |
| 26 | bitmap = decoder.Decode(data, file_contents.length()); |
| 27 | |
| 28 | return bitmap; |
| 29 | } |
| 30 | |
| 31 | static bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) { |
| 32 | void* addr1 = NULL; |
| 33 | void* addr2 = NULL; |
| 34 | |
| 35 | bitmap1.lockPixels(); |
| 36 | addr1 = bitmap1.getAddr32(0, 0); |
| 37 | bitmap1.unlockPixels(); |
| 38 | |
| 39 | bitmap2.lockPixels(); |
| 40 | addr2 = bitmap2.getAddr32(0, 0); |
| 41 | bitmap2.unlockPixels(); |
| 42 | |
| 43 | return 0 == memcmp(addr1, addr2, bitmap1.getSize()); |
| 44 | } |
| 45 | |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 46 | TEST(ExtensionActionTest, TabSpecificState) { |
| 47 | ExtensionAction action; |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 48 | |
| 49 | // title |
| 50 | ASSERT_EQ("", action.GetTitle(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 51 | action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 52 | ASSERT_EQ("foo", action.GetTitle(1)); |
| 53 | ASSERT_EQ("foo", action.GetTitle(100)); |
| 54 | action.SetTitle(100, "bar"); |
| 55 | ASSERT_EQ("foo", action.GetTitle(1)); |
| 56 | ASSERT_EQ("bar", action.GetTitle(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 57 | action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 58 | ASSERT_EQ("baz", action.GetTitle(1)); |
| 59 | action.ClearAllValuesForTab(100); |
| 60 | ASSERT_EQ("baz", action.GetTitle(100)); |
| 61 | |
| 62 | // icon |
| 63 | SkBitmap icon1 = LoadIcon("icon1.png"); |
| 64 | SkBitmap icon2 = LoadIcon("icon2.png"); |
| 65 | ASSERT_TRUE(action.GetIcon(1).isNull()); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 66 | action.SetIcon(ExtensionAction::kDefaultTabId, icon1); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 67 | ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(100))); |
| 68 | action.SetIcon(100, icon2); |
| 69 | ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(1))); |
| 70 | ASSERT_TRUE(BitmapsAreEqual(icon2, action.GetIcon(100))); |
| 71 | |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 72 | // icon index |
| 73 | ASSERT_EQ(-1, action.GetIconIndex(1)); |
| 74 | action.icon_paths()->push_back("foo.png"); |
| 75 | action.icon_paths()->push_back("bar.png"); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 76 | action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 77 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 78 | ASSERT_EQ(1, action.GetIconIndex(100)); |
| 79 | action.SetIconIndex(100, 0); |
| 80 | ASSERT_EQ(0, action.GetIconIndex(100)); |
| 81 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 82 | action.ClearAllValuesForTab(100); |
| 83 | ASSERT_EQ(1, action.GetIconIndex(100)); |
| 84 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 85 | |
| 86 | // visibility |
| 87 | ASSERT_EQ(false, action.GetIsVisible(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 88 | action.SetIsVisible(ExtensionAction::kDefaultTabId, true); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 89 | ASSERT_EQ(true, action.GetIsVisible(1)); |
| 90 | ASSERT_EQ(true, action.GetIsVisible(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 91 | action.SetIsVisible(ExtensionAction::kDefaultTabId, false); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 92 | ASSERT_EQ(false, action.GetIsVisible(1)); |
| 93 | ASSERT_EQ(false, action.GetIsVisible(100)); |
| 94 | action.SetIsVisible(100, true); |
| 95 | ASSERT_EQ(false, action.GetIsVisible(1)); |
| 96 | ASSERT_EQ(true, action.GetIsVisible(100)); |
| 97 | action.ClearAllValuesForTab(100); |
| 98 | ASSERT_EQ(false, action.GetIsVisible(1)); |
| 99 | ASSERT_EQ(false, action.GetIsVisible(100)); |
| 100 | |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 101 | // badge text |
| 102 | ASSERT_EQ("", action.GetBadgeText(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 103 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 104 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 105 | ASSERT_EQ("foo", action.GetBadgeText(100)); |
| 106 | action.SetBadgeText(100, "bar"); |
| 107 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 108 | ASSERT_EQ("bar", action.GetBadgeText(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 109 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 110 | ASSERT_EQ("baz", action.GetBadgeText(1)); |
| 111 | action.ClearAllValuesForTab(100); |
| 112 | ASSERT_EQ("baz", action.GetBadgeText(100)); |
| 113 | |
| 114 | // badge text color |
| 115 | ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 116 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 117 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 118 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); |
| 119 | action.SetBadgeTextColor(100, 0xFF00FF00); |
| 120 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 121 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 122 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 123 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); |
| 124 | action.ClearAllValuesForTab(100); |
| 125 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); |
| 126 | |
| 127 | // badge background color |
| 128 | ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 129 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 130 | 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 131 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 132 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); |
| 133 | action.SetBadgeBackgroundColor(100, 0xFF00FF00); |
| 134 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 135 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 136 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 137 | 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 138 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); |
| 139 | action.ClearAllValuesForTab(100); |
| 140 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); |
| 141 | } |