OLD | NEW |
(Empty) | |
| 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 "apps/pref_names.h" |
| 6 #include "apps/prefs.h" |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/prefs/pref_service_builder.h" |
| 13 #include "base/prefs/testing_pref_store.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profiles_state.h" |
| 16 #include "chrome/browser/ui/app_list/app_list_service.h" |
| 17 #include "chrome/browser/ui/app_list/app_list_service_impl.h" |
| 18 #include "chrome/browser/ui/app_list/test/fake_keep_alive_service.h" |
| 19 #include "chrome/browser/ui/app_list/test/fake_profile.h" |
| 20 #include "chrome/browser/ui/app_list/test/fake_profile_store.h" |
| 21 #include "chrome/common/chrome_constants.h" |
| 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 class TestingAppListServiceImpl : public AppListServiceImpl { |
| 27 public: |
| 28 TestingAppListServiceImpl(const CommandLine& command_line, |
| 29 PrefService* local_state, |
| 30 scoped_ptr<ProfileStore> profile_store, |
| 31 scoped_ptr<KeepAliveService> keep_alive_service) |
| 32 : AppListServiceImpl(command_line, |
| 33 local_state, |
| 34 profile_store.Pass(), |
| 35 keep_alive_service.Pass()), |
| 36 showing_for_profile_(NULL) { |
| 37 } |
| 38 |
| 39 Profile* showing_for_profile() const { |
| 40 return showing_for_profile_; |
| 41 } |
| 42 |
| 43 void HandleCommandLineFlags(Profile* profile) { |
| 44 AppListServiceImpl::HandleCommandLineFlags(profile); |
| 45 } |
| 46 |
| 47 virtual void CreateForProfile(Profile* requested_profile) OVERRIDE { |
| 48 } |
| 49 |
| 50 virtual void ShowForProfile(Profile* requested_profile) OVERRIDE { |
| 51 showing_for_profile_ = requested_profile; |
| 52 } |
| 53 |
| 54 virtual void DismissAppList() OVERRIDE { |
| 55 showing_for_profile_ = NULL; |
| 56 } |
| 57 |
| 58 virtual bool IsAppListVisible() const OVERRIDE { |
| 59 return !!showing_for_profile_; |
| 60 } |
| 61 |
| 62 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE { |
| 63 return NULL; |
| 64 } |
| 65 |
| 66 virtual AppListControllerDelegate* CreateControllerDelegate() OVERRIDE { |
| 67 return NULL; |
| 68 } |
| 69 |
| 70 private: |
| 71 Profile* showing_for_profile_; |
| 72 }; |
| 73 |
| 74 class AppListServiceUnitTest : public testing::Test { |
| 75 public: |
| 76 virtual void SetUp() OVERRIDE { |
| 77 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM)); |
| 78 } |
| 79 |
| 80 void SetupWithCommandLine(const CommandLine& command_line) { |
| 81 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd")); |
| 82 profile1_.reset( |
| 83 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1"))); |
| 84 profile2_.reset( |
| 85 new FakeProfile("p2", user_data_dir_.AppendASCII("profile2"))); |
| 86 PrefRegistrySimple* pref_registry = new PrefRegistrySimple; |
| 87 |
| 88 AppListService::RegisterPrefs(pref_registry); |
| 89 apps::RegisterPrefs(pref_registry); |
| 90 profiles::RegisterPrefs(pref_registry); |
| 91 |
| 92 PrefServiceBuilder builder; |
| 93 builder.WithUserPrefs(new TestingPrefStore); |
| 94 local_state_.reset(builder.Create(pref_registry)); |
| 95 |
| 96 keep_alive_service_ = new FakeKeepAliveService; |
| 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_), |
| 102 scoped_ptr<KeepAliveService>(keep_alive_service_))); |
| 103 } |
| 104 |
| 105 base::FilePath user_data_dir_; |
| 106 scoped_ptr<PrefService> local_state_; |
| 107 FakeProfileStore* profile_store_; |
| 108 FakeKeepAliveService* keep_alive_service_; |
| 109 scoped_ptr<TestingAppListServiceImpl> service_; |
| 110 scoped_ptr<FakeProfile> profile1_; |
| 111 scoped_ptr<FakeProfile> profile2_; |
| 112 }; |
| 113 |
| 114 TEST_F(AppListServiceUnitTest, EnablingStateIsPersisted) { |
| 115 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 116 service_->EnableAppList(profile1_.get()); |
| 117 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 118 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append( |
| 119 local_state_->GetFilePath(prefs::kAppListProfile))); |
| 120 } |
| 121 |
| 122 TEST_F(AppListServiceUnitTest, ShowingForProfileLoadsAProfile) { |
| 123 profile_store_->LoadProfile(profile1_.get()); |
| 124 service_->EnableAppList(profile1_.get()); |
| 125 service_->Show(); |
| 126 EXPECT_EQ(profile1_.get(), service_->showing_for_profile()); |
| 127 EXPECT_TRUE(service_->IsAppListVisible()); |
| 128 } |
| 129 |
| 130 TEST_F(AppListServiceUnitTest, RemovedProfileResetsToInitialProfile) { |
| 131 service_->EnableAppList(profile1_.get()); |
| 132 profile_store_->RemoveProfile(profile1_.get()); |
| 133 base::FilePath initial_profile_path = |
| 134 user_data_dir_.AppendASCII(chrome::kInitialProfile); |
| 135 EXPECT_EQ(initial_profile_path, |
| 136 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 137 } |
| 138 |
| 139 TEST_F(AppListServiceUnitTest, |
| 140 RemovedProfileResetsToLastUsedProfileIfExists) { |
| 141 local_state_->SetString(prefs::kProfileLastUsed, "last-used"); |
| 142 service_->EnableAppList(profile1_.get()); |
| 143 profile_store_->RemoveProfile(profile1_.get()); |
| 144 base::FilePath last_used_profile_path = |
| 145 user_data_dir_.AppendASCII("last-used"); |
| 146 EXPECT_EQ(last_used_profile_path, |
| 147 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 148 } |
| 149 |
| 150 TEST_F(AppListServiceUnitTest, SwitchingProfilesPersists) { |
| 151 profile_store_->LoadProfile(profile1_.get()); |
| 152 profile_store_->LoadProfile(profile2_.get()); |
| 153 service_->EnableAppList(profile1_.get()); |
| 154 service_->SetProfilePath(profile2_->GetPath()); |
| 155 service_->Show(); |
| 156 EXPECT_EQ(profile2_.get(), service_->showing_for_profile()); |
| 157 EXPECT_EQ(profile2_->GetPath(), |
| 158 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 159 service_->SetProfilePath(profile1_->GetPath()); |
| 160 EXPECT_EQ(profile1_->GetPath(), |
| 161 service_->GetProfilePath(profile_store_->GetUserDataDir())); |
| 162 } |
| 163 |
| 164 TEST_F(AppListServiceUnitTest, EnableViaCommandLineFlag) { |
| 165 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 166 command_line.AppendSwitch(switches::kEnableAppList); |
| 167 SetupWithCommandLine(command_line); |
| 168 service_->HandleCommandLineFlags(profile1_.get()); |
| 169 EXPECT_TRUE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 170 } |
| 171 |
| 172 TEST_F(AppListServiceUnitTest, DisableViaCommandLineFlag) { |
| 173 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 174 command_line.AppendSwitch(switches::kDisableAppList); |
| 175 SetupWithCommandLine(command_line); |
| 176 service_->HandleCommandLineFlags(profile1_.get()); |
| 177 EXPECT_FALSE(local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled)); |
| 178 } |
OLD | NEW |