- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / activity_log_enabled_unittest.cc
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.
4
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"
16
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"
21 #endif
22
23 namespace extensions {
24
25 class ActivityLogEnabledTest : public ChromeRenderViewHostTestHarness {
26  protected:
27   virtual void SetUp() OVERRIDE {
28     ChromeRenderViewHostTestHarness::SetUp();
29 #if defined OS_CHROMEOS
30     test_user_manager_.reset(new chromeos::ScopedTestUserManager());
31 #endif
32   }
33
34   virtual void TearDown() OVERRIDE {
35 #if defined OS_CHROMEOS
36     test_user_manager_.reset();
37 #endif
38     ChromeRenderViewHostTestHarness::TearDown();
39   }
40
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_;
45 #endif
46 };
47
48 TEST_F(ActivityLogEnabledTest, NoSwitch) {
49   scoped_ptr<TestingProfile> profile(
50     static_cast<TestingProfile*>(CreateBrowserContext()));
51   EXPECT_FALSE(
52       profile->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
53
54   ActivityLog* activity_log = ActivityLog::GetInstance(profile.get());
55
56   EXPECT_FALSE(
57     profile->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
58   EXPECT_FALSE(activity_log->IsDatabaseEnabled());
59   EXPECT_FALSE(activity_log->IsWatchdogAppActive());
60 }
61
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()));
67
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());
75
76   EXPECT_FALSE(
77       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
78   EXPECT_FALSE(
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());
84 }
85
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()));
91
92   EXPECT_FALSE(
93       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
94   EXPECT_FALSE(
95       profile2->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
96
97   profile1->GetPrefs()->SetBoolean(prefs::kWatchdogExtensionActive, true);
98   ActivityLog* activity_log1 = ActivityLog::GetInstance(profile1.get());
99   ActivityLog* activity_log2 = ActivityLog::GetInstance(profile2.get());
100
101   EXPECT_TRUE(
102       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
103   EXPECT_FALSE(
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());
109 }
110
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();
124
125   ActivityLog* activity_log1 = ActivityLog::GetInstance(profile1.get());
126   ActivityLog* activity_log2 = ActivityLog::GetInstance(profile2.get());
127
128   // Allow Activity Log to install extension tracker.
129   base::RunLoop().RunUntilIdle();
130
131   EXPECT_FALSE(
132       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
133   EXPECT_FALSE(
134       profile2->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
135
136   scoped_refptr<Extension> extension =
137       ExtensionBuilder()
138           .SetManifest(DictionaryBuilder()
139                        .Set("name", "Watchdog Extension ")
140                        .Set("version", "1.0.0")
141                        .Set("manifest_version", 2))
142           .SetID(kActivityLogExtensionId)
143           .Build();
144   extension_service1->AddExtension(extension.get());
145
146   EXPECT_TRUE(
147       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
148   EXPECT_FALSE(
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());
154
155   extension_service1->DisableExtension(kActivityLogExtensionId,
156                                        Extension::DISABLE_USER_ACTION);
157
158   EXPECT_FALSE(
159       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
160   EXPECT_FALSE(
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());
166
167   extension_service1->EnableExtension(kActivityLogExtensionId);
168
169   EXPECT_TRUE(
170       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
171   EXPECT_FALSE(
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());
177
178   extension_service1->UninstallExtension(kActivityLogExtensionId, false, NULL);
179
180   EXPECT_FALSE(
181       profile1->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
182   EXPECT_FALSE(
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());
188 }
189
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);
196
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();
206
207   ActivityLog* activity_log = ActivityLog::GetInstance(profile.get());
208   // Allow Activity Log to install extension tracker.
209   base::RunLoop().RunUntilIdle();
210
211   EXPECT_TRUE(activity_log->IsDatabaseEnabled());
212   EXPECT_FALSE(
213       profile->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
214   EXPECT_FALSE(activity_log->IsWatchdogAppActive());
215
216   // Enable the extension.
217   scoped_refptr<Extension> extension =
218       ExtensionBuilder()
219           .SetManifest(DictionaryBuilder()
220                        .Set("name", "Watchdog Extension ")
221                        .Set("version", "1.0.0")
222                        .Set("manifest_version", 2))
223           .SetID(kActivityLogExtensionId)
224           .Build();
225   extension_service->AddExtension(extension.get());
226
227   EXPECT_TRUE(activity_log->IsDatabaseEnabled());
228   EXPECT_TRUE(
229       profile->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
230   EXPECT_TRUE(activity_log->IsWatchdogAppActive());
231
232   extension_service->UninstallExtension(kActivityLogExtensionId, false, NULL);
233
234   EXPECT_TRUE(activity_log->IsDatabaseEnabled());
235   EXPECT_FALSE(
236       profile->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActive));
237   EXPECT_FALSE(activity_log->IsWatchdogAppActive());
238
239   // Cleanup.
240   *CommandLine::ForCurrentProcess() = saved_cmdline_;
241 }
242
243 } // namespace extensions