extension_action_unittest.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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
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"
9#include "chrome/common/extensions/extension_action.h"
10#include "googleurl/src/gurl.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/skia/include/core/SkBitmap.h"
13#include "ui/gfx/skia_util.h"
14#include "webkit/glue/image_decoder.h"
15
16using gfx::BitmapsAreEqual;
17
18static 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
35TEST(ExtensionActionTest, TabSpecificState) {
36  ExtensionAction action;
37
38  // title
39  ASSERT_EQ("", action.GetTitle(1));
40  action.SetTitle(ExtensionAction::kDefaultTabId, "foo");
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));
46  action.SetTitle(ExtensionAction::kDefaultTabId, "baz");
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());
55  action.SetIcon(ExtensionAction::kDefaultTabId, icon1);
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
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");
65  action.SetIconIndex(ExtensionAction::kDefaultTabId, 1);
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
76  ASSERT_FALSE(action.GetIsVisible(1));
77  action.SetIsVisible(ExtensionAction::kDefaultTabId, true);
78  ASSERT_TRUE(action.GetIsVisible(1));
79  ASSERT_TRUE(action.GetIsVisible(100));
80  action.SetIsVisible(ExtensionAction::kDefaultTabId, false);
81  ASSERT_FALSE(action.GetIsVisible(1));
82  ASSERT_FALSE(action.GetIsVisible(100));
83  action.SetIsVisible(100, true);
84  ASSERT_FALSE(action.GetIsVisible(1));
85  ASSERT_TRUE(action.GetIsVisible(100));
86  action.ClearAllValuesForTab(100);
87  ASSERT_FALSE(action.GetIsVisible(1));
88  ASSERT_FALSE(action.GetIsVisible(100));
89
90  // badge text
91  ASSERT_EQ("", action.GetBadgeText(1));
92  action.SetBadgeText(ExtensionAction::kDefaultTabId, "foo");
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));
98  action.SetBadgeText(ExtensionAction::kDefaultTabId, "baz");
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));
105  action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFFFF0000u);
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));
111  action.SetBadgeTextColor(ExtensionAction::kDefaultTabId, 0xFF0000FFu);
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));
118  action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
119                                 0xFFFF0000u);
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));
125  action.SetBadgeBackgroundColor(ExtensionAction::kDefaultTabId,
126                                 0xFF0000FFu);
127  ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(1));
128  action.ClearAllValuesForTab(100);
129  ASSERT_EQ(0xFF0000FFu, action.GetBadgeBackgroundColor(100));
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));
157}
158