1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// 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 <string>
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/extensions/active_script_controller.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.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/location_bar_controller.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/value_builder.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif
namespace extensions {
namespace {
class LocationBarControllerUnitTest : public ChromeRenderViewHostTestHarness {
protected:
void SetUp() override {
active_script_override_.reset(new FeatureSwitch::ScopedOverride(
FeatureSwitch::scripts_require_action(), true));
ChromeRenderViewHostTestHarness::SetUp();
#if defined OS_CHROMEOS
test_user_manager_.reset(new chromeos::ScopedTestUserManager());
#endif
TabHelper::CreateForWebContents(web_contents());
// Create an ExtensionService so the LocationBarController can find its
// extensions.
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
extension_service_ = static_cast<TestExtensionSystem*>(
ExtensionSystem::Get(profile))->CreateExtensionService(
&command_line, base::FilePath(), false);
}
void TearDown() override {
#if defined OS_CHROMEOS
test_user_manager_.reset();
#endif
ChromeRenderViewHostTestHarness::TearDown();
}
int tab_id() {
return SessionTabHelper::IdForTab(web_contents());
}
const Extension* AddExtension(bool has_page_actions,
const std::string& name) {
DictionaryBuilder manifest;
manifest.Set("name", name)
.Set("version", "1.0.0")
.Set("manifest_version", 2)
.Set("permissions", ListBuilder().Append("tabs"));
if (has_page_actions) {
manifest.Set("page_action", DictionaryBuilder()
.Set("default_title", "Hello"));
}
scoped_refptr<const Extension> extension =
ExtensionBuilder().SetManifest(manifest.Pass())
.SetID(crx_file::id_util::GenerateId(name))
.Build();
extension_service_->AddExtension(extension.get());
return extension.get();
}
ExtensionService* extension_service_;
private:
#if defined OS_CHROMEOS
chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
chromeos::ScopedTestCrosSettings test_cros_settings_;
scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
#endif
// Since we also test that we show page actions for pending script requests,
// we need to enable that feature.
scoped_ptr<FeatureSwitch::ScopedOverride> active_script_override_;
};
// Test that the location bar gets the proper current actions.
TEST_F(LocationBarControllerUnitTest, LocationBarDisplaysPageActions) {
// Load up two extensions, one with a page action and one without.
const Extension* page_action = AddExtension(true, "page_actions");
const Extension* no_action = AddExtension(false, "no_actions");
TabHelper* tab_helper = TabHelper::FromWebContents(web_contents());
ASSERT_TRUE(tab_helper);
LocationBarController* controller = tab_helper->location_bar_controller();
ASSERT_TRUE(controller);
// There should only be one action - the action for the extension with a
// page action.
std::vector<ExtensionAction*> current_actions =
controller->GetCurrentActions();
ASSERT_EQ(1u, current_actions.size());
EXPECT_EQ(page_action->id(), current_actions[0]->extension_id());
// If we request a script injection, then the location bar controller should
// also show a page action for that extension.
ActiveScriptController* active_script_controller =
ActiveScriptController::GetForWebContents(web_contents());
ASSERT_TRUE(active_script_controller);
active_script_controller->RequestScriptInjectionForTesting(no_action,
base::Closure());
current_actions = controller->GetCurrentActions();
ASSERT_EQ(2u, current_actions.size());
// Check that each extension is present in the vector.
EXPECT_TRUE(current_actions[0]->extension_id() == no_action->id() ||
current_actions[1]->extension_id() == no_action->id());
EXPECT_TRUE(current_actions[0]->extension_id() == page_action->id() ||
current_actions[1]->extension_id() == page_action->id());
// If we request a script injection for an extension that already has a
// page action, only one action should be visible.
active_script_controller->RequestScriptInjectionForTesting(page_action,
base::Closure());
current_actions = controller->GetCurrentActions();
ASSERT_EQ(2u, current_actions.size());
EXPECT_TRUE(current_actions[0]->extension_id() == no_action->id() ||
current_actions[1]->extension_id() == no_action->id());
EXPECT_TRUE(current_actions[0]->extension_id() == page_action->id() ||
current_actions[1]->extension_id() == page_action->id());
// Navigating away means that only page actions are shown again.
NavigateAndCommit(GURL("http://google.com"));
current_actions = controller->GetCurrentActions();
ASSERT_EQ(1u, current_actions.size());
EXPECT_EQ(page_action->id(), current_actions[0]->extension_id());
}
// Test that navigating clears all state in a page action.
TEST_F(LocationBarControllerUnitTest, NavigationClearsState) {
const Extension* extension = AddExtension(true, "page_actions");
NavigateAndCommit(GURL("http://www.google.com"));
ExtensionAction& page_action =
*ExtensionActionManager::Get(profile())->GetPageAction(*extension);
page_action.SetTitle(tab_id(), "Goodbye");
page_action.SetPopupUrl(tab_id(), extension->GetResourceURL("popup.html"));
ExtensionActionAPI* extension_action_api =
ExtensionActionAPI::Get(profile());
// By default, extensions shouldn't want to act on a page.
EXPECT_FALSE(extension_action_api->ExtensionWantsToRun(extension,
web_contents()));
// Showing the page action should indicate that an extension *does* want to
// run on the page.
page_action.SetIsVisible(tab_id(), true);
EXPECT_TRUE(extension_action_api->ExtensionWantsToRun(extension,
web_contents()));
EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
EXPECT_EQ(extension->GetResourceURL("popup.html"),
page_action.GetPopupUrl(tab_id()));
// Within-page navigation should keep the settings.
NavigateAndCommit(GURL("http://www.google.com/#hash"));
EXPECT_EQ("Goodbye", page_action.GetTitle(tab_id()));
EXPECT_EQ(extension->GetResourceURL("popup.html"),
page_action.GetPopupUrl(tab_id()));
EXPECT_TRUE(extension_action_api->ExtensionWantsToRun(extension,
web_contents()));
// Should discard the settings, and go back to the defaults.
NavigateAndCommit(GURL("http://www.yahoo.com"));
EXPECT_EQ("Hello", page_action.GetTitle(tab_id()));
EXPECT_EQ(GURL(), page_action.GetPopupUrl(tab_id()));
EXPECT_FALSE(extension_action_api->ExtensionWantsToRun(extension,
web_contents()));
}
} // namespace
} // namespace extensions
|