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