1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/ui/top_view_layout_views.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/view.h"
11 using xwalk::TopViewLayout;
15 class TopViewLayoutTest : public testing::Test {
17 virtual void SetUp() OVERRIDE {
18 host_.reset(new View);
21 scoped_ptr<View> host_;
22 scoped_ptr<TopViewLayout> layout_;
25 class ViewWithPreferredSize : public View {
27 explicit ViewWithPreferredSize(const gfx::Size& preferred_size)
28 : preferred_size_(preferred_size) {}
30 void set_preferred_size(const gfx::Size& preferred_size) {
31 preferred_size_ = preferred_size;
34 // View implementation.
35 virtual gfx::Size GetPreferredSize() OVERRIDE { return preferred_size_; }
38 gfx::Size preferred_size_;
43 TEST_F(TopViewLayoutTest, OnlyContentView) {
44 layout_.reset(new TopViewLayout);
45 View* content = new View();
46 host_->AddChildView(content);
47 layout_->set_content_view(content);
49 host_->SetSize(gfx::Size(200, 150));
50 layout_->Layout(host_.get());
51 EXPECT_EQ(gfx::Rect(0, 0, 200, 150), content->bounds());
53 host_->SetSize(gfx::Size(300, 400));
54 layout_->Layout(host_.get());
55 EXPECT_EQ(gfx::Rect(0, 0, 300, 400), content->bounds());
58 TEST_F(TopViewLayoutTest, BothViews) {
59 layout_.reset(new TopViewLayout);
61 View* content = new View();
62 host_->AddChildView(content);
63 layout_->set_content_view(content);
65 View* top_view = new ViewWithPreferredSize(gfx::Size(200, 20));
66 host_->AddChildView(top_view);
67 layout_->set_top_view(top_view);
69 host_->SetSize(gfx::Size(200, 150));
70 layout_->Layout(host_.get());
71 EXPECT_EQ(gfx::Rect(0, 20, 200, 130), content->bounds());
72 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());
74 host_->SetSize(gfx::Size(300, 400));
75 layout_->Layout(host_.get());
76 EXPECT_EQ(gfx::Rect(0, 20, 300, 380), content->bounds());
77 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());
80 TEST_F(TopViewLayoutTest, ChangingTopViewHeight) {
81 layout_.reset(new TopViewLayout);
83 View* content = new View();
84 host_->AddChildView(content);
85 layout_->set_content_view(content);
87 ViewWithPreferredSize* top_view =
88 new ViewWithPreferredSize(gfx::Size(200, 20));
89 host_->AddChildView(top_view);
90 layout_->set_top_view(top_view);
92 host_->SetSize(gfx::Size(200, 150));
93 layout_->Layout(host_.get());
94 EXPECT_EQ(gfx::Rect(0, 20, 200, 130), content->bounds());
95 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());
97 top_view->set_preferred_size(gfx::Size(100, 100));
98 layout_->Layout(host_.get());
99 EXPECT_EQ(gfx::Rect(0, 100, 200, 50), content->bounds());
100 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), top_view->bounds());
102 host_->SetSize(gfx::Size(300, 400));
103 layout_->Layout(host_.get());
104 EXPECT_EQ(gfx::Rect(0, 100, 300, 300), content->bounds());
105 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), top_view->bounds());
107 top_view->set_preferred_size(gfx::Size(200, 20));
108 layout_->Layout(host_.get());
109 EXPECT_EQ(gfx::Rect(0, 20, 300, 380), content->bounds());
110 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());
113 TEST_F(TopViewLayoutTest, ChangingTopViewOverlay) {
114 layout_.reset(new TopViewLayout);
116 View* content = new View();
117 host_->AddChildView(content);
118 layout_->set_content_view(content);
120 View* top_view = new ViewWithPreferredSize(gfx::Size(200, 20));
121 host_->AddChildView(top_view);
122 layout_->set_top_view(top_view);
124 host_->SetSize(gfx::Size(200, 150));
125 layout_->Layout(host_.get());
126 EXPECT_EQ(gfx::Rect(0, 20, 200, 130), content->bounds());
127 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());
129 EXPECT_FALSE(layout_->IsUsingOverlay());
130 layout_->SetUseOverlay(true);
131 EXPECT_TRUE(layout_->IsUsingOverlay());
133 layout_->Layout(host_.get());
134 EXPECT_EQ(gfx::Rect(0, 0, 200, 150), content->bounds());
135 EXPECT_EQ(gfx::Rect(0, 0, 200, 20), top_view->bounds());