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/frame/caption_buttons/frame_caption_button.h"
8 #include "ash/test/ash_test_base.h"
9 #include "grit/ash_resources.h"
10 #include "ui/views/widget/widget.h"
11 #include "ui/views/widget/widget_delegate.h"
12
13 namespace ash {
14
15 namespace {
16
17 class TestWidgetDelegate : public views::WidgetDelegateView {
18 public:
TestWidgetDelegate(bool can_maximize)19 TestWidgetDelegate(bool can_maximize) : can_maximize_(can_maximize) {
20 }
~TestWidgetDelegate()21 virtual ~TestWidgetDelegate() {
22 }
23
CanMaximize() const24 virtual bool CanMaximize() const OVERRIDE {
25 return can_maximize_;
26 }
27
28 private:
29 bool can_maximize_;
30
31 DISALLOW_COPY_AND_ASSIGN(TestWidgetDelegate);
32 };
33
34 } // namespace
35
36 class FrameCaptionButtonContainerViewTest : public ash::test::AshTestBase {
37 public:
38 enum MaximizeAllowed {
39 MAXIMIZE_ALLOWED,
40 MAXIMIZE_DISALLOWED
41 };
42
FrameCaptionButtonContainerViewTest()43 FrameCaptionButtonContainerViewTest() {
44 }
45
~FrameCaptionButtonContainerViewTest()46 virtual ~FrameCaptionButtonContainerViewTest() {
47 }
48
49 // Creates a widget which allows maximizing based on |maximize_allowed|.
50 // The caller takes ownership of the returned widget.
CreateTestWidget(MaximizeAllowed maximize_allowed)51 views::Widget* CreateTestWidget(
52 MaximizeAllowed maximize_allowed) WARN_UNUSED_RESULT {
53 views::Widget* widget = new views::Widget;
54 views::Widget::InitParams params;
55 params.delegate = new TestWidgetDelegate(
56 maximize_allowed == MAXIMIZE_ALLOWED);
57 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
58 params.context = CurrentContext();
59 widget->Init(params);
60 return widget;
61 }
62
63 // Sets |container| to use arbitrary images for the buttons. Setting the
64 // images causes the buttons to have non-empty sizes.
SetMockImages(FrameCaptionButtonContainerView * container)65 void SetMockImages(FrameCaptionButtonContainerView* container) {
66 for (int icon = 0; icon < CAPTION_BUTTON_ICON_COUNT; ++icon) {
67 container->SetButtonImages(
68 static_cast<CaptionButtonIcon>(icon),
69 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE,
70 IDR_AURA_WINDOW_CONTROL_ICON_CLOSE_I,
71 IDR_AURA_WINDOW_CONTROL_BACKGROUND_H,
72 IDR_AURA_WINDOW_CONTROL_BACKGROUND_P);
73 }
74 }
75
76 // Tests that |leftmost| and |rightmost| are at |container|'s edges.
CheckButtonsAtEdges(FrameCaptionButtonContainerView * container,const ash::FrameCaptionButton & leftmost,const ash::FrameCaptionButton & rightmost)77 bool CheckButtonsAtEdges(FrameCaptionButtonContainerView* container,
78 const ash::FrameCaptionButton& leftmost,
79 const ash::FrameCaptionButton& rightmost) {
80 gfx::Rect expected(container->GetPreferredSize());
81
82 gfx::Rect container_size(container->GetPreferredSize());
83 if (leftmost.y() == rightmost.y() &&
84 leftmost.height() == rightmost.height() &&
85 leftmost.x() == expected.x() &&
86 leftmost.y() == expected.y() &&
87 leftmost.height() == expected.height() &&
88 rightmost.bounds().right() == expected.right()) {
89 return true;
90 }
91
92 LOG(ERROR) << "Buttons " << leftmost.bounds().ToString() << " "
93 << rightmost.bounds().ToString() << " not at edges of "
94 << expected.ToString();
95 return false;
96 }
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(FrameCaptionButtonContainerViewTest);
100 };
101
102 // Test how the allowed actions affect which caption buttons are visible.
TEST_F(FrameCaptionButtonContainerViewTest,ButtonVisibility)103 TEST_F(FrameCaptionButtonContainerViewTest, ButtonVisibility) {
104 // All the buttons should be visible when minimizing and maximizing are
105 // allowed.
106 scoped_ptr<views::Widget> widget_can_maximize(
107 CreateTestWidget(MAXIMIZE_ALLOWED));
108 FrameCaptionButtonContainerView container1(widget_can_maximize.get(),
109 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED);
110 SetMockImages(&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(
117 &container1, *t1.minimize_button(), *t1.close_button()));
118
119 // The minimize button should be visible when minimizing is allowed but
120 // maximizing is disallowed.
121 scoped_ptr<views::Widget> widget_cannot_maximize(
122 CreateTestWidget(MAXIMIZE_DISALLOWED));
123 FrameCaptionButtonContainerView container2(widget_cannot_maximize.get(),
124 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED);
125 SetMockImages(&container2);
126 container2.Layout();
127 FrameCaptionButtonContainerView::TestApi t2(&container2);
128 EXPECT_TRUE(t2.minimize_button()->visible());
129 EXPECT_FALSE(t2.size_button()->visible());
130 EXPECT_TRUE(t2.close_button()->visible());
131 EXPECT_TRUE(CheckButtonsAtEdges(
132 &container2, *t2.minimize_button(), *t2.close_button()));
133
134 // Neither the minimize button nor the size button should be visible when
135 // neither minimizing nor maximizing are allowed.
136 FrameCaptionButtonContainerView container3(widget_cannot_maximize.get(),
137 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED);
138 SetMockImages(&container3);
139 container3.Layout();
140 FrameCaptionButtonContainerView::TestApi t3(&container3);
141 EXPECT_FALSE(t3.minimize_button()->visible());
142 EXPECT_FALSE(t3.size_button()->visible());
143 EXPECT_TRUE(t3.close_button()->visible());
144 EXPECT_TRUE(CheckButtonsAtEdges(
145 &container3, *t3.close_button(), *t3.close_button()));
146 }
147
148 } // namespace ash
149