OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative_content/content_action.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/test/values_test_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_action.h" |
| 12 #include "chrome/browser/extensions/extension_action_manager.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/browser/sessions/session_tab_helper.h" |
| 17 #include "chrome/common/extensions/extension_builder.h" |
| 18 #include "chrome/common/extensions/value_builder.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/test/test_browser_thread.h" |
| 21 #include "content/public/test/web_contents_tester.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 #if defined(OS_WIN) |
| 26 #include "ui/base/win/scoped_ole_initializer.h" |
| 27 #endif |
| 28 |
| 29 namespace extensions { |
| 30 namespace { |
| 31 |
| 32 using base::test::ParseJson; |
| 33 using testing::HasSubstr; |
| 34 |
| 35 class TestExtensionEnvironment { |
| 36 public: |
| 37 TestExtensionEnvironment() |
| 38 : ui_thread_(BrowserThread::UI, &loop_), |
| 39 extension_service_(NULL) {} |
| 40 |
| 41 TestingProfile* profile() { return &profile_; } |
| 42 |
| 43 ExtensionService* GetExtensionService() { |
| 44 if (extension_service_ == NULL) { |
| 45 TestExtensionSystem* extension_system = |
| 46 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)); |
| 47 extension_service_ = extension_system->CreateExtensionService( |
| 48 CommandLine::ForCurrentProcess(), FilePath(), false); |
| 49 } |
| 50 return extension_service_; |
| 51 } |
| 52 |
| 53 scoped_refptr<Extension> MakeExtension( |
| 54 const DictionaryValue& manifest_extra) { |
| 55 scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() |
| 56 .Set("name", "Extension") |
| 57 .Set("version", "1.0") |
| 58 .Set("manifest_version", 2) |
| 59 .Build(); |
| 60 manifest->MergeDictionary(&manifest_extra); |
| 61 |
| 62 scoped_refptr<Extension> result = |
| 63 ExtensionBuilder().SetManifest(manifest.Pass()).Build(); |
| 64 GetExtensionService()->AddExtension(result); |
| 65 return result; |
| 66 } |
| 67 |
| 68 scoped_ptr<content::WebContents> MakeTab() { |
| 69 scoped_ptr<content::WebContents> contents( |
| 70 content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); |
| 71 // Create a tab id. |
| 72 SessionTabHelper::CreateForWebContents(contents.get()); |
| 73 return contents.Pass(); |
| 74 } |
| 75 |
| 76 private: |
| 77 MessageLoopForUI loop_; |
| 78 content::TestBrowserThread ui_thread_; |
| 79 #if defined(OS_WIN) |
| 80 ui::ScopedOleInitializer ole_initializer_; |
| 81 #endif |
| 82 TestingProfile profile_; |
| 83 ExtensionService* extension_service_; |
| 84 }; |
| 85 |
| 86 TEST(DeclarativeContentActionTest, InvalidCreation) { |
| 87 TestExtensionEnvironment env; |
| 88 std::string error; |
| 89 bool bad_message = false; |
| 90 scoped_ptr<ContentAction> result; |
| 91 |
| 92 // Test wrong data type passed. |
| 93 error.clear(); |
| 94 result = ContentAction::Create(*ParseJson("[]"), &error, &bad_message); |
| 95 EXPECT_TRUE(bad_message); |
| 96 EXPECT_EQ("", error); |
| 97 EXPECT_FALSE(result); |
| 98 |
| 99 // Test missing instanceType element. |
| 100 error.clear(); |
| 101 result = ContentAction::Create(*ParseJson("{}"), &error, &bad_message); |
| 102 EXPECT_TRUE(bad_message); |
| 103 EXPECT_EQ("", error); |
| 104 EXPECT_FALSE(result); |
| 105 |
| 106 // Test wrong instanceType element. |
| 107 error.clear(); |
| 108 result = ContentAction::Create(*ParseJson( |
| 109 "{\n" |
| 110 " \"instanceType\": \"declarativeContent.UnknownType\",\n" |
| 111 "}"), |
| 112 &error, &bad_message); |
| 113 EXPECT_THAT(error, HasSubstr("invalid instanceType")); |
| 114 EXPECT_FALSE(result); |
| 115 } |
| 116 |
| 117 TEST(DeclarativeContentActionTest, ShowPageAction) { |
| 118 TestExtensionEnvironment env; |
| 119 |
| 120 std::string error; |
| 121 bool bad_message = false; |
| 122 scoped_ptr<ContentAction> result = ContentAction::Create( |
| 123 *ParseJson("{\n" |
| 124 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
| 125 "}"), |
| 126 &error, &bad_message); |
| 127 EXPECT_EQ("", error); |
| 128 EXPECT_FALSE(bad_message); |
| 129 ASSERT_TRUE(result); |
| 130 EXPECT_EQ(ContentAction::ACTION_SHOW_PAGE_ACTION, result->GetType()); |
| 131 |
| 132 scoped_refptr<Extension> extension = env.MakeExtension( |
| 133 *DictionaryBuilder().Set("page_action", DictionaryBuilder() |
| 134 .Set("default_title", "Extension")) |
| 135 .Build()); |
| 136 ExtensionAction* page_action = |
| 137 ExtensionActionManager::Get(env.profile())->GetPageAction(*extension); |
| 138 scoped_ptr<content::WebContents> contents = env.MakeTab(); |
| 139 const int tab_id = ExtensionTabUtil::GetTabId(contents.get()); |
| 140 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 141 ContentAction::ApplyInfo apply_info = { |
| 142 env.profile(), contents.get() |
| 143 }; |
| 144 result->Apply(extension->id(), base::Time(), &apply_info); |
| 145 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 146 result->Apply(extension->id(), base::Time(), &apply_info); |
| 147 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 148 result->Revert(extension->id(), base::Time(), &apply_info); |
| 149 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 150 result->Revert(extension->id(), base::Time(), &apply_info); |
| 151 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 152 } |
| 153 |
| 154 } // namespace |
| 155 } // namespace extensions |
OLD | NEW |