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/prefs/pref_service.h"
7 #include "base/run_loop.h"
8 #include "chrome/browser/extensions/activity_log/activity_log.h"
9 #include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/test_extension_system.h"
12 #include "chrome/common/chrome_switches.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"
16 #include "extensions/browser/uninstall_reason.h"
17 #include "extensions/common/extension_builder.h"
19 #if defined OS_CHROMEOS
20 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
21 #include "chrome/browser/chromeos/settings/cros_settings.h"
22 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 namespace extensions
{
27 const char kExtensionID
[] = "abjoigjokfeibfhiahiijggogladbmfm";
29 class ActivityLogEnabledTest
: public ChromeRenderViewHostTestHarness
{
31 void SetUp() override
{
32 ChromeRenderViewHostTestHarness::SetUp();
33 #if defined OS_CHROMEOS
34 test_user_manager_
.reset(new chromeos::ScopedTestUserManager());
38 void TearDown() override
{
39 #if defined OS_CHROMEOS
40 test_user_manager_
.reset();
42 ChromeRenderViewHostTestHarness::TearDown();
45 #if defined OS_CHROMEOS
46 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
47 chromeos::ScopedTestCrosSettings test_cros_settings_
;
48 scoped_ptr
<chromeos::ScopedTestUserManager
> test_user_manager_
;
52 TEST_F(ActivityLogEnabledTest
, NoSwitch
) {
53 scoped_ptr
<TestingProfile
> profile(
54 static_cast<TestingProfile
*>(CreateBrowserContext()));
56 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
58 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
61 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
62 EXPECT_FALSE(activity_log
->IsDatabaseEnabled());
63 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
66 TEST_F(ActivityLogEnabledTest
, CommandLineSwitch
) {
67 scoped_ptr
<TestingProfile
> profile1(
68 static_cast<TestingProfile
*>(CreateBrowserContext()));
69 scoped_ptr
<TestingProfile
> profile2(
70 static_cast<TestingProfile
*>(CreateBrowserContext()));
72 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
73 base::CommandLine saved_cmdline_
= *base::CommandLine::ForCurrentProcess();
74 base::CommandLine::ForCurrentProcess()->AppendSwitch(
75 switches::kEnableExtensionActivityLogging
);
76 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
77 *base::CommandLine::ForCurrentProcess() = saved_cmdline_
;
78 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
81 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
83 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
84 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
85 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
86 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
87 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
90 TEST_F(ActivityLogEnabledTest
, PrefSwitch
) {
91 scoped_ptr
<TestingProfile
> profile1(
92 static_cast<TestingProfile
*>(CreateBrowserContext()));
93 scoped_ptr
<TestingProfile
> profile2(
94 static_cast<TestingProfile
*>(CreateBrowserContext()));
95 scoped_ptr
<TestingProfile
> profile3(
96 static_cast<TestingProfile
*>(CreateBrowserContext()));
99 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
101 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
103 profile3
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
105 profile1
->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive
, 1);
106 profile3
->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive
, 2);
107 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
108 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
109 ActivityLog
* activity_log3
= ActivityLog::GetInstance(profile3
.get());
112 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
114 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
116 profile3
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
117 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
118 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
119 EXPECT_TRUE(activity_log3
->IsWatchdogAppActive());
120 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
121 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
122 EXPECT_TRUE(activity_log3
->IsDatabaseEnabled());
125 TEST_F(ActivityLogEnabledTest
, WatchdogSwitch
) {
126 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
127 scoped_ptr
<TestingProfile
> profile1(
128 static_cast<TestingProfile
*>(CreateBrowserContext()));
129 scoped_ptr
<TestingProfile
> profile2(
130 static_cast<TestingProfile
*>(CreateBrowserContext()));
131 // Extension service is destroyed by the profile.
132 ExtensionService
* extension_service1
=
133 static_cast<TestExtensionSystem
*>(
134 ExtensionSystem::Get(profile1
.get()))->CreateExtensionService(
135 &command_line
, base::FilePath(), false);
136 static_cast<TestExtensionSystem
*>(
137 ExtensionSystem::Get(profile1
.get()))->SetReady();
139 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
140 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
142 // Allow Activity Log to install extension tracker.
143 base::RunLoop().RunUntilIdle();
146 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
148 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
150 scoped_refptr
<Extension
> extension
=
152 .SetManifest(DictionaryBuilder()
153 .Set("name", "Watchdog Extension ")
154 .Set("version", "1.0.0")
155 .Set("manifest_version", 2))
158 extension_service1
->AddExtension(extension
.get());
161 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
163 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
164 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
165 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
166 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
167 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
169 extension_service1
->DisableExtension(kExtensionID
,
170 Extension::DISABLE_USER_ACTION
);
173 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
175 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
176 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
177 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
178 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
179 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
181 extension_service1
->EnableExtension(kExtensionID
);
184 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
186 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
187 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
188 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
189 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
190 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
192 extension_service1
->UninstallExtension(
194 extensions::UNINSTALL_REASON_FOR_TESTING
,
195 base::Bind(&base::DoNothing
),
199 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
201 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
202 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
203 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
204 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
205 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
207 scoped_refptr
<Extension
> extension2
=
209 .SetManifest(DictionaryBuilder()
210 .Set("name", "Watchdog Extension ")
211 .Set("version", "1.0.0")
212 .Set("manifest_version", 2))
213 .SetID("fpofdchlamddhnajleknffcbmnjfahpg")
215 extension_service1
->AddExtension(extension
.get());
216 extension_service1
->AddExtension(extension2
.get());
218 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
219 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
220 extension_service1
->DisableExtension(kExtensionID
,
221 Extension::DISABLE_USER_ACTION
);
222 extension_service1
->DisableExtension("fpofdchlamddhnajleknffcbmnjfahpg",
223 Extension::DISABLE_USER_ACTION
);
225 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
226 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
229 TEST_F(ActivityLogEnabledTest
, AppAndCommandLine
) {
230 // Set the command line switch.
231 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
232 base::CommandLine saved_cmdline_
= *base::CommandLine::ForCurrentProcess();
233 base::CommandLine::ForCurrentProcess()->AppendSwitch(
234 switches::kEnableExtensionActivityLogging
);
236 scoped_ptr
<TestingProfile
> profile(
237 static_cast<TestingProfile
*>(CreateBrowserContext()));
238 // Extension service is destroyed by the profile.
239 ExtensionService
* extension_service
=
240 static_cast<TestExtensionSystem
*>(
241 ExtensionSystem::Get(profile
.get()))->CreateExtensionService(
242 &command_line
, base::FilePath(), false);
243 static_cast<TestExtensionSystem
*>(
244 ExtensionSystem::Get(profile
.get()))->SetReady();
246 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
247 // Allow Activity Log to install extension tracker.
248 base::RunLoop().RunUntilIdle();
250 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
252 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
253 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
255 // Enable the extension.
256 scoped_refptr
<Extension
> extension
=
258 .SetManifest(DictionaryBuilder()
259 .Set("name", "Watchdog Extension ")
260 .Set("version", "1.0.0")
261 .Set("manifest_version", 2))
264 extension_service
->AddExtension(extension
.get());
266 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
268 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
269 EXPECT_TRUE(activity_log
->IsWatchdogAppActive());
271 extension_service
->UninstallExtension(
273 extensions::UNINSTALL_REASON_FOR_TESTING
,
274 base::Bind(&base::DoNothing
),
277 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
279 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
280 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
283 *base::CommandLine::ForCurrentProcess() = saved_cmdline_
;
286 } // namespace extensions