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