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