Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_service_unittest.cc
blob157c588922659a81b76d4dddcd7f14f508dac304
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/pref_names.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/app_list/app_list_switches.h"
23 class TestingAppListServiceImpl : public AppListServiceImpl {
24 public:
25 TestingAppListServiceImpl(const base::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),
30 destroy_app_list_call_count_(0) {}
32 Profile* showing_for_profile() const {
33 return showing_for_profile_;
36 int destroy_app_list_call_count() const {
37 return destroy_app_list_call_count_;
40 void PerformStartupChecks(Profile* profile) {
41 AppListServiceImpl::PerformStartupChecks(profile);
44 // AppListService overrides:
45 Profile* GetCurrentAppListProfile() override {
46 // We don't return showing_for_profile_ here because that is only defined if
47 // the app list is visible.
48 return NULL;
51 void CreateForProfile(Profile* requested_profile) override {}
53 void ShowForProfile(Profile* requested_profile) override {
54 showing_for_profile_ = requested_profile;
55 RecordAppListLaunch();
58 void ShowForCustomLauncherPage(Profile* profile) override {}
59 void HideCustomLauncherPage() override {}
61 void DismissAppList() override { showing_for_profile_ = NULL; }
63 bool IsAppListVisible() const override { return !!showing_for_profile_; }
65 gfx::NativeWindow GetAppListWindow() override { return NULL; }
67 AppListControllerDelegate* GetControllerDelegate() override { return NULL; }
69 // AppListServiceImpl overrides:
70 void DestroyAppList() override { ++destroy_app_list_call_count_; }
72 private:
73 Profile* showing_for_profile_;
74 int destroy_app_list_call_count_;
76 DISALLOW_COPY_AND_ASSIGN(TestingAppListServiceImpl);
79 class AppListServiceUnitTest : public testing::Test {
80 public:
81 AppListServiceUnitTest() {}
83 void SetUp() override {
84 SetupWithCommandLine(base::CommandLine(base::CommandLine::NO_PROGRAM));
87 protected:
88 void SetupWithCommandLine(const base::CommandLine& command_line) {
89 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd"));
90 profile1_.reset(
91 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1")));
92 profile2_.reset(
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 profile_store_ = new FakeProfileStore(user_data_dir_, local_state_.get());
104 service_.reset(new TestingAppListServiceImpl(
105 command_line,
106 local_state_.get(),
107 scoped_ptr<ProfileStore>(profile_store_)));
110 void EnableAppList() {
111 service_->EnableAppList(profile1_.get(),
112 AppListService::ENABLE_VIA_COMMAND_LINE);
115 base::FilePath user_data_dir_;
116 scoped_ptr<PrefService> local_state_;
117 FakeProfileStore* profile_store_;
118 scoped_ptr<TestingAppListServiceImpl> service_;
119 scoped_ptr<FakeProfile> profile1_;
120 scoped_ptr<FakeProfile> profile2_;
122 DISALLOW_COPY_AND_ASSIGN(AppListServiceUnitTest);
125 TEST_F(AppListServiceUnitTest, EnablingStateIsPersisted) {
126 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
127 EnableAppList();
128 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
129 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append(
130 local_state_->GetFilePath(prefs::kAppListProfile)));
133 TEST_F(AppListServiceUnitTest, ShowingForProfileLoadsAProfile) {
134 profile_store_->LoadProfile(profile1_.get());
135 EnableAppList();
136 service_->Show();
137 EXPECT_EQ(profile1_.get(), service_->showing_for_profile());
138 EXPECT_TRUE(service_->IsAppListVisible());
141 TEST_F(AppListServiceUnitTest, RemovedProfileResetsToInitialProfile) {
142 EnableAppList();
143 profile_store_->RemoveProfile(profile1_.get());
145 // kAppListProfile should have been cleared, and therefore GetProfilePath
146 // should return the initial profile.
147 EXPECT_EQ("", local_state_->GetString(prefs::kAppListProfile));
148 base::FilePath initial_profile_path =
149 user_data_dir_.AppendASCII(chrome::kInitialProfile);
150 EXPECT_EQ(initial_profile_path,
151 service_->GetProfilePath(profile_store_->GetUserDataDir()));
154 TEST_F(AppListServiceUnitTest,
155 RemovedProfileResetsToLastUsedProfileIfExists) {
156 local_state_->SetString(prefs::kProfileLastUsed, "last-used");
157 EnableAppList();
158 profile_store_->RemoveProfile(profile1_.get());
160 // kAppListProfile should have been set to kProfileLastUsed.
161 EXPECT_EQ("last-used", local_state_->GetString(prefs::kAppListProfile));
162 base::FilePath last_used_profile_path =
163 user_data_dir_.AppendASCII("last-used");
164 EXPECT_EQ(last_used_profile_path,
165 service_->GetProfilePath(profile_store_->GetUserDataDir()));
167 // For this test, the AppListViewDelegate is not created because the
168 // app list is never shown, so there is nothing to destroy.
169 EXPECT_EQ(0, service_->destroy_app_list_call_count());
172 TEST_F(AppListServiceUnitTest, RefusesToLoadGuestAppListProfile) {
173 // Unlikely, but if somehow the user's app_list.profile pref was set to the
174 // guest profile, make sure we refuse to load it (or it would crash).
175 local_state_->SetString(
176 prefs::kAppListProfile,
177 base::FilePath(chrome::kGuestProfileDir).MaybeAsASCII());
178 local_state_->SetString(prefs::kProfileLastUsed, "last-used");
179 base::FilePath last_used_profile_path =
180 user_data_dir_.AppendASCII("last-used");
181 EXPECT_EQ(last_used_profile_path,
182 service_->GetProfilePath(profile_store_->GetUserDataDir()));
185 TEST_F(AppListServiceUnitTest, RefusesToLoadGuestLastUsedProfile) {
186 // If the user's most recent browser session was a guest session, make sure we
187 // do not open a guest profile in the launcher (which would crash).
188 local_state_->SetString(
189 prefs::kProfileLastUsed,
190 base::FilePath(chrome::kGuestProfileDir).MaybeAsASCII());
191 base::FilePath initial_profile_path =
192 user_data_dir_.AppendASCII(chrome::kInitialProfile);
193 EXPECT_EQ(initial_profile_path,
194 service_->GetProfilePath(profile_store_->GetUserDataDir()));
197 TEST_F(AppListServiceUnitTest, SwitchingProfilesPersists) {
198 profile_store_->LoadProfile(profile1_.get());
199 profile_store_->LoadProfile(profile2_.get());
200 EnableAppList();
201 service_->SetProfilePath(profile2_->GetPath());
202 service_->Show();
203 EXPECT_EQ(profile2_.get(), service_->showing_for_profile());
204 EXPECT_EQ(profile2_->GetPath(),
205 service_->GetProfilePath(profile_store_->GetUserDataDir()));
206 service_->SetProfilePath(profile1_->GetPath());
207 EXPECT_EQ(profile1_->GetPath(),
208 service_->GetProfilePath(profile_store_->GetUserDataDir()));
211 TEST_F(AppListServiceUnitTest, EnableViaCommandLineFlag) {
212 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
213 command_line.AppendSwitch(app_list::switches::kEnableAppList);
214 SetupWithCommandLine(command_line);
215 service_->PerformStartupChecks(profile1_.get());
216 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
219 TEST_F(AppListServiceUnitTest, DisableViaCommandLineFlag) {
220 base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
221 command_line.AppendSwitch(app_list::switches::kResetAppListInstallState);
222 SetupWithCommandLine(command_line);
223 service_->PerformStartupChecks(profile1_.get());
224 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
227 TEST_F(AppListServiceUnitTest, UMAPrefStates) {
228 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
229 EXPECT_EQ(AppListService::ENABLE_NOT_RECORDED,
230 local_state_->GetInteger(prefs::kAppListEnableMethod));
231 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
233 service_->EnableAppList(profile1_.get(),
234 AppListService::ENABLE_FOR_APP_INSTALL);
236 // After enable, method and time should be recorded.
237 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled));
238 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
239 local_state_->GetInteger(prefs::kAppListEnableMethod));
240 EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime));
242 service_->ShowForProfile(profile1_.get());
244 // After a regular "show", time should be cleared, so UMA is not re-recorded.
245 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
246 local_state_->GetInteger(prefs::kAppListEnableMethod));
247 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
249 // A second enable should be a no-op.
250 service_->EnableAppList(profile1_.get(),
251 AppListService::ENABLE_FOR_APP_INSTALL);
252 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
253 local_state_->GetInteger(prefs::kAppListEnableMethod));
254 EXPECT_EQ(0, local_state_->GetInt64(prefs::kAppListEnableTime));
256 // An app install auto-show here should keep the recorded enable method.
257 service_->ShowForAppInstall(profile1_.get(), "", false);
258 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
259 local_state_->GetInteger(prefs::kAppListEnableMethod));
261 // Clear the enable state, so we can enable again.
262 local_state_->SetBoolean(prefs::kAppLauncherHasBeenEnabled, false);
263 service_->EnableAppList(profile1_.get(),
264 AppListService::ENABLE_FOR_APP_INSTALL);
266 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
267 local_state_->GetInteger(prefs::kAppListEnableMethod));
268 EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime));
270 // An auto-show here should update the enable method to prevent recording it
271 // as ENABLE_FOR_APP_INSTALL.
272 service_->ShowForAppInstall(profile1_.get(), "", false);
273 EXPECT_EQ(AppListService::ENABLE_SHOWN_UNDISCOVERED,
274 local_state_->GetInteger(prefs::kAppListEnableMethod));