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

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

Issue 332021: Move page actions over to ExtensionAction2 (Closed)
Patch Set: Review feedback Created 11 years, 1 month 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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 } 290 }
291 291
292 TEST(ExtensionTest, LoadPageActionHelper) { 292 TEST(ExtensionTest, LoadPageActionHelper) {
293 #if defined(OS_WIN) 293 #if defined(OS_WIN)
294 FilePath path(StringPrintf(L"c:\\extension")); 294 FilePath path(StringPrintf(L"c:\\extension"));
295 #else 295 #else
296 FilePath path(StringPrintf("/extension")); 296 FilePath path(StringPrintf("/extension"));
297 #endif 297 #endif
298 Extension extension(path); 298 Extension extension(path);
299 std::string error_msg; 299 std::string error_msg;
300 scoped_ptr<ExtensionAction> action; 300 scoped_ptr<ExtensionAction2> action;
301 DictionaryValue input; 301 DictionaryValue input;
302 302
303 // First try with an empty dictionary. We should get nothing back. 303 // First try with an empty dictionary. We should get nothing back.
304 ASSERT_TRUE(extension.LoadExtensionActionHelper( 304 ASSERT_TRUE(extension.LoadExtensionAction2Helper(
305 &input, &error_msg, ExtensionAction::PAGE_ACTION) == NULL); 305 &input, &error_msg) == NULL);
306 ASSERT_STRNE("", error_msg.c_str()); 306 ASSERT_STRNE("", error_msg.c_str());
307 error_msg = ""; 307 error_msg = "";
308 308
309 // Now try the same, but as a browser action. Ensure same results. 309 // Now try the same, but as a browser action. Ensure same results.
310 ASSERT_TRUE(extension.LoadExtensionActionHelper( 310 ASSERT_TRUE(extension.LoadExtensionAction2Helper(
311 &input, &error_msg, ExtensionAction::BROWSER_ACTION) == NULL); 311 &input, &error_msg) == NULL);
312 ASSERT_STRNE("", error_msg.c_str()); 312 ASSERT_STRNE("", error_msg.c_str());
313 error_msg = ""; 313 error_msg = "";
314 314
315 // Now setup some values to use in the page action. 315 // Now setup some values to use in the page action.
316 const std::string id("MyExtensionActionId"); 316 const std::string id("MyExtensionActionId");
317 const std::string name("MyExtensionActionName"); 317 const std::string name("MyExtensionActionName");
318 std::string img1("image1.png"); 318 std::string img1("image1.png");
319 std::string img2("image2.png"); 319 std::string img2("image2.png");
320 320
321 // Add the dictionary for the contextual action. 321 // Add the dictionary for the contextual action.
322 input.SetString(keys::kPageActionId, id); 322 input.SetString(keys::kPageActionId, id);
323 input.SetString(keys::kName, name); 323 input.SetString(keys::kName, name);
324 ListValue* icons = new ListValue; 324 ListValue* icons = new ListValue;
325 icons->Set(0, Value::CreateStringValue(img1)); 325 icons->Set(0, Value::CreateStringValue(img1));
326 icons->Set(1, Value::CreateStringValue(img2)); 326 icons->Set(1, Value::CreateStringValue(img2));
327 input.Set(keys::kPageActionIcons, icons); 327 input.Set(keys::kPageActionIcons, icons);
328 328
329 // Parse as page action and read back the values from the object. 329 // Parse and read back the values from the object.
330 action.reset(extension.LoadExtensionActionHelper( 330 action.reset(extension.LoadExtensionAction2Helper(
331 &input, &error_msg, ExtensionAction::PAGE_ACTION)); 331 &input, &error_msg));
332 ASSERT_TRUE(NULL != action.get()); 332 ASSERT_TRUE(NULL != action.get());
333 ASSERT_STREQ("", error_msg.c_str()); 333 ASSERT_STREQ("", error_msg.c_str());
334 ASSERT_STREQ(id.c_str(), action->id().c_str()); 334 ASSERT_STREQ(id.c_str(), action->id().c_str());
335 ASSERT_STREQ(name.c_str(), action->title().c_str()); 335 ASSERT_STREQ(name.c_str(), action->GetTitle(1).c_str());
336 ASSERT_EQ(2u, action->icon_paths().size()); 336 ASSERT_EQ(2u, action->icon_paths()->size());
337 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str()); 337 ASSERT_STREQ(img1.c_str(), action->icon_paths()->at(0).c_str());
338 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str()); 338 ASSERT_STREQ(img2.c_str(), action->icon_paths()->at(1).c_str());
339 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
340
341 // Now try the same, but as a browser action.
342 action.reset(extension.LoadExtensionActionHelper(
343 &input, &error_msg, ExtensionAction::BROWSER_ACTION));
344 ASSERT_TRUE(NULL != action.get());
345 ASSERT_STREQ("", error_msg.c_str());
346 // Browser actions don't have an id, page actions do.
347 ASSERT_STREQ("", action->id().c_str());
348 ASSERT_STREQ(name.c_str(), action->title().c_str());
349 ASSERT_EQ(2u, action->icon_paths().size());
350 ASSERT_STREQ(img1.c_str(), action->icon_paths()[0].c_str());
351 ASSERT_STREQ(img2.c_str(), action->icon_paths()[1].c_str());
352 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
353 339
354 // Explicitly set the same type and parse again. 340 // Explicitly set the same type and parse again.
355 input.SetString(keys::kType, values::kPageActionTypeTab); 341 input.SetString(keys::kType, values::kPageActionTypeTab);
356 action.reset(extension.LoadExtensionActionHelper( 342 action.reset(extension.LoadExtensionAction2Helper(
357 &input, &error_msg, ExtensionAction::BROWSER_ACTION)); 343 &input, &error_msg));
358 ASSERT_TRUE(NULL != action.get()); 344 ASSERT_TRUE(NULL != action.get());
359 ASSERT_STREQ("", error_msg.c_str()); 345 ASSERT_STREQ("", error_msg.c_str());
360 ASSERT_EQ(ExtensionAction::BROWSER_ACTION, action->type());
361
362 // Explicitly set the PAGE_ACTION type and parse again.
363 input.SetString(keys::kType, values::kPageActionTypePermanent);
364 action.reset(extension.LoadExtensionActionHelper(
365 &input, &error_msg, ExtensionAction::PAGE_ACTION));
366 ASSERT_TRUE(NULL != action.get());
367 ASSERT_STREQ("", error_msg.c_str());
368 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
369 346
370 // Make a deep copy of the input and remove one key at a time and see if we 347 // Make a deep copy of the input and remove one key at a time and see if we
371 // get the right error. 348 // get the right error.
372 scoped_ptr<DictionaryValue> copy; 349 scoped_ptr<DictionaryValue> copy;
373 350
374 // First remove id key. 351 // First remove id key.
375 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 352 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
376 copy->Remove(keys::kPageActionId, NULL); 353 copy->Remove(keys::kPageActionId, NULL);
377 action.reset(extension.LoadExtensionActionHelper( 354 action.reset(extension.LoadExtensionAction2Helper(
378 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION)); 355 copy.get(), &error_msg));
379 ASSERT_TRUE(NULL != action.get()); 356 ASSERT_TRUE(NULL != action.get());
380 ASSERT_STREQ("", error_msg.c_str()); 357 ASSERT_STREQ("", error_msg.c_str());
381 error_msg = ""; 358 error_msg = "";
382
383 // Same test (id key), but with browser action.
384 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
385 copy->Remove(keys::kPageActionId, NULL);
386 action.reset(extension.LoadExtensionActionHelper(
387 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
388 // Having no id is valid for browser actions.
389 ASSERT_TRUE(NULL != action.get());
390 ASSERT_STREQ("", error_msg.c_str());
391 error_msg = "";
392 359
393 // Then remove the name key. 360 // Then remove the name key.
394 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 361 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
395 copy->Remove(keys::kName, NULL); 362 copy->Remove(keys::kName, NULL);
396 action.reset(extension.LoadExtensionActionHelper( 363 action.reset(extension.LoadExtensionAction2Helper(
397 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION)); 364 copy.get(), &error_msg));
398 ASSERT_TRUE(NULL == action.get()); 365 ASSERT_TRUE(NULL == action.get());
399 ASSERT_TRUE(MatchPattern(error_msg.c_str(), 366 ASSERT_TRUE(MatchPattern(error_msg.c_str(),
400 errors::kInvalidPageActionDefaultTitle)); 367 errors::kInvalidPageActionDefaultTitle));
401 error_msg = "";
402
403 // Same test (name key), but with browser action.
404 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
405 copy->Remove(keys::kName, NULL);
406 action.reset(extension.LoadExtensionActionHelper(
407 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
408 ASSERT_TRUE(NULL == action.get());
409 ASSERT_TRUE(MatchPattern(error_msg.c_str(),
410 errors::kInvalidPageActionDefaultTitle));
411 error_msg = ""; 368 error_msg = "";
412 369
413 // Then remove the icon paths key. 370 // Then remove the icon paths key.
414 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy())); 371 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
415 copy->Remove(keys::kPageActionIcons, NULL); 372 copy->Remove(keys::kPageActionIcons, NULL);
416 action.reset(extension.LoadExtensionActionHelper( 373 action.reset(extension.LoadExtensionAction2Helper(
417 copy.get(), &error_msg, ExtensionAction::PAGE_ACTION)); 374 copy.get(), &error_msg));
418 ASSERT_TRUE(NULL != action.get()); 375 ASSERT_TRUE(NULL != action.get());
419 error_msg = ""; 376 error_msg = "";
420 377
421 // Same test (name key), but with browser action.
422 copy.reset(static_cast<DictionaryValue*>(input.DeepCopy()));
423 copy->Remove(keys::kPageActionIcons, NULL);
424 action.reset(extension.LoadExtensionActionHelper(
425 copy.get(), &error_msg, ExtensionAction::BROWSER_ACTION));
426 ASSERT_TRUE(NULL != action.get());
427
428 // Now test that we can parse the new format for page actions. 378 // Now test that we can parse the new format for page actions.
429 379
430 // Now setup some values to use in the page action. 380 // Now setup some values to use in the page action.
431 const std::string kTitle("MyExtensionActionTitle"); 381 const std::string kTitle("MyExtensionActionTitle");
432 const std::string kIcon("image1.png"); 382 const std::string kIcon("image1.png");
433 383
434 // Add the dictionary for the contextual action. 384 // Add the dictionary for the contextual action.
435 input.Clear(); 385 input.Clear();
436 input.SetString(keys::kPageActionDefaultTitle, kTitle); 386 input.SetString(keys::kPageActionDefaultTitle, kTitle);
437 input.SetString(keys::kPageActionDefaultIcon, kIcon); 387 input.SetString(keys::kPageActionDefaultIcon, kIcon);
438 388
439 // Parse as page action and read back the values from the object. 389 // Parse and read back the values from the object.
440 action.reset(extension.LoadExtensionActionHelper( 390 action.reset(extension.LoadExtensionAction2Helper(
441 &input, &error_msg, ExtensionAction::PAGE_ACTION)); 391 &input, &error_msg));
442 ASSERT_TRUE(action.get()); 392 ASSERT_TRUE(action.get());
443 ASSERT_STREQ("", error_msg.c_str()); 393 ASSERT_STREQ("", error_msg.c_str());
444 ASSERT_EQ(kTitle, action->title()); 394 ASSERT_EQ(kTitle, action->GetTitle(1));
445 ASSERT_EQ(1u, action->icon_paths().size()); 395 ASSERT_EQ(0u, action->icon_paths()->size());
446 ASSERT_EQ(kIcon, action->icon_paths()[0]);
447 ASSERT_EQ(ExtensionAction::PAGE_ACTION, action->type());
448 } 396 }
449 397
450 TEST(ExtensionTest, IdIsValid) { 398 TEST(ExtensionTest, IdIsValid) {
451 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 399 EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
452 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); 400 EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp"));
453 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop")); 401 EXPECT_TRUE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnop"));
454 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP")); 402 EXPECT_TRUE(Extension::IdIsValid("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"));
455 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno")); 403 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmno"));
456 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa")); 404 EXPECT_FALSE(Extension::IdIsValid("abcdefghijklmnopabcdefghijklmnopa"));
457 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef")); 405 EXPECT_FALSE(Extension::IdIsValid("0123456789abcdef0123456789abcdef"));
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
693 scoped_ptr<Extension> new_extension( 641 scoped_ptr<Extension> new_extension(
694 LoadManifest("allow_silent_upgrade", 642 LoadManifest("allow_silent_upgrade",
695 std::string(kTests[i].base_name) + "_new.json")); 643 std::string(kTests[i].base_name) + "_new.json"));
696 644
697 EXPECT_EQ(kTests[i].expect_success, 645 EXPECT_EQ(kTests[i].expect_success,
698 Extension::IsPrivilegeIncrease(old_extension.get(), 646 Extension::IsPrivilegeIncrease(old_extension.get(),
699 new_extension.get())) 647 new_extension.get()))
700 << kTests[i].base_name; 648 << kTests[i].base_name;
701 } 649 }
702 } 650 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698