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/apps/scoped_keep_alive.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/app_list/app_list_shower_delegate.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) {}
21 void ShowForProfile(Profile* requested_profile) {
22 CreateViewForProfile(requested_profile);
23 ShowForCurrentProfile();
27 void HandleViewBeingDestroyed() override {
28 AppListShower::HandleViewBeingDestroyed();
33 bool IsAppListVisible() const override { return visible_; }
35 app_list::AppListView* MakeViewForCurrentProfile() override {
40 void UpdateViewForNewProfile() override {}
42 void Show() override { visible_ = true; }
44 void Hide() override { visible_ = false; }
46 bool HasView() const override { return has_view_; }
52 DISALLOW_COPY_AND_ASSIGN(FakeAppListShower);
57 class AppListShowerUnitTest : public testing::Test,
58 public AppListShowerDelegate {
60 AppListShowerUnitTest()
62 views_dismissed_(0) {}
64 void SetUp() override {
65 shower_.reset(new FakeAppListShower(this));
66 profile1_ = CreateProfile("p1").Pass();
67 profile2_ = CreateProfile("p2").Pass();
70 void TearDown() override {}
72 scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
73 return make_scoped_ptr(new FakeProfile(name));
76 // AppListShowerDelegate:
77 AppListViewDelegate* GetViewDelegateForCreate() override { return NULL; }
79 bool HasKeepAlive() const {
80 return shower_->keep_alive_.get() != NULL;
83 void OnViewCreated() override { ++views_created_; }
84 void OnViewDismissed() override { ++views_dismissed_; }
85 void MoveNearCursor(app_list::AppListView* view) override {}
88 scoped_ptr<FakeAppListShower> shower_;
89 scoped_ptr<FakeProfile> profile1_;
90 scoped_ptr<FakeProfile> profile2_;
96 TEST_F(AppListShowerUnitTest, Preconditions) {
97 EXPECT_FALSE(shower_->IsAppListVisible());
98 EXPECT_FALSE(shower_->HasView());
99 EXPECT_FALSE(HasKeepAlive());
102 TEST_F(AppListShowerUnitTest, ShowForProfilePutsViewOnScreen) {
103 shower_->ShowForProfile(profile1_.get());
104 EXPECT_TRUE(shower_->IsAppListVisible());
105 EXPECT_TRUE(shower_->HasView());
106 EXPECT_TRUE(HasKeepAlive());
109 TEST_F(AppListShowerUnitTest, HidingViewRemovesKeepalive) {
110 shower_->ShowForProfile(profile1_.get());
111 shower_->DismissAppList();
112 EXPECT_FALSE(shower_->IsAppListVisible());
113 EXPECT_TRUE(shower_->HasView());
114 EXPECT_FALSE(HasKeepAlive());
117 TEST_F(AppListShowerUnitTest, HideAndShowReusesView) {
118 EXPECT_EQ(0, views_created_);
119 shower_->ShowForProfile(profile1_.get());
120 EXPECT_EQ(1, views_created_);
121 EXPECT_EQ(0, views_dismissed_);
122 shower_->DismissAppList();
123 EXPECT_EQ(1, views_dismissed_);
124 shower_->ShowForProfile(profile1_.get());
125 EXPECT_EQ(1, views_created_);
128 TEST_F(AppListShowerUnitTest, CloseAndShowRecreatesView) {
129 shower_->ShowForProfile(profile1_.get());
130 shower_->HandleViewBeingDestroyed();
131 // Destroying implies hiding. A separate notification shouldn't go out.
132 EXPECT_EQ(0, views_dismissed_);
133 shower_->ShowForProfile(profile1_.get());
134 EXPECT_EQ(2, views_created_);
137 TEST_F(AppListShowerUnitTest, CloseRemovesView) {
138 shower_->ShowForProfile(profile1_.get());
139 shower_->HandleViewBeingDestroyed();
140 EXPECT_FALSE(shower_->IsAppListVisible());
141 EXPECT_FALSE(shower_->HasView());
142 EXPECT_FALSE(HasKeepAlive());
145 TEST_F(AppListShowerUnitTest, CloseAppListClearsProfile) {
146 EXPECT_EQ(NULL, shower_->profile());
147 shower_->ShowForProfile(profile1_.get());
148 EXPECT_EQ(profile1_.get(), shower_->profile());
149 shower_->HandleViewBeingDestroyed();
150 EXPECT_EQ(NULL, shower_->profile());
153 TEST_F(AppListShowerUnitTest, SwitchingProfiles) {
154 shower_->ShowForProfile(profile1_.get());
155 EXPECT_EQ("p1", shower_->profile()->GetProfileName());
156 shower_->ShowForProfile(profile2_.get());
157 EXPECT_EQ("p2", shower_->profile()->GetProfileName());
159 // Shouldn't create new view for second profile - it should switch in place.
160 EXPECT_EQ(1, views_created_);
161 EXPECT_EQ(0, views_dismissed_);