| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" | |
| 6 | |
| 7 #include "ash/common/ash_layout_constants.h" | |
| 8 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" | |
| 9 #include "ash/common/wm_shell.h" | |
| 10 #include "ash/frame/caption_buttons/frame_caption_button.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/test/ash_test_base.h" | |
| 13 #include "grit/ash_resources.h" | |
| 14 #include "ui/gfx/geometry/rect.h" | |
| 15 #include "ui/gfx/vector_icons_public.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 #include "ui/views/widget/widget_delegate.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class TestWidgetDelegate : public views::WidgetDelegateView { | |
| 24 public: | |
| 25 TestWidgetDelegate(bool can_maximize, bool can_minimize) | |
| 26 : can_maximize_(can_maximize), can_minimize_(can_minimize) {} | |
| 27 ~TestWidgetDelegate() override {} | |
| 28 | |
| 29 bool CanMaximize() const override { return can_maximize_; } | |
| 30 | |
| 31 bool CanMinimize() const override { return can_minimize_; } | |
| 32 | |
| 33 private: | |
| 34 bool can_maximize_; | |
| 35 bool can_minimize_; | |
| 36 | |
| 37 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate); | |
| 38 }; | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 class FrameCaptionButtonContainerViewTest : public ash::test::AshTestBase { | |
| 43 public: | |
| 44 enum MaximizeAllowed { MAXIMIZE_ALLOWED, MAXIMIZE_DISALLOWED }; | |
| 45 | |
| 46 enum MinimizeAllowed { MINIMIZE_ALLOWED, MINIMIZE_DISALLOWED }; | |
| 47 | |
| 48 FrameCaptionButtonContainerViewTest() {} | |
| 49 | |
| 50 ~FrameCaptionButtonContainerViewTest() override {} | |
| 51 | |
| 52 // Creates a widget which allows maximizing based on |maximize_allowed|. | |
| 53 // The caller takes ownership of the returned widget. | |
| 54 views::Widget* CreateTestWidget(MaximizeAllowed maximize_allowed, | |
| 55 MinimizeAllowed minimize_allowed) | |
| 56 WARN_UNUSED_RESULT { | |
| 57 views::Widget* widget = new views::Widget; | |
| 58 views::Widget::InitParams params; | |
| 59 params.delegate = | |
| 60 new TestWidgetDelegate(maximize_allowed == MAXIMIZE_ALLOWED, | |
| 61 minimize_allowed == MINIMIZE_ALLOWED); | |
| 62 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 63 params.context = CurrentContext(); | |
| 64 widget->Init(params); | |
| 65 return widget; | |
| 66 } | |
| 67 | |
| 68 // Sets arbitrary images for the icons and assign the default caption button | |
| 69 // size to the buttons in |container|. | |
| 70 void InitContainer(FrameCaptionButtonContainerView* container) { | |
| 71 container->SetButtonSize( | |
| 72 GetAshLayoutSize(AshLayoutSize::NON_BROWSER_CAPTION_BUTTON)); | |
| 73 for (int icon = 0; icon < CAPTION_BUTTON_ICON_COUNT; ++icon) { | |
| 74 container->SetButtonImage(static_cast<CaptionButtonIcon>(icon), | |
| 75 gfx::VectorIconId::WINDOW_CONTROL_CLOSE); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Tests that |leftmost| and |rightmost| are at |container|'s edges. | |
| 80 bool CheckButtonsAtEdges(FrameCaptionButtonContainerView* container, | |
| 81 const ash::FrameCaptionButton& leftmost, | |
| 82 const ash::FrameCaptionButton& rightmost) { | |
| 83 gfx::Rect expected(container->GetPreferredSize()); | |
| 84 | |
| 85 gfx::Rect container_size(container->GetPreferredSize()); | |
| 86 if (leftmost.y() == rightmost.y() && | |
| 87 leftmost.height() == rightmost.height() && | |
| 88 leftmost.x() == expected.x() && leftmost.y() == expected.y() && | |
| 89 leftmost.height() == expected.height() && | |
| 90 rightmost.bounds().right() == expected.right()) { | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 LOG(ERROR) << "Buttons " << leftmost.bounds().ToString() << " " | |
| 95 << rightmost.bounds().ToString() << " not at edges of " | |
| 96 << expected.ToString(); | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 DISALLOW_COPY_AND_ASSIGN(FrameCaptionButtonContainerViewTest); | |
| 102 }; | |
| 103 | |
| 104 // Test how the allowed actions affect which caption buttons are visible. | |
| 105 TEST_F(FrameCaptionButtonContainerViewTest, ButtonVisibility) { | |
| 106 // All the buttons should be visible when minimizing and maximizing are | |
| 107 // allowed. | |
| 108 FrameCaptionButtonContainerView container1( | |
| 109 CreateTestWidget(MAXIMIZE_ALLOWED, MINIMIZE_ALLOWED)); | |
| 110 InitContainer(&container1); | |
| 111 container1.Layout(); | |
| 112 FrameCaptionButtonContainerView::TestApi t1(&container1); | |
| 113 EXPECT_TRUE(t1.minimize_button()->visible()); | |
| 114 EXPECT_TRUE(t1.size_button()->visible()); | |
| 115 EXPECT_TRUE(t1.close_button()->visible()); | |
| 116 EXPECT_TRUE(CheckButtonsAtEdges(&container1, *t1.minimize_button(), | |
| 117 *t1.close_button())); | |
| 118 | |
| 119 // The minimize button should be visible when minimizing is allowed but | |
| 120 // maximizing is disallowed. | |
| 121 FrameCaptionButtonContainerView container2( | |
| 122 CreateTestWidget(MAXIMIZE_DISALLOWED, MINIMIZE_ALLOWED)); | |
| 123 InitContainer(&container2); | |
| 124 container2.Layout(); | |
| 125 FrameCaptionButtonContainerView::TestApi t2(&container2); | |
| 126 EXPECT_TRUE(t2.minimize_button()->visible()); | |
| 127 EXPECT_FALSE(t2.size_button()->visible()); | |
| 128 EXPECT_TRUE(t2.close_button()->visible()); | |
| 129 EXPECT_TRUE(CheckButtonsAtEdges(&container2, *t2.minimize_button(), | |
| 130 *t2.close_button())); | |
| 131 | |
| 132 // Neither the minimize button nor the size button should be visible when | |
| 133 // neither minimizing nor maximizing are allowed. | |
| 134 FrameCaptionButtonContainerView container3( | |
| 135 CreateTestWidget(MAXIMIZE_DISALLOWED, MINIMIZE_DISALLOWED)); | |
| 136 InitContainer(&container3); | |
| 137 container3.Layout(); | |
| 138 FrameCaptionButtonContainerView::TestApi t3(&container3); | |
| 139 EXPECT_FALSE(t3.minimize_button()->visible()); | |
| 140 EXPECT_FALSE(t3.size_button()->visible()); | |
| 141 EXPECT_TRUE(t3.close_button()->visible()); | |
| 142 EXPECT_TRUE( | |
| 143 CheckButtonsAtEdges(&container3, *t3.close_button(), *t3.close_button())); | |
| 144 } | |
| 145 | |
| 146 // Tests that the layout animations trigered by button visibility result in the | |
| 147 // correct placement of the buttons. | |
| 148 TEST_F(FrameCaptionButtonContainerViewTest, | |
| 149 TestUpdateSizeButtonVisibilityAnimation) { | |
| 150 FrameCaptionButtonContainerView container( | |
| 151 CreateTestWidget(MAXIMIZE_ALLOWED, MINIMIZE_ALLOWED)); | |
| 152 InitContainer(&container); | |
| 153 container.SetBoundsRect(gfx::Rect(container.GetPreferredSize())); | |
| 154 container.Layout(); | |
| 155 | |
| 156 FrameCaptionButtonContainerView::TestApi test(&container); | |
| 157 gfx::Rect initial_minimize_button_bounds = test.minimize_button()->bounds(); | |
| 158 gfx::Rect initial_size_button_bounds = test.size_button()->bounds(); | |
| 159 gfx::Rect initial_close_button_bounds = test.close_button()->bounds(); | |
| 160 gfx::Rect initial_container_bounds = container.bounds(); | |
| 161 | |
| 162 ASSERT_EQ(initial_size_button_bounds.x(), | |
| 163 initial_minimize_button_bounds.right()); | |
| 164 ASSERT_EQ(initial_close_button_bounds.x(), | |
| 165 initial_size_button_bounds.right()); | |
| 166 | |
| 167 // Hidden size button should result in minimize button animating to the | |
| 168 // right. The size button should not be visible, but should not have moved. | |
| 169 WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager( | |
| 170 true); | |
| 171 container.UpdateSizeButtonVisibility(); | |
| 172 test.EndAnimations(); | |
| 173 // Parent needs to layout in response to size change. | |
| 174 container.Layout(); | |
| 175 | |
| 176 EXPECT_TRUE(test.minimize_button()->visible()); | |
| 177 EXPECT_FALSE(test.size_button()->visible()); | |
| 178 EXPECT_TRUE(test.close_button()->visible()); | |
| 179 gfx::Rect minimize_button_bounds = test.minimize_button()->bounds(); | |
| 180 gfx::Rect close_button_bounds = test.close_button()->bounds(); | |
| 181 EXPECT_EQ(close_button_bounds.x(), minimize_button_bounds.right()); | |
| 182 EXPECT_EQ(initial_size_button_bounds, test.size_button()->bounds()); | |
| 183 EXPECT_EQ(initial_close_button_bounds.size(), close_button_bounds.size()); | |
| 184 EXPECT_LT(container.GetPreferredSize().width(), | |
| 185 initial_container_bounds.width()); | |
| 186 | |
| 187 // Revealing the size button should cause the minimize button to return to its | |
| 188 // original position. | |
| 189 WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager( | |
| 190 false); | |
| 191 container.UpdateSizeButtonVisibility(); | |
| 192 // Calling code needs to layout in response to size change. | |
| 193 container.Layout(); | |
| 194 test.EndAnimations(); | |
| 195 EXPECT_TRUE(test.minimize_button()->visible()); | |
| 196 EXPECT_TRUE(test.size_button()->visible()); | |
| 197 EXPECT_TRUE(test.close_button()->visible()); | |
| 198 EXPECT_EQ(initial_minimize_button_bounds, test.minimize_button()->bounds()); | |
| 199 EXPECT_EQ(initial_size_button_bounds, test.size_button()->bounds()); | |
| 200 EXPECT_EQ(initial_close_button_bounds, test.close_button()->bounds()); | |
| 201 EXPECT_EQ(container.GetPreferredSize().width(), | |
| 202 initial_container_bounds.width()); | |
| 203 } | |
| 204 | |
| 205 } // namespace ash | |
| OLD | NEW |