Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative_content/content_action_unittest.cc |
| diff --git a/chrome/browser/extensions/api/declarative_content/content_action_unittest.cc b/chrome/browser/extensions/api/declarative_content/content_action_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..34da3ca102f91e3f5cc534c10e3df17736200905 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative_content/content_action_unittest.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright (c) 2012 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. |
| + |
| +#include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/message_loop.h" |
| +#include "base/test/values_test_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/extension_action.h" |
| +#include "chrome/browser/extensions/extension_action_manager.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_tab_util.h" |
| +#include "chrome/browser/extensions/test_extension_system.h" |
| +#include "chrome/browser/sessions/session_tab_helper.h" |
| +#include "chrome/common/extensions/extension_builder.h" |
| +#include "chrome/common/extensions/value_builder.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "content/public/test/web_contents_tester.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace extensions { |
| +namespace { |
| + |
| +using base::test::ParseJson; |
| +using testing::HasSubstr; |
| + |
| +class TestExtensionEnvironment { |
| + public: |
| + TestExtensionEnvironment() |
| + : ui_thread_(BrowserThread::UI, &loop_), |
| + extension_service_(NULL) {} |
| + |
| + TestingProfile* profile() { return &profile_; } |
| + |
| + ExtensionService* GetExtensionService() { |
| + if (extension_service_ == NULL) { |
| + TestExtensionSystem* extension_system = |
| + static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)); |
| + extension_service_ = extension_system->CreateExtensionService( |
| + CommandLine::ForCurrentProcess(), FilePath(), false); |
| + } |
| + return extension_service_; |
| + } |
| + |
| + scoped_refptr<Extension> MakeExtension(DictionaryBuilder& manifest_extra) { |
|
vabr (Chromium)
2013/01/23 12:32:56
Lint flagged this non-const reference. Can you use
Jeffrey Yasskin
2013/01/23 22:13:17
I could, or I could move the .Build() call outside
vabr (Chromium)
2013/01/24 08:21:01
Indeed, people will learn quite fast that manifest
Jeffrey Yasskin
2013/01/25 00:08:29
I changed it to take const DictionaryValue&.
|
| + scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() |
| + .Set("name", "Extension") |
| + .Set("version", "1.0") |
| + .Set("manifest_version", 2) |
| + .Build(); |
| + manifest->MergeDictionary(manifest_extra.Build().get()); |
| + |
| + scoped_refptr<Extension> result = |
| + ExtensionBuilder().SetManifest(manifest.Pass()).Build(); |
| + GetExtensionService()->AddExtension(result); |
| + return result; |
| + } |
| + |
| + scoped_ptr<content::WebContents> MakeTab() { |
| + scoped_ptr<content::WebContents> contents( |
| + content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); |
| + // Create a tab id. |
| + SessionTabHelper::CreateForWebContents(contents.get()); |
| + return contents.Pass(); |
| + } |
| + |
| + private: |
| + MessageLoop loop_; |
| + content::TestBrowserThread ui_thread_; |
| + TestingProfile profile_; |
| + ExtensionService* extension_service_; |
| +}; |
| + |
| +TEST(DeclarativeContentActionTest, InvalidCreation) { |
| + TestExtensionEnvironment env; |
| + std::string error; |
| + bool bad_message = false; |
| + scoped_ptr<ContentAction> result; |
| + |
| + // Test wrong data type passed. |
| + error.clear(); |
| + result = ContentAction::Create(*ParseJson("[]"), &error, &bad_message); |
| + EXPECT_TRUE(bad_message); |
| + EXPECT_EQ("", error); |
| + EXPECT_FALSE(result); |
| + |
| + // Test missing instanceType element. |
| + error.clear(); |
| + result = ContentAction::Create(*ParseJson("{}"), &error, &bad_message); |
| + EXPECT_TRUE(bad_message); |
| + EXPECT_EQ("", error); |
| + EXPECT_FALSE(result); |
| + |
| + // Test wrong instanceType element. |
| + error.clear(); |
| + result = ContentAction::Create(*ParseJson( |
| + "{\n" |
| + " \"instanceType\": \"declarativeContent.UnknownType\",\n" |
| + "}"), |
| + &error, &bad_message); |
| + EXPECT_THAT(error, HasSubstr("invalid instanceType")); |
| + EXPECT_FALSE(result); |
| +} |
| + |
| +TEST(DeclarativeContentActionTest, ShowPageAction) { |
| + TestExtensionEnvironment env; |
| + |
| + std::string error; |
| + bool bad_message = false; |
| + scoped_ptr<ContentAction> result = ContentAction::Create( |
| + *ParseJson("{\n" |
| + " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
| + "}"), |
| + &error, &bad_message); |
| + EXPECT_EQ("", error); |
| + EXPECT_FALSE(bad_message); |
| + ASSERT_TRUE(result); |
| + EXPECT_EQ(ContentAction::ACTION_SHOW_PAGE_ACTION, result->GetType()); |
| + |
| + scoped_refptr<Extension> extension = env.MakeExtension( |
| + DictionaryBuilder().Set("page_action", DictionaryBuilder() |
| + .Set("default_title", "Extension"))); |
| + ExtensionAction* page_action = |
| + ExtensionActionManager::Get(env.profile())->GetPageAction(*extension); |
| + scoped_ptr<content::WebContents> contents = env.MakeTab(); |
| + const int tab_id = ExtensionTabUtil::GetTabId(contents.get()); |
| + EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| + ContentAction::ApplyInfo apply_info = { |
| + env.profile(), contents.get() |
| + }; |
| + result->Apply(extension->id(), base::Time(), &apply_info); |
| + EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| + result->Apply(extension->id(), base::Time(), &apply_info); |
| + EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| + result->Revert(extension->id(), base::Time(), &apply_info); |
| + EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| + result->Revert(extension->id(), base::Time(), &apply_info); |
| + EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| +} |
| + |
| +} // namespace |
| +} // namespace extensions |