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 namespace extensions { |
| 26 namespace { |
| 27 |
| 28 using base::test::ParseJson; |
| 29 using testing::HasSubstr; |
| 30 |
| 31 class TestExtensionEnvironment { |
| 32 public: |
| 33 TestExtensionEnvironment() |
| 34 : ui_thread_(BrowserThread::UI, &loop_), |
| 35 extension_service_(NULL) {} |
| 36 |
| 37 TestingProfile* profile() { return &profile_; } |
| 38 |
| 39 ExtensionService* GetExtensionService() { |
| 40 if (extension_service_ == NULL) { |
| 41 TestExtensionSystem* extension_system = |
| 42 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)); |
| 43 extension_service_ = extension_system->CreateExtensionService( |
| 44 CommandLine::ForCurrentProcess(), FilePath(), false); |
| 45 } |
| 46 return extension_service_; |
| 47 } |
| 48 |
| 49 scoped_refptr<Extension> MakeExtension(DictionaryBuilder& manifest_extra) { |
| 50 scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder() |
| 51 .Set("name", "Extension") |
| 52 .Set("version", "1.0") |
| 53 .Set("manifest_version", 2) |
| 54 .Build(); |
| 55 manifest->MergeDictionary(manifest_extra.Build().get()); |
| 56 |
| 57 scoped_refptr<Extension> result = |
| 58 ExtensionBuilder().SetManifest(manifest.Pass()).Build(); |
| 59 GetExtensionService()->AddExtension(result); |
| 60 return result; |
| 61 } |
| 62 |
| 63 scoped_ptr<content::WebContents> MakeTab() { |
| 64 scoped_ptr<content::WebContents> contents( |
| 65 content::WebContentsTester::CreateTestWebContents(&profile_, NULL)); |
| 66 // Create a tab id. |
| 67 SessionTabHelper::CreateForWebContents(contents.get()); |
| 68 return contents.Pass(); |
| 69 } |
| 70 |
| 71 private: |
| 72 MessageLoop loop_; |
| 73 content::TestBrowserThread ui_thread_; |
| 74 TestingProfile profile_; |
| 75 ExtensionService* extension_service_; |
| 76 }; |
| 77 |
| 78 TEST(DeclarativeContentActionTest, InvalidCreation) { |
| 79 TestExtensionEnvironment env; |
| 80 std::string error; |
| 81 bool bad_message = false; |
| 82 scoped_ptr<ContentAction> result; |
| 83 |
| 84 // Test wrong data type passed. |
| 85 error.clear(); |
| 86 result = ContentAction::Create(*ParseJson("[]"), &error, &bad_message); |
| 87 EXPECT_TRUE(bad_message); |
| 88 EXPECT_EQ("", error); |
| 89 EXPECT_FALSE(result); |
| 90 |
| 91 // Test missing instanceType element. |
| 92 error.clear(); |
| 93 result = ContentAction::Create(*ParseJson("{}"), &error, &bad_message); |
| 94 EXPECT_TRUE(bad_message); |
| 95 EXPECT_EQ("", error); |
| 96 EXPECT_FALSE(result); |
| 97 |
| 98 // Test wrong instanceType element. |
| 99 error.clear(); |
| 100 result = ContentAction::Create(*ParseJson( |
| 101 "{\n" |
| 102 " \"instanceType\": \"declarativeContent.UnknownType\",\n" |
| 103 "}"), |
| 104 &error, &bad_message); |
| 105 EXPECT_THAT(error, HasSubstr("invalid instanceType")); |
| 106 EXPECT_FALSE(result); |
| 107 } |
| 108 |
| 109 TEST(DeclarativeContentActionTest, ShowPageAction) { |
| 110 TestExtensionEnvironment env; |
| 111 |
| 112 std::string error; |
| 113 bool bad_message = false; |
| 114 scoped_ptr<ContentAction> result = ContentAction::Create( |
| 115 *ParseJson("{\n" |
| 116 " \"instanceType\": \"declarativeContent.ShowPageAction\",\n" |
| 117 "}"), |
| 118 &error, &bad_message); |
| 119 EXPECT_EQ("", error); |
| 120 EXPECT_FALSE(bad_message); |
| 121 ASSERT_TRUE(result); |
| 122 EXPECT_EQ(ContentAction::ACTION_SHOW_PAGE_ACTION, result->GetType()); |
| 123 |
| 124 scoped_refptr<Extension> extension = env.MakeExtension( |
| 125 DictionaryBuilder().Set("page_action", DictionaryBuilder() |
| 126 .Set("default_title", "Extension"))); |
| 127 ExtensionAction* page_action = |
| 128 ExtensionActionManager::Get(env.profile())->GetPageAction(*extension); |
| 129 scoped_ptr<content::WebContents> contents = env.MakeTab(); |
| 130 const int tab_id = ExtensionTabUtil::GetTabId(contents.get()); |
| 131 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 132 ContentAction::ApplyInfo apply_info = { |
| 133 env.profile(), contents.get() |
| 134 }; |
| 135 result->Apply(extension->id(), base::Time(), &apply_info); |
| 136 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 137 result->Apply(extension->id(), base::Time(), &apply_info); |
| 138 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 139 result->Revert(extension->id(), base::Time(), &apply_info); |
| 140 EXPECT_TRUE(page_action->GetIsVisible(tab_id)); |
| 141 result->Revert(extension->id(), base::Time(), &apply_info); |
| 142 EXPECT_FALSE(page_action->GetIsVisible(tab_id)); |
| 143 } |
| 144 |
| 145 } // namespace |
| 146 } // namespace extensions |
OLD | NEW |