1 // Copyright 2013 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.
5 #include "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "chrome/browser/extensions/activity_log/activity_log.h"
8 #include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/test_extension_system.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/extensions/extension_builder.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_profile.h"
17 #if defined OS_CHROMEOS
18 #include "chrome/browser/chromeos/login/user_manager.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/chromeos/settings/device_settings_service.h"
23 namespace extensions
{
25 class ActivityLogEnabledTest
: public ChromeRenderViewHostTestHarness
{
27 virtual void SetUp() OVERRIDE
{
28 ChromeRenderViewHostTestHarness::SetUp();
29 #if defined OS_CHROMEOS
30 test_user_manager_
.reset(new chromeos::ScopedTestUserManager());
34 virtual void TearDown() OVERRIDE
{
35 #if defined OS_CHROMEOS
36 test_user_manager_
.reset();
38 ChromeRenderViewHostTestHarness::TearDown();
41 #if defined OS_CHROMEOS
42 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
43 chromeos::ScopedTestCrosSettings test_cros_settings_
;
44 scoped_ptr
<chromeos::ScopedTestUserManager
> test_user_manager_
;
48 TEST_F(ActivityLogEnabledTest
, NoSwitch
) {
49 scoped_ptr
<TestingProfile
> profile(
50 static_cast<TestingProfile
*>(CreateBrowserContext()));
52 profile
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
54 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
57 profile
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
58 EXPECT_FALSE(activity_log
->IsDatabaseEnabled());
59 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
62 TEST_F(ActivityLogEnabledTest
, CommandLineSwitch
) {
63 scoped_ptr
<TestingProfile
> profile1(
64 static_cast<TestingProfile
*>(CreateBrowserContext()));
65 scoped_ptr
<TestingProfile
> profile2(
66 static_cast<TestingProfile
*>(CreateBrowserContext()));
68 CommandLine
command_line(CommandLine::NO_PROGRAM
);
69 CommandLine saved_cmdline_
= *CommandLine::ForCurrentProcess();
70 CommandLine::ForCurrentProcess()->AppendSwitch(
71 switches::kEnableExtensionActivityLogging
);
72 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
73 *CommandLine::ForCurrentProcess() = saved_cmdline_
;
74 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
77 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
79 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
80 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
81 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
82 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
83 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
86 TEST_F(ActivityLogEnabledTest
, PrefSwitch
) {
87 scoped_ptr
<TestingProfile
> profile1(
88 static_cast<TestingProfile
*>(CreateBrowserContext()));
89 scoped_ptr
<TestingProfile
> profile2(
90 static_cast<TestingProfile
*>(CreateBrowserContext()));
93 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
95 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
97 profile1
->GetPrefs()->SetBoolean(prefs::kWatchdogExtensionActive
, true);
98 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
99 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
102 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
104 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
105 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
106 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
107 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
108 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
111 TEST_F(ActivityLogEnabledTest
, WatchdogSwitch
) {
112 CommandLine
command_line(CommandLine::NO_PROGRAM
);
113 scoped_ptr
<TestingProfile
> profile1(
114 static_cast<TestingProfile
*>(CreateBrowserContext()));
115 scoped_ptr
<TestingProfile
> profile2(
116 static_cast<TestingProfile
*>(CreateBrowserContext()));
117 // Extension service is destroyed by the profile.
118 ExtensionService
* extension_service1
=
119 static_cast<TestExtensionSystem
*>(
120 ExtensionSystem::Get(profile1
.get()))->CreateExtensionService(
121 &command_line
, base::FilePath(), false);
122 static_cast<TestExtensionSystem
*>(
123 ExtensionSystem::Get(profile1
.get()))->SetReady();
125 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
126 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
128 // Allow Activity Log to install extension tracker.
129 base::RunLoop().RunUntilIdle();
132 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
134 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
136 scoped_refptr
<Extension
> extension
=
138 .SetManifest(DictionaryBuilder()
139 .Set("name", "Watchdog Extension ")
140 .Set("version", "1.0.0")
141 .Set("manifest_version", 2))
142 .SetID(kActivityLogExtensionId
)
144 extension_service1
->AddExtension(extension
.get());
147 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
149 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
150 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
151 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
152 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
153 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
155 extension_service1
->DisableExtension(kActivityLogExtensionId
,
156 Extension::DISABLE_USER_ACTION
);
159 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
161 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
162 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
163 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
164 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
165 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
167 extension_service1
->EnableExtension(kActivityLogExtensionId
);
170 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
172 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
173 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
174 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
175 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
176 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
178 extension_service1
->UninstallExtension(kActivityLogExtensionId
, false, NULL
);
181 profile1
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
183 profile2
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
184 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
185 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
186 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
187 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
190 TEST_F(ActivityLogEnabledTest
, AppAndCommandLine
) {
191 // Set the command line switch.
192 CommandLine
command_line(CommandLine::NO_PROGRAM
);
193 CommandLine saved_cmdline_
= *CommandLine::ForCurrentProcess();
194 CommandLine::ForCurrentProcess()->AppendSwitch(
195 switches::kEnableExtensionActivityLogging
);
197 scoped_ptr
<TestingProfile
> profile(
198 static_cast<TestingProfile
*>(CreateBrowserContext()));
199 // Extension service is destroyed by the profile.
200 ExtensionService
* extension_service
=
201 static_cast<TestExtensionSystem
*>(
202 ExtensionSystem::Get(profile
.get()))->CreateExtensionService(
203 &command_line
, base::FilePath(), false);
204 static_cast<TestExtensionSystem
*>(
205 ExtensionSystem::Get(profile
.get()))->SetReady();
207 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
208 // Allow Activity Log to install extension tracker.
209 base::RunLoop().RunUntilIdle();
211 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
213 profile
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
214 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
216 // Enable the extension.
217 scoped_refptr
<Extension
> extension
=
219 .SetManifest(DictionaryBuilder()
220 .Set("name", "Watchdog Extension ")
221 .Set("version", "1.0.0")
222 .Set("manifest_version", 2))
223 .SetID(kActivityLogExtensionId
)
225 extension_service
->AddExtension(extension
.get());
227 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
229 profile
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
230 EXPECT_TRUE(activity_log
->IsWatchdogAppActive());
232 extension_service
->UninstallExtension(kActivityLogExtensionId
, false, NULL
);
234 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
236 profile
->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive
));
237 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
240 *CommandLine::ForCurrentProcess() = saved_cmdline_
;
243 } // namespace extensions