Extensions cleanup: Merge IsSyncableApp+Extension, ShouldSyncApp+Extension
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / activity_log_enabled_unittest.cc
blob8c77afdcc84ab2feecac65fa914bba1966bb7948
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"
23 #endif
25 namespace extensions {
27 const char kExtensionID[] = "abjoigjokfeibfhiahiijggogladbmfm";
29 class ActivityLogEnabledTest : public ChromeRenderViewHostTestHarness {
30 protected:
31 void SetUp() override {
32 ChromeRenderViewHostTestHarness::SetUp();
33 #if defined OS_CHROMEOS
34 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
35 #endif
38 void TearDown() override {
39 #if defined OS_CHROMEOS
40 test_user_manager_.reset();
41 #endif
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_;
49 #endif
52 TEST_F(ActivityLogEnabledTest, NoSwitch) {
53 scoped_ptr<TestingProfile> profile(
54 static_cast<TestingProfile*>(CreateBrowserContext()));
55 EXPECT_FALSE(
56 profile->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
58 ActivityLog* activity_log = ActivityLog::GetInstance(profile.get());
60 EXPECT_EQ(0,
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());
80 EXPECT_EQ(0,
81 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
82 EXPECT_EQ(0,
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()));
98 EXPECT_EQ(0,
99 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
100 EXPECT_EQ(0,
101 profile2->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
102 EXPECT_EQ(0,
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());
111 EXPECT_EQ(1,
112 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
113 EXPECT_EQ(0,
114 profile2->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
115 EXPECT_EQ(2,
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();
145 EXPECT_EQ(0,
146 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
147 EXPECT_EQ(0,
148 profile2->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
150 scoped_refptr<Extension> extension =
151 ExtensionBuilder()
152 .SetManifest(DictionaryBuilder()
153 .Set("name", "Watchdog Extension ")
154 .Set("version", "1.0.0")
155 .Set("manifest_version", 2))
156 .SetID(kExtensionID)
157 .Build();
158 extension_service1->AddExtension(extension.get());
160 EXPECT_EQ(1,
161 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
162 EXPECT_EQ(0,
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);
172 EXPECT_EQ(0,
173 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
174 EXPECT_EQ(0,
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);
183 EXPECT_EQ(1,
184 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
185 EXPECT_EQ(0,
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(
193 kExtensionID,
194 extensions::UNINSTALL_REASON_FOR_TESTING,
195 base::Bind(&base::DoNothing),
196 NULL);
198 EXPECT_EQ(0,
199 profile1->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
200 EXPECT_EQ(0,
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 =
208 ExtensionBuilder()
209 .SetManifest(DictionaryBuilder()
210 .Set("name", "Watchdog Extension ")
211 .Set("version", "1.0.0")
212 .Set("manifest_version", 2))
213 .SetID("fpofdchlamddhnajleknffcbmnjfahpg")
214 .Build();
215 extension_service1->AddExtension(extension.get());
216 extension_service1->AddExtension(extension2.get());
217 EXPECT_EQ(2,
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);
224 EXPECT_EQ(0,
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());
251 EXPECT_EQ(0,
252 profile->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
253 EXPECT_FALSE(activity_log->IsWatchdogAppActive());
255 // Enable the extension.
256 scoped_refptr<Extension> extension =
257 ExtensionBuilder()
258 .SetManifest(DictionaryBuilder()
259 .Set("name", "Watchdog Extension ")
260 .Set("version", "1.0.0")
261 .Set("manifest_version", 2))
262 .SetID(kExtensionID)
263 .Build();
264 extension_service->AddExtension(extension.get());
266 EXPECT_TRUE(activity_log->IsDatabaseEnabled());
267 EXPECT_EQ(1,
268 profile->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
269 EXPECT_TRUE(activity_log->IsWatchdogAppActive());
271 extension_service->UninstallExtension(
272 kExtensionID,
273 extensions::UNINSTALL_REASON_FOR_TESTING,
274 base::Bind(&base::DoNothing),
275 NULL);
277 EXPECT_TRUE(activity_log->IsDatabaseEnabled());
278 EXPECT_EQ(0,
279 profile->GetPrefs()->GetInteger(prefs::kWatchdogExtensionActive));
280 EXPECT_FALSE(activity_log->IsWatchdogAppActive());
282 // Cleanup.
283 *base::CommandLine::ForCurrentProcess() = saved_cmdline_;
286 } // namespace extensions