From ae7fe7177e80bd409a1757b7421feb59973afa08 Mon Sep 17 00:00:00 2001 From: "finnur@chromium.org" Date: Thu, 2 Jul 2009 20:33:58 +0000 Subject: Add a couple of more extension unit tests for page actions. One for the helper function that parses a page action from the manifest and another that tests the page action api. BUG=None TEST=None Review URL: http://codereview.chromium.org/151181 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19840 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/common/extensions/extension.h | 2 + chrome/common/extensions/extension_unittest.cc | 91 +++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) (limited to 'chrome/common') diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 1705e12..0b04281 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -340,6 +340,8 @@ class Extension { // URL for fetching an update manifest GURL update_url_; + FRIEND_TEST(ExtensionTest, LoadPageActionHelper); + DISALLOW_COPY_AND_ASSIGN(Extension); }; diff --git a/chrome/common/extensions/extension_unittest.cc b/chrome/common/extensions/extension_unittest.cc index 38b599e..47f8ef8 100644 --- a/chrome/common/extensions/extension_unittest.cc +++ b/chrome/common/extensions/extension_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -243,6 +243,94 @@ TEST(ExtensionTest, GetResourceURLAndPath) { Extension::GetResourcePath(extension.path(), "../baz.js").value()); } +TEST(ExtensionTest, LoadPageActionHelper) { + Extension extension; + std::string error_msg; + scoped_ptr page_action; + DictionaryValue input; + + // First try with an empty dictionary. We should get nothing back. + ASSERT_EQ(NULL, extension.LoadPageActionHelper(&input, 0, &error_msg)); + ASSERT_STRNE("", error_msg.c_str()); + error_msg = ""; + + // Now setup some values to use in the page action. + const std::string id("MyPageActionId"); + const std::string name("MyPageActionName"); + FilePath::StringType img1 = FILE_PATH_LITERAL("image1.png"); + FilePath::StringType img2 = FILE_PATH_LITERAL("image2.png"); + + // Add the page_actions dictionary. + input.SetString(Extension::kPageActionIdKey, id); + input.SetString(Extension::kNameKey, name); + ListValue* icons = new ListValue; + icons->Set(0, Value::CreateStringValue(img1)); + icons->Set(1, Value::CreateStringValue(img2)); + input.Set(Extension::kIconPathsKey, icons); + + // Parse the page action and read back the values from the object. + page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); + ASSERT_TRUE(NULL != page_action.get()); + ASSERT_STREQ("", error_msg.c_str()); + ASSERT_STREQ(id.c_str(), page_action->id().c_str()); + ASSERT_STREQ(name.c_str(), page_action->name().c_str()); + ASSERT_EQ(2u, page_action->icon_paths().size()); + ASSERT_STREQ(img1.c_str(), page_action->icon_paths()[0].value().c_str()); + ASSERT_STREQ(img2.c_str(), page_action->icon_paths()[1].value().c_str()); + // Type hasn't been set, but it defaults to PERMANENT. + ASSERT_EQ(PageAction::PERMANENT, page_action->type()); + + // Explicitly set the same type and parse again. + input.SetString(Extension::kTypeKey, Extension::kPageActionTypePermanent); + page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); + ASSERT_TRUE(NULL != page_action.get()); + ASSERT_STREQ("", error_msg.c_str()); + ASSERT_EQ(PageAction::PERMANENT, page_action->type()); + + // Explicitly set the TAB type and parse again. + input.SetString(Extension::kTypeKey, Extension::kPageActionTypeTab); + page_action.reset(extension.LoadPageActionHelper(&input, 0, &error_msg)); + ASSERT_TRUE(NULL != page_action.get()); + ASSERT_STREQ("", error_msg.c_str()); + ASSERT_EQ(PageAction::TAB, page_action->type()); + + // Make a deep copy of the input and remove one key at a time and see if we + // get the right error. + scoped_ptr copy; + + // First remove id key. + copy.reset(static_cast(input.DeepCopy())); + copy->Remove(Extension::kPageActionIdKey, NULL); + page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); + ASSERT_TRUE(NULL == page_action.get()); + ASSERT_TRUE(MatchPattern(error_msg.c_str(), + Extension::kInvalidPageActionIdError)); + + // Then remove the name key. + copy.reset(static_cast(input.DeepCopy())); + copy->Remove(Extension::kNameKey, NULL); + page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); + ASSERT_TRUE(NULL == page_action.get()); + ASSERT_TRUE(MatchPattern(error_msg.c_str(), + Extension::kInvalidNameError)); + + // Then remove the icon paths key. + copy.reset(static_cast(input.DeepCopy())); + copy->Remove(Extension::kIconPathsKey, NULL); + page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); + ASSERT_TRUE(NULL == page_action.get()); + ASSERT_TRUE(MatchPattern(error_msg.c_str(), + Extension::kInvalidPageActionIconPathsError)); + + // Then set the type to something bogus. + copy.reset(static_cast(input.DeepCopy())); + copy->SetString(Extension::kTypeKey, "something_bogus"); + page_action.reset(extension.LoadPageActionHelper(copy.get(), 0, &error_msg)); + ASSERT_TRUE(NULL == page_action.get()); + ASSERT_TRUE(MatchPattern(error_msg.c_str(), + Extension::kInvalidPageActionTypeValueError)); +} + TEST(ExtensionTest, IdIsValid) { EXPECT_TRUE(Extension::IdIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); EXPECT_TRUE(Extension::IdIsValid("pppppppppppppppppppppppppppppppp")); @@ -321,4 +409,3 @@ TEST(ExtensionTest, UpdateUrls) { EXPECT_TRUE(MatchPattern(error, Extension::kInvalidUpdateURLError)); } } - -- cgit v1.1