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