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/pref_names.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "extensions/common/extension_builder.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 const char* kExtensionID
= "abjoigjokfeibfhiahiijggogladbmfm";
27 class ActivityLogEnabledTest
: public ChromeRenderViewHostTestHarness
{
29 virtual void SetUp() OVERRIDE
{
30 ChromeRenderViewHostTestHarness::SetUp();
31 #if defined OS_CHROMEOS
32 test_user_manager_
.reset(new chromeos::ScopedTestUserManager());
36 virtual void TearDown() OVERRIDE
{
37 #if defined OS_CHROMEOS
38 test_user_manager_
.reset();
40 ChromeRenderViewHostTestHarness::TearDown();
43 #if defined OS_CHROMEOS
44 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
45 chromeos::ScopedTestCrosSettings test_cros_settings_
;
46 scoped_ptr
<chromeos::ScopedTestUserManager
> test_user_manager_
;
50 TEST_F(ActivityLogEnabledTest
, NoSwitch
) {
51 scoped_ptr
<TestingProfile
> profile(
52 static_cast<TestingProfile
*>(CreateBrowserContext()));
54 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
56 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
59 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
60 EXPECT_FALSE(activity_log
->IsDatabaseEnabled());
61 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
64 TEST_F(ActivityLogEnabledTest
, CommandLineSwitch
) {
65 scoped_ptr
<TestingProfile
> profile1(
66 static_cast<TestingProfile
*>(CreateBrowserContext()));
67 scoped_ptr
<TestingProfile
> profile2(
68 static_cast<TestingProfile
*>(CreateBrowserContext()));
70 CommandLine
command_line(CommandLine::NO_PROGRAM
);
71 CommandLine saved_cmdline_
= *CommandLine::ForCurrentProcess();
72 CommandLine::ForCurrentProcess()->AppendSwitch(
73 switches::kEnableExtensionActivityLogging
);
74 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
75 *CommandLine::ForCurrentProcess() = saved_cmdline_
;
76 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
79 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
81 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
82 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
83 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
84 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
85 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
88 TEST_F(ActivityLogEnabledTest
, PrefSwitch
) {
89 scoped_ptr
<TestingProfile
> profile1(
90 static_cast<TestingProfile
*>(CreateBrowserContext()));
91 scoped_ptr
<TestingProfile
> profile2(
92 static_cast<TestingProfile
*>(CreateBrowserContext()));
93 scoped_ptr
<TestingProfile
> profile3(
94 static_cast<TestingProfile
*>(CreateBrowserContext()));
97 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
99 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
101 profile3
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
103 profile1
->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive
, 1);
104 profile3
->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive
, 2);
105 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
106 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
107 ActivityLog
* activity_log3
= ActivityLog::GetInstance(profile3
.get());
110 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
112 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
114 profile3
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
115 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
116 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
117 EXPECT_TRUE(activity_log3
->IsWatchdogAppActive());
118 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
119 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
120 EXPECT_TRUE(activity_log3
->IsDatabaseEnabled());
123 TEST_F(ActivityLogEnabledTest
, WatchdogSwitch
) {
124 CommandLine
command_line(CommandLine::NO_PROGRAM
);
125 scoped_ptr
<TestingProfile
> profile1(
126 static_cast<TestingProfile
*>(CreateBrowserContext()));
127 scoped_ptr
<TestingProfile
> profile2(
128 static_cast<TestingProfile
*>(CreateBrowserContext()));
129 // Extension service is destroyed by the profile.
130 ExtensionService
* extension_service1
=
131 static_cast<TestExtensionSystem
*>(
132 ExtensionSystem::Get(profile1
.get()))->CreateExtensionService(
133 &command_line
, base::FilePath(), false);
134 static_cast<TestExtensionSystem
*>(
135 ExtensionSystem::Get(profile1
.get()))->SetReady();
137 ActivityLog
* activity_log1
= ActivityLog::GetInstance(profile1
.get());
138 ActivityLog
* activity_log2
= ActivityLog::GetInstance(profile2
.get());
140 // Allow Activity Log to install extension tracker.
141 base::RunLoop().RunUntilIdle();
144 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
146 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
148 scoped_refptr
<Extension
> extension
=
150 .SetManifest(DictionaryBuilder()
151 .Set("name", "Watchdog Extension ")
152 .Set("version", "1.0.0")
153 .Set("manifest_version", 2))
156 extension_service1
->AddExtension(extension
.get());
159 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
161 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
162 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
163 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
164 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
165 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
167 extension_service1
->DisableExtension(kExtensionID
,
168 Extension::DISABLE_USER_ACTION
);
171 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
173 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
174 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
175 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
176 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
177 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
179 extension_service1
->EnableExtension(kExtensionID
);
182 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
184 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
185 EXPECT_TRUE(activity_log1
->IsWatchdogAppActive());
186 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
187 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
188 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
190 extension_service1
->UninstallExtension(kExtensionID
, false, NULL
);
193 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
195 profile2
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
196 EXPECT_FALSE(activity_log1
->IsWatchdogAppActive());
197 EXPECT_FALSE(activity_log2
->IsWatchdogAppActive());
198 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
199 EXPECT_FALSE(activity_log2
->IsDatabaseEnabled());
201 scoped_refptr
<Extension
> extension2
=
203 .SetManifest(DictionaryBuilder()
204 .Set("name", "Watchdog Extension ")
205 .Set("version", "1.0.0")
206 .Set("manifest_version", 2))
207 .SetID("fpofdchlamddhnajleknffcbmnjfahpg")
209 extension_service1
->AddExtension(extension
.get());
210 extension_service1
->AddExtension(extension2
.get());
212 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
213 EXPECT_TRUE(activity_log1
->IsDatabaseEnabled());
214 extension_service1
->DisableExtension(kExtensionID
,
215 Extension::DISABLE_USER_ACTION
);
216 extension_service1
->DisableExtension("fpofdchlamddhnajleknffcbmnjfahpg",
217 Extension::DISABLE_USER_ACTION
);
219 profile1
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
220 EXPECT_FALSE(activity_log1
->IsDatabaseEnabled());
223 TEST_F(ActivityLogEnabledTest
, AppAndCommandLine
) {
224 // Set the command line switch.
225 CommandLine
command_line(CommandLine::NO_PROGRAM
);
226 CommandLine saved_cmdline_
= *CommandLine::ForCurrentProcess();
227 CommandLine::ForCurrentProcess()->AppendSwitch(
228 switches::kEnableExtensionActivityLogging
);
230 scoped_ptr
<TestingProfile
> profile(
231 static_cast<TestingProfile
*>(CreateBrowserContext()));
232 // Extension service is destroyed by the profile.
233 ExtensionService
* extension_service
=
234 static_cast<TestExtensionSystem
*>(
235 ExtensionSystem::Get(profile
.get()))->CreateExtensionService(
236 &command_line
, base::FilePath(), false);
237 static_cast<TestExtensionSystem
*>(
238 ExtensionSystem::Get(profile
.get()))->SetReady();
240 ActivityLog
* activity_log
= ActivityLog::GetInstance(profile
.get());
241 // Allow Activity Log to install extension tracker.
242 base::RunLoop().RunUntilIdle();
244 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
246 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
247 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
249 // Enable the extension.
250 scoped_refptr
<Extension
> extension
=
252 .SetManifest(DictionaryBuilder()
253 .Set("name", "Watchdog Extension ")
254 .Set("version", "1.0.0")
255 .Set("manifest_version", 2))
258 extension_service
->AddExtension(extension
.get());
260 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
262 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
263 EXPECT_TRUE(activity_log
->IsWatchdogAppActive());
265 extension_service
->UninstallExtension(kExtensionID
, false, NULL
);
267 EXPECT_TRUE(activity_log
->IsDatabaseEnabled());
269 profile
->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive
));
270 EXPECT_FALSE(activity_log
->IsWatchdogAppActive());
273 *CommandLine::ForCurrentProcess() = saved_cmdline_
;
276 } // namespace extensions