Implement listing tests to a JSON file for iOS gtest test launcher
[chromium-blink-merge.git] / ash / frame / caption_buttons / frame_caption_button_container_view_unittest.cc
blobfe53880815e0b29192e0768eca5c9dcc58e87767
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.
5 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h"
7 #include "ash/frame/caption_buttons/frame_caption_button.h"
8 #include "ash/shell.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "grit/ash_resources.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/widget/widget_delegate.h"
16 namespace ash {
18 namespace {
20 class TestWidgetDelegate : public views::WidgetDelegateView {
21 public:
22 TestWidgetDelegate(bool can_maximize, bool can_minimize)
23 : can_maximize_(can_maximize), can_minimize_(can_minimize) {}
24 ~TestWidgetDelegate() override {}
26 bool CanMaximize() const override { return can_maximize_; }
28 bool CanMinimize() const override { return can_minimize_; }
30 private:
31 bool can_maximize_;
32 bool can_minimize_;
34 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate);
37 } // namespace
39 class FrameCaptionButtonContainerViewTest : public ash::test::AshTestBase {
40 public:
41 enum MaximizeAllowed {
42 MAXIMIZE_ALLOWED,
43 MAXIMIZE_DISALLOWED
46 enum MinimizeAllowed {
47 MINIMIZE_ALLOWED,
48 MINIMIZE_DISALLOWED
51 FrameCaptionButtonContainerViewTest() {
54 ~FrameCaptionButtonContainerViewTest() override {}
56 // Creates a widget which allows maximizing based on |maximize_allowed|.
57 // The caller takes ownership of the returned widget.
58 views::Widget* CreateTestWidget(
59 MaximizeAllowed maximize_allowed,
60 MinimizeAllowed minimize_allowed) WARN_UNUSED_RESULT {
61 views::Widget* widget = new views::Widget;
62 views::Widget::InitParams params;
63 params.delegate = new TestWidgetDelegate(
64 maximize_allowed == MAXIMIZE_ALLOWED,
65 minimize_allowed == MINIMIZE_ALLOWED);
66 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
67 params.context = CurrentContext();
68 widget->Init(params);
69 return widget;
72 // Sets |container| to use arbitrary images for the buttons. Setting the
73 // images causes the buttons to have non-empty sizes.
74 void SetMockImages(FrameCaptionButtonContainerView* container) {
75 for (int icon = 0; icon < CAPTION_BUTTON_ICON_COUNT; ++icon) {
76 container->SetButtonImages(
77 static_cast<CaptionButtonIcon>(icon),
78 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE,
79 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE_I,
80 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
81 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
85 // Tests that |leftmost| and |rightmost| are at |container|'s edges.
86 bool CheckButtonsAtEdges(FrameCaptionButtonContainerView* container,
87 const ash::FrameCaptionButton& leftmost,
88 const ash::FrameCaptionButton& rightmost) {
89 gfx::Rect expected(container->GetPreferredSize());
91 gfx::Rect container_size(container->GetPreferredSize());
92 if (leftmost.y() == rightmost.y() &&
93 leftmost.height() == rightmost.height() &&
94 leftmost.x() == expected.x() &&
95 leftmost.y() == expected.y() &&
96 leftmost.height() == expected.height() &&
97 rightmost.bounds().right() == expected.right()) {
98 return true;
101 LOG(ERROR) << "Buttons " << leftmost.bounds().ToString() << " "
102 << rightmost.bounds().ToString() << " not at edges of "
103 << expected.ToString();
104 return false;
107 private:
108 DISALLOW_COPY_AND_ASSIGN(FrameCaptionButtonContainerViewTest);
111 // Test how the allowed actions affect which caption buttons are visible.
112 TEST_F(FrameCaptionButtonContainerViewTest, ButtonVisibility) {
113 // All the buttons should be visible when minimizing and maximizing are
114 // allowed.
115 FrameCaptionButtonContainerView container1(
116 CreateTestWidget(MAXIMIZE_ALLOWED, MINIMIZE_ALLOWED));
117 SetMockImages(&container1);
118 container1.Layout();
119 FrameCaptionButtonContainerView::TestApi t1(&container1);
120 EXPECT_TRUE(t1.minimize_button()->visible());
121 EXPECT_TRUE(t1.size_button()->visible());
122 EXPECT_TRUE(t1.close_button()->visible());
123 EXPECT_TRUE(CheckButtonsAtEdges(
124 &container1, *t1.minimize_button(), *t1.close_button()));
126 // The minimize button should be visible when minimizing is allowed but
127 // maximizing is disallowed.
128 FrameCaptionButtonContainerView container2(
129 CreateTestWidget(MAXIMIZE_DISALLOWED, MINIMIZE_ALLOWED));
130 SetMockImages(&container2);
131 container2.Layout();
132 FrameCaptionButtonContainerView::TestApi t2(&container2);
133 EXPECT_TRUE(t2.minimize_button()->visible());
134 EXPECT_FALSE(t2.size_button()->visible());
135 EXPECT_TRUE(t2.close_button()->visible());
136 EXPECT_TRUE(CheckButtonsAtEdges(
137 &container2, *t2.minimize_button(), *t2.close_button()));
139 // Neither the minimize button nor the size button should be visible when
140 // neither minimizing nor maximizing are allowed.
141 FrameCaptionButtonContainerView container3(
142 CreateTestWidget(MAXIMIZE_DISALLOWED, MINIMIZE_DISALLOWED));
143 SetMockImages(&container3);
144 container3.Layout();
145 FrameCaptionButtonContainerView::TestApi t3(&container3);
146 EXPECT_FALSE(t3.minimize_button()->visible());
147 EXPECT_FALSE(t3.size_button()->visible());
148 EXPECT_TRUE(t3.close_button()->visible());
149 EXPECT_TRUE(CheckButtonsAtEdges(
150 &container3, *t3.close_button(), *t3.close_button()));
153 // Tests that the layout animations trigered by button visibility result in the
154 // correct placement of the buttons.
155 TEST_F(FrameCaptionButtonContainerViewTest,
156 TestUpdateSizeButtonVisibilityAnimation) {
157 FrameCaptionButtonContainerView container(
158 CreateTestWidget(MAXIMIZE_ALLOWED, MINIMIZE_ALLOWED));
159 SetMockImages(&container);
160 container.SetBoundsRect(gfx::Rect(container.GetPreferredSize()));
161 container.Layout();
163 FrameCaptionButtonContainerView::TestApi test(&container);
164 gfx::Rect initial_minimize_button_bounds = test.minimize_button()->bounds();
165 gfx::Rect initial_size_button_bounds = test.size_button()->bounds();
166 gfx::Rect initial_close_button_bounds = test.close_button()->bounds();
167 gfx::Rect initial_container_bounds = container.bounds();
169 ASSERT_EQ(initial_size_button_bounds.x(),
170 initial_minimize_button_bounds.right());
171 ASSERT_EQ(initial_close_button_bounds.x(),
172 initial_size_button_bounds.right());
174 // Hidden size button should result in minimize button animating to the
175 // right. The size button should not be visible, but should not have moved.
176 Shell::GetInstance()->maximize_mode_controller()->
177 EnableMaximizeModeWindowManager(true);
178 container.UpdateSizeButtonVisibility();
179 test.EndAnimations();
180 // Parent needs to layout in response to size change.
181 container.Layout();
183 EXPECT_TRUE(test.minimize_button()->visible());
184 EXPECT_FALSE(test.size_button()->visible());
185 EXPECT_TRUE(test.close_button()->visible());
186 gfx::Rect minimize_button_bounds = test.minimize_button()->bounds();
187 gfx::Rect close_button_bounds = test.close_button()->bounds();
188 EXPECT_EQ(close_button_bounds.x(), minimize_button_bounds.right());
189 EXPECT_EQ(initial_size_button_bounds, test.size_button()->bounds());
190 EXPECT_EQ(initial_close_button_bounds.size(), close_button_bounds.size());
191 EXPECT_LT(container.GetPreferredSize().width(),
192 initial_container_bounds.width());
194 // Revealing the size button should cause the minimize button to return to its
195 // original position.
196 Shell::GetInstance()->maximize_mode_controller()->
197 EnableMaximizeModeWindowManager(false);
198 container.UpdateSizeButtonVisibility();
199 // Calling code needs to layout in response to size change.
200 container.Layout();
201 test.EndAnimations();
202 EXPECT_TRUE(test.minimize_button()->visible());
203 EXPECT_TRUE(test.size_button()->visible());
204 EXPECT_TRUE(test.close_button()->visible());
205 EXPECT_EQ(initial_minimize_button_bounds, test.minimize_button()->bounds());
206 EXPECT_EQ(initial_size_button_bounds, test.size_button()->bounds());
207 EXPECT_EQ(initial_close_button_bounds, test.close_button()->bounds());
208 EXPECT_EQ(container.GetPreferredSize().width(),
209 initial_container_bounds.width());
212 } // namespace ash