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 "athena/home/athena_start_page_view.h"
7 #include "athena/home/home_card_constants.h"
8 #include "athena/test/base/athena_test_base.h"
9 #include "base/format_macros.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/app_list/app_list_switches.h"
13 #include "ui/app_list/search_box_model.h"
14 #include "ui/app_list/test/app_list_test_model.h"
15 #include "ui/app_list/test/app_list_test_view_delegate.h"
16 #include "ui/app_list/views/search_box_view.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/views/controls/textfield/textfield.h"
22 class AthenaTestViewDelegate
: public app_list::test::AppListTestViewDelegate
{
24 AthenaTestViewDelegate() {}
25 ~AthenaTestViewDelegate() override
{}
28 // app_list::AppListViewDelegate:
29 views::View
* CreateStartPageWebView(const gfx::Size
& size
) override
{
30 return new views::View();
33 DISALLOW_COPY_AND_ASSIGN(AthenaTestViewDelegate
);
36 class AthenaStartPageViewTest
: public test::AthenaTestBase
{
38 AthenaStartPageViewTest() {}
39 ~AthenaStartPageViewTest() override
{}
42 void SetUp() override
{
43 test::AthenaTestBase::SetUp();
44 for (size_t i
= 0; i
< GetMaxIconNum(); ++i
)
47 view_
.reset(new AthenaStartPageView(&view_delegate_
));
48 SetSize(gfx::Size(1280, 800));
50 void TearDown() override
{
52 test::AthenaTestBase::TearDown();
56 void SetSize(const gfx::Size
& new_size
) {
57 view_
->SetSize(new_size
);
61 void AddTestItem(size_t index
) {
62 app_list::test::AppListTestModel
* model
= view_delegate_
.GetTestModel();
63 model
->AddItem(new app_list::test::AppListTestModel::AppListTestItem(
64 GetAppIdFor(index
), model
));
67 static size_t GetMaxIconNum() {
68 return AthenaStartPageView::GetMaxIconNumForTest();
71 static std::string
GetAppIdFor(size_t index
) {
72 return base::StringPrintf("id-%" PRIuS
, index
);
75 app_list::AppListModel
* GetModel() { return view_delegate_
.GetTestModel(); }
77 views::View
* GetIconsContainer() { return view_
->app_icon_container_
; }
79 gfx::Rect
GetIconsBounds() const {
80 return view_
->app_icon_container_
->layer()->GetTargetBounds();
83 gfx::Rect
GetControlBounds() const {
84 return view_
->control_icon_container_
->layer()->GetTargetBounds();
87 gfx::Rect
GetSearchBoxBounds() const {
88 return view_
->search_box_container_
->layer()->GetTargetBounds();
91 gfx::Rect
GetLogoBounds() const {
92 return view_
->logo_
->layer()->GetTargetBounds();
95 bool IsLogoVisible() const {
96 return view_
->logo_
->layer()->GetTargetOpacity() > 0 &&
97 view_
->logo_
->layer()->GetTargetVisibility();
100 gfx::Size
GetSearchBoxPreferredSize() {
101 return view_
->search_box_container_
->GetPreferredSize();
104 void SetSearchQuery(const base::string16
& query
) {
105 view_delegate_
.GetModel()->search_box()->SetText(query
);
108 base::string16
GetVisibleQuery() {
109 return view_
->search_box_view_
->search_box()->text();
112 float layout_state() { return view_
->layout_state_
; }
114 scoped_ptr
<AthenaStartPageView
> view_
;
117 AthenaTestViewDelegate view_delegate_
;
119 DISALLOW_COPY_AND_ASSIGN(AthenaStartPageViewTest
);
122 TEST_F(AthenaStartPageViewTest
, BasicLayout
) {
123 // BOTTOM state. logo is invisible. icons, search box, and controls are
124 // arranged horizontally.
125 EXPECT_FALSE(IsLogoVisible());
127 // Three components are aligned at the middle point.
128 EXPECT_NEAR(GetIconsBounds().CenterPoint().y(),
129 GetControlBounds().CenterPoint().y(),
131 EXPECT_NEAR(GetIconsBounds().CenterPoint().y(),
132 GetSearchBoxBounds().CenterPoint().y(),
134 EXPECT_NEAR(GetControlBounds().CenterPoint().y(),
135 GetSearchBoxBounds().CenterPoint().y(),
138 // Horizonttaly aligned in the order of icons, search_box, and controls.
139 EXPECT_LE(GetIconsBounds().right(), GetSearchBoxBounds().x());
140 EXPECT_LE(GetSearchBoxBounds().right(), GetControlBounds().x());
141 EXPECT_LE(0, GetIconsBounds().y());
143 // Search box should appear in the middle.
144 EXPECT_NEAR(GetSearchBoxBounds().CenterPoint().x(),
145 view_
->bounds().CenterPoint().x(),
148 // Should fit inside of the home card height.
149 EXPECT_GE(kHomeCardHeight
, GetIconsBounds().height());
150 EXPECT_GE(kHomeCardHeight
, GetSearchBoxBounds().height());
151 EXPECT_GE(kHomeCardHeight
, GetControlBounds().height());
152 EXPECT_EQ(GetSearchBoxPreferredSize().ToString(),
153 GetSearchBoxBounds().size().ToString());
155 // CENTERED state. logo is visible. search box appears below the logo,
156 // icons and controls are arranged horizontally and below the search box.
157 view_
->SetLayoutState(1.0f
);
158 EXPECT_TRUE(IsLogoVisible());
159 EXPECT_NEAR(GetLogoBounds().x() + GetLogoBounds().width() / 2,
160 GetSearchBoxBounds().x() + GetSearchBoxBounds().width() / 2,
162 EXPECT_LE(GetLogoBounds().bottom(), GetSearchBoxBounds().y());
163 EXPECT_EQ(GetIconsBounds().y(), GetControlBounds().y());
164 EXPECT_LE(GetIconsBounds().right(), GetControlBounds().x());
165 EXPECT_LE(GetSearchBoxBounds().bottom(), GetIconsBounds().y());
168 TEST_F(AthenaStartPageViewTest
, NarrowLayout
) {
169 SetSize(gfx::Size(800, 1280));
171 // BOTTOM state. Similar to BasicLayout.
172 EXPECT_FALSE(IsLogoVisible());
173 // Three components are aligned at the middle point.
174 EXPECT_NEAR(GetIconsBounds().CenterPoint().y(),
175 GetControlBounds().CenterPoint().y(),
177 EXPECT_NEAR(GetIconsBounds().CenterPoint().y(),
178 GetSearchBoxBounds().CenterPoint().y(),
180 EXPECT_NEAR(GetControlBounds().CenterPoint().y(),
181 GetSearchBoxBounds().CenterPoint().y(),
184 // Horizonttaly aligned in the order of icons, search_box, and controls.
185 EXPECT_LE(GetIconsBounds().right(), GetSearchBoxBounds().x());
186 EXPECT_LE(GetSearchBoxBounds().right(), GetControlBounds().x());
187 EXPECT_LE(0, GetIconsBounds().y());
189 // Search box should appear in the middle.
190 EXPECT_NEAR(GetSearchBoxBounds().CenterPoint().x(),
191 view_
->bounds().CenterPoint().x(),
194 // Should fit inside of the home card height.
195 EXPECT_GE(kHomeCardHeight
, GetIconsBounds().height());
196 EXPECT_GE(kHomeCardHeight
, GetSearchBoxBounds().height());
197 EXPECT_GE(kHomeCardHeight
, GetControlBounds().height());
199 // Search box is narrower because of the size is too narrow.
200 EXPECT_GT(GetSearchBoxPreferredSize().width(), GetSearchBoxBounds().width());
201 EXPECT_EQ(GetSearchBoxPreferredSize().height(),
202 GetSearchBoxBounds().height());
204 // CENTERED state. Search box should be back to the preferred size.
205 view_
->SetLayoutState(1.0f
);
206 EXPECT_EQ(GetSearchBoxPreferredSize().ToString(),
207 GetSearchBoxBounds().size().ToString());
209 // Back to BOTTOM state, the search box shrinks again.
210 view_
->SetLayoutState(0.0f
);
211 EXPECT_GT(GetSearchBoxPreferredSize().width(), GetSearchBoxBounds().width());
213 // Then set back to the original size, now the size is wide enough so the
214 // search box bounds becomes as preferred.
215 SetSize(gfx::Size(1280, 800));
216 EXPECT_EQ(GetSearchBoxPreferredSize().ToString(),
217 GetSearchBoxBounds().size().ToString());
220 TEST_F(AthenaStartPageViewTest
, SearchBox
) {
221 view_
->SetLayoutState(1.0f
);
222 EXPECT_TRUE(IsLogoVisible());
224 const gfx::Rect base_search_box_bounds
= GetSearchBoxBounds();
226 const base::string16 query
= base::UTF8ToUTF16("test");
227 SetSearchQuery(query
);
229 EXPECT_FALSE(IsLogoVisible());
230 EXPECT_GT(base_search_box_bounds
.y(), GetSearchBoxBounds().y());
231 EXPECT_EQ(query
, GetVisibleQuery());
233 SetSearchQuery(base::string16());
234 EXPECT_TRUE(IsLogoVisible());
235 EXPECT_EQ(base_search_box_bounds
.ToString(), GetSearchBoxBounds().ToString());
236 EXPECT_TRUE(GetVisibleQuery().empty());
239 TEST_F(AthenaStartPageViewTest
, SearchFromBottom
) {
240 view_
->SetLayoutState(0.0f
);
242 const base::string16 query
= base::UTF8ToUTF16("test");
243 SetSearchQuery(query
);
245 EXPECT_FALSE(IsLogoVisible());
246 EXPECT_EQ(query
, GetVisibleQuery());
247 EXPECT_EQ(1.0f
, layout_state());
249 SetSearchQuery(base::string16());
250 EXPECT_TRUE(IsLogoVisible());
251 EXPECT_TRUE(GetVisibleQuery().empty());
252 EXPECT_EQ(1.0f
, layout_state());
255 TEST_F(AthenaStartPageViewTest
, AppAddRemove
) {
256 gfx::Rect icons_bounds
= GetIconsBounds();
257 EXPECT_EQ(GetMaxIconNum(),
258 static_cast<size_t>(GetIconsContainer()->child_count()));
260 GetModel()->DeleteItem(GetAppIdFor(1));
262 // The removed icon disappear, however its bound should not change.
263 EXPECT_EQ(GetMaxIconNum() - 1,
264 static_cast<size_t>(GetIconsContainer()->child_count()));
265 EXPECT_EQ(icons_bounds
.size().ToString(), GetIconsBounds().size().ToString());
267 AddTestItem(GetMaxIconNum() + 1);
268 EXPECT_EQ(GetMaxIconNum(),
269 static_cast<size_t>(GetIconsContainer()->child_count()));
270 EXPECT_EQ(icons_bounds
.size().ToString(), GetIconsBounds().size().ToString());
272 // Adding more doesn't cause any effects.
273 AddTestItem(GetMaxIconNum() + 2);
274 EXPECT_EQ(GetMaxIconNum(),
275 static_cast<size_t>(GetIconsContainer()->child_count()));
276 EXPECT_EQ(icons_bounds
.size().ToString(), GetIconsBounds().size().ToString());
279 } // namespace athena