Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / test / app_list_shower_unittest.cc
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.
4
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"
14
15 class FakeAppList : public AppList {
16  public:
17   explicit FakeAppList(Profile* profile)
18       : profile_(profile) {
19   }
20
21   std::string profile_name() {
22     return profile_->GetProfileName();
23   }
24
25   // AppList overrides.
26   virtual void Show() OVERRIDE {
27     visible_ = true;
28   }
29
30   virtual void Hide() OVERRIDE {
31     visible_ = false;
32   }
33
34   virtual void MoveNearCursor() OVERRIDE {
35   }
36
37   virtual bool IsVisible() OVERRIDE {
38     return visible_;
39   }
40
41   virtual void Prerender() OVERRIDE {
42     prerendered_ = true;
43   }
44
45   virtual void ReactivateOnNextFocusLoss() OVERRIDE {
46   }
47
48   virtual gfx::NativeWindow GetWindow() OVERRIDE {
49     return NULL;
50   }
51
52   virtual void SetProfile(Profile* profile) OVERRIDE {
53     profile_ = profile;
54   }
55
56   Profile* profile_;
57   bool visible_;
58   bool prerendered_;
59 };
60
61 class FakeFactory : public AppListFactory {
62  public:
63   FakeFactory()
64       : views_created_(0) {
65   }
66
67   virtual AppList* CreateAppList(
68       Profile* profile,
69       AppListService* service,
70       const base::Closure& on_should_dismiss) OVERRIDE {
71     views_created_++;
72     return new FakeAppList(profile);
73   }
74
75   int views_created_;
76 };
77
78 class AppListShowerUnitTest : public testing::Test {
79  public:
80   virtual void SetUp() OVERRIDE {
81     keep_alive_service_ = new FakeKeepAliveService;
82     factory_ = new FakeFactory;
83     shower_.reset(
84         new AppListShower(scoped_ptr<AppListFactory>(factory_),
85                           scoped_ptr<KeepAliveService>(keep_alive_service_),
86                           NULL /* service */));
87     profile1_ = CreateProfile("p1").Pass();
88     profile2_ = CreateProfile("p2").Pass();
89   }
90
91   virtual void TearDown() OVERRIDE {
92   }
93
94   scoped_ptr<FakeProfile> CreateProfile(const std::string& name) {
95     return make_scoped_ptr(new FakeProfile(name));
96   }
97
98   FakeAppList* GetCurrentAppList() {
99     return static_cast<FakeAppList*>(shower_->app_list());
100   }
101
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_;
109 };
110
111 TEST_F(AppListShowerUnitTest, Preconditions) {
112   EXPECT_FALSE(shower_->IsAppListVisible());
113   EXPECT_FALSE(shower_->HasView());
114   EXPECT_FALSE(keep_alive_service_->is_keeping_alive());
115 }
116
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());
122 }
123
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());
130 }
131
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_);
137 }
138
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_);
144 }
145
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());
152 }
153
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());
160 }
161
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());
167
168   // Shouldn't create new view for second profile - it should switch in place.
169   EXPECT_EQ(1, factory_->views_created_);
170 }