| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/prefs/pref_service_factory.h" | 10 #include "base/prefs/pref_service_factory.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 local_state, | 31 local_state, |
| 32 profile_store.Pass(), | 32 profile_store.Pass(), |
| 33 keep_alive_service.Pass()), | 33 keep_alive_service.Pass()), |
| 34 showing_for_profile_(NULL) { | 34 showing_for_profile_(NULL) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 Profile* showing_for_profile() const { | 37 Profile* showing_for_profile() const { |
| 38 return showing_for_profile_; | 38 return showing_for_profile_; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void HandleCommandLineFlags(Profile* profile) { | 41 void PerformStartupChecks(Profile* profile) { |
| 42 AppListServiceImpl::HandleCommandLineFlags(profile); | 42 AppListServiceImpl::PerformStartupChecks(profile); |
| 43 } | 43 } |
| 44 | 44 |
| 45 virtual Profile* GetCurrentAppListProfile() OVERRIDE { | 45 virtual Profile* GetCurrentAppListProfile() OVERRIDE { |
| 46 // We don't return showing_for_profile_ here because that is only defined if | 46 // We don't return showing_for_profile_ here because that is only defined if |
| 47 // the app list is visible. | 47 // the app list is visible. |
| 48 return NULL; | 48 return NULL; |
| 49 } | 49 } |
| 50 | 50 |
| 51 virtual void CreateForProfile(Profile* requested_profile) OVERRIDE { | 51 virtual void CreateForProfile(Profile* requested_profile) OVERRIDE { |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual void ShowForProfile(Profile* requested_profile) OVERRIDE { | 54 virtual void ShowForProfile(Profile* requested_profile) OVERRIDE { |
| 55 showing_for_profile_ = requested_profile; | 55 showing_for_profile_ = requested_profile; |
| 56 RecordAppListLaunch(); |
| 56 } | 57 } |
| 57 | 58 |
| 58 virtual void DismissAppList() OVERRIDE { | 59 virtual void DismissAppList() OVERRIDE { |
| 59 showing_for_profile_ = NULL; | 60 showing_for_profile_ = NULL; |
| 60 } | 61 } |
| 61 | 62 |
| 62 virtual bool IsAppListVisible() const OVERRIDE { | 63 virtual bool IsAppListVisible() const OVERRIDE { |
| 63 return !!showing_for_profile_; | 64 return !!showing_for_profile_; |
| 64 } | 65 } |
| 65 | 66 |
| 66 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE { | 67 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE { |
| 67 return NULL; | 68 return NULL; |
| 68 } | 69 } |
| 69 | 70 |
| 70 virtual AppListControllerDelegate* GetControllerDelegate() OVERRIDE { | 71 virtual AppListControllerDelegate* GetControllerDelegate() OVERRIDE { |
| 71 return NULL; | 72 return NULL; |
| 72 } | 73 } |
| 73 | 74 |
| 74 private: | 75 private: |
| 75 Profile* showing_for_profile_; | 76 Profile* showing_for_profile_; |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 class AppListServiceUnitTest : public testing::Test { | 79 class AppListServiceUnitTest : public testing::Test { |
| 79 public: | 80 public: |
| 81 AppListServiceUnitTest() {} |
| 82 |
| 80 virtual void SetUp() OVERRIDE { | 83 virtual void SetUp() OVERRIDE { |
| 81 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM)); | 84 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM)); |
| 82 } | 85 } |
| 83 | 86 |
| 87 protected: |
| 84 void SetupWithCommandLine(const CommandLine& command_line) { | 88 void SetupWithCommandLine(const CommandLine& command_line) { |
| 85 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd")); | 89 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd")); |
| 86 profile1_.reset( | 90 profile1_.reset( |
| 87 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1"))); | 91 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1"))); |
| 88 profile2_.reset( | 92 profile2_.reset( |
| 89 new FakeProfile("p2", user_data_dir_.AppendASCII("profile2"))); | 93 new FakeProfile("p2", user_data_dir_.AppendASCII("profile2"))); |
| 90 PrefRegistrySimple* pref_registry = new PrefRegistrySimple; | 94 PrefRegistrySimple* pref_registry = new PrefRegistrySimple; |
| 91 | 95 |
| 92 AppListService::RegisterPrefs(pref_registry); | 96 AppListService::RegisterPrefs(pref_registry); |
| 93 profiles::RegisterPrefs(pref_registry); | 97 profiles::RegisterPrefs(pref_registry); |
| 94 | 98 |
| 95 base::PrefServiceFactory factory; | 99 base::PrefServiceFactory factory; |
| 96 factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore)); | 100 factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore)); |
| 97 local_state_ = factory.Create(pref_registry).Pass(); | 101 local_state_ = factory.Create(pref_registry).Pass(); |
| 98 | 102 |
| 99 keep_alive_service_ = new FakeKeepAliveService; | 103 keep_alive_service_ = new FakeKeepAliveService; |
| 100 profile_store_ = new FakeProfileStore(user_data_dir_); | 104 profile_store_ = new FakeProfileStore(user_data_dir_); |
| 101 service_.reset(new TestingAppListServiceImpl( | 105 service_.reset(new TestingAppListServiceImpl( |
| 102 command_line, | 106 command_line, |
| 103 local_state_.get(), | 107 local_state_.get(), |
| 104 scoped_ptr<ProfileStore>(profile_store_), | 108 scoped_ptr<ProfileStore>(profile_store_), |
| 105 scoped_ptr<KeepAliveService>(keep_alive_service_))); | 109 scoped_ptr<KeepAliveService>(keep_alive_service_))); |
| 106 } | 110 } |
| 107 | 111 |
| 112 void EnableAppList() { |
| 113 service_->EnableAppList(profile1_.get(), |
| 114 AppListService::ENABLE_VIA_COMMAND_LINE); |
| 115 } |
| 116 |
| 108 base::FilePath user_data_dir_; | 117 base::FilePath user_data_dir_; |
| 109 scoped_ptr<PrefService> local_state_; | 118 scoped_ptr<PrefService> local_state_; |
| 110 FakeProfileStore* profile_store_; | 119 FakeProfileStore* profile_store_; |
| 111 FakeKeepAliveService* keep_alive_service_; | 120 FakeKeepAliveService* keep_alive_service_; |
| 112 scoped_ptr<TestingAppListServiceImpl> service_; | 121 scoped_ptr<TestingAppListServiceImpl> service_; |
| 113 scoped_ptr<FakeProfile> profile1_; | 122 scoped_ptr<FakeProfile> profile1_; |
| 114 scoped_ptr<FakeProfile> profile2_; | 123 scoped_ptr<FakeProfile> profile2_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(AppListServiceUnitTest); |
| 115 }; | 126 }; |
| 116 | 127 |
| 117 TEST_F(AppListServiceUnitTest, EnablingStateIsPersisted) { | 128 TEST_F(AppListServiceUnitTest, EnablingStateIsPersisted) { |
| 118 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); | 129 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 119 service_->EnableAppList(profile1_.get()); | 130 EnableAppList(); |
| 120 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); | 131 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 121 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append( | 132 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append( |
| 122 local_state_->GetFilePath(prefs::kAppListProfile))); | 133 local_state_->GetFilePath(prefs::kAppListProfile))); |
| 123 } | 134 } |
| 124 | 135 |
| 125 TEST_F(AppListServiceUnitTest, ShowingForProfileLoadsAProfile) { | 136 TEST_F(AppListServiceUnitTest, ShowingForProfileLoadsAProfile) { |
| 126 profile_store_->LoadProfile(profile1_.get()); | 137 profile_store_->LoadProfile(profile1_.get()); |
| 127 service_->EnableAppList(profile1_.get()); | 138 EnableAppList(); |
| 128 service_->Show(); | 139 service_->Show(); |
| 129 EXPECT_EQ(profile1_.get(), service_->showing_for_profile()); | 140 EXPECT_EQ(profile1_.get(), service_->showing_for_profile()); |
| 130 EXPECT_TRUE(service_->IsAppListVisible()); | 141 EXPECT_TRUE(service_->IsAppListVisible()); |
| 131 } | 142 } |
| 132 | 143 |
| 133 TEST_F(AppListServiceUnitTest, RemovedProfileResetsToInitialProfile) { | 144 TEST_F(AppListServiceUnitTest, RemovedProfileResetsToInitialProfile) { |
| 134 service_->EnableAppList(profile1_.get()); | 145 EnableAppList(); |
| 135 profile_store_->RemoveProfile(profile1_.get()); | 146 profile_store_->RemoveProfile(profile1_.get()); |
| 136 base::FilePath initial_profile_path = | 147 base::FilePath initial_profile_path = |
| 137 user_data_dir_.AppendASCII(chrome::kInitialProfile); | 148 user_data_dir_.AppendASCII(chrome::kInitialProfile); |
| 138 EXPECT_EQ(initial_profile_path, | 149 EXPECT_EQ(initial_profile_path, |
| 139 service_->GetProfilePath(profile_store_->GetUserDataDir())); | 150 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 140 } | 151 } |
| 141 | 152 |
| 142 TEST_F(AppListServiceUnitTest, | 153 TEST_F(AppListServiceUnitTest, |
| 143 RemovedProfileResetsToLastUsedProfileIfExists) { | 154 RemovedProfileResetsToLastUsedProfileIfExists) { |
| 144 local_state_->SetString(prefs::kProfileLastUsed, "last-used"); | 155 local_state_->SetString(prefs::kProfileLastUsed, "last-used"); |
| 145 service_->EnableAppList(profile1_.get()); | 156 EnableAppList(); |
| 146 profile_store_->RemoveProfile(profile1_.get()); | 157 profile_store_->RemoveProfile(profile1_.get()); |
| 147 base::FilePath last_used_profile_path = | 158 base::FilePath last_used_profile_path = |
| 148 user_data_dir_.AppendASCII("last-used"); | 159 user_data_dir_.AppendASCII("last-used"); |
| 149 EXPECT_EQ(last_used_profile_path, | 160 EXPECT_EQ(last_used_profile_path, |
| 150 service_->GetProfilePath(profile_store_->GetUserDataDir())); | 161 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 151 } | 162 } |
| 152 | 163 |
| 153 TEST_F(AppListServiceUnitTest, SwitchingProfilesPersists) { | 164 TEST_F(AppListServiceUnitTest, SwitchingProfilesPersists) { |
| 154 profile_store_->LoadProfile(profile1_.get()); | 165 profile_store_->LoadProfile(profile1_.get()); |
| 155 profile_store_->LoadProfile(profile2_.get()); | 166 profile_store_->LoadProfile(profile2_.get()); |
| 156 service_->EnableAppList(profile1_.get()); | 167 EnableAppList(); |
| 157 service_->SetProfilePath(profile2_->GetPath()); | 168 service_->SetProfilePath(profile2_->GetPath()); |
| 158 service_->Show(); | 169 service_->Show(); |
| 159 EXPECT_EQ(profile2_.get(), service_->showing_for_profile()); | 170 EXPECT_EQ(profile2_.get(), service_->showing_for_profile()); |
| 160 EXPECT_EQ(profile2_->GetPath(), | 171 EXPECT_EQ(profile2_->GetPath(), |
| 161 service_->GetProfilePath(profile_store_->GetUserDataDir())); | 172 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 162 service_->SetProfilePath(profile1_->GetPath()); | 173 service_->SetProfilePath(profile1_->GetPath()); |
| 163 EXPECT_EQ(profile1_->GetPath(), | 174 EXPECT_EQ(profile1_->GetPath(), |
| 164 service_->GetProfilePath(profile_store_->GetUserDataDir())); | 175 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 165 } | 176 } |
| 166 | 177 |
| 167 TEST_F(AppListServiceUnitTest, EnableViaCommandLineFlag) { | 178 TEST_F(AppListServiceUnitTest, EnableViaCommandLineFlag) { |
| 168 CommandLine command_line(CommandLine::NO_PROGRAM); | 179 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 169 command_line.AppendSwitch(switches::kEnableAppList); | 180 command_line.AppendSwitch(switches::kEnableAppList); |
| 170 SetupWithCommandLine(command_line); | 181 SetupWithCommandLine(command_line); |
| 171 service_->HandleCommandLineFlags(profile1_.get()); | 182 service_->PerformStartupChecks(profile1_.get()); |
| 172 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); | 183 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 173 } | 184 } |
| 174 | 185 |
| 175 TEST_F(AppListServiceUnitTest, DisableViaCommandLineFlag) { | 186 TEST_F(AppListServiceUnitTest, DisableViaCommandLineFlag) { |
| 176 CommandLine command_line(CommandLine::NO_PROGRAM); | 187 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 177 command_line.AppendSwitch(switches::kResetAppListInstallState); | 188 command_line.AppendSwitch(switches::kResetAppListInstallState); |
| 178 SetupWithCommandLine(command_line); | 189 SetupWithCommandLine(command_line); |
| 179 service_->HandleCommandLineFlags(profile1_.get()); | 190 service_->PerformStartupChecks(profile1_.get()); |
| 180 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); | 191 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 181 } | 192 } |
| 193 |
| 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)); |
| 199 |
| 200 service_->EnableAppList(profile1_.get(), |
| 201 AppListService::ENABLE_FOR_APP_INSTALL); |
| 202 |
| 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)); |
| 208 |
| 209 service_->ShowForProfile(profile1_.get()); |
| 210 |
| 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)); |
| 215 |
| 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)); |
| 222 |
| 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)); |
| 227 |
| 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); |
| 232 |
| 233 EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL, |
| 234 local_state_->GetInteger(prefs::kAppListEnableMethod)); |
| 235 EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime)); |
| 236 |
| 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)); |
| 242 } |
| OLD | NEW |