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

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

Issue 545068: Allow extensions to add, remove, or change their page action popup. (Closed)
Patch Set: Rebase, to run trybots with test data in place. Created 10 years, 11 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
« 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/common/extensions/extension.h" 5 #include "chrome/common/extensions/extension.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/extensions/extension_constants.h" 13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/extension_error_reporter.h" 14 #include "chrome/common/extensions/extension_error_reporter.h"
15 #include "chrome/common/extensions/extension_error_utils.h"
15 #include "chrome/common/json_value_serializer.h" 16 #include "chrome/common/json_value_serializer.h"
17 #include "chrome/common/url_constants.h"
16 #include "net/base/mime_sniffer.h" 18 #include "net/base/mime_sniffer.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 namespace keys = extension_manifest_keys; 21 namespace keys = extension_manifest_keys;
20 namespace values = extension_manifest_values; 22 namespace values = extension_manifest_values;
21 namespace errors = extension_manifest_errors; 23 namespace errors = extension_manifest_errors;
22 24
23 class ExtensionTest : public testing::Test { 25 class ExtensionTest : public testing::Test {
24 }; 26 };
25 27
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 copy->Remove(keys::kPageActionIcons, NULL); 401 copy->Remove(keys::kPageActionIcons, NULL);
400 action.reset(extension.LoadExtensionActionHelper(copy.get(), &error_msg)); 402 action.reset(extension.LoadExtensionActionHelper(copy.get(), &error_msg));
401 ASSERT_TRUE(NULL != action.get()); 403 ASSERT_TRUE(NULL != action.get());
402 error_msg = ""; 404 error_msg = "";
403 405
404 // Now test that we can parse the new format for page actions. 406 // Now test that we can parse the new format for page actions.
405 407
406 // Now setup some values to use in the page action. 408 // Now setup some values to use in the page action.
407 const std::string kTitle("MyExtensionActionTitle"); 409 const std::string kTitle("MyExtensionActionTitle");
408 const std::string kIcon("image1.png"); 410 const std::string kIcon("image1.png");
411 const std::string kPopupHtmlFile("a_popup.html");
409 412
410 // Add the dictionary for the contextual action. 413 // Add the dictionary for the contextual action.
411 input.Clear(); 414 input.Clear();
412 input.SetString(keys::kPageActionDefaultTitle, kTitle); 415 input.SetString(keys::kPageActionDefaultTitle, kTitle);
413 input.SetString(keys::kPageActionDefaultIcon, kIcon); 416 input.SetString(keys::kPageActionDefaultIcon, kIcon);
414 417
415 // Parse and read back the values from the object. 418 // Parse and read back the values from the object.
416 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg)); 419 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
417 ASSERT_TRUE(action.get()); 420 ASSERT_TRUE(action.get());
418 ASSERT_TRUE(error_msg.empty()); 421 ASSERT_TRUE(error_msg.empty());
(...skipping 15 matching lines...) Expand all
434 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg)); 437 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
435 ASSERT_TRUE(NULL != action.get()); 438 ASSERT_TRUE(NULL != action.get());
436 ASSERT_EQ(kTitle, action->GetTitle(1)); 439 ASSERT_EQ(kTitle, action->GetTitle(1));
437 ASSERT_TRUE(error_msg.empty()); 440 ASSERT_TRUE(error_msg.empty());
438 441
439 input.Remove(keys::kPageActionDefaultTitle, NULL); 442 input.Remove(keys::kPageActionDefaultTitle, NULL);
440 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg)); 443 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
441 ASSERT_TRUE(NULL == action.get()); 444 ASSERT_TRUE(NULL == action.get());
442 ASSERT_STREQ(errors::kInvalidPageActionName, error_msg.c_str()); 445 ASSERT_STREQ(errors::kInvalidPageActionName, error_msg.c_str());
443 error_msg = ""; 446 error_msg = "";
447
448 // Test that keys "popup" and "default_popup" both work, but can not
449 // be used at the same time.
450 input.Clear();
451 input.SetString(keys::kPageActionDefaultTitle, kTitle);
452 input.SetString(keys::kPageActionDefaultIcon, kIcon);
453
454 // LoadExtensionActionHelper expects the extension member |extension_url_|
455 // to be set.
456 extension.extension_url_ = GURL(std::string(chrome::kExtensionScheme) +
457 chrome::kStandardSchemeSeparator +
458 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/");
459
460 // Add key "popup", expect success.
461 input.SetString(keys::kPageActionPopup, kPopupHtmlFile);
462 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
463 ASSERT_TRUE(NULL != action.get());
464 ASSERT_TRUE(error_msg.empty());
465 ASSERT_STREQ(
466 extension.extension_url_.Resolve(kPopupHtmlFile).spec().c_str(),
467 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str());
468
469 // Add key "default_popup", expect failure.
470 input.SetString(keys::kPageActionDefaultPopup, kPopupHtmlFile);
471 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
472 ASSERT_TRUE(NULL == action.get());
473 ASSERT_STREQ(
474 ExtensionErrorUtils::FormatErrorMessage(
475 errors::kInvalidPageActionOldAndNewKeys,
476 WideToASCII(keys::kPageActionDefaultPopup),
477 WideToASCII(keys::kPageActionPopup)).c_str(),
478 error_msg.c_str());
479 error_msg = "";
480
481 // Remove key "popup", expect success.
482 input.Remove(keys::kPageActionPopup, NULL);
483 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
484 ASSERT_TRUE(NULL != action.get());
485 ASSERT_TRUE(error_msg.empty());
486 ASSERT_STREQ(
487 extension.extension_url_.Resolve(kPopupHtmlFile).spec().c_str(),
488 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str());
489
490 // Setting default_popup to "" is the same as having no popup.
491 input.Remove(keys::kPageActionDefaultPopup, NULL);
492 input.SetString(keys::kPageActionDefaultPopup, "");
493 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
494 ASSERT_TRUE(NULL != action.get());
495 ASSERT_TRUE(error_msg.empty());
496 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId));
497 ASSERT_STREQ(
498 "",
499 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str());
500
501 // Setting popup to "" is the same as having no popup.
502 input.Remove(keys::kPageActionDefaultPopup, NULL);
503 input.SetString(keys::kPageActionPopup, "");
504 action.reset(extension.LoadExtensionActionHelper(&input, &error_msg));
505 ASSERT_TRUE(NULL != action.get());
506 ASSERT_TRUE(error_msg.empty());
507 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId));
508 ASSERT_STREQ(
509 "",
510 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str());
444 } 511 }
445 512
446 TEST(ExtensionTest, IdIsValid) { 513 TEST(ExtensionTest, IdIsValid) {
447 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 514 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
448 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); 515 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp"));
449 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); 516 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop"));
450 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); 517 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"));
451 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); 518 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno"));
452 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); 519 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa"));
453 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); 520 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef"));
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 scoped_ptr<Extension> new_extension( 760 scoped_ptr<Extension> new_extension(
694 LoadManifest("allow_silent_upgrade", 761 LoadManifest("allow_silent_upgrade",
695 std::string(kTests[i].base_name) + "_new.json")); 762 std::string(kTests[i].base_name) + "_new.json"));
696 763
697 EXPECT_EQ(kTests[i].expect_success, 764 EXPECT_EQ(kTests[i].expect_success,
698 Extension::IsPrivilegeIncrease(old_extension.get(), 765 Extension::IsPrivilegeIncrease(old_extension.get(),
699 new_extension.get())) 766 new_extension.get()))
700 << kTests[i].base_name; 767 << kTests[i].base_name;
701 } 768 }
702 } 769 }
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