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