Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_action_unittest.cc
bloba83cd752c46cb0383bd69468e150759ed8bef778
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/api/declarative_content/content_action.h"
7 #include "base/base64.h"
8 #include "base/run_loop.h"
9 #include "base/test/values_test_util.h"
10 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service_test_base.h"
13 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/extensions/test_extension_environment.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_builder.h"
22 #include "extensions/common/value_builder.h"
23 #include "ipc/ipc_message_utils.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/ipc/gfx_param_traits.h"
30 namespace extensions {
31 namespace {
33 using base::test::ParseJson;
34 using testing::HasSubstr;
37 scoped_ptr<base::DictionaryValue> SimpleManifest() {
38 return DictionaryBuilder()
39 .Set("name", "extension")
40 .Set("manifest_version", 2)
41 .Set("version", "1.0")
42 .Build();
45 class RequestContentScriptTest : public ExtensionServiceTestBase {
46 public:
47 RequestContentScriptTest()
48 : extension_(ExtensionBuilder().SetManifest(SimpleManifest()).Build()) {}
50 // TODO(rdevlin.cronin): This should be SetUp(), but an issues with invoking
51 // InitializeEmptyExtensionService() within SetUp() means that we have to
52 // call this manually within every test. This can be cleaned up once said
53 // issue is fixed.
54 virtual void Init() {
55 InitializeEmptyExtensionService();
56 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))->
57 SetReady();
58 base::RunLoop().RunUntilIdle();
61 Profile* profile() { return profile_.get(); }
62 Extension* extension() { return extension_.get(); }
64 private:
65 scoped_refptr<Extension> extension_;
68 TEST(DeclarativeContentActionTest, InvalidCreation) {
69 TestExtensionEnvironment env;
70 std::string error;
71 scoped_ptr<const ContentAction> result;
73 // Test wrong data type passed.
74 error.clear();
75 result = ContentAction::Create(
76 NULL, NULL, *ParseJson("[]"), &error);
77 EXPECT_EQ("", error);
78 EXPECT_FALSE(result.get());
80 // Test missing instanceType element.
81 error.clear();
82 result = ContentAction::Create(
83 NULL, NULL, *ParseJson("{}"), &error);
84 EXPECT_EQ("", error);
85 EXPECT_FALSE(result.get());
87 // Test wrong instanceType element.
88 error.clear();
89 result = ContentAction::Create(NULL, NULL, *ParseJson(
90 "{\n"
91 " \"instanceType\": \"declarativeContent.UnknownType\",\n"
92 "}"),
93 &error);
94 EXPECT_THAT(error, HasSubstr("invalid instanceType"));
95 EXPECT_FALSE(result.get());
98 TEST(DeclarativeContentActionTest, ShowPageActionWithoutPageAction) {
99 TestExtensionEnvironment env;
101 const Extension* extension = env.MakeExtension(base::DictionaryValue());
102 std::string error;
103 scoped_ptr<const ContentAction> result = ContentAction::Create(
104 NULL,
105 extension,
106 *ParseJson(
107 "{\n"
108 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
109 "}"),
110 &error);
111 EXPECT_THAT(error, testing::HasSubstr("without a page action"));
112 ASSERT_FALSE(result.get());
115 TEST(DeclarativeContentActionTest, ShowPageAction) {
116 TestExtensionEnvironment env;
118 const Extension* extension = env.MakeExtension(
119 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }"));
120 std::string error;
121 scoped_ptr<const ContentAction> result = ContentAction::Create(
122 NULL,
123 extension,
124 *ParseJson(
125 "{\n"
126 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
127 "}"),
128 &error);
129 EXPECT_EQ("", error);
130 ASSERT_TRUE(result.get());
132 ExtensionAction* page_action =
133 ExtensionActionManager::Get(env.profile())->GetPageAction(*extension);
134 scoped_ptr<content::WebContents> contents = env.MakeTab();
135 const int tab_id = ExtensionTabUtil::GetTabId(contents.get());
136 EXPECT_FALSE(page_action->GetIsVisible(tab_id));
137 ContentAction::ApplyInfo apply_info = {
138 extension, env.profile(), contents.get(), 100
140 result->Apply(apply_info);
141 EXPECT_TRUE(page_action->GetIsVisible(tab_id));
142 result->Apply(apply_info);
143 EXPECT_TRUE(page_action->GetIsVisible(tab_id));
144 result->Revert(apply_info);
145 EXPECT_TRUE(page_action->GetIsVisible(tab_id));
146 result->Revert(apply_info);
147 EXPECT_FALSE(page_action->GetIsVisible(tab_id));
150 TEST(DeclarativeContentActionTest, SetIcon) {
151 TestExtensionEnvironment env;
153 // Simulate the process of passing ImageData to SetIcon::Create.
154 SkBitmap bitmap;
155 EXPECT_TRUE(bitmap.tryAllocN32Pixels(19, 19));
156 bitmap.eraseARGB(0,0,0,0);
157 uint32_t* pixels = bitmap.getAddr32(0, 0);
158 for (int i = 0; i < 19 * 19; ++i)
159 pixels[i] = i;
160 IPC::Message bitmap_pickle;
161 IPC::WriteParam(&bitmap_pickle, bitmap);
162 std::string binary_data = std::string(
163 static_cast<const char*>(bitmap_pickle.data()), bitmap_pickle.size());
164 std::string data64;
165 base::Base64Encode(binary_data, &data64);
167 scoped_ptr<base::DictionaryValue> dict =
168 DictionaryBuilder().Set("instanceType", "declarativeContent.SetIcon")
169 .Set("imageData",
170 DictionaryBuilder().Set("19", data64)).Build();
172 const Extension* extension = env.MakeExtension(
173 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }"));
174 std::string error;
175 scoped_ptr<const ContentAction> result = ContentAction::Create(
176 NULL,
177 extension,
178 *dict,
179 &error);
180 EXPECT_EQ("", error);
181 ASSERT_TRUE(result.get());
183 ExtensionAction* page_action =
184 ExtensionActionManager::Get(env.profile())->GetPageAction(*extension);
185 scoped_ptr<content::WebContents> contents = env.MakeTab();
186 const int tab_id = ExtensionTabUtil::GetTabId(contents.get());
187 EXPECT_FALSE(page_action->GetIsVisible(tab_id));
188 ContentAction::ApplyInfo apply_info = {
189 extension, env.profile(), contents.get(), 100
192 // The declarative icon shouldn't exist unless the content action is applied.
193 EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).IsEmpty());
194 result->Apply(apply_info);
195 EXPECT_FALSE(page_action->GetDeclarativeIcon(tab_id).IsEmpty());
196 result->Revert(apply_info);
197 EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).IsEmpty());
200 TEST_F(RequestContentScriptTest, MissingScripts) {
201 Init();
202 std::string error;
203 scoped_ptr<const ContentAction> result = ContentAction::Create(
204 profile(),
205 extension(),
206 *ParseJson(
207 "{\n"
208 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
209 " \"allFrames\": true,\n"
210 " \"matchAboutBlank\": true\n"
211 "}"),
212 &error);
213 EXPECT_THAT(error, testing::HasSubstr("Missing parameter is required"));
214 ASSERT_FALSE(result.get());
217 TEST_F(RequestContentScriptTest, CSS) {
218 Init();
219 std::string error;
220 scoped_ptr<const ContentAction> result = ContentAction::Create(
221 profile(),
222 extension(),
223 *ParseJson(
224 "{\n"
225 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
226 " \"css\": [\"style.css\"]\n"
227 "}"),
228 &error);
229 EXPECT_EQ("", error);
230 ASSERT_TRUE(result.get());
233 TEST_F(RequestContentScriptTest, JS) {
234 Init();
235 std::string error;
236 scoped_ptr<const ContentAction> result = ContentAction::Create(
237 profile(),
238 extension(),
239 *ParseJson(
240 "{\n"
241 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
242 " \"js\": [\"script.js\"]\n"
243 "}"),
244 &error);
245 EXPECT_EQ("", error);
246 ASSERT_TRUE(result.get());
249 TEST_F(RequestContentScriptTest, CSSBadType) {
250 Init();
251 std::string error;
252 scoped_ptr<const ContentAction> result = ContentAction::Create(
253 profile(),
254 extension(),
255 *ParseJson(
256 "{\n"
257 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
258 " \"css\": \"style.css\"\n"
259 "}"),
260 &error);
261 ASSERT_FALSE(result.get());
264 TEST_F(RequestContentScriptTest, JSBadType) {
265 Init();
266 std::string error;
267 scoped_ptr<const ContentAction> result = ContentAction::Create(
268 profile(),
269 extension(),
270 *ParseJson(
271 "{\n"
272 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
273 " \"js\": \"script.js\"\n"
274 "}"),
275 &error);
276 ASSERT_FALSE(result.get());
279 TEST_F(RequestContentScriptTest, AllFrames) {
280 Init();
281 std::string error;
282 scoped_ptr<const ContentAction> result = ContentAction::Create(
283 profile(),
284 extension(),
285 *ParseJson(
286 "{\n"
287 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
288 " \"js\": [\"script.js\"],\n"
289 " \"allFrames\": true\n"
290 "}"),
291 &error);
292 EXPECT_EQ("", error);
293 ASSERT_TRUE(result.get());
296 TEST_F(RequestContentScriptTest, MatchAboutBlank) {
297 Init();
298 std::string error;
299 scoped_ptr<const ContentAction> result = ContentAction::Create(
300 profile(),
301 extension(),
302 *ParseJson(
303 "{\n"
304 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
305 " \"js\": [\"script.js\"],\n"
306 " \"matchAboutBlank\": true\n"
307 "}"),
308 &error);
309 EXPECT_EQ("", error);
310 ASSERT_TRUE(result.get());
313 TEST_F(RequestContentScriptTest, AllFramesBadType) {
314 Init();
315 std::string error;
316 scoped_ptr<const ContentAction> result = ContentAction::Create(
317 profile(),
318 extension(),
319 *ParseJson(
320 "{\n"
321 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
322 " \"js\": [\"script.js\"],\n"
323 " \"allFrames\": null\n"
324 "}"),
325 &error);
326 ASSERT_FALSE(result.get());
329 TEST_F(RequestContentScriptTest, MatchAboutBlankBadType) {
330 Init();
331 std::string error;
332 scoped_ptr<const ContentAction> result = ContentAction::Create(
333 profile(),
334 extension(),
335 *ParseJson(
336 "{\n"
337 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
338 " \"js\": [\"script.js\"],\n"
339 " \"matchAboutBlank\": null\n"
340 "}"),
341 &error);
342 ASSERT_FALSE(result.get());
345 } // namespace
346 } // namespace extensions