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