Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/common/extensions/extension_unittest.cc

Issue 307048: Save a little work when fetching the default title by only checking the name... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/extensions/extension_constants.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 #if defined(OS_WIN) 293 #if defined(OS_WIN)
294 FilePath path(StringPrintf(L"c:\\extension")); 294 FilePath path(StringPrintf(L"c:\\extension"));
295 #else 295 #else
296 FilePath path(StringPrintf("/extension")); 296 FilePath path(StringPrintf("/extension"));
297 #endif 297 #endif
298 Extension extension(path); 298 Extension extension(path);
299 std::string error_msg; 299 std::string error_msg;
300 scoped_ptr<ExtensionAction> action; 300 scoped_ptr<ExtensionAction> action;
301 DictionaryValue input; 301 DictionaryValue input;
302 302
303 // First try with an empty dictionary. We should get nothing back. 303 // First try with an empty dictionary.
304 ASSERT_TRUE(extension.LoadExtensionActionHelper( 304 ASSERT_TRUE(extension.LoadExtensionActionHelper(
305 &input, &error_msg, ExtensionAction::PAGE_ACTION) == NULL); 305 &input, &error_msg, ExtensionAction::PAGE_ACTION) != NULL);
306 ASSERT_STRNE("", error_msg.c_str()); 306 ASSERT_STREQ("", error_msg.c_str());
307 error_msg = ""; 307 error_msg = "";
308 308
309 // Now try the same, but as a browser action. Ensure same results. 309 // Now try the same, but as a browser action. Ensure same results.
310 ASSERT_TRUE(extension.LoadExtensionActionHelper( 310 ASSERT_TRUE(extension.LoadExtensionActionHelper(
311 &input, &error_msg, ExtensionAction::BROWSER_ACTION) == NULL); 311 &input, &error_msg, ExtensionAction::BROWSER_ACTION) != NULL);
312 ASSERT_STRNE("", error_msg.c_str()); 312 ASSERT_STREQ("", error_msg.c_str());
313 error_msg = ""; 313 error_msg = "";
314 314
315 // Now setup some values to use in the page action. 315 // Now setup some values to use in the page action.
316 const std::string id("MyExtensionActionId"); 316 const std::string id("MyExtensionActionId");
317 const std::string name("MyExtensionActionName"); 317 const std::string name("MyExtensionActionName");
318 std::string img1("image1.png"); 318 std::string img1("image1.png");
319 std::string img2("image2.png"); 319 std::string img2("image2.png");
320 320
321 // Add the dictionary for the contextual action. 321 // Add the dictionary for the contextual action.
322 input.SetString(keys::kPageActionId, id); 322 input.SetString(keys::kPageActionId, id);
323 input.SetString(keys::kName, name); 323 input.SetString(keys::kName, name);
324 ListValue* icons = new ListValue; 324 ListValue* icons = new ListValue;
325 icons->Set(0, Value::CreateStringValue(img1)); 325 icons->Set(0, Value::CreateStringValue(img1));
326 icons->Set(1, Value::CreateStringValue(img2)); 326 icons->Set(1, Value::CreateStringValue(img2));
327 input.Set(keys::kPageActionIcons, icons); 327 input.Set(keys::kPageActionIcons, icons);
328 328
329 // Parse as page action and read back the values from the object. 329 // Parse as page action and read back the values from the object.
330 action.reset(extension.LoadExtensionActionHelper( 330 action.reset(extension.LoadExtensionActionHelper(
331 &input, &error_msg, ExtensionAction::PAGE_ACTION)); 331 &input, &error_msg, ExtensionAction::PAGE_ACTION));
332 ASSERT_TRUE(NULL != action.get()); 332 ASSERT_TRUE(NULL != action.get());
333 ASSERT_STREQ("", error_msg.c_str()); 333 ASSERT_STREQ("", error_msg.c_str());
334 ASSERT_STREQ(id.c_str(), action->id().c_str()); 334 ASSERT_STREQ(id.c_str(), action->id().c_str());
335 // No title, so fall back to name.
335 ASSERT_STREQ(name.c_str(), action->title().c_str()); 336 ASSERT_STREQ(name.c_str(), action->title().c_str());
336 ASSERT_EQ(2u, action->icon_paths().size()); 337 ASSERT_EQ(2u, action->icon_paths().size());
337 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); 338 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str());
338 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); 339 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str());
339 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type()); 340 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
340 341
341 // Now try the same, but as a browser action. 342 // Now try the same, but as a browser action.
342 action.reset(extension.LoadExtensionActionHelper( 343 action.reset(extension.LoadExtensionActionHelper(
343 &input, &error_msg, ExtensionAction::BROWSER_ACTION)); 344 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
344 ASSERT_TRUE(NULL != action.get()); 345 ASSERT_TRUE(NULL != action.get());
345 ASSERT_STREQ("", error_msg.c_str()); 346 ASSERT_STREQ("", error_msg.c_str());
346 // Browser actions don't have an id, page actions do. 347 // Browser actions don't have an id, page actions do.
347 ASSERT_STREQ("", action->id().c_str()); 348 ASSERT_STREQ("", action->id().c_str());
349 // No title, so fall back to name.
348 ASSERT_STREQ(name.c_str(), action->title().c_str()); 350 ASSERT_STREQ(name.c_str(), action->title().c_str());
349 ASSERT_EQ(2u, action->icon_paths().size()); 351 ASSERT_EQ(2u, action->icon_paths().size());
350 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); 352 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str());
351 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); 353 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str());
352 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type()); 354 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
353 355
354 // Explicitly set the same type and parse again. 356 // Explicitly set the same type and parse again.
355 input.SetString(keys::kType, values::kPageActionTypeTab); 357 input.SetString(keys::kType, values::kPageActionTypeTab);
356 action.reset(extension.LoadExtensionActionHelper( 358 action.reset(extension.LoadExtensionActionHelper(
357 &input, &error_msg, ExtensionAction::BROWSER_ACTION)); 359 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
(...skipping 25 matching lines...) Expand all
383 // Same test (id key), but with browser action. 385 // Same test (id key), but with browser action.
384 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 386 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
385 copy->Remove(keys::kPageActionId, NULL); 387 copy->Remove(keys::kPageActionId, NULL);
386 action.reset(extension.LoadExtensionActionHelper( 388 action.reset(extension.LoadExtensionActionHelper(
387 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION)); 389 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
388 // Having no id is valid for browser actions. 390 // Having no id is valid for browser actions.
389 ASSERT_TRUE(NULL != action.get()); 391 ASSERT_TRUE(NULL != action.get());
390 ASSERT_STREQ("", error_msg.c_str()); 392 ASSERT_STREQ("", error_msg.c_str());
391 error_msg = ""; 393 error_msg = "";
392 394
393 // Then remove the name key. 395 // Then remove the name key. It's optional, so no error.
394 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 396 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
395 copy->Remove(keys::kName, NULL); 397 copy->Remove(keys::kName, NULL);
396 action.reset(extension.LoadExtensionActionHelper( 398 action.reset(extension.LoadExtensionActionHelper(
397 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION)); 399 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION));
398 ASSERT_TRUE(NULL == action.get()); 400 ASSERT_TRUE(NULL != action.get());
399 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 401 ASSERT_STREQ("", action->title().c_str());
400 errors::kInvalidPageActionDefaultTitle)); 402 ASSERT_STREQ("", error_msg.c_str());
401 error_msg = ""; 403 error_msg = "";
402 404
403 // Same test (name key), but with browser action. 405 // Same test (name key), but with browser action.
404 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 406 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
405 copy->Remove(keys::kName, NULL); 407 copy->Remove(keys::kName, NULL);
406 action.reset(extension.LoadExtensionActionHelper( 408 action.reset(extension.LoadExtensionActionHelper(
407 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION)); 409 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
408 ASSERT_TRUE(NULL == action.get()); 410 ASSERT_TRUE(NULL != action.get());
409 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 411 ASSERT_STREQ("", action->title().c_str());
410 errors::kInvalidPageActionDefaultTitle)); 412 ASSERT_STREQ("", error_msg.c_str());
411 error_msg = ""; 413 error_msg = "";
412 414
413 // Then remove the icon paths key. 415 // Then remove the icon paths key.
414 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 416 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
415 copy->Remove(keys::kPageActionIcons, NULL); 417 copy->Remove(keys::kPageActionIcons, NULL);
416 action.reset(extension.LoadExtensionActionHelper( 418 action.reset(extension.LoadExtensionActionHelper(
417 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION)); 419 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION));
418 ASSERT_TRUE(NULL != action.get()); 420 ASSERT_TRUE(NULL != action.get());
419 error_msg = ""; 421 error_msg = "";
420 422
421 // Same test (name key), but with browser action. 423 // Same test (icon paths key), but with browser action.
422 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 424 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
423 copy->Remove(keys::kPageActionIcons, NULL); 425 copy->Remove(keys::kPageActionIcons, NULL);
424 action.reset(extension.LoadExtensionActionHelper( 426 action.reset(extension.LoadExtensionActionHelper(
425 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION)); 427 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
426 ASSERT_TRUE(NULL != action.get()); 428 ASSERT_TRUE(NULL != action.get());
427 429
428 // Now test that we can parse the new format for page actions. 430 // Now test that we can parse the new format for page actions.
429 431
430 // Now setup some values to use in the page action. 432 // Now setup some values to use in the page action.
431 const std::string kTitle("MyExtensionActionTitle"); 433 const std::string kTitle("MyExtensionActionTitle");
432 const std::string kIcon("image1.png"); 434 const std::string kIcon("image1.png");
433 435
434 // Add the dictionary for the contextual action. 436 // Add the dictionary for the contextual action.
435 input.Clear(); 437 input.Clear();
436 input.SetString(keys::kPageActionDefaultTitle, kTitle); 438 input.SetString(keys::kPageActionDefaultTitle, kTitle);
437 input.SetString(keys::kPageActionDefaultIcon, kIcon); 439 input.SetString(keys::kPageActionDefaultIcon, kIcon);
438 440
439 // Parse as page action and read back the values from the object. 441 // Parse as page action and read back the values from the object.
440 action.reset(extension.LoadExtensionActionHelper( 442 action.reset(extension.LoadExtensionActionHelper(
441 &input, &error_msg, ExtensionAction::PAGE_ACTION)); 443 &input, &error_msg, ExtensionAction::PAGE_ACTION));
442 ASSERT_TRUE(action.get()); 444 ASSERT_TRUE(action.get());
443 ASSERT_STREQ("", error_msg.c_str()); 445 ASSERT_STREQ("", error_msg.c_str());
444 ASSERT_EQ(kTitle, action->title()); 446 ASSERT_EQ(kTitle, action->title());
445 ASSERT_EQ(1u, action->icon_paths().size()); 447 ASSERT_EQ(1u, action->icon_paths().size());
446 ASSERT_EQ(kIcon, action->icon_paths()[0]); 448 ASSERT_EQ(kIcon, action->icon_paths()[0]);
447 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type()); 449 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
450
451 // Same test, but with browser action.
452 action.reset(extension.LoadExtensionActionHelper(
453 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
454 ASSERT_TRUE(action.get());
455 ASSERT_STREQ("", error_msg.c_str());
456 ASSERT_EQ(kTitle, action->title());
457 ASSERT_EQ(1u, action->icon_paths().size());
458 ASSERT_EQ(kIcon, action->icon_paths()[0]);
459 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
460
461 // Invalid title should give an error even with a valid name.
462 input.Clear();
463 input.SetInteger(keys::kPageActionDefaultTitle, 42);
464 input.SetString(keys::kName, name);
465 action.reset(extension.LoadExtensionActionHelper(
466 &input, &error_msg, ExtensionAction::PAGE_ACTION));
467 ASSERT_TRUE(NULL == action.get());
468 ASSERT_STREQ(errors::kInvalidPageActionDefaultTitle, error_msg.c_str());
469 error_msg = "";
470
471 // Invalid name should give an error only with no title.
472 input.SetString(keys::kPageActionDefaultTitle, kTitle);
473 input.SetInteger(keys::kName, 123);
474 action.reset(extension.LoadExtensionActionHelper(
475 &input, &error_msg, ExtensionAction::PAGE_ACTION));
476 ASSERT_TRUE(NULL != action.get());
477 ASSERT_EQ(kTitle, action->title());
478 ASSERT_STREQ("", error_msg.c_str());
479 error_msg = "";
480
481 input.Remove(keys::kPageActionDefaultTitle, NULL);
482 action.reset(extension.LoadExtensionActionHelper(
483 &input, &error_msg, ExtensionAction::PAGE_ACTION));
484 ASSERT_TRUE(NULL == action.get());
485 ASSERT_STREQ(errors::kInvalidPageActionName, error_msg.c_str());
486 error_msg = "";
487
488 // Same test (invalid title) for browser actions.
489 input.Clear();
490 input.SetInteger(keys::kPageActionDefaultTitle, 42);
491 input.SetString(keys::kName, name);
492 action.reset(extension.LoadExtensionActionHelper(
493 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
494 ASSERT_TRUE(NULL == action.get());
495 ASSERT_STREQ(errors::kInvalidPageActionDefaultTitle, error_msg.c_str());
496 error_msg = "";
497
498 // Same tests (invalid name) for browser actions.
499 input.SetString(keys::kPageActionDefaultTitle, kTitle);
500 input.SetInteger(keys::kName, 123);
501 action.reset(extension.LoadExtensionActionHelper(
502 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
503 ASSERT_TRUE(NULL != action.get());
504 ASSERT_EQ(kTitle, action->title());
505 ASSERT_STREQ("", error_msg.c_str());
506 error_msg = "";
507
508 input.Remove(keys::kPageActionDefaultTitle, NULL);
509 action.reset(extension.LoadExtensionActionHelper(
510 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
511 ASSERT_TRUE(NULL == action.get());
512 ASSERT_STREQ(errors::kInvalidPageActionName, error_msg.c_str());
513 error_msg = "";
448 } 514 }
449 515
450 TEST(ExtensionTest, IdIsValid) { 516 TEST(ExtensionTest, IdIsValid) {
451 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 517 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
452 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); 518 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp"));
453 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); 519 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop"));
454 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); 520 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"));
455 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); 521 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno"));
456 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); 522 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa"));
457 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); 523 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef"));
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 scoped_ptr<Extension> new_extension( 759 scoped_ptr<Extension> new_extension(
694 LoadManifest("allow_silent_upgrade", 760 LoadManifest("allow_silent_upgrade",
695 std::string(kTests[i].base_name) + "_new.json")); 761 std::string(kTests[i].base_name) + "_new.json"));
696 762
697 EXPECT_EQ(kTests[i].expect_success, 763 EXPECT_EQ(kTests[i].expect_success,
698 Extension::IsPrivilegeIncrease(old_extension.get(), 764 Extension::IsPrivilegeIncrease(old_extension.get(),
699 new_extension.get())) 765 new_extension.get()))
700 << kTests[i].base_name; 766 << kTests[i].base_name;
701 } 767 }
702 } 768 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_constants.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698