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
{
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")
45 class RequestContentScriptTest
: public ExtensionServiceTestBase
{
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
55 InitializeEmptyExtensionService();
56 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile()))->
58 base::RunLoop().RunUntilIdle();
61 Profile
* profile() { return profile_
.get(); }
62 Extension
* extension() { return extension_
.get(); }
65 scoped_refptr
<Extension
> extension_
;
68 TEST(DeclarativeContentActionTest
, InvalidCreation
) {
69 TestExtensionEnvironment env
;
71 scoped_ptr
<const ContentAction
> result
;
73 // Test wrong data type passed.
75 result
= ContentAction::Create(
76 NULL
, NULL
, *ParseJson("[]"), &error
);
78 EXPECT_FALSE(result
.get());
80 // Test missing instanceType element.
82 result
= ContentAction::Create(
83 NULL
, NULL
, *ParseJson("{}"), &error
);
85 EXPECT_FALSE(result
.get());
87 // Test wrong instanceType element.
89 result
= ContentAction::Create(NULL
, NULL
, *ParseJson(
91 " \"instanceType\": \"declarativeContent.UnknownType\",\n"
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());
103 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
108 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
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\" } }"));
121 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
126 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n"
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.
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
)
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());
165 base::Base64Encode(binary_data
, &data64
);
167 scoped_ptr
<base::DictionaryValue
> dict
=
168 DictionaryBuilder().Set("instanceType", "declarativeContent.SetIcon")
170 DictionaryBuilder().Set("19", data64
)).Build();
172 const Extension
* extension
= env
.MakeExtension(
173 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }"));
175 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
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
) {
203 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
208 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
209 " \"allFrames\": true,\n"
210 " \"matchAboutBlank\": true\n"
213 EXPECT_THAT(error
, testing::HasSubstr("Missing parameter is required"));
214 ASSERT_FALSE(result
.get());
217 TEST_F(RequestContentScriptTest
, CSS
) {
220 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
225 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
226 " \"css\": [\"style.css\"]\n"
229 EXPECT_EQ("", error
);
230 ASSERT_TRUE(result
.get());
233 TEST_F(RequestContentScriptTest
, JS
) {
236 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
241 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
242 " \"js\": [\"script.js\"]\n"
245 EXPECT_EQ("", error
);
246 ASSERT_TRUE(result
.get());
249 TEST_F(RequestContentScriptTest
, CSSBadType
) {
252 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
257 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
258 " \"css\": \"style.css\"\n"
261 ASSERT_FALSE(result
.get());
264 TEST_F(RequestContentScriptTest
, JSBadType
) {
267 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
272 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
273 " \"js\": \"script.js\"\n"
276 ASSERT_FALSE(result
.get());
279 TEST_F(RequestContentScriptTest
, AllFrames
) {
282 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
287 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
288 " \"js\": [\"script.js\"],\n"
289 " \"allFrames\": true\n"
292 EXPECT_EQ("", error
);
293 ASSERT_TRUE(result
.get());
296 TEST_F(RequestContentScriptTest
, MatchAboutBlank
) {
299 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
304 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
305 " \"js\": [\"script.js\"],\n"
306 " \"matchAboutBlank\": true\n"
309 EXPECT_EQ("", error
);
310 ASSERT_TRUE(result
.get());
313 TEST_F(RequestContentScriptTest
, AllFramesBadType
) {
316 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
321 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
322 " \"js\": [\"script.js\"],\n"
323 " \"allFrames\": null\n"
326 ASSERT_FALSE(result
.get());
329 TEST_F(RequestContentScriptTest
, MatchAboutBlankBadType
) {
332 scoped_ptr
<const ContentAction
> result
= ContentAction::Create(
337 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n"
338 " \"js\": [\"script.js\"],\n"
339 " \"matchAboutBlank\": null\n"
342 ASSERT_FALSE(result
.get());
346 } // namespace extensions