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.
5 #include "base/files/file_path.h"
6 #include "chrome/browser/profiles/profile.h"
7 #include "chrome/browser/ui/app_list/app_list.h"
8 #include "chrome/browser/ui/app_list/app_list_factory.h"
9 #include "chrome/browser/ui/app_list/app_list_shower.h"
10 #include "chrome/browser/ui/app_list/keep_alive_service.h"
11 #include "chrome/browser/ui/app_list/test/fake_keep_alive_service.h"
12 #include "chrome/browser/ui/app_list/test/fake_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 class FakeAppList : public AppList {
17 explicit FakeAppList(Profile* profile)
21 std::string profile_name() {
22 return profile_->GetProfileName();
26 virtual void Show() OVERRIDE {
30 virtual void Hide() OVERRIDE {
34 virtual void MoveNearCursor() OVERRIDE {
37 virtual bool IsVisible() OVERRIDE {
41 virtual void Prerender() OVERRIDE {
45 virtual void ReactivateOnNextFocusLoss() OVERRIDE {
48 virtual gfx::NativeWindow GetWindow() OVERRIDE {
52 virtual void SetProfile(Profile* profile) OVERRIDE {
61 class FakeFactory : public AppListFactory {
67 virtual AppList* CreateAppList(
69 AppListService* service,
70 const base::Closure& on_should_dismiss) OVERRIDE {
72 return new FakeAppList(profile);
78 class AppListShowerUnitTest : public testing::Test {
80 virtual void SetUp() OVERRIDE {
81 keep_alive_service_ = new FakeKeepAliveService;
82 factory_ = new FakeFactory;
84 new AppListShower(scoped_ptr<AppListFactory>(factory_),
85 scoped_ptr<KeepAliveService>(keep_alive_service_),
87 profile1_ = CreateProfile("p1").Pass();
88 profile2_ = CreateProfile("p2").Pass();
91 virtual void TearDown() OVERRIDE {
94 scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
95 return make_scoped_ptr(new FakeProfile(name));
98 FakeAppList* GetCurrentAppList() {
99 return static_cast<FakeAppList*>(shower_->app_list());
102 // Owned by |shower_|.
103 FakeKeepAliveService* keep_alive_service_;
104 // Owned by |shower_|.
105 FakeFactory* factory_;
106 scoped_ptr<AppListShower> shower_;
107 scoped_ptr<FakeProfile> profile1_;
108 scoped_ptr<FakeProfile> profile2_;
111 TEST_F(AppListShowerUnitTest, Preconditions) {
112 EXPECT_FALSE(shower_->IsAppListVisible());
113 EXPECT_FALSE(shower_->HasView());
114 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
117 TEST_F(AppListShowerUnitTest, ShowForProfilePutsViewOnScreen) {
118 shower_->ShowForProfile(profile1_.get());
119 EXPECT_TRUE(shower_->IsAppListVisible());
120 EXPECT_TRUE(shower_->HasView());
121 EXPECT_TRUE(keep_alive_service_->is_keeping_alive());
124 TEST_F(AppListShowerUnitTest, HidingViewRemovesKeepalive) {
125 shower_->ShowForProfile(profile1_.get());
126 shower_->DismissAppList();
127 EXPECT_FALSE(shower_->IsAppListVisible());
128 EXPECT_TRUE(shower_->HasView());
129 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
132 TEST_F(AppListShowerUnitTest, HideAndShowReusesView) {
133 shower_->ShowForProfile(profile1_.get());
134 shower_->DismissAppList();
135 shower_->ShowForProfile(profile1_.get());
136 EXPECT_EQ(1, factory_->views_created_);
139 TEST_F(AppListShowerUnitTest, CloseAndShowRecreatesView) {
140 shower_->ShowForProfile(profile1_.get());
141 shower_->CloseAppList();
142 shower_->ShowForProfile(profile1_.get());
143 EXPECT_EQ(2, factory_->views_created_);
146 TEST_F(AppListShowerUnitTest, CloseRemovesView) {
147 shower_->ShowForProfile(profile1_.get());
148 shower_->CloseAppList();
149 EXPECT_FALSE(shower_->IsAppListVisible());
150 EXPECT_FALSE(shower_->HasView());
151 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
154 TEST_F(AppListShowerUnitTest, CloseAppListClearsProfile) {
155 EXPECT_EQ(NULL, shower_->profile());
156 shower_->ShowForProfile(profile1_.get());
157 EXPECT_EQ(profile1_.get(), shower_->profile());
158 shower_->CloseAppList();
159 EXPECT_EQ(NULL, shower_->profile());
162 TEST_F(AppListShowerUnitTest, SwitchingProfiles) {
163 shower_->ShowForProfile(profile1_.get());
164 EXPECT_EQ("p1", GetCurrentAppList()->profile_name());
165 shower_->ShowForProfile(profile2_.get());
166 EXPECT_EQ("p2", GetCurrentAppList()->profile_name());
168 // Shouldn't create new view for second profile - it should switch in place.
169 EXPECT_EQ(1, factory_->views_created_);