| 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/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/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 const std::string& test_file) { | 89 const std::string& test_file) { |
| 90 return LoadManifest(dir, test_file, Extension::NO_FLAGS); | 90 return LoadManifest(dir, test_file, Extension::NO_FLAGS); |
| 91 } | 91 } |
| 92 | 92 |
| 93 static scoped_refptr<Extension> LoadManifestStrict( | 93 static scoped_refptr<Extension> LoadManifestStrict( |
| 94 const std::string& dir, | 94 const std::string& dir, |
| 95 const std::string& test_file) { | 95 const std::string& test_file) { |
| 96 return LoadManifest(dir, test_file, Extension::NO_FLAGS); | 96 return LoadManifest(dir, test_file, Extension::NO_FLAGS); |
| 97 } | 97 } |
| 98 | 98 |
| 99 static ExtensionAction* LoadAction(const std::string& manifest) { | 99 static scoped_ptr<ExtensionAction> LoadAction(const std::string& manifest) { |
| 100 scoped_refptr<Extension> extension = LoadManifest("page_action", | 100 scoped_refptr<Extension> extension = LoadManifest("page_action", |
| 101 manifest); | 101 manifest); |
| 102 return new ExtensionAction(*(extension->page_action())); | 102 return extension->page_action()->CopyForTest(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 static void LoadActionAndExpectError(const std::string& manifest, | 105 static void LoadActionAndExpectError(const std::string& manifest, |
| 106 const std::string& expected_error) { | 106 const std::string& expected_error) { |
| 107 std::string error; | 107 std::string error; |
| 108 scoped_refptr<Extension> extension = LoadManifestUnchecked("page_action", | 108 scoped_refptr<Extension> extension = LoadManifestUnchecked("page_action", |
| 109 manifest, Extension::INTERNAL, Extension::NO_FLAGS, &error); | 109 manifest, Extension::INTERNAL, Extension::NO_FLAGS, &error); |
| 110 EXPECT_FALSE(extension); | 110 EXPECT_FALSE(extension); |
| 111 EXPECT_EQ(expected_error, error); | 111 EXPECT_EQ(expected_error, error); |
| 112 } | 112 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 EXPECT_EQ(extension->path().AppendASCII("test.html").value(), | 197 EXPECT_EQ(extension->path().AppendASCII("test.html").value(), |
| 198 extension->GetResource("test.html").GetFilePath().value()); | 198 extension->GetResource("test.html").GetFilePath().value()); |
| 199 EXPECT_EQ(extension->path().AppendASCII("test.js").value(), | 199 EXPECT_EQ(extension->path().AppendASCII("test.js").value(), |
| 200 extension->GetResource("test.js").GetFilePath().value()); | 200 extension->GetResource("test.js").GetFilePath().value()); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST(ExtensionTest, LoadPageActionHelper) { | 203 TEST(ExtensionTest, LoadPageActionHelper) { |
| 204 scoped_ptr<ExtensionAction> action; | 204 scoped_ptr<ExtensionAction> action; |
| 205 | 205 |
| 206 // First try with an empty dictionary. | 206 // First try with an empty dictionary. |
| 207 action.reset(LoadAction("page_action_empty.json")); | 207 action = LoadAction("page_action_empty.json"); |
| 208 ASSERT_TRUE(action != NULL); | 208 ASSERT_TRUE(action != NULL); |
| 209 | 209 |
| 210 // Now setup some values to use in the action. | 210 // Now setup some values to use in the action. |
| 211 const std::string id("MyExtensionActionId"); | 211 const std::string id("MyExtensionActionId"); |
| 212 const std::string name("MyExtensionActionName"); | 212 const std::string name("MyExtensionActionName"); |
| 213 std::string img1("image1.png"); | 213 std::string img1("image1.png"); |
| 214 std::string img2("image2.png"); | 214 std::string img2("image2.png"); |
| 215 | 215 |
| 216 action.reset(LoadAction("page_action.json")); | 216 action = LoadAction("page_action.json"); |
| 217 ASSERT_TRUE(NULL != action.get()); | 217 ASSERT_TRUE(NULL != action.get()); |
| 218 ASSERT_EQ(id, action->id()); | 218 ASSERT_EQ(id, action->id()); |
| 219 | 219 |
| 220 // No title, so fall back to name. | 220 // No title, so fall back to name. |
| 221 ASSERT_EQ(name, action->GetTitle(1)); | 221 ASSERT_EQ(name, action->GetTitle(1)); |
| 222 ASSERT_EQ(2u, action->icon_paths()->size()); | 222 ASSERT_EQ(2u, action->icon_paths()->size()); |
| 223 ASSERT_EQ(img1, (*action->icon_paths())[0]); | 223 ASSERT_EQ(img1, (*action->icon_paths())[0]); |
| 224 ASSERT_EQ(img2, (*action->icon_paths())[1]); | 224 ASSERT_EQ(img2, (*action->icon_paths())[1]); |
| 225 | 225 |
| 226 // Same test with explicitly set type. | 226 // Same test with explicitly set type. |
| 227 action.reset(LoadAction("page_action_type.json")); | 227 action = LoadAction("page_action_type.json"); |
| 228 ASSERT_TRUE(NULL != action.get()); | 228 ASSERT_TRUE(NULL != action.get()); |
| 229 | 229 |
| 230 // Try an action without id key. | 230 // Try an action without id key. |
| 231 action.reset(LoadAction("page_action_no_id.json")); | 231 action = LoadAction("page_action_no_id.json"); |
| 232 ASSERT_TRUE(NULL != action.get()); | 232 ASSERT_TRUE(NULL != action.get()); |
| 233 | 233 |
| 234 // Then try without the name key. It's optional, so no error. | 234 // Then try without the name key. It's optional, so no error. |
| 235 action.reset(LoadAction("page_action_no_name.json")); | 235 action = LoadAction("page_action_no_name.json"); |
| 236 ASSERT_TRUE(NULL != action.get()); | 236 ASSERT_TRUE(NULL != action.get()); |
| 237 ASSERT_TRUE(action->GetTitle(1).empty()); | 237 ASSERT_TRUE(action->GetTitle(1).empty()); |
| 238 | 238 |
| 239 // Then try without the icon paths key. | 239 // Then try without the icon paths key. |
| 240 action.reset(LoadAction("page_action_no_icon.json")); | 240 action = LoadAction("page_action_no_icon.json"); |
| 241 ASSERT_TRUE(NULL != action.get()); | 241 ASSERT_TRUE(NULL != action.get()); |
| 242 | 242 |
| 243 // Now test that we can parse the new format for page actions. | 243 // Now test that we can parse the new format for page actions. |
| 244 const std::string kTitle("MyExtensionActionTitle"); | 244 const std::string kTitle("MyExtensionActionTitle"); |
| 245 const std::string kIcon("image1.png"); | 245 const std::string kIcon("image1.png"); |
| 246 const std::string kPopupHtmlFile("a_popup.html"); | 246 const std::string kPopupHtmlFile("a_popup.html"); |
| 247 | 247 |
| 248 action.reset(LoadAction("page_action_new_format.json")); | 248 action = LoadAction("page_action_new_format.json"); |
| 249 ASSERT_TRUE(action.get()); | 249 ASSERT_TRUE(action.get()); |
| 250 ASSERT_EQ(kTitle, action->GetTitle(1)); | 250 ASSERT_EQ(kTitle, action->GetTitle(1)); |
| 251 ASSERT_EQ(0u, action->icon_paths()->size()); | 251 ASSERT_EQ(0u, action->icon_paths()->size()); |
| 252 | 252 |
| 253 // Invalid title should give an error even with a valid name. | 253 // Invalid title should give an error even with a valid name. |
| 254 LoadActionAndExpectError("page_action_invalid_title.json", | 254 LoadActionAndExpectError("page_action_invalid_title.json", |
| 255 errors::kInvalidPageActionDefaultTitle); | 255 errors::kInvalidPageActionDefaultTitle); |
| 256 | 256 |
| 257 // Invalid name should give an error only with no title. | 257 // Invalid name should give an error only with no title. |
| 258 action.reset(LoadAction("page_action_invalid_name.json")); | 258 action = LoadAction("page_action_invalid_name.json"); |
| 259 ASSERT_TRUE(NULL != action.get()); | 259 ASSERT_TRUE(NULL != action.get()); |
| 260 ASSERT_EQ(kTitle, action->GetTitle(1)); | 260 ASSERT_EQ(kTitle, action->GetTitle(1)); |
| 261 | 261 |
| 262 LoadActionAndExpectError("page_action_invalid_name_no_title.json", | 262 LoadActionAndExpectError("page_action_invalid_name_no_title.json", |
| 263 errors::kInvalidPageActionName); | 263 errors::kInvalidPageActionName); |
| 264 | 264 |
| 265 // Test that keys "popup" and "default_popup" both work, but can not | 265 // Test that keys "popup" and "default_popup" both work, but can not |
| 266 // be used at the same time. | 266 // be used at the same time. |
| 267 // These tests require an extension_url, so we also load the manifest. | 267 // These tests require an extension_url, so we also load the manifest. |
| 268 | 268 |
| 269 // Only use "popup", expect success. | 269 // Only use "popup", expect success. |
| 270 scoped_refptr<Extension> extension = LoadManifest("page_action", | 270 scoped_refptr<Extension> extension = LoadManifest("page_action", |
| 271 "page_action_popup.json"); | 271 "page_action_popup.json"); |
| 272 action.reset(LoadAction("page_action_popup.json")); | 272 action = LoadAction("page_action_popup.json"); |
| 273 ASSERT_TRUE(NULL != action.get()); | 273 ASSERT_TRUE(NULL != action.get()); |
| 274 ASSERT_STREQ( | 274 ASSERT_STREQ( |
| 275 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), | 275 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), |
| 276 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); | 276 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); |
| 277 | 277 |
| 278 // Use both "popup" and "default_popup", expect failure. | 278 // Use both "popup" and "default_popup", expect failure. |
| 279 LoadActionAndExpectError("page_action_popup_and_default_popup.json", | 279 LoadActionAndExpectError("page_action_popup_and_default_popup.json", |
| 280 ExtensionErrorUtils::FormatErrorMessage( | 280 ExtensionErrorUtils::FormatErrorMessage( |
| 281 errors::kInvalidPageActionOldAndNewKeys, | 281 errors::kInvalidPageActionOldAndNewKeys, |
| 282 keys::kPageActionDefaultPopup, | 282 keys::kPageActionDefaultPopup, |
| 283 keys::kPageActionPopup)); | 283 keys::kPageActionPopup)); |
| 284 | 284 |
| 285 // Use only "default_popup", expect success. | 285 // Use only "default_popup", expect success. |
| 286 extension = LoadManifest("page_action", "page_action_popup.json"); | 286 extension = LoadManifest("page_action", "page_action_popup.json"); |
| 287 action.reset(LoadAction("page_action_default_popup.json")); | 287 action = LoadAction("page_action_default_popup.json"); |
| 288 ASSERT_TRUE(NULL != action.get()); | 288 ASSERT_TRUE(NULL != action.get()); |
| 289 ASSERT_STREQ( | 289 ASSERT_STREQ( |
| 290 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), | 290 extension->url().Resolve(kPopupHtmlFile).spec().c_str(), |
| 291 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); | 291 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); |
| 292 | 292 |
| 293 // Setting default_popup to "" is the same as having no popup. | 293 // Setting default_popup to "" is the same as having no popup. |
| 294 action.reset(LoadAction("page_action_empty_default_popup.json")); | 294 action = LoadAction("page_action_empty_default_popup.json"); |
| 295 ASSERT_TRUE(NULL != action.get()); | 295 ASSERT_TRUE(NULL != action.get()); |
| 296 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId)); | 296 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId)); |
| 297 ASSERT_STREQ( | 297 ASSERT_STREQ( |
| 298 "", | 298 "", |
| 299 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); | 299 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); |
| 300 | 300 |
| 301 // Setting popup to "" is the same as having no popup. | 301 // Setting popup to "" is the same as having no popup. |
| 302 action.reset(LoadAction("page_action_empty_popup.json")); | 302 action = LoadAction("page_action_empty_popup.json"); |
| 303 | 303 |
| 304 ASSERT_TRUE(NULL != action.get()); | 304 ASSERT_TRUE(NULL != action.get()); |
| 305 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId)); | 305 EXPECT_FALSE(action->HasPopup(ExtensionAction::kDefaultTabId)); |
| 306 ASSERT_STREQ( | 306 ASSERT_STREQ( |
| 307 "", | 307 "", |
| 308 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); | 308 action->GetPopupUrl(ExtensionAction::kDefaultTabId).spec().c_str()); |
| 309 } | 309 } |
| 310 | 310 |
| 311 TEST(ExtensionTest, IdIsValid) { | 311 TEST(ExtensionTest, IdIsValid) { |
| 312 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 312 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
| (...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1079 } | 1079 } |
| 1080 | 1080 |
| 1081 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { | 1081 TEST(ExtensionTest, GetSyncTypeExtensionWithTwoPlugins) { |
| 1082 scoped_refptr<Extension> extension( | 1082 scoped_refptr<Extension> extension( |
| 1083 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), | 1083 MakeSyncTestExtension(EXTENSION, GURL(), GURL(), |
| 1084 Extension::INTERNAL, 2, FilePath())); | 1084 Extension::INTERNAL, 2, FilePath())); |
| 1085 if (extension) | 1085 if (extension) |
| 1086 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); | 1086 EXPECT_EQ(extension->GetSyncType(), Extension::SYNC_TYPE_NONE); |
| 1087 } | 1087 } |
| 1088 #endif // !defined(OS_CHROMEOS) | 1088 #endif // !defined(OS_CHROMEOS) |
| OLD | NEW |