blob: 8cc2aac5bdd20ff69737a4f62224b596eab5ebc2 [file] [log] [blame]
[email protected]3349b592012-04-26 12:35:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4a3dab22009-11-11 17:36:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]92c6f9b92009-10-24 04:35:085#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]5d246db22009-10-27 06:17:579#include "chrome/common/extensions/extension_action.h"
[email protected]c3a4bd992010-08-18 20:25:0110#include "googleurl/src/gurl.h"
[email protected]92c6f9b92009-10-24 04:35:0811#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/skia/include/core/SkBitmap.h"
[email protected]08397d52011-02-05 01:53:3813#include "ui/gfx/skia_util.h"
[email protected]92c6f9b92009-10-24 04:35:0814#include "webkit/glue/image_decoder.h"
15
[email protected]18539ee2010-09-16 21:39:2916using gfx::BitmapsAreEqual;
17
[email protected]92c6f9b92009-10-24 04:35:0818static 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]5d246db22009-10-27 06:17:5735TEST(ExtensionActionTest, TabSpecificState) {
[email protected]3349b592012-04-26 12:35:2836 ExtensionAction action("");
[email protected]92c6f9b92009-10-24 04:35:0837
38 // title
39 ASSERT_EQ("", action.GetTitle(1));
[email protected]5d246db22009-10-27 06:17:5740 action.SetTitle(ExtensionAction::kDefaultTabId, "foo");
[email protected]92c6f9b92009-10-24 04:35:0841 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]5d246db22009-10-27 06:17:5746 action.SetTitle(ExtensionAction::kDefaultTabId, "baz");
[email protected]92c6f9b92009-10-24 04:35:0847 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]5d246db22009-10-27 06:17:5755 action.SetIcon(ExtensionAction::kDefaultTabId, icon1);
[email protected]92c6f9b92009-10-24 04:35:0856 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]56ce6e52009-10-27 00:10:5261 // 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]5d246db22009-10-27 06:17:5765 action.SetIconIndex(ExtensionAction::kDefaultTabId, 1);
[email protected]56ce6e52009-10-27 00:10:5266 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]e3a8a922010-10-02 02:04:3076 ASSERT_FALSE(action.GetIsVisible(1));
[email protected]5d246db22009-10-27 06:17:5777 action.SetIsVisible(ExtensionAction::kDefaultTabId, true);
[email protected]e3a8a922010-10-02 02:04:3078 ASSERT_TRUE(action.GetIsVisible(1));
79 ASSERT_TRUE(action.GetIsVisible(100));
[email protected]5d246db22009-10-27 06:17:5780 action.SetIsVisible(ExtensionAction::kDefaultTabId, false);
[email protected]e3a8a922010-10-02 02:04:3081 ASSERT_FALSE(action.GetIsVisible(1));
82 ASSERT_FALSE(action.GetIsVisible(100));
[email protected]56ce6e52009-10-27 00:10:5283 action.SetIsVisible(100, true);
[email protected]e3a8a922010-10-02 02:04:3084 ASSERT_FALSE(action.GetIsVisible(1));
85 ASSERT_TRUE(action.GetIsVisible(100));
[email protected]56ce6e52009-10-27 00:10:5286 action.ClearAllValuesForTab(100);
[email protected]e3a8a922010-10-02 02:04:3087 ASSERT_FALSE(action.GetIsVisible(1));
88 ASSERT_FALSE(action.GetIsVisible(100));
[email protected]56ce6e52009-10-27 00:10:5289
[email protected]92c6f9b92009-10-24 04:35:0890 // badge text
91 ASSERT_EQ("", action.GetBadgeText(1));
[email protected]5d246db22009-10-27 06:17:5792 action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
[email protected]92c6f9b92009-10-24 04:35:0893 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]5d246db22009-10-27 06:17:5798 action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
[email protected]92c6f9b92009-10-24 04:35:0899 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]5d246db22009-10-27 06:17:57105 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
[email protected]92c6f9b92009-10-24 04:35:08106 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]5d246db22009-10-27 06:17:57111 action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
[email protected]92c6f9b92009-10-24 04:35:08112 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]5d246db22009-10-27 06:17:57118 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
[email protected]56ce6e52009-10-27 00:10:52119 0xFFFF0000u);
[email protected]92c6f9b92009-10-24 04:35:08120 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]5d246db22009-10-27 06:17:57125 action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
[email protected]56ce6e52009-10-27 00:10:52126 0xFF0000FFu);
[email protected]92c6f9b92009-10-24 04:35:08127 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
128 action.ClearAllValuesForTab(100);
129 ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
[email protected]a388c4842010-01-27 17:13:15130
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]92c6f9b92009-10-24 04:35:08157}