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.
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/extensions/component_loader.h"
12 #include "chrome/browser/extensions/extension_action.h"
13 #include "chrome/browser/extensions/extension_action_manager.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_tab_util.h"
16 #include "chrome/browser/extensions/script_bubble_controller.h"
17 #include "chrome/browser/extensions/test_extension_system.h"
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/extension_builder.h"
25 #include "extensions/common/feature_switch.h"
26 #include "extensions/common/value_builder.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/login/user_manager.h"
30 #include "chrome/browser/chromeos/settings/cros_settings.h"
31 #include "chrome/browser/chromeos/settings/device_settings_service.h"
34 namespace extensions
{
37 class ScriptBubbleControllerTest
: public ChromeRenderViewHostTestHarness
{
39 ScriptBubbleControllerTest()
40 : enable_script_bubble_(FeatureSwitch::script_bubble(), true) {
43 virtual void SetUp() OVERRIDE
{
44 ChromeRenderViewHostTestHarness::SetUp();
45 #if defined OS_CHROMEOS
46 test_user_manager_
.reset(new chromeos::ScopedTestUserManager());
48 CommandLine
command_line(CommandLine::NO_PROGRAM
);
50 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
51 extension_service_
= static_cast<TestExtensionSystem
*>(
52 ExtensionSystem::Get(profile
))->CreateExtensionService(
53 &command_line
, base::FilePath(), false);
54 extension_service_
->Init();
56 TabHelper::CreateForWebContents(web_contents());
58 script_bubble_controller_
=
59 TabHelper::FromWebContents(web_contents())->script_bubble_controller();
62 virtual void TearDown() OVERRIDE
{
63 #if defined OS_CHROMEOS
64 test_user_manager_
.reset();
66 ChromeRenderViewHostTestHarness::TearDown();
70 return ExtensionTabUtil::GetTabId(web_contents());
73 ExtensionService
* extension_service_
;
74 ScriptBubbleController
* script_bubble_controller_
;
77 FeatureSwitch::ScopedOverride enable_script_bubble_
;
79 #if defined OS_CHROMEOS
80 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
81 chromeos::ScopedTestCrosSettings test_cros_settings_
;
82 scoped_ptr
<chromeos::ScopedTestUserManager
> test_user_manager_
;
86 TEST_F(ScriptBubbleControllerTest
, Basics
) {
88 base::FilePath
root(FILE_PATH_LITERAL("c:\\"));
90 base::FilePath
root(FILE_PATH_LITERAL("/root"));
92 scoped_refptr
<const Extension
> extension1
=
94 .SetPath(root
.AppendASCII("f1"))
95 .SetManifest(DictionaryBuilder()
98 .Set("manifest_version", 2)
99 .Set("permissions", ListBuilder()
100 .Append("activeTab")))
103 scoped_refptr
<const Extension
> extension2
=
105 .SetPath(root
.AppendASCII("f2"))
106 .SetManifest(DictionaryBuilder()
109 .Set("manifest_version", 2)
110 .Set("permissions", ListBuilder()
111 .Append("activeTab")))
114 scoped_refptr
<const Extension
> extension3
=
116 .SetPath(root
.AppendASCII("f3"))
117 .SetManifest(DictionaryBuilder()
120 .Set("manifest_version", 2)
121 .Set("permissions", ListBuilder()
122 .Append("activeTab")))
125 extension_service_
->AddExtension(extension1
.get());
126 extension_service_
->AddExtension(extension2
.get());
127 extension_service_
->AddExtension(extension3
.get());
129 EXPECT_EQ(0u, script_bubble_controller_
->extensions_running_scripts().size());
131 NavigateAndCommit(GURL("http://www.google.com"));
133 // Running a script on the tab causes the bubble to be visible.
134 TabHelper::ScriptExecutionObserver::ExecutingScriptsMap executing_scripts
;
135 executing_scripts
[extension1
->id()].insert("script1");
136 script_bubble_controller_
->OnScriptsExecuted(
139 web_contents()->GetController().GetVisibleEntry()->GetPageID(),
140 web_contents()->GetController().GetVisibleEntry()->GetURL());
141 EXPECT_EQ(1u, script_bubble_controller_
->extensions_running_scripts().size());
142 std::set
<std::string
> extension_ids
;
143 extension_ids
.insert(extension1
->id());
144 EXPECT_EQ(1u, script_bubble_controller_
->extensions_running_scripts().size());
145 EXPECT_TRUE(extension_ids
==
146 script_bubble_controller_
->extensions_running_scripts());
148 // Running a script from another extension increments the count.
149 executing_scripts
.clear();
150 executing_scripts
[extension2
->id()].insert("script2");
151 script_bubble_controller_
->OnScriptsExecuted(
154 web_contents()->GetController().GetVisibleEntry()->GetPageID(),
155 web_contents()->GetController().GetVisibleEntry()->GetURL());
156 EXPECT_EQ(2u, script_bubble_controller_
->extensions_running_scripts().size());
157 extension_ids
.insert(extension2
->id());
158 EXPECT_TRUE(extension_ids
==
159 script_bubble_controller_
->extensions_running_scripts());
161 // Running another script from an already-seen extension does not affect
163 executing_scripts
.clear();
164 executing_scripts
[extension2
->id()].insert("script3");
165 script_bubble_controller_
->OnScriptsExecuted(
168 web_contents()->GetController().GetVisibleEntry()->GetPageID(),
169 web_contents()->GetController().GetVisibleEntry()->GetURL());
170 EXPECT_EQ(2u, script_bubble_controller_
->extensions_running_scripts().size());
172 // Running tabs.executeScript from an already-seen extension does not affect
174 executing_scripts
.clear();
175 executing_scripts
[extension1
->id()] = std::set
<std::string
>();
176 script_bubble_controller_
->OnScriptsExecuted(
177 web_contents(), executing_scripts
, 0, GURL());
178 EXPECT_EQ(2u, script_bubble_controller_
->extensions_running_scripts().size());
180 // Running tabs.executeScript from a new extension increments the count.
181 executing_scripts
.clear();
182 executing_scripts
[extension3
->id()] = std::set
<std::string
>();
183 script_bubble_controller_
->OnScriptsExecuted(
184 web_contents(), executing_scripts
, 0, GURL());
185 EXPECT_EQ(3u, script_bubble_controller_
->extensions_running_scripts().size());
187 // Navigating away resets the badge.
188 NavigateAndCommit(GURL("http://www.google.com"));
189 EXPECT_EQ(0u, script_bubble_controller_
->extensions_running_scripts().size());
193 } // namespace extensions