OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" | 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/values_test_util.h" | 9 #include "base/test/values_test_util.h" |
10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 TEST(DeclarativeContentActionTest, InvalidCreation) { | 68 TEST(DeclarativeContentActionTest, InvalidCreation) { |
69 TestExtensionEnvironment env; | 69 TestExtensionEnvironment env; |
70 std::string error; | 70 std::string error; |
71 bool bad_message = false; | 71 bool bad_message = false; |
72 scoped_refptr<const ContentAction> result; | 72 scoped_refptr<const ContentAction> result; |
73 | 73 |
74 // Test wrong data type passed. | 74 // Test wrong data type passed. |
75 error.clear(); | 75 error.clear(); |
76 result = ContentAction::Create( | 76 result = ContentAction::Create( |
77 NULL, HostID(), NULL, *ParseJson("[]"), &error, &bad_message); | 77 NULL, NULL, *ParseJson("[]"), &error, &bad_message); |
78 EXPECT_TRUE(bad_message); | 78 EXPECT_TRUE(bad_message); |
79 EXPECT_EQ("", error); | 79 EXPECT_EQ("", error); |
80 EXPECT_FALSE(result.get()); | 80 EXPECT_FALSE(result.get()); |
81 | 81 |
82 // Test missing instanceType element. | 82 // Test missing instanceType element. |
83 error.clear(); | 83 error.clear(); |
84 result = ContentAction::Create( | 84 result = ContentAction::Create( |
85 NULL, HostID(), NULL, *ParseJson("{}"), &error, &bad_message); | 85 NULL, NULL, *ParseJson("{}"), &error, &bad_message); |
86 EXPECT_TRUE(bad_message); | 86 EXPECT_TRUE(bad_message); |
87 EXPECT_EQ("", error); | 87 EXPECT_EQ("", error); |
88 EXPECT_FALSE(result.get()); | 88 EXPECT_FALSE(result.get()); |
89 | 89 |
90 // Test wrong instanceType element. | 90 // Test wrong instanceType element. |
91 error.clear(); | 91 error.clear(); |
92 result = ContentAction::Create(NULL, HostID(), NULL, *ParseJson( | 92 result = ContentAction::Create(NULL, NULL, *ParseJson( |
93 "{\n" | 93 "{\n" |
94 " \"instanceType\": \"declarativeContent.UnknownType\",\n" | 94 " \"instanceType\": \"declarativeContent.UnknownType\",\n" |
95 "}"), | 95 "}"), |
96 &error, &bad_message); | 96 &error, &bad_message); |
97 EXPECT_THAT(error, HasSubstr("invalid instanceType")); | 97 EXPECT_THAT(error, HasSubstr("invalid instanceType")); |
98 EXPECT_FALSE(result.get()); | 98 EXPECT_FALSE(result.get()); |
99 } | 99 } |
100 | 100 |
101 TEST(DeclarativeContentActionTest, ShowPageActionWithoutPageAction) { | 101 TEST(DeclarativeContentActionTest, ShowPageActionWithoutPageAction) { |
102 TestExtensionEnvironment env; | 102 TestExtensionEnvironment env; |
103 | 103 |
104 const Extension* extension = env.MakeExtension(base::DictionaryValue()); | 104 const Extension* extension = env.MakeExtension(base::DictionaryValue()); |
105 std::string error; | 105 std::string error; |
106 bool bad_message = false; | 106 bool bad_message = false; |
107 scoped_refptr<const ContentAction> result = ContentAction::Create( | 107 scoped_refptr<const ContentAction> result = ContentAction::Create( |
108 NULL, | 108 NULL, |
109 HostID(HostID::EXTENSIONS, extension->id()), | |
110 extension, | 109 extension, |
111 *ParseJson( | 110 *ParseJson( |
112 "{\n" | 111 "{\n" |
113 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" | 112 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
114 "}"), | 113 "}"), |
115 &error, | 114 &error, |
116 &bad_message); | 115 &bad_message); |
117 EXPECT_THAT(error, testing::HasSubstr("without a page action")); | 116 EXPECT_THAT(error, testing::HasSubstr("without a page action")); |
118 EXPECT_FALSE(bad_message); | 117 EXPECT_FALSE(bad_message); |
119 ASSERT_FALSE(result.get()); | 118 ASSERT_FALSE(result.get()); |
120 } | 119 } |
121 | 120 |
122 TEST(DeclarativeContentActionTest, ShowPageAction) { | 121 TEST(DeclarativeContentActionTest, ShowPageAction) { |
123 TestExtensionEnvironment env; | 122 TestExtensionEnvironment env; |
124 | 123 |
125 const Extension* extension = env.MakeExtension( | 124 const Extension* extension = env.MakeExtension( |
126 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); | 125 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); |
127 std::string error; | 126 std::string error; |
128 bool bad_message = false; | 127 bool bad_message = false; |
129 scoped_refptr<const ContentAction> result = ContentAction::Create( | 128 scoped_refptr<const ContentAction> result = ContentAction::Create( |
130 NULL, | 129 NULL, |
131 HostID(HostID::EXTENSIONS, extension->id()), | |
132 extension, | 130 extension, |
133 *ParseJson( | 131 *ParseJson( |
134 "{\n" | 132 "{\n" |
135 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" | 133 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
136 "}"), | 134 "}"), |
137 &error, | 135 &error, |
138 &bad_message); | 136 &bad_message); |
139 EXPECT_EQ("", error); | 137 EXPECT_EQ("", error); |
140 EXPECT_FALSE(bad_message); | 138 EXPECT_FALSE(bad_message); |
141 ASSERT_TRUE(result.get()); | 139 ASSERT_TRUE(result.get()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 DictionaryBuilder().Set("instanceType", "declarativeContent.SetIcon") | 178 DictionaryBuilder().Set("instanceType", "declarativeContent.SetIcon") |
181 .Set("imageData", | 179 .Set("imageData", |
182 DictionaryBuilder().Set("19", data64)).Build(); | 180 DictionaryBuilder().Set("19", data64)).Build(); |
183 | 181 |
184 const Extension* extension = env.MakeExtension( | 182 const Extension* extension = env.MakeExtension( |
185 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); | 183 *ParseJson("{\"page_action\": { \"default_title\": \"Extension\" } }")); |
186 std::string error; | 184 std::string error; |
187 bool bad_message = false; | 185 bool bad_message = false; |
188 scoped_refptr<const ContentAction> result = ContentAction::Create( | 186 scoped_refptr<const ContentAction> result = ContentAction::Create( |
189 NULL, | 187 NULL, |
190 HostID(HostID::EXTENSIONS, extension->id()), | |
191 extension, | 188 extension, |
192 *dict, | 189 *dict, |
193 &error, | 190 &error, |
194 &bad_message); | 191 &bad_message); |
195 EXPECT_EQ("", error); | 192 EXPECT_EQ("", error); |
196 EXPECT_FALSE(bad_message); | 193 EXPECT_FALSE(bad_message); |
197 ASSERT_TRUE(result.get()); | 194 ASSERT_TRUE(result.get()); |
198 EXPECT_EQ(ContentAction::ACTION_SET_ICON, result->GetType()); | 195 EXPECT_EQ(ContentAction::ACTION_SET_ICON, result->GetType()); |
199 | 196 |
200 ExtensionAction* page_action = | 197 ExtensionAction* page_action = |
(...skipping 12 matching lines...) Expand all Loading... |
213 result->Revert(extension->id(), base::Time(), &apply_info); | 210 result->Revert(extension->id(), base::Time(), &apply_info); |
214 EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).IsEmpty()); | 211 EXPECT_TRUE(page_action->GetDeclarativeIcon(tab_id).IsEmpty()); |
215 } | 212 } |
216 | 213 |
217 TEST_F(RequestContentScriptTest, MissingScripts) { | 214 TEST_F(RequestContentScriptTest, MissingScripts) { |
218 Init(); | 215 Init(); |
219 std::string error; | 216 std::string error; |
220 bool bad_message = false; | 217 bool bad_message = false; |
221 scoped_refptr<const ContentAction> result = ContentAction::Create( | 218 scoped_refptr<const ContentAction> result = ContentAction::Create( |
222 profile(), | 219 profile(), |
223 HostID(HostID::EXTENSIONS, extension()->id()), | |
224 extension(), | 220 extension(), |
225 *ParseJson( | 221 *ParseJson( |
226 "{\n" | 222 "{\n" |
227 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 223 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
228 " \"allFrames\": true,\n" | 224 " \"allFrames\": true,\n" |
229 " \"matchAboutBlank\": true\n" | 225 " \"matchAboutBlank\": true\n" |
230 "}"), | 226 "}"), |
231 &error, | 227 &error, |
232 &bad_message); | 228 &bad_message); |
233 EXPECT_THAT(error, testing::HasSubstr("Missing parameter is required")); | 229 EXPECT_THAT(error, testing::HasSubstr("Missing parameter is required")); |
234 EXPECT_FALSE(bad_message); | 230 EXPECT_FALSE(bad_message); |
235 ASSERT_FALSE(result.get()); | 231 ASSERT_FALSE(result.get()); |
236 } | 232 } |
237 | 233 |
238 TEST_F(RequestContentScriptTest, CSS) { | 234 TEST_F(RequestContentScriptTest, CSS) { |
239 Init(); | 235 Init(); |
240 std::string error; | 236 std::string error; |
241 bool bad_message = false; | 237 bool bad_message = false; |
242 scoped_refptr<const ContentAction> result = ContentAction::Create( | 238 scoped_refptr<const ContentAction> result = ContentAction::Create( |
243 profile(), | 239 profile(), |
244 HostID(HostID::EXTENSIONS, extension()->id()), | |
245 extension(), | 240 extension(), |
246 *ParseJson( | 241 *ParseJson( |
247 "{\n" | 242 "{\n" |
248 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 243 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
249 " \"css\": [\"style.css\"]\n" | 244 " \"css\": [\"style.css\"]\n" |
250 "}"), | 245 "}"), |
251 &error, | 246 &error, |
252 &bad_message); | 247 &bad_message); |
253 EXPECT_EQ("", error); | 248 EXPECT_EQ("", error); |
254 EXPECT_FALSE(bad_message); | 249 EXPECT_FALSE(bad_message); |
255 ASSERT_TRUE(result.get()); | 250 ASSERT_TRUE(result.get()); |
256 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 251 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
257 } | 252 } |
258 | 253 |
259 TEST_F(RequestContentScriptTest, JS) { | 254 TEST_F(RequestContentScriptTest, JS) { |
260 Init(); | 255 Init(); |
261 std::string error; | 256 std::string error; |
262 bool bad_message = false; | 257 bool bad_message = false; |
263 scoped_refptr<const ContentAction> result = ContentAction::Create( | 258 scoped_refptr<const ContentAction> result = ContentAction::Create( |
264 profile(), | 259 profile(), |
265 HostID(HostID::EXTENSIONS, extension()->id()), | |
266 extension(), | 260 extension(), |
267 *ParseJson( | 261 *ParseJson( |
268 "{\n" | 262 "{\n" |
269 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 263 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
270 " \"js\": [\"script.js\"]\n" | 264 " \"js\": [\"script.js\"]\n" |
271 "}"), | 265 "}"), |
272 &error, | 266 &error, |
273 &bad_message); | 267 &bad_message); |
274 EXPECT_EQ("", error); | 268 EXPECT_EQ("", error); |
275 EXPECT_FALSE(bad_message); | 269 EXPECT_FALSE(bad_message); |
276 ASSERT_TRUE(result.get()); | 270 ASSERT_TRUE(result.get()); |
277 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 271 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
278 } | 272 } |
279 | 273 |
280 TEST_F(RequestContentScriptTest, CSSBadType) { | 274 TEST_F(RequestContentScriptTest, CSSBadType) { |
281 Init(); | 275 Init(); |
282 std::string error; | 276 std::string error; |
283 bool bad_message = false; | 277 bool bad_message = false; |
284 scoped_refptr<const ContentAction> result = ContentAction::Create( | 278 scoped_refptr<const ContentAction> result = ContentAction::Create( |
285 profile(), | 279 profile(), |
286 HostID(HostID::EXTENSIONS, extension()->id()), | |
287 extension(), | 280 extension(), |
288 *ParseJson( | 281 *ParseJson( |
289 "{\n" | 282 "{\n" |
290 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 283 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
291 " \"css\": \"style.css\"\n" | 284 " \"css\": \"style.css\"\n" |
292 "}"), | 285 "}"), |
293 &error, | 286 &error, |
294 &bad_message); | 287 &bad_message); |
295 EXPECT_TRUE(bad_message); | 288 EXPECT_TRUE(bad_message); |
296 ASSERT_FALSE(result.get()); | 289 ASSERT_FALSE(result.get()); |
297 } | 290 } |
298 | 291 |
299 TEST_F(RequestContentScriptTest, JSBadType) { | 292 TEST_F(RequestContentScriptTest, JSBadType) { |
300 Init(); | 293 Init(); |
301 std::string error; | 294 std::string error; |
302 bool bad_message = false; | 295 bool bad_message = false; |
303 scoped_refptr<const ContentAction> result = ContentAction::Create( | 296 scoped_refptr<const ContentAction> result = ContentAction::Create( |
304 profile(), | 297 profile(), |
305 HostID(HostID::EXTENSIONS, extension()->id()), | |
306 extension(), | 298 extension(), |
307 *ParseJson( | 299 *ParseJson( |
308 "{\n" | 300 "{\n" |
309 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 301 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
310 " \"js\": \"script.js\"\n" | 302 " \"js\": \"script.js\"\n" |
311 "}"), | 303 "}"), |
312 &error, | 304 &error, |
313 &bad_message); | 305 &bad_message); |
314 EXPECT_TRUE(bad_message); | 306 EXPECT_TRUE(bad_message); |
315 ASSERT_FALSE(result.get()); | 307 ASSERT_FALSE(result.get()); |
316 } | 308 } |
317 | 309 |
318 TEST_F(RequestContentScriptTest, AllFrames) { | 310 TEST_F(RequestContentScriptTest, AllFrames) { |
319 Init(); | 311 Init(); |
320 std::string error; | 312 std::string error; |
321 bool bad_message = false; | 313 bool bad_message = false; |
322 scoped_refptr<const ContentAction> result = ContentAction::Create( | 314 scoped_refptr<const ContentAction> result = ContentAction::Create( |
323 profile(), | 315 profile(), |
324 HostID(HostID::EXTENSIONS, extension()->id()), | |
325 extension(), | 316 extension(), |
326 *ParseJson( | 317 *ParseJson( |
327 "{\n" | 318 "{\n" |
328 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 319 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
329 " \"js\": [\"script.js\"],\n" | 320 " \"js\": [\"script.js\"],\n" |
330 " \"allFrames\": true\n" | 321 " \"allFrames\": true\n" |
331 "}"), | 322 "}"), |
332 &error, | 323 &error, |
333 &bad_message); | 324 &bad_message); |
334 EXPECT_EQ("", error); | 325 EXPECT_EQ("", error); |
335 EXPECT_FALSE(bad_message); | 326 EXPECT_FALSE(bad_message); |
336 ASSERT_TRUE(result.get()); | 327 ASSERT_TRUE(result.get()); |
337 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 328 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
338 } | 329 } |
339 | 330 |
340 TEST_F(RequestContentScriptTest, MatchAboutBlank) { | 331 TEST_F(RequestContentScriptTest, MatchAboutBlank) { |
341 Init(); | 332 Init(); |
342 std::string error; | 333 std::string error; |
343 bool bad_message = false; | 334 bool bad_message = false; |
344 scoped_refptr<const ContentAction> result = ContentAction::Create( | 335 scoped_refptr<const ContentAction> result = ContentAction::Create( |
345 profile(), | 336 profile(), |
346 HostID(HostID::EXTENSIONS, extension()->id()), | |
347 extension(), | 337 extension(), |
348 *ParseJson( | 338 *ParseJson( |
349 "{\n" | 339 "{\n" |
350 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 340 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
351 " \"js\": [\"script.js\"],\n" | 341 " \"js\": [\"script.js\"],\n" |
352 " \"matchAboutBlank\": true\n" | 342 " \"matchAboutBlank\": true\n" |
353 "}"), | 343 "}"), |
354 &error, | 344 &error, |
355 &bad_message); | 345 &bad_message); |
356 EXPECT_EQ("", error); | 346 EXPECT_EQ("", error); |
357 EXPECT_FALSE(bad_message); | 347 EXPECT_FALSE(bad_message); |
358 ASSERT_TRUE(result.get()); | 348 ASSERT_TRUE(result.get()); |
359 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); | 349 EXPECT_EQ(ContentAction::ACTION_REQUEST_CONTENT_SCRIPT, result->GetType()); |
360 } | 350 } |
361 | 351 |
362 TEST_F(RequestContentScriptTest, AllFramesBadType) { | 352 TEST_F(RequestContentScriptTest, AllFramesBadType) { |
363 Init(); | 353 Init(); |
364 std::string error; | 354 std::string error; |
365 bool bad_message = false; | 355 bool bad_message = false; |
366 scoped_refptr<const ContentAction> result = ContentAction::Create( | 356 scoped_refptr<const ContentAction> result = ContentAction::Create( |
367 profile(), | 357 profile(), |
368 HostID(HostID::EXTENSIONS, extension()->id()), | |
369 extension(), | 358 extension(), |
370 *ParseJson( | 359 *ParseJson( |
371 "{\n" | 360 "{\n" |
372 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 361 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
373 " \"js\": [\"script.js\"],\n" | 362 " \"js\": [\"script.js\"],\n" |
374 " \"allFrames\": null\n" | 363 " \"allFrames\": null\n" |
375 "}"), | 364 "}"), |
376 &error, | 365 &error, |
377 &bad_message); | 366 &bad_message); |
378 EXPECT_TRUE(bad_message); | 367 EXPECT_TRUE(bad_message); |
379 ASSERT_FALSE(result.get()); | 368 ASSERT_FALSE(result.get()); |
380 } | 369 } |
381 | 370 |
382 TEST_F(RequestContentScriptTest, MatchAboutBlankBadType) { | 371 TEST_F(RequestContentScriptTest, MatchAboutBlankBadType) { |
383 Init(); | 372 Init(); |
384 std::string error; | 373 std::string error; |
385 bool bad_message = false; | 374 bool bad_message = false; |
386 scoped_refptr<const ContentAction> result = ContentAction::Create( | 375 scoped_refptr<const ContentAction> result = ContentAction::Create( |
387 profile(), | 376 profile(), |
388 HostID(HostID::EXTENSIONS, extension()->id()), | |
389 extension(), | 377 extension(), |
390 *ParseJson( | 378 *ParseJson( |
391 "{\n" | 379 "{\n" |
392 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" | 380 " \"instanceType\": \"declarativeContent.RequestContentScript\",\n" |
393 " \"js\": [\"script.js\"],\n" | 381 " \"js\": [\"script.js\"],\n" |
394 " \"matchAboutBlank\": null\n" | 382 " \"matchAboutBlank\": null\n" |
395 "}"), | 383 "}"), |
396 &error, | 384 &error, |
397 &bad_message); | 385 &bad_message); |
398 EXPECT_TRUE(bad_message); | 386 EXPECT_TRUE(bad_message); |
399 ASSERT_FALSE(result.get()); | 387 ASSERT_FALSE(result.get()); |
400 } | 388 } |
401 | 389 |
402 } // namespace | 390 } // namespace |
403 } // namespace extensions | 391 } // namespace extensions |
OLD | NEW |