Revert of Merge apps/pref* to extensions/browser/pref* (patchset #1 of https://codere...
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_service_unittest.cc
bloba3d7b33af987a8aaa9eb8eb1464194428bb2149a
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_profile.h"
17 #include "chrome/browser/ui/app_list/test/fake_profile_store.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 class TestingAppListServiceImpl : public AppListServiceImpl {
24 public:
25 TestingAppListServiceImpl(const CommandLine& command_line,
26 PrefService* local_state,
27 scoped_ptr<ProfileStore> profile_store)
28 : AppListServiceImpl(command_line, local_state, profile_store.Pass()),
29 showing_for_profile_(NULL) {}
31 Profile* showing_for_profile() const {
32 return showing_for_profile_;
35 void PerformStartupChecks(Profile* profile) {
36 AppListServiceImpl::PerformStartupChecks(profile);
39 virtual Profile* GetCurrentAppListProfile() OVERRIDE {
40 // We don't return showing_for_profile_ here because that is only defined if
41 // the app list is visible.
42 return NULL;
45 virtual void CreateForProfile(Profile* requested_profile) OVERRIDE {
48 virtual void ShowForProfile(Profile* requested_profile) OVERRIDE {
49 showing_for_profile_ = requested_profile;
50 RecordAppListLaunch();
53 virtual void DismissAppList() OVERRIDE {
54 showing_for_profile_ = NULL;
57 virtual bool IsAppListVisible() const OVERRIDE {
58 return !!showing_for_profile_;
61 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE {
62 return NULL;
65 virtual AppListControllerDelegate* GetControllerDelegate() OVERRIDE {
66 return NULL;
69 private:
70 Profile* showing_for_profile_;
73 class AppListServiceUnitTest : public testing::Test {
74 public:
75 AppListServiceUnitTest() {}
77 virtual void SetUp() OVERRIDE {
78 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM));
81 protected:
82 void SetupWithCommandLine(const CommandLine& command_line) {
83 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd"));
84 profile1_.reset(
85 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1")));
86 profile2_.reset(
87 new FakeProfile("p2", user_data_dir_.AppendASCII("profile2")));
88 PrefRegistrySimple* pref_registry = new PrefRegistrySimple;
90 AppListService::RegisterPrefs(pref_registry);
91 profiles::RegisterPrefs(pref_registry);
93 base::PrefServiceFactory factory;
94 factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore));
95 local_state_ = factory.Create(pref_registry).Pass();
97 profile_store_ = new FakeProfileStore(user_data_dir_);
98 service_.reset(new TestingAppListServiceImpl(
99 command_line,
100 local_state_.get(),
101 scoped_ptr<ProfileStore>(profile_store_)));
104 void EnableAppList() {
105 service_->EnableAppList(profile1_.get(),
106 AppListService::ENABLE_VIA_COMMAND_LINE);
109 base::FilePath user_data_dir_;
110 scoped_ptr<PrefService> local_state_;
111 FakeProfileStore* profile_store_;
112 scoped_ptr<TestingAppListServiceImpl> service_;
113 scoped_ptr<FakeProfile> profile1_;
114 scoped_ptr<FakeProfile> profile2_;
116 DISALLOW_COPY_AND_ASSIGN(AppListServiceUnitTest);
119 TEST_F(AppListServiceUnitTest, EnablingStateIsPersisted) {
120 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
121 EnableAppList();
122 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
123 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append(
124 local_state_->GetFilePath(prefs::kAppListProfile)));
127 TEST_F(AppListServiceUnitTest, ShowingForProfileLoadsAProfile) {
128 profile_store_->LoadProfile(profile1_.get());
129 EnableAppList();
130 service_->Show();
131 EXPECT_EQ(profile1_.get(), service_->showing_for_profile());
132 EXPECT_TRUE(service_->IsAppListVisible());
135 TEST_F(AppListServiceUnitTest, RemovedProfileResetsToInitialProfile) {
136 EnableAppList();
137 profile_store_->RemoveProfile(profile1_.get());
138 base::FilePath initial_profile_path =
139 user_data_dir_.AppendASCII(chrome::kInitialProfile);
140 EXPECT_EQ(initial_profile_path,
141 service_->GetProfilePath(profile_store_->GetUserDataDir()));
144 TEST_F(AppListServiceUnitTest,
145 RemovedProfileResetsToLastUsedProfileIfExists) {
146 local_state_->SetString(prefs::kProfileLastUsed, "last-used");
147 EnableAppList();
148 profile_store_->RemoveProfile(profile1_.get());
149 base::FilePath last_used_profile_path =
150 user_data_dir_.AppendASCII("last-used");
151 EXPECT_EQ(last_used_profile_path,
152 service_->GetProfilePath(profile_store_->GetUserDataDir()));
155 TEST_F(AppListServiceUnitTest, SwitchingProfilesPersists) {
156 profile_store_->LoadProfile(profile1_.get());
157 profile_store_->LoadProfile(profile2_.get());
158 EnableAppList();
159 service_->SetProfilePath(profile2_->GetPath());
160 service_->Show();
161 EXPECT_EQ(profile2_.get(), service_->showing_for_profile());
162 EXPECT_EQ(profile2_->GetPath(),
163 service_->GetProfilePath(profile_store_->GetUserDataDir()));
164 service_->SetProfilePath(profile1_->GetPath());
165 EXPECT_EQ(profile1_->GetPath(),
166 service_->GetProfilePath(profile_store_->GetUserDataDir()));
169 TEST_F(AppListServiceUnitTest, EnableViaCommandLineFlag) {
170 CommandLine command_line(CommandLine::NO_PROGRAM);
171 command_line.AppendSwitch(switches::kEnableAppList);
172 SetupWithCommandLine(command_line);
173 service_->PerformStartupChecks(profile1_.get());
174 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
177 TEST_F(AppListServiceUnitTest, DisableViaCommandLineFlag) {
178 CommandLine command_line(CommandLine::NO_PROGRAM);
179 command_line.AppendSwitch(switches::kResetAppListInstallState);
180 SetupWithCommandLine(command_line);
181 service_->PerformStartupChecks(profile1_.get());
182 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
185 TEST_F(AppListServiceUnitTest, UMAPrefStates) {
186 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
187 EXPECT_EQ(AppListService::ENABLE_NOT_RECORDED,
188 local_state_->GetInteger(prefs::kAppListEnableMethod));
189 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
191 service_->EnableAppList(profile1_.get(),
192 AppListService::ENABLE_FOR_APP_INSTALL);
194 // After enable, method and time should be recorded.
195 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
196 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
197 local_state_->GetInteger(prefs::kAppListEnableMethod));
198 EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime));
200 service_->ShowForProfile(profile1_.get());
202 // After a regular "show", time should be cleared, so UMA is not re-recorded.
203 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
204 local_state_->GetInteger(prefs::kAppListEnableMethod));
205 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
207 // A second enable should be a no-op.
208 service_->EnableAppList(profile1_.get(),
209 AppListService::ENABLE_FOR_APP_INSTALL);
210 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
211 local_state_->GetInteger(prefs::kAppListEnableMethod));
212 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
214 // An auto-show here should keep the recorded enable method.
215 service_->AutoShowForProfile(profile1_.get());
216 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
217 local_state_->GetInteger(prefs::kAppListEnableMethod));
219 // Clear the enable state, so we can enable again.
220 local_state_->SetBoolean(prefs::kAppLauncherHasBeenEnabled, false);
221 service_->EnableAppList(profile1_.get(),
222 AppListService::ENABLE_FOR_APP_INSTALL);
224 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
225 local_state_->GetInteger(prefs::kAppListEnableMethod));
226 EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime));
228 // An auto-show here should update the enable method to prevent recording it
229 // as ENABLE_FOR_APP_INSTALL.
230 service_->AutoShowForProfile(profile1_.get());
231 EXPECT_EQ(AppListService::ENABLE_SHOWN_UNDISCOVERED,
232 local_state_->GetInteger(prefs::kAppListEnableMethod));