1 // Copyright 2014 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 "chrome/browser/ui/app_list/app_list_shower_views.h"
7 #include "base/files/file_path.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/app_list/app_list_shower_delegate.h"
10 #include "chrome/browser/ui/app_list/scoped_keep_alive.h"
11 #include "chrome/browser/ui/app_list/test/fake_profile.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class FakeAppListShower : public AppListShower {
18 explicit FakeAppListShower(AppListShowerDelegate* delegate)
19 : AppListShower(delegate), has_view_(false), visible_(false) {}
22 virtual void HandleViewBeingDestroyed() OVERRIDE {
23 AppListShower::HandleViewBeingDestroyed();
28 virtual bool IsAppListVisible() const OVERRIDE { return visible_; }
30 virtual app_list::AppListView* MakeViewForCurrentProfile() OVERRIDE {
35 virtual void UpdateViewForNewProfile() OVERRIDE {}
37 virtual void Show() OVERRIDE {
41 virtual void Hide() OVERRIDE {
45 virtual bool HasView() const OVERRIDE {
53 DISALLOW_COPY_AND_ASSIGN(FakeAppListShower);
58 class AppListShowerUnitTest : public testing::Test,
59 public AppListShowerDelegate {
61 AppListShowerUnitTest()
63 views_dismissed_(0) {}
65 virtual void SetUp() OVERRIDE {
66 shower_.reset(new FakeAppListShower(this));
67 profile1_ = CreateProfile("p1").Pass();
68 profile2_ = CreateProfile("p2").Pass();
71 virtual void TearDown() OVERRIDE {
74 scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
75 return make_scoped_ptr(new FakeProfile(name));
78 // AppListCreatorDelegate:
79 virtual AppListControllerDelegate* GetControllerDelegateForCreate() OVERRIDE {
83 bool HasKeepAlive() const {
84 return shower_->keep_alive_.get() != NULL;
87 virtual void OnViewCreated() OVERRIDE { ++views_created_; }
88 virtual void OnViewDismissed() OVERRIDE { ++views_dismissed_; }
89 virtual void MoveNearCursor(app_list::AppListView* view) OVERRIDE {}
92 scoped_ptr<FakeAppListShower> shower_;
93 scoped_ptr<FakeProfile> profile1_;
94 scoped_ptr<FakeProfile> profile2_;
100 TEST_F(AppListShowerUnitTest, Preconditions) {
101 EXPECT_FALSE(shower_->IsAppListVisible());
102 EXPECT_FALSE(shower_->HasView());
103 EXPECT_FALSE(HasKeepAlive());
106 TEST_F(AppListShowerUnitTest, ShowForProfilePutsViewOnScreen) {
107 shower_->ShowForProfile(profile1_.get());
108 EXPECT_TRUE(shower_->IsAppListVisible());
109 EXPECT_TRUE(shower_->HasView());
110 EXPECT_TRUE(HasKeepAlive());
113 TEST_F(AppListShowerUnitTest, HidingViewRemovesKeepalive) {
114 shower_->ShowForProfile(profile1_.get());
115 shower_->DismissAppList();
116 EXPECT_FALSE(shower_->IsAppListVisible());
117 EXPECT_TRUE(shower_->HasView());
118 EXPECT_FALSE(HasKeepAlive());
121 TEST_F(AppListShowerUnitTest, HideAndShowReusesView) {
122 EXPECT_EQ(0, views_created_);
123 shower_->ShowForProfile(profile1_.get());
124 EXPECT_EQ(1, views_created_);
125 EXPECT_EQ(0, views_dismissed_);
126 shower_->DismissAppList();
127 EXPECT_EQ(1, views_dismissed_);
128 shower_->ShowForProfile(profile1_.get());
129 EXPECT_EQ(1, views_created_);
132 TEST_F(AppListShowerUnitTest, CloseAndShowRecreatesView) {
133 shower_->ShowForProfile(profile1_.get());
134 shower_->HandleViewBeingDestroyed();
135 // Destroying implies hiding. A separate notification shouldn't go out.
136 EXPECT_EQ(0, views_dismissed_);
137 shower_->ShowForProfile(profile1_.get());
138 EXPECT_EQ(2, views_created_);
141 TEST_F(AppListShowerUnitTest, CloseRemovesView) {
142 shower_->ShowForProfile(profile1_.get());
143 shower_->HandleViewBeingDestroyed();
144 EXPECT_FALSE(shower_->IsAppListVisible());
145 EXPECT_FALSE(shower_->HasView());
146 EXPECT_FALSE(HasKeepAlive());
149 TEST_F(AppListShowerUnitTest, CloseAppListClearsProfile) {
150 EXPECT_EQ(NULL, shower_->profile());
151 shower_->ShowForProfile(profile1_.get());
152 EXPECT_EQ(profile1_.get(), shower_->profile());
153 shower_->HandleViewBeingDestroyed();
154 EXPECT_EQ(NULL, shower_->profile());
157 TEST_F(AppListShowerUnitTest, SwitchingProfiles) {
158 shower_->ShowForProfile(profile1_.get());
159 EXPECT_EQ("p1", shower_->profile()->GetProfileName());
160 shower_->ShowForProfile(profile2_.get());
161 EXPECT_EQ("p2", shower_->profile()->GetProfileName());
163 // Shouldn't create new view for second profile - it should switch in place.
164 EXPECT_EQ(1, views_created_);
165 EXPECT_EQ(0, views_dismissed_);