OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/file_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("baz.js")).value(), | 272 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("baz.js")).value(), |
273 Extension::GetResourcePath(extension.path(), "bar/../baz.js") | 273 Extension::GetResourcePath(extension.path(), "bar/../baz.js") |
274 .value()); | 274 .value()); |
275 EXPECT_EQ(FilePath().value(), | 275 EXPECT_EQ(FilePath().value(), |
276 Extension::GetResourcePath(extension.path(), "../baz.js").value()); | 276 Extension::GetResourcePath(extension.path(), "../baz.js").value()); |
277 } | 277 } |
278 | 278 |
279 TEST(ExtensionTest, LoadPageActionHelper) { | 279 TEST(ExtensionTest, LoadPageActionHelper) { |
280 Extension extension; | 280 Extension extension; |
281 std::string error_msg; | 281 std::string error_msg; |
282 scoped_ptr<PageAction> page_action; | 282 scoped_ptr<ContextualAction> action; |
283 DictionaryValue input; | 283 DictionaryValue input; |
284 | 284 |
285 // First try with an empty dictionary. We should get nothing back. | 285 // First try with an empty dictionary. We should get nothing back. |
286 ASSERT_EQ(NULL, extension.LoadPageActionHelper(&input, 0, &error_msg)); | 286 ASSERT_EQ(NULL, extension.LoadContextualActionHelper( |
| 287 &input, 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 288 ASSERT_STRNE("", error_msg.c_str()); |
| 289 error_msg = ""; |
| 290 |
| 291 // Now try the same, but as a browser action. Ensure same results. |
| 292 ASSERT_EQ(NULL, extension.LoadContextualActionHelper( |
| 293 &input, 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
287 ASSERT_STRNE("", error_msg.c_str()); | 294 ASSERT_STRNE("", error_msg.c_str()); |
288 error_msg = ""; | 295 error_msg = ""; |
289 | 296 |
290 // Now setup some values to use in the page action. | 297 // Now setup some values to use in the page action. |
291 const std::string id("MyPageActionId"); | 298 const std::string id("MyContextualActionId"); |
292 const std::string name("MyPageActionName"); | 299 const std::string name("MyContextualActionName"); |
293 std::string img1("image1.png"); | 300 std::string img1("image1.png"); |
294 std::string img2("image2.png"); | 301 std::string img2("image2.png"); |
295 | 302 |
296 // Add the page_actions dictionary. | 303 // Add the dictionary for the contextual action. |
297 input.SetString(keys::kPageActionId, id); | 304 input.SetString(keys::kPageActionId, id); |
298 input.SetString(keys::kName, name); | 305 input.SetString(keys::kName, name); |
299 ListValue* icons = new ListValue; | 306 ListValue* icons = new ListValue; |
300 icons->Set(0, Value::CreateStringValue(img1)); | 307 icons->Set(0, Value::CreateStringValue(img1)); |
301 icons->Set(1, Value::CreateStringValue(img2)); | 308 icons->Set(1, Value::CreateStringValue(img2)); |
302 input.Set(keys::kPageActionIcons, icons); | 309 input.Set(keys::kPageActionIcons, icons); |
303 | 310 |
304 // Parse the page action and read back the values from the object. | 311 // Parse as page action and read back the values from the object. |
305 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | 312 action.reset(extension.LoadContextualActionHelper( |
306 ASSERT_TRUE(NULL != page_action.get()); | 313 &input, 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 314 ASSERT_TRUE(NULL != action.get()); |
307 ASSERT_STREQ("", error_msg.c_str()); | 315 ASSERT_STREQ("", error_msg.c_str()); |
308 ASSERT_STREQ(id.c_str(), page_action->id().c_str()); | 316 ASSERT_STREQ(id.c_str(), action->id().c_str()); |
309 ASSERT_STREQ(name.c_str(), page_action->name().c_str()); | 317 ASSERT_STREQ(name.c_str(), action->name().c_str()); |
310 ASSERT_EQ(2u, page_action->icon_paths().size()); | 318 ASSERT_EQ(2u, action->icon_paths().size()); |
311 ASSERT_STREQ(img1.c_str(), page_action->icon_paths()[0].c_str()); | 319 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); |
312 ASSERT_STREQ(img2.c_str(), page_action->icon_paths()[1].c_str()); | 320 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); |
313 // Type hasn't been set, but it defaults to PERMANENT. | 321 ASSERT_EQ(ContextualAction::PAGE_ACTION, action->type()); |
314 ASSERT_EQ(PageAction::PERMANENT, page_action->type()); | 322 |
| 323 // Now try the same, but as a browser action. |
| 324 action.reset(extension.LoadContextualActionHelper( |
| 325 &input, 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
| 326 ASSERT_TRUE(NULL != action.get()); |
| 327 ASSERT_STREQ("", error_msg.c_str()); |
| 328 // Browser actions don't have an id, page actions do. |
| 329 ASSERT_STREQ("", action->id().c_str()); |
| 330 ASSERT_STREQ(name.c_str(), action->name().c_str()); |
| 331 ASSERT_EQ(2u, action->icon_paths().size()); |
| 332 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); |
| 333 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); |
| 334 ASSERT_EQ(ContextualAction::BROWSER_ACTION, action->type()); |
315 | 335 |
316 // Explicitly set the same type and parse again. | 336 // Explicitly set the same type and parse again. |
| 337 input.SetString(keys::kType, values::kPageActionTypeTab); |
| 338 action.reset(extension.LoadContextualActionHelper( |
| 339 &input, 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
| 340 ASSERT_TRUE(NULL != action.get()); |
| 341 ASSERT_STREQ("", error_msg.c_str()); |
| 342 ASSERT_EQ(ContextualAction::BROWSER_ACTION, action->type()); |
| 343 |
| 344 // Explicitly set the PAGE_ACTION type and parse again. |
317 input.SetString(keys::kType, values::kPageActionTypePermanent); | 345 input.SetString(keys::kType, values::kPageActionTypePermanent); |
318 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | 346 action.reset(extension.LoadContextualActionHelper( |
319 ASSERT_TRUE(NULL != page_action.get()); | 347 &input, 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 348 ASSERT_TRUE(NULL != action.get()); |
320 ASSERT_STREQ("", error_msg.c_str()); | 349 ASSERT_STREQ("", error_msg.c_str()); |
321 ASSERT_EQ(PageAction::PERMANENT, page_action->type()); | 350 ASSERT_EQ(ContextualAction::PAGE_ACTION, action->type()); |
322 | |
323 // Explicitly set the TAB type and parse again. | |
324 input.SetString(keys::kType, values::kPageActionTypeTab); | |
325 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | |
326 ASSERT_TRUE(NULL != page_action.get()); | |
327 ASSERT_STREQ("", error_msg.c_str()); | |
328 ASSERT_EQ(PageAction::TAB, page_action->type()); | |
329 | 351 |
330 // Make a deep copy of the input and remove one key at a time and see if we | 352 // Make a deep copy of the input and remove one key at a time and see if we |
331 // get the right error. | 353 // get the right error. |
332 scoped_ptr<DictionaryValue> copy; | 354 scoped_ptr<DictionaryValue> copy; |
333 | 355 |
334 // First remove id key. | 356 // First remove id key. |
335 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | 357 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
336 copy->Remove(keys::kPageActionId, NULL); | 358 copy->Remove(keys::kPageActionId, NULL); |
337 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | 359 action.reset(extension.LoadContextualActionHelper( |
338 ASSERT_TRUE(NULL == page_action.get()); | 360 copy.get(), 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 361 ASSERT_TRUE(NULL == action.get()); |
339 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | 362 ASSERT_TRUE(MatchPattern(error_msg.c_str(), |
340 errors::kInvalidPageActionId)); | 363 errors::kInvalidPageActionId)); |
| 364 error_msg = ""; |
| 365 |
| 366 // Same test (id key), but with browser action. |
| 367 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
| 368 copy->Remove(keys::kPageActionId, NULL); |
| 369 action.reset(extension.LoadContextualActionHelper( |
| 370 copy.get(), 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
| 371 // Having no id is valid for browser actions. |
| 372 ASSERT_TRUE(NULL != action.get()); |
| 373 ASSERT_STREQ("", error_msg.c_str()); |
| 374 error_msg = ""; |
341 | 375 |
342 // Then remove the name key. | 376 // Then remove the name key. |
343 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | 377 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
344 copy->Remove(keys::kName, NULL); | 378 copy->Remove(keys::kName, NULL); |
345 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | 379 action.reset(extension.LoadContextualActionHelper( |
346 ASSERT_TRUE(NULL == page_action.get()); | 380 copy.get(), 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 381 ASSERT_TRUE(NULL == action.get()); |
347 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | 382 ASSERT_TRUE(MatchPattern(error_msg.c_str(), |
348 errors::kInvalidName)); | 383 errors::kInvalidName)); |
| 384 error_msg = ""; |
| 385 |
| 386 // Same test (name key), but with browser action. |
| 387 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
| 388 copy->Remove(keys::kName, NULL); |
| 389 action.reset(extension.LoadContextualActionHelper( |
| 390 copy.get(), 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
| 391 ASSERT_TRUE(NULL == action.get()); |
| 392 ASSERT_TRUE(MatchPattern(error_msg.c_str(), |
| 393 errors::kInvalidName)); |
| 394 error_msg = ""; |
349 | 395 |
350 // Then remove the icon paths key. | 396 // Then remove the icon paths key. |
351 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | 397 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
352 copy->Remove(keys::kPageActionIcons, NULL); | 398 copy->Remove(keys::kPageActionIcons, NULL); |
353 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | 399 action.reset(extension.LoadContextualActionHelper( |
354 ASSERT_TRUE(NULL == page_action.get()); | 400 copy.get(), 0, &error_msg, ContextualAction::PAGE_ACTION)); |
| 401 ASSERT_TRUE(NULL == action.get()); |
355 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | 402 ASSERT_TRUE(MatchPattern(error_msg.c_str(), |
356 errors::kInvalidPageActionIconPaths)); | 403 errors::kInvalidPageActionIconPaths)); |
| 404 error_msg = ""; |
357 | 405 |
358 // Then set the type to something bogus. | 406 // Same test (name key), but with browser action. |
359 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | 407 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); |
360 copy->SetString(keys::kType, "something_bogus"); | 408 copy->Remove(keys::kPageActionIcons, NULL); |
361 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | 409 action.reset(extension.LoadContextualActionHelper( |
362 ASSERT_TRUE(NULL == page_action.get()); | 410 copy.get(), 0, &error_msg, ContextualAction::BROWSER_ACTION)); |
| 411 ASSERT_TRUE(NULL == action.get()); |
363 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | 412 ASSERT_TRUE(MatchPattern(error_msg.c_str(), |
364 errors::kInvalidPageActionTypeValue)); | 413 errors::kInvalidPageActionIconPaths)); |
365 } | 414 } |
366 | 415 |
367 TEST(ExtensionTest, IdIsValid) { | 416 TEST(ExtensionTest, IdIsValid) { |
368 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 417 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
369 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); | 418 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); |
370 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); | 419 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); |
371 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); | 420 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); |
372 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); | 421 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); |
373 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); | 422 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); |
374 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); | 423 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 scoped_ptr<Extension> new_extension( | 649 scoped_ptr<Extension> new_extension( |
601 LoadManifest("allow_silent_upgrade", | 650 LoadManifest("allow_silent_upgrade", |
602 std::string(kTests[i].base_name) + "_new.json")); | 651 std::string(kTests[i].base_name) + "_new.json")); |
603 | 652 |
604 EXPECT_EQ(kTests[i].expect_success, | 653 EXPECT_EQ(kTests[i].expect_success, |
605 Extension::IsPrivilegeIncrease(old_extension.get(), | 654 Extension::IsPrivilegeIncrease(old_extension.get(), |
606 new_extension.get())) | 655 new_extension.get())) |
607 << kTests[i].base_name; | 656 << kTests[i].base_name; |
608 } | 657 } |
609 } | 658 } |
OLD | NEW |