Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "chrome/common/chrome_paths.h" | 8 #include "chrome/common/chrome_paths.h" |
| 9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
| 10 #include "chrome/common/extensions/extension_error_reporter.h" | 10 #include "chrome/common/extensions/extension_error_reporter.h" |
| 11 #include "chrome/common/json_value_serializer.h" | 11 #include "chrome/common/json_value_serializer.h" |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("bar")) | 236 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("bar")) |
| 237 .Append(FILE_PATH_LITERAL("baz.js")).value(), | 237 .Append(FILE_PATH_LITERAL("baz.js")).value(), |
| 238 Extension::GetResourcePath(extension.path(), "bar/baz.js").value()); | 238 Extension::GetResourcePath(extension.path(), "bar/baz.js").value()); |
| 239 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("baz.js")).value(), | 239 EXPECT_EQ(path.Append(FILE_PATH_LITERAL("baz.js")).value(), |
| 240 Extension::GetResourcePath(extension.path(), "bar/../baz.js") | 240 Extension::GetResourcePath(extension.path(), "bar/../baz.js") |
| 241 .value()); | 241 .value()); |
| 242 EXPECT_EQ(FilePath().value(), | 242 EXPECT_EQ(FilePath().value(), |
| 243 Extension::GetResourcePath(extension.path(), "../baz.js").value()); | 243 Extension::GetResourcePath(extension.path(), "../baz.js").value()); |
| 244 } | 244 } |
| 245 | 245 |
| 246 TEST(ExtensionTest, LoadPageActionHelper) { | |
| 247 Extension extension; | |
| 248 std::string error_msg; | |
| 249 scoped_ptr<PageAction> page_action; | |
| 250 DictionaryValue input; | |
| 251 | |
| 252 // First try with an empty dictionary. We should get nothing back. | |
| 253 ASSERT_EQ(NULL, extension.LoadPageActionHelper(&input, 0, &error_msg)); | |
| 254 ASSERT_STRNE("", error_msg.c_str()); | |
| 255 error_msg = ""; | |
| 256 | |
| 257 // Now setup some values to use in the page action. | |
| 258 const std::string id("MyPageActionId"); | |
| 259 const std::string name("MyPageActionName"); | |
| 260 FilePath::StringType img1 = FILE_PATH_LITERAL("image1.png"); | |
| 261 FilePath::StringType img2 = FILE_PATH_LITERAL("image2.png"); | |
| 262 | |
| 263 // Add the page_actions dictionary. | |
| 264 input.SetString(Extension::kPageActionIdKey, id); | |
| 265 input.SetString(Extension::kNameKey, name); | |
| 266 ListValue* icons = new ListValue; | |
| 267 icons->Set(0, Value::CreateStringValue(img1)); | |
| 268 icons->Set(1, Value::CreateStringValue(img2)); | |
| 269 input.Set(Extension::kIconPathsKey, icons); | |
| 270 | |
| 271 // Parse the page action and read back the values from the object. | |
| 272 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | |
| 273 ASSERT_TRUE(NULL != page_action.get()); | |
| 274 ASSERT_STREQ("", error_msg.c_str()); | |
| 275 ASSERT_STREQ(id.c_str(), page_action->id().c_str()); | |
| 276 ASSERT_STREQ(name.c_str(), page_action->name().c_str()); | |
| 277 ASSERT_EQ(static_cast<size_t>(2), page_action->icon_paths().size()); | |
|
rafaelw
2009/07/02 20:28:09
2u works as well. It's what extensions_service_uni
| |
| 278 ASSERT_STREQ(img1.c_str(), page_action->icon_paths()[0].value().c_str()); | |
| 279 ASSERT_STREQ(img2.c_str(), page_action->icon_paths()[1].value().c_str()); | |
| 280 // Type hasn't been set, but it defaults to PERMANENT. | |
| 281 ASSERT_EQ(PageAction::PERMANENT, page_action->type()); | |
| 282 | |
| 283 // Explicitly set the same type and parse again. | |
| 284 input.SetString(Extension::kTypeKey, Extension::kPageActionTypePermanent); | |
| 285 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | |
| 286 ASSERT_TRUE(NULL != page_action.get()); | |
| 287 ASSERT_STREQ("", error_msg.c_str()); | |
| 288 ASSERT_EQ(PageAction::PERMANENT, page_action->type()); | |
| 289 | |
| 290 // Explicitly set the TAB type and parse again. | |
| 291 input.SetString(Extension::kTypeKey, Extension::kPageActionTypeTab); | |
| 292 page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); | |
| 293 ASSERT_TRUE(NULL != page_action.get()); | |
| 294 ASSERT_STREQ("", error_msg.c_str()); | |
| 295 ASSERT_EQ(PageAction::TAB, page_action->type()); | |
| 296 | |
| 297 // Make a deep copy of the input and remove one key at a time and see if we | |
| 298 // get the right error. | |
| 299 scoped_ptr<DictionaryValue> copy; | |
| 300 | |
| 301 // First remove id key. | |
| 302 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | |
| 303 copy->Remove(Extension::kPageActionIdKey, NULL); | |
| 304 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | |
| 305 ASSERT_TRUE(NULL == page_action.get()); | |
| 306 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | |
| 307 Extension::kInvalidPageActionIdError)); | |
| 308 | |
| 309 // Then remove the name key. | |
| 310 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | |
| 311 copy->Remove(Extension::kNameKey, NULL); | |
| 312 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | |
| 313 ASSERT_TRUE(NULL == page_action.get()); | |
| 314 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | |
| 315 Extension::kInvalidNameError)); | |
| 316 | |
| 317 // Then remove the icon paths key. | |
| 318 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | |
| 319 copy->Remove(Extension::kIconPathsKey, NULL); | |
| 320 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | |
| 321 ASSERT_TRUE(NULL == page_action.get()); | |
| 322 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | |
| 323 Extension::kInvalidPageActionIconPathsError)); | |
| 324 | |
| 325 // Then set the type to something bogus. | |
| 326 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); | |
| 327 copy->SetString(Extension::kTypeKey, "something_bogus"); | |
| 328 page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); | |
| 329 ASSERT_TRUE(NULL == page_action.get()); | |
| 330 ASSERT_TRUE(MatchPattern(error_msg.c_str(), | |
| 331 Extension::kInvalidPageActionTypeValueError)); | |
| 332 } | |
| 333 | |
| 246 TEST(ExtensionTest, IdIsValid) { | 334 TEST(ExtensionTest, IdIsValid) { |
| 247 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 335 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
| 248 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); | 336 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); |
| 249 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); | 337 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); |
| 250 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); | 338 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); |
| 251 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); | 339 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); |
| 252 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); | 340 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); |
| 253 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); | 341 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); |
| 254 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnoq")); | 342 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnoq")); |
| 255 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno0")); | 343 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno0")); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 Extension extension; | 402 Extension extension; |
| 315 std::string error; | 403 std::string error; |
| 316 input_value.SetString(Extension::kVersionKey, "1.0"); | 404 input_value.SetString(Extension::kVersionKey, "1.0"); |
| 317 input_value.SetString(Extension::kNameKey, "Test"); | 405 input_value.SetString(Extension::kNameKey, "Test"); |
| 318 input_value.SetString(Extension::kUpdateURLKey, invalid[i]); | 406 input_value.SetString(Extension::kUpdateURLKey, invalid[i]); |
| 319 | 407 |
| 320 EXPECT_FALSE(extension.InitFromValue(input_value, false, &error)); | 408 EXPECT_FALSE(extension.InitFromValue(input_value, false, &error)); |
| 321 EXPECT_TRUE(MatchPattern(error, Extension::kInvalidUpdateURLError)); | 409 EXPECT_TRUE(MatchPattern(error, Extension::kInvalidUpdateURLError)); |
| 322 } | 410 } |
| 323 } | 411 } |
| 324 | |
| OLD | NEW |