Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(601)

Side by Side Diff: chrome/browser/ui/app_list/app_list_service_unittest.cc

Issue 24707002: Add unit tests for AppListServiceImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respond to comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 virtual void CreateForProfile(Profile* requested_profile) OVERRIDE {
44 }
45
46 virtual void ShowForProfile(Profile* requested_profile) OVERRIDE {
47 showing_for_profile_ = requested_profile;
48 }
49
50 virtual void DismissAppList() OVERRIDE {
51 showing_for_profile_ = NULL;
52 }
53
54 virtual bool IsAppListVisible() const OVERRIDE {
55 return !!showing_for_profile_;
56 }
57
58 virtual gfx::NativeWindow GetAppListWindow() OVERRIDE {
59 return NULL;
60 }
61
62 virtual AppListControllerDelegate* CreateControllerDelegate() OVERRIDE {
63 return NULL;
64 }
65
66 private:
67 Profile* showing_for_profile_;
68 };
69
70 class AppListControllerUnitTest : public testing::Test {
71 public:
72 virtual void SetUp() OVERRIDE {
73 SetupWithCommandLine(CommandLine(CommandLine::NO_PROGRAM));
74 }
75
76 void SetupWithCommandLine(const CommandLine& command_line) {
77 user_data_dir_ = base::FilePath(FILE_PATH_LITERAL("udd"));
78 profile1_.reset(
79 new FakeProfile("p1", user_data_dir_.AppendASCII("profile1")));
80 profile2_.reset(
81 new FakeProfile("p2", user_data_dir_.AppendASCII("profile2")));
82 PrefRegistrySimple* pref_registry = new PrefRegistrySimple;
83
84 AppListService::RegisterPrefs(pref_registry);
85 apps::RegisterPrefs(pref_registry);
86 profiles::RegisterPrefs(pref_registry);
87
88 PrefServiceBuilder builder;
89 builder.WithUserPrefs(new TestingPrefStore);
90 local_state_.reset(builder.Create(pref_registry));
91
92 keep_alive_service_ = new FakeKeepAliveService;
93 profile_store_ = new FakeProfileStore(user_data_dir_);
94 service_.reset(new TestingAppListServiceImpl(
95 command_line,
96 local_state_.get(),
97 make_scoped_ptr(profile_store_),
98 make_scoped_ptr(keep_alive_service_)));
99 }
100
101 base::FilePath user_data_dir_;
102 scoped_ptr<PrefService> local_state_;
103 FakeProfileStore* profile_store_;
104 FakeKeepAliveService* keep_alive_service_;
105 scoped_ptr<TestingAppListServiceImpl> service_;
106 scoped_ptr<FakeProfile> profile1_;
107 scoped_ptr<FakeProfile> profile2_;
108 };
109
110 TEST_F(AppListControllerUnitTest, EnablingStateIsPersisted) {
111 EXPECT_FALSE(local_state_->GetBoolean(
112 apps::prefs::kAppLauncherHasBeenEnabled));
113 service_->EnableAppList(profile1_.get());
114 EXPECT_TRUE(local_state_->GetBoolean(
115 apps::prefs::kAppLauncherHasBeenEnabled));
116 EXPECT_EQ(profile1_->GetPath(), user_data_dir_.Append(
117 local_state_->GetFilePath(prefs::kAppListProfile)));
118 }
119
120 TEST_F(AppListControllerUnitTest, ShowingForProfileLoadsAProfile) {
121 profile_store_->LoadProfile(profile1_.get());
122 service_->EnableAppList(profile1_.get());
123 service_->Show();
124 EXPECT_EQ(profile1_.get(), service_->showing_for_profile());
125 EXPECT_TRUE(service_->IsAppListVisible());
126 }
127
128 TEST_F(AppListControllerUnitTest, RemovedProfileResetsToInitialProfile) {
129 service_->EnableAppList(profile1_.get());
130 profile_store_->RemoveProfile(profile1_.get());
131 base::FilePath initial_profile_path =
132 user_data_dir_.AppendASCII(chrome::kInitialProfile);
133 EXPECT_EQ(initial_profile_path,
134 service_->GetProfilePath(profile_store_->GetUserDataDir()));
135 }
136
137 TEST_F(AppListControllerUnitTest,
138 RemovedProfileResetsToLastUsedProfileIfExists) {
139 local_state_->SetString(prefs::kProfileLastUsed, "last-used");
140 service_->EnableAppList(profile1_.get());
141 profile_store_->RemoveProfile(profile1_.get());
142 base::FilePath last_used_profile_path =
143 user_data_dir_.AppendASCII("last-used");
144 EXPECT_EQ(last_used_profile_path,
145 service_->GetProfilePath(profile_store_->GetUserDataDir()));
146 }
147
148 TEST_F(AppListControllerUnitTest, SwitchingProfilesPersists) {
149 profile_store_->LoadProfile(profile1_.get());
150 profile_store_->LoadProfile(profile2_.get());
151 service_->EnableAppList(profile1_.get());
152 service_->SetProfilePath(profile2_->GetPath());
153 service_->Show();
154 EXPECT_EQ(profile2_.get(), service_->showing_for_profile());
155 EXPECT_EQ(profile2_->GetPath(),
156 service_->GetProfilePath(profile_store_->GetUserDataDir()));
157 service_->SetProfilePath(profile1_->GetPath());
158 EXPECT_EQ(profile1_->GetPath(),
159 service_->GetProfilePath(profile_store_->GetUserDataDir()));
160 }
161
162 TEST_F(AppListControllerUnitTest, EnableViaCommandLineFlag) {
163 CommandLine command_line(CommandLine::NO_PROGRAM);
164 command_line.AppendSwitch(switches::kEnableAppList);
165 SetupWithCommandLine(command_line);
166 service_->HandleCommandLineFlags(profile1_.get());
167 EXPECT_TRUE(local_state_->GetBoolean(
168 apps::prefs::kAppLauncherHasBeenEnabled));
169 }
170
171 TEST_F(AppListControllerUnitTest, DisableViaCommandLineFlag) {
172 CommandLine command_line(CommandLine::NO_PROGRAM);
173 command_line.AppendSwitch(switches::kDisableAppList);
174 SetupWithCommandLine(command_line);
175 service_->HandleCommandLineFlags(profile1_.get());
176 EXPECT_FALSE(local_state_->GetBoolean(
177 apps::prefs::kAppLauncherHasBeenEnabled));
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698