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