OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/extensions/api/extension_action/page_action_handler.h" |
| 6 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 7 #include "chrome/common/extensions/manifest_handler.h" |
| 8 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h" |
| 9 #include "extensions/common/error_utils.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace errors = extension_manifest_errors; |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 class PageActionManifestTest : public ExtensionManifestTest { |
| 17 protected: |
| 18 virtual void SetUp() OVERRIDE { |
| 19 ExtensionManifestTest::SetUp(); |
| 20 ManifestMultiKeyHandler::Register(new PageActionHandler); |
| 21 } |
| 22 |
| 23 virtual const char* test_data_dir() OVERRIDE { |
| 24 return "page_action"; |
| 25 } |
| 26 |
| 27 scoped_ptr<ActionInfo> LoadAction(const std::string& manifest_filename); |
| 28 }; |
| 29 |
| 30 scoped_ptr<ActionInfo> PageActionManifestTest::LoadAction( |
| 31 const std::string& manifest_filename) { |
| 32 scoped_refptr<Extension> extension = LoadAndExpectSuccess( |
| 33 manifest_filename.c_str()); |
| 34 const ActionInfo* page_action_info = ActionInfo::GetPageActionInfo(extension); |
| 35 EXPECT_TRUE(page_action_info); |
| 36 if (page_action_info) { |
| 37 return make_scoped_ptr(new ActionInfo(*page_action_info)); |
| 38 } |
| 39 ADD_FAILURE() << "Expected manifest in " << manifest_filename |
| 40 << " to include a page_action section."; |
| 41 return scoped_ptr<ActionInfo>(); |
| 42 } |
| 43 |
| 44 TEST_F(PageActionManifestTest, ManifestVersion2) { |
| 45 scoped_refptr<Extension> extension( |
| 46 LoadAndExpectSuccess("page_action_manifest_version_2.json")); |
| 47 ASSERT_TRUE(extension); |
| 48 const ActionInfo* page_action_info = ActionInfo::GetPageActionInfo(extension); |
| 49 ASSERT_TRUE(page_action_info); |
| 50 |
| 51 EXPECT_EQ("", page_action_info->id); |
| 52 EXPECT_TRUE(page_action_info->default_icon.empty()); |
| 53 EXPECT_EQ("", page_action_info->default_title); |
| 54 EXPECT_TRUE(page_action_info->default_popup_url.is_empty()); |
| 55 |
| 56 LoadAndExpectError("page_action_manifest_version_2b.json", |
| 57 errors::kInvalidPageActionPopup); |
| 58 } |
| 59 |
| 60 TEST_F(PageActionManifestTest, LoadPageActionHelper) { |
| 61 scoped_ptr<ActionInfo> action; |
| 62 |
| 63 // First try with an empty dictionary. |
| 64 action = LoadAction("page_action_empty.json"); |
| 65 ASSERT_TRUE(action != NULL); |
| 66 |
| 67 // Now setup some values to use in the action. |
| 68 const std::string id("MyExtensionActionId"); |
| 69 const std::string name("MyExtensionActionName"); |
| 70 std::string img1("image1.png"); |
| 71 |
| 72 action = LoadAction("page_action.json"); |
| 73 ASSERT_TRUE(NULL != action.get()); |
| 74 ASSERT_EQ(id, action->id); |
| 75 |
| 76 // No title, so fall back to name. |
| 77 ASSERT_EQ(name, action->default_title); |
| 78 ASSERT_EQ(img1, |
| 79 action->default_icon.Get(extension_misc::EXTENSION_ICON_ACTION, |
| 80 ExtensionIconSet::MATCH_EXACTLY)); |
| 81 |
| 82 // Same test with explicitly set type. |
| 83 action = LoadAction("page_action_type.json"); |
| 84 ASSERT_TRUE(NULL != action.get()); |
| 85 |
| 86 // Try an action without id key. |
| 87 action = LoadAction("page_action_no_id.json"); |
| 88 ASSERT_TRUE(NULL != action.get()); |
| 89 |
| 90 // Then try without the name key. It's optional, so no error. |
| 91 action = LoadAction("page_action_no_name.json"); |
| 92 ASSERT_TRUE(NULL != action.get()); |
| 93 ASSERT_TRUE(action->default_title.empty()); |
| 94 |
| 95 // Then try without the icon paths key. |
| 96 action = LoadAction("page_action_no_icon.json"); |
| 97 ASSERT_TRUE(NULL != action.get()); |
| 98 |
| 99 // Now test that we can parse the new format for page actions. |
| 100 const std::string kTitle("MyExtensionActionTitle"); |
| 101 const std::string kIcon("image1.png"); |
| 102 const std::string kPopupHtmlFile("a_popup.html"); |
| 103 |
| 104 action = LoadAction("page_action_new_format.json"); |
| 105 ASSERT_TRUE(action.get()); |
| 106 ASSERT_EQ(kTitle, action->default_title); |
| 107 ASSERT_FALSE(action->default_icon.empty()); |
| 108 |
| 109 // Invalid title should give an error even with a valid name. |
| 110 LoadAndExpectError("page_action_invalid_title.json", |
| 111 errors::kInvalidPageActionDefaultTitle); |
| 112 |
| 113 // Invalid name should give an error only with no title. |
| 114 action = LoadAction("page_action_invalid_name.json"); |
| 115 ASSERT_TRUE(NULL != action.get()); |
| 116 ASSERT_EQ(kTitle, action->default_title); |
| 117 |
| 118 LoadAndExpectError("page_action_invalid_name_no_title.json", |
| 119 errors::kInvalidPageActionName); |
| 120 |
| 121 // Test that keys "popup" and "default_popup" both work, but can not |
| 122 // be used at the same time. |
| 123 // These tests require an extension_url, so we also load the manifest. |
| 124 |
| 125 // Only use "popup", expect success. |
| 126 scoped_refptr<Extension> extension = |
| 127 LoadAndExpectSuccess("page_action_popup.json"); |
| 128 // TODO(yoz): this is dumb |
| 129 action = LoadAction("page_action_popup.json"); |
| 130 ASSERT_TRUE(NULL != action.get()); |
| 131 ASSERT_STREQ( |
| 132 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), |
| 133 action->default_popup_url.spec().c_str()); |
| 134 |
| 135 // Use both "popup" and "default_popup", expect failure. |
| 136 LoadAndExpectError("page_action_popup_and_default_popup.json", |
| 137 ErrorUtils::FormatErrorMessage( |
| 138 errors::kInvalidPageActionOldAndNewKeys, |
| 139 keys::kPageActionDefaultPopup, |
| 140 keys::kPageActionPopup)); |
| 141 |
| 142 // Use only "default_popup", expect success. |
| 143 extension = LoadAndExpectSuccess("page_action_popup.json"); |
| 144 action = LoadAction("page_action_default_popup.json"); |
| 145 ASSERT_TRUE(NULL != action.get()); |
| 146 ASSERT_STREQ( |
| 147 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), |
| 148 action->default_popup_url.spec().c_str()); |
| 149 |
| 150 // Setting default_popup to "" is the same as having no popup. |
| 151 action = LoadAction("page_action_empty_default_popup.json"); |
| 152 ASSERT_TRUE(NULL != action.get()); |
| 153 EXPECT_TRUE(action->default_popup_url.is_empty()); |
| 154 ASSERT_STREQ( |
| 155 "", |
| 156 action->default_popup_url.spec().c_str()); |
| 157 |
| 158 // Setting popup to "" is the same as having no popup. |
| 159 action = LoadAction("page_action_empty_popup.json"); |
| 160 |
| 161 ASSERT_TRUE(NULL != action.get()); |
| 162 EXPECT_TRUE(action->default_popup_url.is_empty()); |
| 163 ASSERT_STREQ( |
| 164 "", |
| 165 action->default_popup_url.spec().c_str()); |
| 166 } |
| 167 |
| 168 } // extensions |
OLD | NEW |