Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / app_list_service_unittest.cc
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_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"
22
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) {}
30
31   Profile* showing_for_profile() const {
32     return showing_for_profile_;
33   }
34
35   void PerformStartupChecks(Profile* profile) {
36     AppListServiceImpl::PerformStartupChecks(profile);
37   }
38
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;
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     RecordAppListLaunch();
51   }
52
53   virtual void DismissAppList() OVERRIDE {
54     showing_for_profile_ = NULL;
55   }
56
57   virtual bool IsAppListVisible() const OVERRIDE {
58     return !!showing_for_profile_;
59   }
60
61   virtual gfx::NativeWindow GetAppListWindow() OVERRIDE {
62     return NULL;
63   }
64
65   virtual AppListControllerDelegate* GetControllerDelegate() OVERRIDE {
66     return NULL;
67   }
68
69  private:
70   Profile* showing_for_profile_;
71 };
72
73 class AppListServiceUnitTest : public testing::Test {
74  public:
75   AppListServiceUnitTest() {}
76
77   virtual void SetUp() OVERRIDE {
78     SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM));
79   }
80
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;
89
90     AppListService::RegisterPrefs(pref_registry);
91     profiles::RegisterPrefs(pref_registry);
92
93     base::PrefServiceFactory factory;
94     factory.set_user_prefs(make_scoped_refptr(new TestingPrefStore));
95     local_state_ = factory.Create(pref_registry).Pass();
96
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   }
103
104   void EnableAppList() {
105     service_->EnableAppList(profile1_.get(),
106                             AppListService::ENABLE_VIA_COMMAND_LINE);
107   }
108
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_;
115
116   DISALLOW_COPY_AND_ASSIGN(AppListServiceUnitTest);
117 };
118
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)));
125 }
126
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());
133 }
134
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()));
142 }
143
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()));
153 }
154
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()));
167 }
168
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));
175 }
176
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));
183 }
184
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));
190
191   service_->EnableAppList(profile1_.get(),
192                           AppListService::ENABLE_FOR_APP_INSTALL);
193
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));
199
200   service_->ShowForProfile(profile1_.get());
201
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));
206
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));
213
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));
218
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);
223
224   EXPECT_EQ(AppListService::ENABLE_FOR_APP_INSTALL,
225             local_state_->GetInteger(prefs::kAppListEnableMethod));
226   EXPECT_NE(0, local_state_->GetInt64(prefs::kAppListEnableTime));
227
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));
233 }