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 "ui/gfx/image/image_skia.h"
27 #include "ui/gfx/ipc/gfx_param_traits.h"
29 namespace extensions
{
32 using base::test::ParseJson
;
33 using testing::HasSubstr
;
36 scoped_ptr
<base::DictionaryValue
> SimpleManifest() {
37 return DictionaryBuilder()
38 .Set("name", "extension")
39 .Set("manifest_version", 2)
40 .Set("version", "1.0")
44 class RequestContentScriptTest
: public ExtensionServiceTestBase
{
46 RequestContentScriptTest()
47 : extension_(ExtensionBuilder().SetManifest(SimpleManifest()).Build()) {};
49 // TODO(rdevlin.cronin): This should be SetUp(), but an issues with invoking
50 // InitializeEmptyExtensionService() within SetUp() means that we have to
51 // call this manually within every test. This can be cleaned up once said
54 InitializeEmptyExtensionService();
55 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile()))->
57 base::RunLoop().RunUntilIdle();
60 Profile
* profile() { return profile_
.get(); }
61 Extension
* extension() { return extension_
.get(); }
64 scoped_refptr
<Extension
> extension_
;
67 TEST(DeclarativeContentActionTest
, InvalidCreation
) {
68 TestExtensionEnvironment env
;
70 bool bad_message
= false;
71 scoped_refptr
<const ContentAction
> result
;
73 // Test wrong data type passed.
75 result
= ContentAction::Create(
76 NULL
, NULL
, *ParseJson("[]"), &error
, &bad_message
);
77 EXPECT_TRUE(bad_message
);
79 EXPECT_FALSE(result
.get());
81 // Test missing instanceType element.
83 result
= ContentAction::Create(
84 NULL
, NULL
, *ParseJson("{}"), &error
, &bad_message
);
85 EXPECT_TRUE(bad_message
);
87 EXPECT_FALSE(result
.get());
89 // Test wrong instanceType element.
91 result
= ContentAction::Create(NULL
, NULL
, *ParseJson(
93 " \"instanceType\": \"declarativeContent.UnknownType\",\n"
95 &error
, &bad_message
);
96 EXPECT_THAT(error
, HasSubstr("invalid instanceType"));
97 EXPECT_FALSE(result
.get());
100 TEST(DeclarativeContentActionTest
, ShowPageActionWithoutPageAction
) {
101 TestExtensionEnvironment env
;
103 const Extension
* extension
= env
.MakeExtension(base::DictionaryValue());
105 bool bad_message
= false;
106 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
111 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
115 EXPECT_THAT(error
, testing::HasSubstr("without a page action"));
116 EXPECT_FALSE(bad_message
);
117 ASSERT_FALSE(result
.get());
120 TEST(DeclarativeContentActionTest
, ShowPageAction
) {
121 TestExtensionEnvironment env
;
123 const Extension
* extension
= env
.MakeExtension(
124 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }"));
126 bool bad_message
= false;
127 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
132 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
136 EXPECT_EQ("", error
);
137 EXPECT_FALSE(bad_message
);
138 ASSERT_TRUE(result
.get());
139 EXPECT_EQ(ContentAction::ACTION_SHOW_PAGE_ACTION
, result
->GetType());
141 ExtensionAction
* page_action
=
142 ExtensionActionManager::Get(env
.profile())->GetPageAction(*extension
);
143 scoped_ptr
<content::WebContents
> contents
= env
.MakeTab();
144 const int tab_id
= ExtensionTabUtil::GetTabId(contents
.get());
145 EXPECT_FALSE(page_action
->GetIsVisible(tab_id
));
146 ContentAction::ApplyInfo apply_info
= {
147 env
.profile(), contents
.get()
149 result
->Apply(extension
->id(), base::Time(), &apply_info
);
150 EXPECT_TRUE(page_action
->GetIsVisible(tab_id
));
151 result
->Apply(extension
->id(), base::Time(), &apply_info
);
152 EXPECT_TRUE(page_action
->GetIsVisible(tab_id
));
153 result
->Revert(extension
->id(), base::Time(), &apply_info
);
154 EXPECT_TRUE(page_action
->GetIsVisible(tab_id
));
155 result
->Revert(extension
->id(), base::Time(), &apply_info
);
156 EXPECT_FALSE(page_action
->GetIsVisible(tab_id
));
159 TEST(DeclarativeContentActionTest
, SetIcon
) {
160 TestExtensionEnvironment env
;
162 // Simulate the process of passing ImageData to SetIcon::Create.
164 EXPECT_TRUE(bitmap
.tryAllocN32Pixels(19, 19));
165 bitmap
.eraseARGB(0,0,0,0);
166 uint32_t* pixels
= bitmap
.getAddr32(0, 0);
167 for (int i
= 0; i
< 19 * 19; ++i
)
169 IPC::Message bitmap_pickle
;
170 IPC::WriteParam(&bitmap_pickle
, bitmap
);
171 std::string binary_data
= std::string(
172 static_cast<const char*>(bitmap_pickle
.data()), bitmap_pickle
.size());
174 base::Base64Encode(binary_data
, &data64
);
176 scoped_ptr
<base::DictionaryValue
> dict
=
177 DictionaryBuilder().Set("instanceType", "declarativeContent.SetIcon")
179 DictionaryBuilder().Set("19", data64
)).Build();
181 const Extension
* extension
= env
.MakeExtension(
182 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }"));
184 bool bad_message
= false;
185 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
191 EXPECT_EQ("", error
);
192 EXPECT_FALSE(bad_message
);
193 ASSERT_TRUE(result
.get());
194 EXPECT_EQ(ContentAction::ACTION_SET_ICON
, result
->GetType());
196 ExtensionAction
* page_action
=
197 ExtensionActionManager::Get(env
.profile())->GetPageAction(*extension
);
198 scoped_ptr
<content::WebContents
> contents
= env
.MakeTab();
199 const int tab_id
= ExtensionTabUtil::GetTabId(contents
.get());
200 EXPECT_FALSE(page_action
->GetIsVisible(tab_id
));
201 ContentAction::ApplyInfo apply_info
= {
202 env
.profile(), contents
.get()
205 // The declarative icon shouldn't exist unless the content action is applied.
206 EXPECT_TRUE(page_action
->GetDeclarativeIcon(tab_id
).bitmap()->empty());
207 result
->Apply(extension
->id(), base::Time(), &apply_info
);
208 EXPECT_FALSE(page_action
->GetDeclarativeIcon(tab_id
).bitmap()->empty());
209 result
->Revert(extension
->id(), base::Time(), &apply_info
);
210 EXPECT_TRUE(page_action
->GetDeclarativeIcon(tab_id
).bitmap()->empty());
213 TEST_F(RequestContentScriptTest
, MissingScripts
) {
216 bool bad_message
= false;
217 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
222 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
223 " \"allFrames\": true,\n"
224 " \"matchAboutBlank\": true\n"
228 EXPECT_THAT(error
, testing::HasSubstr("Missing parameter is required"));
229 EXPECT_FALSE(bad_message
);
230 ASSERT_FALSE(result
.get());
233 TEST_F(RequestContentScriptTest
, CSS
) {
236 bool bad_message
= false;
237 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
242 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
243 " \"css\": [\"style.css\"]\n"
247 EXPECT_EQ("", error
);
248 EXPECT_FALSE(bad_message
);
249 ASSERT_TRUE(result
.get());
250 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT
, result
->GetType());
253 TEST_F(RequestContentScriptTest
, JS
) {
256 bool bad_message
= false;
257 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
262 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
263 " \"js\": [\"script.js\"]\n"
267 EXPECT_EQ("", error
);
268 EXPECT_FALSE(bad_message
);
269 ASSERT_TRUE(result
.get());
270 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT
, result
->GetType());
273 TEST_F(RequestContentScriptTest
, CSSBadType
) {
276 bool bad_message
= false;
277 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
282 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
283 " \"css\": \"style.css\"\n"
287 EXPECT_TRUE(bad_message
);
288 ASSERT_FALSE(result
.get());
291 TEST_F(RequestContentScriptTest
, JSBadType
) {
294 bool bad_message
= false;
295 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
300 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
301 " \"js\": \"script.js\"\n"
305 EXPECT_TRUE(bad_message
);
306 ASSERT_FALSE(result
.get());
309 TEST_F(RequestContentScriptTest
, AllFrames
) {
312 bool bad_message
= false;
313 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
318 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
319 " \"js\": [\"script.js\"],\n"
320 " \"allFrames\": true\n"
324 EXPECT_EQ("", error
);
325 EXPECT_FALSE(bad_message
);
326 ASSERT_TRUE(result
.get());
327 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT
, result
->GetType());
330 TEST_F(RequestContentScriptTest
, MatchAboutBlank
) {
333 bool bad_message
= false;
334 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
339 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
340 " \"js\": [\"script.js\"],\n"
341 " \"matchAboutBlank\": true\n"
345 EXPECT_EQ("", error
);
346 EXPECT_FALSE(bad_message
);
347 ASSERT_TRUE(result
.get());
348 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT
, result
->GetType());
351 TEST_F(RequestContentScriptTest
, AllFramesBadType
) {
354 bool bad_message
= false;
355 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
360 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
361 " \"js\": [\"script.js\"],\n"
362 " \"allFrames\": null\n"
366 EXPECT_TRUE(bad_message
);
367 ASSERT_FALSE(result
.get());
370 TEST_F(RequestContentScriptTest
, MatchAboutBlankBadType
) {
373 bool bad_message
= false;
374 scoped_refptr
<const ContentAction
> result
= ContentAction::Create(
379 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
380 " \"js\": [\"script.js\"],\n"
381 " \"matchAboutBlank\": null\n"
385 EXPECT_TRUE(bad_message
);
386 ASSERT_FALSE(result
.get());
390 } // namespace extensions