OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" | 5 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" |
6 | 6 |
7 #include "ash/frame/caption_buttons/frame_caption_button.h" | 7 #include "ash/frame/caption_buttons/frame_caption_button.h" |
8 #include "ash/shell.h" | |
9 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | |
11 #include "grit/ash_resources.h" | 9 #include "grit/ash_resources.h" |
12 #include "ui/compositor/layer.h" | |
13 #include "ui/gfx/geometry/rect.h" | |
14 #include "ui/views/widget/widget.h" | 10 #include "ui/views/widget/widget.h" |
15 #include "ui/views/widget/widget_delegate.h" | 11 #include "ui/views/widget/widget_delegate.h" |
16 | 12 |
17 namespace ash { | 13 namespace ash { |
18 | 14 |
19 namespace { | 15 namespace { |
20 | 16 |
21 class TestWidgetDelegate : public views::WidgetDelegateView { | 17 class TestWidgetDelegate : public views::WidgetDelegateView { |
22 public: | 18 public: |
23 TestWidgetDelegate(bool can_maximize) : can_maximize_(can_maximize) { | 19 TestWidgetDelegate(bool can_maximize) : can_maximize_(can_maximize) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 101 |
106 // Test how the allowed actions affect which caption buttons are visible. | 102 // Test how the allowed actions affect which caption buttons are visible. |
107 TEST_F(FrameCaptionButtonContainerViewTest, ButtonVisibility) { | 103 TEST_F(FrameCaptionButtonContainerViewTest, ButtonVisibility) { |
108 // All the buttons should be visible when minimizing and maximizing are | 104 // All the buttons should be visible when minimizing and maximizing are |
109 // allowed. | 105 // allowed. |
110 scoped_ptr<views::Widget> widget_can_maximize( | 106 scoped_ptr<views::Widget> widget_can_maximize( |
111 CreateTestWidget(MAXIMIZE_ALLOWED)); | 107 CreateTestWidget(MAXIMIZE_ALLOWED)); |
112 FrameCaptionButtonContainerView container1(widget_can_maximize.get(), | 108 FrameCaptionButtonContainerView container1(widget_can_maximize.get(), |
113 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); | 109 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); |
114 SetMockImages(&container1); | 110 SetMockImages(&container1); |
115 container1.SetBoundsRect(gfx::Rect(container1.GetPreferredSize())); | |
116 container1.Layout(); | 111 container1.Layout(); |
117 FrameCaptionButtonContainerView::TestApi t1(&container1); | 112 FrameCaptionButtonContainerView::TestApi t1(&container1); |
118 EXPECT_TRUE(t1.minimize_button()->visible()); | 113 EXPECT_TRUE(t1.minimize_button()->visible()); |
119 EXPECT_TRUE(t1.size_button()->visible()); | 114 EXPECT_TRUE(t1.size_button()->visible()); |
120 EXPECT_TRUE(t1.close_button()->visible()); | 115 EXPECT_TRUE(t1.close_button()->visible()); |
121 EXPECT_TRUE(CheckButtonsAtEdges( | 116 EXPECT_TRUE(CheckButtonsAtEdges( |
122 &container1, *t1.minimize_button(), *t1.close_button())); | 117 &container1, *t1.minimize_button(), *t1.close_button())); |
123 | 118 |
124 // The minimize button should be visible when minimizing is allowed but | 119 // The minimize button should be visible when minimizing is allowed but |
125 // maximizing is disallowed. | 120 // maximizing is disallowed. |
126 scoped_ptr<views::Widget> widget_cannot_maximize( | 121 scoped_ptr<views::Widget> widget_cannot_maximize( |
127 CreateTestWidget(MAXIMIZE_DISALLOWED)); | 122 CreateTestWidget(MAXIMIZE_DISALLOWED)); |
128 FrameCaptionButtonContainerView container2(widget_cannot_maximize.get(), | 123 FrameCaptionButtonContainerView container2(widget_cannot_maximize.get(), |
129 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); | 124 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); |
130 SetMockImages(&container2); | 125 SetMockImages(&container2); |
131 container2.SetBoundsRect(gfx::Rect(container2.GetPreferredSize())); | |
132 container2.Layout(); | 126 container2.Layout(); |
133 FrameCaptionButtonContainerView::TestApi t2(&container2); | 127 FrameCaptionButtonContainerView::TestApi t2(&container2); |
134 EXPECT_TRUE(t2.minimize_button()->visible()); | 128 EXPECT_TRUE(t2.minimize_button()->visible()); |
135 EXPECT_FALSE(t2.size_button()->visible()); | 129 EXPECT_FALSE(t2.size_button()->visible()); |
136 EXPECT_TRUE(t2.close_button()->visible()); | 130 EXPECT_TRUE(t2.close_button()->visible()); |
137 EXPECT_TRUE(CheckButtonsAtEdges( | 131 EXPECT_TRUE(CheckButtonsAtEdges( |
138 &container2, *t2.minimize_button(), *t2.close_button())); | 132 &container2, *t2.minimize_button(), *t2.close_button())); |
139 | 133 |
140 // Neither the minimize button nor the size button should be visible when | 134 // Neither the minimize button nor the size button should be visible when |
141 // neither minimizing nor maximizing are allowed. | 135 // neither minimizing nor maximizing are allowed. |
142 FrameCaptionButtonContainerView container3(widget_cannot_maximize.get(), | 136 FrameCaptionButtonContainerView container3(widget_cannot_maximize.get(), |
143 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED); | 137 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED); |
144 SetMockImages(&container3); | 138 SetMockImages(&container3); |
145 container3.SetBoundsRect(gfx::Rect(container3.GetPreferredSize())); | |
146 container3.Layout(); | 139 container3.Layout(); |
147 FrameCaptionButtonContainerView::TestApi t3(&container3); | 140 FrameCaptionButtonContainerView::TestApi t3(&container3); |
148 EXPECT_FALSE(t3.minimize_button()->visible()); | 141 EXPECT_FALSE(t3.minimize_button()->visible()); |
149 EXPECT_FALSE(t3.size_button()->visible()); | 142 EXPECT_FALSE(t3.size_button()->visible()); |
150 EXPECT_TRUE(t3.close_button()->visible()); | 143 EXPECT_TRUE(t3.close_button()->visible()); |
151 EXPECT_TRUE(CheckButtonsAtEdges( | 144 EXPECT_TRUE(CheckButtonsAtEdges( |
152 &container3, *t3.close_button(), *t3.close_button())); | 145 &container3, *t3.close_button(), *t3.close_button())); |
153 } | 146 } |
154 | 147 |
155 // Tests that the layout animations trigered by button visibilty result in the | |
156 // correct placement of the buttons. | |
157 TEST_F(FrameCaptionButtonContainerViewTest, | |
158 TestUpdateSizeButtonVisibilityAnimation) { | |
159 scoped_ptr<views::Widget> widget_can_maximize( | |
160 CreateTestWidget(MAXIMIZE_ALLOWED)); | |
161 FrameCaptionButtonContainerView container(widget_can_maximize.get(), | |
162 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); | |
163 SetMockImages(&container); | |
164 container.SetBoundsRect(gfx::Rect(container.GetPreferredSize())); | |
165 container.Layout(); | |
166 | |
167 FrameCaptionButtonContainerView::TestApi test(&container); | |
168 gfx::Rect initial_minimize_button_bounds = test.minimize_button()->bounds(); | |
169 gfx::Rect initial_size_button_bounds = test.size_button()->bounds(); | |
170 gfx::Rect initial_close_button_bounds = test.close_button()->bounds(); | |
171 gfx::Rect initial_container_bounds = container.bounds(); | |
172 | |
173 ASSERT_EQ(initial_size_button_bounds.x(), | |
174 initial_minimize_button_bounds.right()); | |
175 ASSERT_EQ(initial_close_button_bounds.x(), | |
176 initial_size_button_bounds.right()); | |
177 | |
178 // Hidden size button should result in minimize button animating to the | |
179 // right. The size button should not be visible, but should not have moved. | |
180 Shell::GetInstance()->maximize_mode_controller()-> | |
181 EnableMaximizeModeWindowManager(true); | |
182 container.UpdateSizeButtonVisibility(); | |
183 container.Layout(); | |
184 | |
185 EXPECT_TRUE(test.minimize_button()->visible()); | |
186 EXPECT_FALSE(test.size_button()->visible()); | |
187 EXPECT_TRUE(test.close_button()->visible()); | |
188 gfx::Rect minimize_button_bounds = test.minimize_button()->bounds(); | |
189 gfx::Rect close_button_bounds = test.close_button()->bounds(); | |
190 EXPECT_EQ(close_button_bounds.x(), minimize_button_bounds.right()); | |
191 EXPECT_EQ(initial_size_button_bounds, test.size_button()->bounds()); | |
192 EXPECT_EQ(initial_close_button_bounds, close_button_bounds); | |
193 EXPECT_LT(container.GetPreferredSize().width(), | |
194 initial_container_bounds.width()); | |
195 | |
196 // Revealing the size button should cause the minimze button to return to its | |
197 // original position. | |
198 Shell::GetInstance()->maximize_mode_controller()-> | |
199 EnableMaximizeModeWindowManager(false); | |
200 container.UpdateSizeButtonVisibility(); | |
201 container.Layout(); | |
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()); | |
210 } | |
211 | |
212 // Tests that after an animation to maximized state, that the layer tree has | |
213 // been updated to reflect the swapped layers. | |
214 TEST_F(FrameCaptionButtonContainerViewTest, AnimationUpdatesLayerTree) { | |
215 scoped_ptr<views::Widget> widget_can_maximize( | |
216 CreateTestWidget(MAXIMIZE_ALLOWED)); | |
217 FrameCaptionButtonContainerView container(widget_can_maximize.get(), | |
218 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED); | |
219 SetMockImages(&container); | |
220 container.SetBoundsRect(gfx::Rect(container.GetPreferredSize())); | |
221 container.Layout(); | |
222 ui::Layer* original_layer = container.layer(); | |
223 | |
224 // The container must be a child of the window to be affected by the | |
225 // animation. | |
226 widget_can_maximize->non_client_view()->AddChildView(&container); | |
227 widget_can_maximize->Show(); | |
228 // The original cache of the layer tree must be created before the animation | |
229 widget_can_maximize->UpdateRootLayers(); | |
230 widget_can_maximize->GetRootLayers(); | |
231 | |
232 // After the maximizing animation has completed, |original_layer| will have | |
233 // been deleted. Dereferencing it could cause a segfault. | |
234 widget_can_maximize->Maximize(); | |
235 RunAllPendingInMessageLoop(); | |
236 EXPECT_TRUE(widget_can_maximize->IsMaximized()); | |
237 | |
238 std::vector<ui::Layer*> layers = widget_can_maximize->GetRootLayers(); | |
239 | |
240 EXPECT_GT(layers.size(), 0u); | |
241 EXPECT_NE(layers.end(), | |
242 std::find(layers.begin(), layers.end(), container.layer())); | |
243 EXPECT_EQ(layers.end(), | |
244 std::find(layers.begin(), layers.end(), original_layer)); | |
245 EXPECT_NE(original_layer, container.layer()); | |
246 } | |
247 | |
248 } // namespace ash | 148 } // namespace ash |
OLD | NEW |