[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" |
| 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] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 10 | #include "googleurl/src/gurl.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 13 | #include "ui/gfx/skia_util.h" |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 14 | #include "webkit/glue/image_decoder.h" |
| 15 | |
[email protected] | 18539ee | 2010-09-16 21:39:29 | [diff] [blame] | 16 | using gfx::BitmapsAreEqual; |
| 17 | |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 18 | static SkBitmap LoadIcon(const std::string& filename) { |
| 19 | FilePath path; |
| 20 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 21 | path = path.AppendASCII("extensions").AppendASCII(filename); |
| 22 | |
| 23 | std::string file_contents; |
| 24 | file_util::ReadFileToString(path, &file_contents); |
| 25 | const unsigned char* data = |
| 26 | reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 27 | |
| 28 | SkBitmap bitmap; |
| 29 | webkit_glue::ImageDecoder decoder; |
| 30 | bitmap = decoder.Decode(data, file_contents.length()); |
| 31 | |
| 32 | return bitmap; |
| 33 | } |
| 34 | |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 35 | TEST(ExtensionActionTest, TabSpecificState) { |
[email protected] | 3349b59 | 2012-04-26 12:35:28 | [diff] [blame^] | 36 | ExtensionAction action(""); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 37 | |
| 38 | // title |
| 39 | ASSERT_EQ("", action.GetTitle(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 40 | action.SetTitle(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 41 | ASSERT_EQ("foo", action.GetTitle(1)); |
| 42 | ASSERT_EQ("foo", action.GetTitle(100)); |
| 43 | action.SetTitle(100, "bar"); |
| 44 | ASSERT_EQ("foo", action.GetTitle(1)); |
| 45 | ASSERT_EQ("bar", action.GetTitle(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 46 | action.SetTitle(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 47 | ASSERT_EQ("baz", action.GetTitle(1)); |
| 48 | action.ClearAllValuesForTab(100); |
| 49 | ASSERT_EQ("baz", action.GetTitle(100)); |
| 50 | |
| 51 | // icon |
| 52 | SkBitmap icon1 = LoadIcon("icon1.png"); |
| 53 | SkBitmap icon2 = LoadIcon("icon2.png"); |
| 54 | ASSERT_TRUE(action.GetIcon(1).isNull()); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 55 | action.SetIcon(ExtensionAction::kDefaultTabId, icon1); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 56 | ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(100))); |
| 57 | action.SetIcon(100, icon2); |
| 58 | ASSERT_TRUE(BitmapsAreEqual(icon1, action.GetIcon(1))); |
| 59 | ASSERT_TRUE(BitmapsAreEqual(icon2, action.GetIcon(100))); |
| 60 | |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 61 | // icon index |
| 62 | ASSERT_EQ(-1, action.GetIconIndex(1)); |
| 63 | action.icon_paths()->push_back("foo.png"); |
| 64 | action.icon_paths()->push_back("bar.png"); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 65 | action.SetIconIndex(ExtensionAction::kDefaultTabId, 1); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 66 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 67 | ASSERT_EQ(1, action.GetIconIndex(100)); |
| 68 | action.SetIconIndex(100, 0); |
| 69 | ASSERT_EQ(0, action.GetIconIndex(100)); |
| 70 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 71 | action.ClearAllValuesForTab(100); |
| 72 | ASSERT_EQ(1, action.GetIconIndex(100)); |
| 73 | ASSERT_EQ(1, action.GetIconIndex(1)); |
| 74 | |
| 75 | // visibility |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 76 | ASSERT_FALSE(action.GetIsVisible(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 77 | action.SetIsVisible(ExtensionAction::kDefaultTabId, true); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 78 | ASSERT_TRUE(action.GetIsVisible(1)); |
| 79 | ASSERT_TRUE(action.GetIsVisible(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 80 | action.SetIsVisible(ExtensionAction::kDefaultTabId, false); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 81 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 82 | ASSERT_FALSE(action.GetIsVisible(100)); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 83 | action.SetIsVisible(100, true); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 84 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 85 | ASSERT_TRUE(action.GetIsVisible(100)); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 86 | action.ClearAllValuesForTab(100); |
[email protected] | e3a8a92 | 2010-10-02 02:04:30 | [diff] [blame] | 87 | ASSERT_FALSE(action.GetIsVisible(1)); |
| 88 | ASSERT_FALSE(action.GetIsVisible(100)); |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 89 | |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 90 | // badge text |
| 91 | ASSERT_EQ("", action.GetBadgeText(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 92 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 93 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 94 | ASSERT_EQ("foo", action.GetBadgeText(100)); |
| 95 | action.SetBadgeText(100, "bar"); |
| 96 | ASSERT_EQ("foo", action.GetBadgeText(1)); |
| 97 | ASSERT_EQ("bar", action.GetBadgeText(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 98 | action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz"); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 99 | ASSERT_EQ("baz", action.GetBadgeText(1)); |
| 100 | action.ClearAllValuesForTab(100); |
| 101 | ASSERT_EQ("baz", action.GetBadgeText(100)); |
| 102 | |
| 103 | // badge text color |
| 104 | ASSERT_EQ(0x00000000u, action.GetBadgeTextColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 105 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 106 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 107 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(100)); |
| 108 | action.SetBadgeTextColor(100, 0xFF00FF00); |
| 109 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeTextColor(1)); |
| 110 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeTextColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 111 | action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 112 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(1)); |
| 113 | action.ClearAllValuesForTab(100); |
| 114 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeTextColor(100)); |
| 115 | |
| 116 | // badge background color |
| 117 | ASSERT_EQ(0x00000000u, action.GetBadgeBackgroundColor(1)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 118 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 119 | 0xFFFF0000u); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 120 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 121 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(100)); |
| 122 | action.SetBadgeBackgroundColor(100, 0xFF00FF00); |
| 123 | ASSERT_EQ(0xFFFF0000u, action.GetBadgeBackgroundColor(1)); |
| 124 | ASSERT_EQ(0xFF00FF00u, action.GetBadgeBackgroundColor(100)); |
[email protected] | 5d246db2 | 2009-10-27 06:17:57 | [diff] [blame] | 125 | action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId, |
[email protected] | 56ce6e5 | 2009-10-27 00:10:52 | [diff] [blame] | 126 | 0xFF0000FFu); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 127 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1)); |
| 128 | action.ClearAllValuesForTab(100); |
| 129 | ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100)); |
[email protected] | a388c484 | 2010-01-27 17:13:15 | [diff] [blame] | 130 | |
| 131 | // popup url |
| 132 | GURL url_unset; |
| 133 | GURL url_foo("http://www.example.com/foo.html"); |
| 134 | GURL url_bar("http://www.example.com/bar.html"); |
| 135 | GURL url_baz("http://www.example.com/baz.html"); |
| 136 | |
| 137 | ASSERT_EQ(url_unset, action.GetPopupUrl(1)); |
| 138 | ASSERT_EQ(url_unset, action.GetPopupUrl(100)); |
| 139 | ASSERT_FALSE(action.HasPopup(1)); |
| 140 | ASSERT_FALSE(action.HasPopup(100)); |
| 141 | |
| 142 | action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_foo); |
| 143 | ASSERT_EQ(url_foo, action.GetPopupUrl(1)); |
| 144 | ASSERT_EQ(url_foo, action.GetPopupUrl(100)); |
| 145 | |
| 146 | action.SetPopupUrl(100, url_bar); |
| 147 | ASSERT_EQ(url_foo, action.GetPopupUrl(1)); |
| 148 | ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 149 | |
| 150 | action.SetPopupUrl(ExtensionAction::kDefaultTabId, url_baz); |
| 151 | ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 152 | ASSERT_EQ(url_bar, action.GetPopupUrl(100)); |
| 153 | |
| 154 | action.ClearAllValuesForTab(100); |
| 155 | ASSERT_EQ(url_baz, action.GetPopupUrl(1)); |
| 156 | ASSERT_EQ(url_baz, action.GetPopupUrl(100)); |
[email protected] | 92c6f9b9 | 2009-10-24 04:35:08 | [diff] [blame] | 157 | } |