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 RegainNextLostFocus() OVERRIDE {
48 virtual gfx::NativeWindow GetWindow() OVERRIDE {
52 virtual void SetProfile(Profile* profile) OVERRIDE {
61 class FakeFactory : public AppListFactory {
67 virtual AppList* CreateAppList(
69 const base::Closure& on_should_dismiss) OVERRIDE {
71 return new FakeAppList(profile);
77 class AppListShowerUnitTest : public testing::Test {
79 virtual void SetUp() OVERRIDE {
80 keep_alive_service_ = new FakeKeepAliveService;
81 factory_ = new FakeFactory;
83 new AppListShower(scoped_ptr<AppListFactory>(factory_),
84 scoped_ptr<KeepAliveService>(keep_alive_service_)));
85 profile1_ = CreateProfile("p1").Pass();
86 profile2_ = CreateProfile("p2").Pass();
89 virtual void TearDown() OVERRIDE {
92 scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
93 return make_scoped_ptr(new FakeProfile(name));
96 FakeAppList* GetCurrentAppList() {
97 return static_cast<FakeAppList*>(shower_->app_list());
100 // Owned by |shower_|.
101 FakeKeepAliveService* keep_alive_service_;
102 // Owned by |shower_|.
103 FakeFactory* factory_;
104 scoped_ptr<AppListShower> shower_;
105 scoped_ptr<FakeProfile> profile1_;
106 scoped_ptr<FakeProfile> profile2_;
109 TEST_F(AppListShowerUnitTest, Preconditions) {
110 EXPECT_FALSE(shower_->IsAppListVisible());
111 EXPECT_FALSE(shower_->HasView());
112 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
115 TEST_F(AppListShowerUnitTest, ShowForProfilePutsViewOnScreen) {
116 shower_->ShowForProfile(profile1_.get());
117 EXPECT_TRUE(shower_->IsAppListVisible());
118 EXPECT_TRUE(shower_->HasView());
119 EXPECT_TRUE(keep_alive_service_->is_keeping_alive());
122 TEST_F(AppListShowerUnitTest, HidingViewRemovesKeepalive) {
123 shower_->ShowForProfile(profile1_.get());
124 shower_->DismissAppList();
125 EXPECT_FALSE(shower_->IsAppListVisible());
126 EXPECT_TRUE(shower_->HasView());
127 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
130 TEST_F(AppListShowerUnitTest, HideAndShowReusesView) {
131 shower_->ShowForProfile(profile1_.get());
132 shower_->DismissAppList();
133 shower_->ShowForProfile(profile1_.get());
134 EXPECT_EQ(1, factory_->views_created_);
137 TEST_F(AppListShowerUnitTest, CloseAndShowRecreatesView) {
138 shower_->ShowForProfile(profile1_.get());
139 shower_->CloseAppList();
140 shower_->ShowForProfile(profile1_.get());
141 EXPECT_EQ(2, factory_->views_created_);
144 TEST_F(AppListShowerUnitTest, CloseRemovesView) {
145 shower_->ShowForProfile(profile1_.get());
146 shower_->CloseAppList();
147 EXPECT_FALSE(shower_->IsAppListVisible());
148 EXPECT_FALSE(shower_->HasView());
149 EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
152 TEST_F(AppListShowerUnitTest, CloseAppListClearsProfile) {
153 EXPECT_EQ(NULL, shower_->profile());
154 shower_->ShowForProfile(profile1_.get());
155 EXPECT_EQ(profile1_.get(), shower_->profile());
156 shower_->CloseAppList();
157 EXPECT_EQ(NULL, shower_->profile());
160 TEST_F(AppListShowerUnitTest, SwitchingProfiles) {
161 shower_->ShowForProfile(profile1_.get());
162 EXPECT_EQ("p1", GetCurrentAppList()->profile_name());
163 shower_->ShowForProfile(profile2_.get());
164 EXPECT_EQ("p2", GetCurrentAppList()->profile_name());
166 // Shouldn't create new view for second profile - it should switch in place.
167 EXPECT_EQ(1, factory_->views_created_);