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/files/file_path.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/pref_service_factory.h"
11 #include "base/prefs/testing_pref_store.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profiles_state.h"
14 #include "chrome/browser/ui/app_list/app_list_service.h"
15 #include "chrome/browser/ui/app_list/app_list_service_impl.h"
16 #include "chrome/browser/ui/app_list/test/fake_keep_alive_service.h"
17 #include "chrome/browser/ui/app_list/test/fake_profile.h"
18 #include "chrome/browser/ui/app_list/test/fake_profile_store.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 class TestingAppListServiceImpl
: public AppListServiceImpl
{
26 TestingAppListServiceImpl(const CommandLine
& command_line
,
27 PrefService
* local_state
,
28 scoped_ptr
<ProfileStore
> profile_store
,
29 scoped_ptr
<KeepAliveService
> keep_alive_service
)
30 : AppListServiceImpl(command_line
,
33 keep_alive_service
.Pass()),
34 showing_for_profile_(NULL
) {
37 Profile
* showing_for_profile() const {
38 return showing_for_profile_
;
41 void PerformStartupChecks(Profile
* profile
) {
42 AppListServiceImpl::PerformStartupChecks(profile
);
45 virtual Profile
* GetCurrentAppListProfile() OVERRIDE
{
46 // We don't return showing_for_profile_ here because that is only defined if
47 // the app list is visible.
51 virtual void CreateForProfile(Profile
* requested_profile
) OVERRIDE
{
54 virtual void ShowForProfile(Profile
* requested_profile
) OVERRIDE
{
55 showing_for_profile_
= requested_profile
;
56 RecordAppListLaunch();
59 virtual void DismissAppList() OVERRIDE
{
60 showing_for_profile_
= NULL
;
63 virtual bool IsAppListVisible() const OVERRIDE
{
64 return !!showing_for_profile_
;
67 virtual gfx::NativeWindow
GetAppListWindow() OVERRIDE
{
71 virtual AppListControllerDelegate
* GetControllerDelegate() OVERRIDE
{
76 Profile
* showing_for_profile_
;
79 class AppListServiceUnitTest
: public testing::Test
{
81 AppListServiceUnitTest() {}
83 virtual void SetUp() OVERRIDE
{
84 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM
));
88 void SetupWithCommandLine(const CommandLine
& command_line
) {
89 user_data_dir_
= base::FilePath(FILE_PATH_LITERAL("udd"));
91 new FakeProfile("p1", user_data_dir_
.AppendASCII("profile1")));
93 new FakeProfile("p2", user_data_dir_
.AppendASCII("profile2")));
94 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple
;
96 AppListService::RegisterPrefs(pref_registry
);
97 profiles::RegisterPrefs(pref_registry
);
99 base::PrefServiceFactory factory
;
100 factory
.set_user_prefs(make_scoped_refptr(new TestingPrefStore
));
101 local_state_
= factory
.Create(pref_registry
).Pass();
103 keep_alive_service_
= new FakeKeepAliveService
;
104 profile_store_
= new FakeProfileStore(user_data_dir_
);
105 service_
.reset(new TestingAppListServiceImpl(
108 scoped_ptr
<ProfileStore
>(profile_store_
),
109 scoped_ptr
<KeepAliveService
>(keep_alive_service_
)));
112 void EnableAppList() {
113 service_
->EnableAppList(profile1_
.get(),
114 AppListService::ENABLE_VIA_COMMAND_LINE
);
117 base::FilePath user_data_dir_
;
118 scoped_ptr
<PrefService
> local_state_
;
119 FakeProfileStore
* profile_store_
;
120 FakeKeepAliveService
* keep_alive_service_
;
121 scoped_ptr
<TestingAppListServiceImpl
> service_
;
122 scoped_ptr
<FakeProfile
> profile1_
;
123 scoped_ptr
<FakeProfile
> profile2_
;
125 DISALLOW_COPY_AND_ASSIGN(AppListServiceUnitTest
);
128 TEST_F(AppListServiceUnitTest
, EnablingStateIsPersisted
) {
129 EXPECT_FALSE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
131 EXPECT_TRUE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
132 EXPECT_EQ(profile1_
->GetPath(), user_data_dir_
.Append(
133 local_state_
->GetFilePath(prefs::kAppListProfile
)));
136 TEST_F(AppListServiceUnitTest
, ShowingForProfileLoadsAProfile
) {
137 profile_store_
->LoadProfile(profile1_
.get());
140 EXPECT_EQ(profile1_
.get(), service_
->showing_for_profile());
141 EXPECT_TRUE(service_
->IsAppListVisible());
144 TEST_F(AppListServiceUnitTest
, RemovedProfileResetsToInitialProfile
) {
146 profile_store_
->RemoveProfile(profile1_
.get());
147 base::FilePath initial_profile_path
=
148 user_data_dir_
.AppendASCII(chrome::kInitialProfile
);
149 EXPECT_EQ(initial_profile_path
,
150 service_
->GetProfilePath(profile_store_
->GetUserDataDir()));
153 TEST_F(AppListServiceUnitTest
,
154 RemovedProfileResetsToLastUsedProfileIfExists
) {
155 local_state_
->SetString(prefs::kProfileLastUsed
, "last-used");
157 profile_store_
->RemoveProfile(profile1_
.get());
158 base::FilePath last_used_profile_path
=
159 user_data_dir_
.AppendASCII("last-used");
160 EXPECT_EQ(last_used_profile_path
,
161 service_
->GetProfilePath(profile_store_
->GetUserDataDir()));
164 TEST_F(AppListServiceUnitTest
, SwitchingProfilesPersists
) {
165 profile_store_
->LoadProfile(profile1_
.get());
166 profile_store_
->LoadProfile(profile2_
.get());
168 service_
->SetProfilePath(profile2_
->GetPath());
170 EXPECT_EQ(profile2_
.get(), service_
->showing_for_profile());
171 EXPECT_EQ(profile2_
->GetPath(),
172 service_
->GetProfilePath(profile_store_
->GetUserDataDir()));
173 service_
->SetProfilePath(profile1_
->GetPath());
174 EXPECT_EQ(profile1_
->GetPath(),
175 service_
->GetProfilePath(profile_store_
->GetUserDataDir()));
178 TEST_F(AppListServiceUnitTest
, EnableViaCommandLineFlag
) {
179 CommandLine
command_line(CommandLine::NO_PROGRAM
);
180 command_line
.AppendSwitch(switches::kEnableAppList
);
181 SetupWithCommandLine(command_line
);
182 service_
->PerformStartupChecks(profile1_
.get());
183 EXPECT_TRUE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
186 TEST_F(AppListServiceUnitTest
, DisableViaCommandLineFlag
) {
187 CommandLine
command_line(CommandLine::NO_PROGRAM
);
188 command_line
.AppendSwitch(switches::kResetAppListInstallState
);
189 SetupWithCommandLine(command_line
);
190 service_
->PerformStartupChecks(profile1_
.get());
191 EXPECT_FALSE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
194 TEST_F(AppListServiceUnitTest
, UMAPrefStates
) {
195 EXPECT_FALSE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
196 EXPECT_EQ(AppListService::ENABLE_NOT_RECORDED
,
197 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
198 EXPECT_EQ(0, local_state_
->GetInt64(prefs::kAppListEnableTime
));
200 service_
->EnableAppList(profile1_
.get(),
201 AppListService::ENABLE_FOR_APP_INSTALL
);
203 // After enable, method and time should be recorded.
204 EXPECT_TRUE(local_state_
->GetBoolean(prefs::kAppLauncherHasBeenEnabled
));
205 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL
,
206 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
207 EXPECT_NE(0, local_state_
->GetInt64(prefs::kAppListEnableTime
));
209 service_
->ShowForProfile(profile1_
.get());
211 // After a regular "show", time should be cleared, so UMA is not re-recorded.
212 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL
,
213 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
214 EXPECT_EQ(0, local_state_
->GetInt64(prefs::kAppListEnableTime
));
216 // A second enable should be a no-op.
217 service_
->EnableAppList(profile1_
.get(),
218 AppListService::ENABLE_FOR_APP_INSTALL
);
219 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL
,
220 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
221 EXPECT_EQ(0, local_state_
->GetInt64(prefs::kAppListEnableTime
));
223 // An auto-show here should keep the recorded enable method.
224 service_
->AutoShowForProfile(profile1_
.get());
225 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL
,
226 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
228 // Clear the enable state, so we can enable again.
229 local_state_
->SetBoolean(prefs::kAppLauncherHasBeenEnabled
, false);
230 service_
->EnableAppList(profile1_
.get(),
231 AppListService::ENABLE_FOR_APP_INSTALL
);
233 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL
,
234 local_state_
->GetInteger(prefs::kAppListEnableMethod
));
235 EXPECT_NE(0, local_state_
->GetInt64(prefs::kAppListEnableTime
));
237 // An auto-show here should update the enable method to prevent recording it
238 // as ENABLE_FOR_APP_INSTALL.
239 service_
->AutoShowForProfile(profile1_
.get());
240 EXPECT_EQ(AppListService::ENABLE_SHOWN_UNDISCOVERED
,
241 local_state_
->GetInteger(prefs::kAppListEnableMethod
));