Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: chrome/common/extensions/extension_unittest.cc

Issue 275007: Update page action manifest parsing to match the new format. (Closed)
Patch Set: cleanup Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 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/file_util.h" 6 #include "base/file_util.h"
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "chrome/common/chrome_paths.h" 9 #include "chrome/common/chrome_paths.h"
10 #include "chrome/common/extensions/extension.h" 10 #include "chrome/common/extensions/extension.h"
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 #else 270 #else
271 FilePath path(StringPrintf("/extension")); 271 FilePath path(StringPrintf("/extension"));
272 #endif 272 #endif
273 Extension extension(path); 273 Extension extension(path);
274 std::string error_msg; 274 std::string error_msg;
275 scoped_ptr<ExtensionAction> action; 275 scoped_ptr<ExtensionAction> action;
276 DictionaryValue input; 276 DictionaryValue input;
277 277
278 // First try with an empty dictionary. We should get nothing back. 278 // First try with an empty dictionary. We should get nothing back.
279 ASSERT_TRUE(extension.LoadExtensionActionHelper( 279 ASSERT_TRUE(extension.LoadExtensionActionHelper(
280 &input, 0, &error_msg, ExtensionAction::PAGE_ACTION) == NULL); 280 &input, &error_msg, ExtensionAction::PAGE_ACTION) == NULL);
281 ASSERT_STRNE("", error_msg.c_str()); 281 ASSERT_STRNE("", error_msg.c_str());
282 error_msg = ""; 282 error_msg = "";
283 283
284 // Now try the same, but as a browser action. Ensure same results. 284 // Now try the same, but as a browser action. Ensure same results.
285 ASSERT_TRUE(extension.LoadExtensionActionHelper( 285 ASSERT_TRUE(extension.LoadExtensionActionHelper(
286 &input, 0, &error_msg, ExtensionAction::BROWSER_ACTION) == NULL); 286 &input, &error_msg, ExtensionAction::BROWSER_ACTION) == NULL);
287 ASSERT_STRNE("", error_msg.c_str()); 287 ASSERT_STRNE("", error_msg.c_str());
288 error_msg = ""; 288 error_msg = "";
289 289
290 // Now setup some values to use in the page action. 290 // Now setup some values to use in the page action.
291 const std::string id("MyExtensionActionId"); 291 const std::string id("MyExtensionActionId");
292 const std::string name("MyExtensionActionName"); 292 const std::string name("MyExtensionActionName");
293 std::string img1("image1.png"); 293 std::string img1("image1.png");
294 std::string img2("image2.png"); 294 std::string img2("image2.png");
295 295
296 // Add the dictionary for the contextual action. 296 // Add the dictionary for the contextual action.
297 input.SetString(keys::kPageActionId, id); 297 input.SetString(keys::kPageActionId, id);
298 input.SetString(keys::kName, name); 298 input.SetString(keys::kName, name);
299 ListValue* icons = new ListValue; 299 ListValue* icons = new ListValue;
300 icons->Set(0, Value::CreateStringValue(img1)); 300 icons->Set(0, Value::CreateStringValue(img1));
301 icons->Set(1, Value::CreateStringValue(img2)); 301 icons->Set(1, Value::CreateStringValue(img2));
302 input.Set(keys::kPageActionIcons, icons); 302 input.Set(keys::kPageActionIcons, icons);
303 303
304 // Parse as page action and read back the values from the object. 304 // Parse as page action and read back the values from the object.
305 action.reset(extension.LoadExtensionActionHelper( 305 action.reset(extension.LoadExtensionActionHelper(
306 &input, 0, &error_msg, ExtensionAction::PAGE_ACTION)); 306 &input, &error_msg, ExtensionAction::PAGE_ACTION));
307 ASSERT_TRUE(NULL != action.get()); 307 ASSERT_TRUE(NULL != action.get());
308 ASSERT_STREQ("", error_msg.c_str()); 308 ASSERT_STREQ("", error_msg.c_str());
309 ASSERT_STREQ(id.c_str(), action->id().c_str()); 309 ASSERT_STREQ(id.c_str(), action->id().c_str());
310 ASSERT_STREQ(name.c_str(), action->name().c_str()); 310 ASSERT_STREQ(name.c_str(), action->title().c_str());
311 ASSERT_EQ(2u, action->icon_paths().size()); 311 ASSERT_EQ(2u, action->icon_paths().size());
312 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); 312 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str());
313 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); 313 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str());
314 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type()); 314 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
315 315
316 // Now try the same, but as a browser action. 316 // Now try the same, but as a browser action.
317 action.reset(extension.LoadExtensionActionHelper( 317 action.reset(extension.LoadExtensionActionHelper(
318 &input, 0, &error_msg, ExtensionAction::BROWSER_ACTION)); 318 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
319 ASSERT_TRUE(NULL != action.get()); 319 ASSERT_TRUE(NULL != action.get());
320 ASSERT_STREQ("", error_msg.c_str()); 320 ASSERT_STREQ("", error_msg.c_str());
321 // Browser actions don't have an id, page actions do. 321 // Browser actions don't have an id, page actions do.
322 ASSERT_STREQ("", action->id().c_str()); 322 ASSERT_STREQ("", action->id().c_str());
323 ASSERT_STREQ(name.c_str(), action->name().c_str()); 323 ASSERT_STREQ(name.c_str(), action->title().c_str());
324 ASSERT_EQ(2u, action->icon_paths().size()); 324 ASSERT_EQ(2u, action->icon_paths().size());
325 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); 325 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str());
326 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); 326 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str());
327 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type()); 327 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
328 328
329 // Explicitly set the same type and parse again. 329 // Explicitly set the same type and parse again.
330 input.SetString(keys::kType, values::kPageActionTypeTab); 330 input.SetString(keys::kType, values::kPageActionTypeTab);
331 action.reset(extension.LoadExtensionActionHelper( 331 action.reset(extension.LoadExtensionActionHelper(
332 &input, 0, &error_msg, ExtensionAction::BROWSER_ACTION)); 332 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
333 ASSERT_TRUE(NULL != action.get()); 333 ASSERT_TRUE(NULL != action.get());
334 ASSERT_STREQ("", error_msg.c_str()); 334 ASSERT_STREQ("", error_msg.c_str());
335 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type()); 335 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
336 336
337 // Explicitly set the PAGE_ACTION type and parse again. 337 // Explicitly set the PAGE_ACTION type and parse again.
338 input.SetString(keys::kType, values::kPageActionTypePermanent); 338 input.SetString(keys::kType, values::kPageActionTypePermanent);
339 action.reset(extension.LoadExtensionActionHelper( 339 action.reset(extension.LoadExtensionActionHelper(
340 &input, 0, &error_msg, ExtensionAction::PAGE_ACTION)); 340 &input, &error_msg, ExtensionAction::PAGE_ACTION));
341 ASSERT_TRUE(NULL != action.get()); 341 ASSERT_TRUE(NULL != action.get());
342 ASSERT_STREQ("", error_msg.c_str()); 342 ASSERT_STREQ("", error_msg.c_str());
343 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type()); 343 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
344 344
345 // Make a deep copy of the input and remove one key at a time and see if we 345 // Make a deep copy of the input and remove one key at a time and see if we
346 // get the right error. 346 // get the right error.
347 scoped_ptr<DictionaryValue> copy; 347 scoped_ptr<DictionaryValue> copy;
348 348
349 // First remove id key. 349 // First remove id key.
350 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 350 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
351 copy->Remove(keys::kPageActionId, NULL); 351 copy->Remove(keys::kPageActionId, NULL);
352 action.reset(extension.LoadExtensionActionHelper( 352 action.reset(extension.LoadExtensionActionHelper(
353 copy.get(), 0, &error_msg, ExtensionAction::PAGE_ACTION)); 353 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION));
354 ASSERT_TRUE(NULL == action.get()); 354 ASSERT_TRUE(NULL != action.get());
355 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 355 ASSERT_STREQ("", error_msg.c_str());
356 errors::kInvalidPageActionId));
357 error_msg = ""; 356 error_msg = "";
358 357
359 // Same test (id key), but with browser action. 358 // Same test (id key), but with browser action.
360 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 359 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
361 copy->Remove(keys::kPageActionId, NULL); 360 copy->Remove(keys::kPageActionId, NULL);
362 action.reset(extension.LoadExtensionActionHelper( 361 action.reset(extension.LoadExtensionActionHelper(
363 copy.get(), 0, &error_msg, ExtensionAction::BROWSER_ACTION)); 362 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
364 // Having no id is valid for browser actions. 363 // Having no id is valid for browser actions.
365 ASSERT_TRUE(NULL != action.get()); 364 ASSERT_TRUE(NULL != action.get());
366 ASSERT_STREQ("", error_msg.c_str()); 365 ASSERT_STREQ("", error_msg.c_str());
367 error_msg = ""; 366 error_msg = "";
368 367
369 // Then remove the name key. 368 // Then remove the name key.
370 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 369 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
371 copy->Remove(keys::kName, NULL); 370 copy->Remove(keys::kName, NULL);
372 action.reset(extension.LoadExtensionActionHelper( 371 action.reset(extension.LoadExtensionActionHelper(
373 copy.get(), 0, &error_msg, ExtensionAction::PAGE_ACTION)); 372 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION));
374 ASSERT_TRUE(NULL == action.get()); 373 ASSERT_TRUE(NULL == action.get());
375 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 374 ASSERT_TRUE(MatchPattern(error_msg.c_str(),
376 errors::kInvalidName)); 375 errors::kInvalidPageActionDefaultTitle));
377 error_msg = ""; 376 error_msg = "";
378 377
379 // Same test (name key), but with browser action. 378 // Same test (name key), but with browser action.
380 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 379 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
381 copy->Remove(keys::kName, NULL); 380 copy->Remove(keys::kName, NULL);
382 action.reset(extension.LoadExtensionActionHelper( 381 action.reset(extension.LoadExtensionActionHelper(
383 copy.get(), 0, &error_msg, ExtensionAction::BROWSER_ACTION)); 382 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
384 ASSERT_TRUE(NULL == action.get()); 383 ASSERT_TRUE(NULL == action.get());
385 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 384 ASSERT_TRUE(MatchPattern(error_msg.c_str(),
386 errors::kInvalidName)); 385 errors::kInvalidPageActionDefaultTitle));
387 error_msg = ""; 386 error_msg = "";
388 387
389 // Then remove the icon paths key. 388 // Then remove the icon paths key.
390 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 389 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
391 copy->Remove(keys::kPageActionIcons, NULL); 390 copy->Remove(keys::kPageActionIcons, NULL);
392 action.reset(extension.LoadExtensionActionHelper( 391 action.reset(extension.LoadExtensionActionHelper(
393 copy.get(), 0, &error_msg, ExtensionAction::PAGE_ACTION)); 392 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION));
394 ASSERT_TRUE(NULL == action.get()); 393 ASSERT_TRUE(NULL != action.get());
395 ASSERT_TRUE(MatchPattern(error_msg.c_str(),
396 errors::kInvalidPageActionIconPaths));
397 error_msg = ""; 394 error_msg = "";
398 395
399 // Same test (name key), but with browser action (icons are not required for 396 // Same test (name key), but with browser action.
400 // browser actions).
401 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 397 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
402 copy->Remove(keys::kPageActionIcons, NULL); 398 copy->Remove(keys::kPageActionIcons, NULL);
403 action.reset(extension.LoadExtensionActionHelper( 399 action.reset(extension.LoadExtensionActionHelper(
404 copy.get(), 0, &error_msg, ExtensionAction::BROWSER_ACTION)); 400 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
405 ASSERT_TRUE(NULL != action.get()); 401 ASSERT_TRUE(NULL != action.get());
402
403 // Now test that we can parse the new format for page actions.
404
405 // Now setup some values to use in the page action.
406 const std::string kTitle("MyExtensionActionTitle");
407 const std::string kIcon("image1.png");
408
409 // Add the dictionary for the contextual action.
410 input.Clear();
411 input.SetString(keys::kPageActionDefaultTitle, kTitle);
412 input.SetString(keys::kPageActionDefaultIcon, kIcon);
413
414 // Parse as page action and read back the values from the object.
415 action.reset(extension.LoadExtensionActionHelper(
416 &input, &error_msg, ExtensionAction::PAGE_ACTION));
417 ASSERT_TRUE(action.get());
418 ASSERT_STREQ("", error_msg.c_str());
419 ASSERT_EQ(kTitle, action->title());
420 ASSERT_EQ(1u, action->icon_paths().size());
421 ASSERT_EQ(kIcon, action->icon_paths()[0]);
422 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
406 } 423 }
407 424
408 TEST(ExtensionTest, IdIsValid) { 425 TEST(ExtensionTest, IdIsValid) {
409 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 426 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
410 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); 427 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp"));
411 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); 428 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop"));
412 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); 429 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"));
413 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); 430 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno"));
414 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); 431 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa"));
415 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); 432 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef"));
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 scoped_ptr<Extension> new_extension( 668 scoped_ptr<Extension> new_extension(
652 LoadManifest("allow_silent_upgrade", 669 LoadManifest("allow_silent_upgrade",
653 std::string(kTests[i].base_name) + "_new.json")); 670 std::string(kTests[i].base_name) + "_new.json"));
654 671
655 EXPECT_EQ(kTests[i].expect_success, 672 EXPECT_EQ(kTests[i].expect_success,
656 Extension::IsPrivilegeIncrease(old_extension.get(), 673 Extension::IsPrivilegeIncrease(old_extension.get(),
657 new_extension.get())) 674 new_extension.get()))
658 << kTests[i].base_name; 675 << kTests[i].base_name;
659 } 676 }
660 } 677 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_constants.cc ('k') | chrome/renderer/resources/extension_process_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698