Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / app_list_shower_views_unittest.cc
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.
4
5 #include "chrome/browser/ui/app_list/app_list_shower_views.h"
6
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"
13
14 namespace {
15
16 class FakeAppListShower : public AppListShower {
17  public:
18   explicit FakeAppListShower(AppListShowerDelegate* delegate)
19       : AppListShower(delegate), has_view_(false), visible_(false) {}
20
21   void ShowForProfile(Profile* requested_profile) {
22     CreateViewForProfile(requested_profile);
23     ShowForCurrentProfile();
24   }
25
26   // AppListShower:
27   void HandleViewBeingDestroyed() override {
28     AppListShower::HandleViewBeingDestroyed();
29     has_view_ = false;
30     visible_ = false;
31   }
32
33   bool IsAppListVisible() const override { return visible_; }
34
35   app_list::AppListView* MakeViewForCurrentProfile() override {
36     has_view_ = true;
37     return NULL;
38   }
39
40   void UpdateViewForNewProfile() override {}
41
42   void Show() override { visible_ = true; }
43
44   void Hide() override { visible_ = false; }
45
46   bool HasView() const override { return has_view_; }
47
48  private:
49   bool has_view_;
50   bool visible_;
51
52   DISALLOW_COPY_AND_ASSIGN(FakeAppListShower);
53 };
54
55 }  // namespace
56
57 class AppListShowerUnitTest : public testing::Test,
58                               public AppListShowerDelegate {
59  public:
60   AppListShowerUnitTest()
61       : views_created_(0),
62         views_dismissed_(0) {}
63
64   void SetUp() override {
65     shower_.reset(new FakeAppListShower(this));
66     profile1_ = CreateProfile("p1").Pass();
67     profile2_ = CreateProfile("p2").Pass();
68   }
69
70   void TearDown() override {}
71
72   scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
73     return make_scoped_ptr(new FakeProfile(name));
74   }
75
76   // AppListShowerDelegate:
77   AppListViewDelegate* GetViewDelegateForCreate() override { return NULL; }
78
79   bool HasKeepAlive() const {
80     return shower_->keep_alive_.get() != NULL;
81   }
82
83   void OnViewCreated() override { ++views_created_; }
84   void OnViewDismissed() override { ++views_dismissed_; }
85   void MoveNearCursor(app_list::AppListView* view) override {}
86
87  protected:
88   scoped_ptr<FakeAppListShower> shower_;
89   scoped_ptr<FakeProfile> profile1_;
90   scoped_ptr<FakeProfile> profile2_;
91
92   int views_created_;
93   int views_dismissed_;
94 };
95
96 TEST_F(AppListShowerUnitTest, Preconditions) {
97   EXPECT_FALSE(shower_->IsAppListVisible());
98   EXPECT_FALSE(shower_->HasView());
99   EXPECT_FALSE(HasKeepAlive());
100 }
101
102 TEST_F(AppListShowerUnitTest, ShowForProfilePutsViewOnScreen) {
103   shower_->ShowForProfile(profile1_.get());
104   EXPECT_TRUE(shower_->IsAppListVisible());
105   EXPECT_TRUE(shower_->HasView());
106   EXPECT_TRUE(HasKeepAlive());
107 }
108
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());
115 }
116
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_);
126 }
127
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_);
135 }
136
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());
143 }
144
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());
151 }
152
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());
158
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_);
162 }