OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "ui/views/controls/single_split_view.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 #include "ui/events/event_utils.h" | |
13 #include "ui/views/border.h" | |
14 #include "ui/views/controls/single_split_view_listener.h" | |
15 | |
16 namespace { | |
17 | |
18 static void VerifySplitViewLayout(const views::SingleSplitView& split) { | |
19 ASSERT_EQ(2, split.child_count()); | |
20 | |
21 const views::View* leading = split.child_at(0); | |
22 const views::View* trailing = split.child_at(1); | |
23 | |
24 if (split.bounds().IsEmpty()) { | |
25 EXPECT_TRUE(leading->bounds().IsEmpty()); | |
26 EXPECT_TRUE(trailing->bounds().IsEmpty()); | |
27 return; | |
28 } | |
29 | |
30 EXPECT_FALSE(leading->bounds().IsEmpty()); | |
31 EXPECT_FALSE(trailing->bounds().IsEmpty()); | |
32 EXPECT_FALSE(leading->bounds().Intersects(trailing->bounds())); | |
33 | |
34 if (split.orientation() == views::SingleSplitView::HORIZONTAL_SPLIT) { | |
35 EXPECT_EQ(leading->bounds().height(), | |
36 split.bounds().height() - split.GetInsets().height()); | |
37 EXPECT_EQ(trailing->bounds().height(), | |
38 split.bounds().height() - split.GetInsets().height()); | |
39 EXPECT_LT(leading->bounds().width() + trailing->bounds().width(), | |
40 split.bounds().width() - split.GetInsets().width()); | |
41 } else if (split.orientation() == views::SingleSplitView::VERTICAL_SPLIT) { | |
42 EXPECT_EQ(leading->bounds().width(), | |
43 split.bounds().width() - split.GetInsets().width()); | |
44 EXPECT_EQ(trailing->bounds().width(), | |
45 split.bounds().width() - split.GetInsets().width()); | |
46 EXPECT_LT(leading->bounds().height() + trailing->bounds().height(), | |
47 split.bounds().height() - split.GetInsets().height()); | |
48 } else { | |
49 NOTREACHED(); | |
50 } | |
51 } | |
52 | |
53 class SingleSplitViewListenerImpl : public views::SingleSplitViewListener { | |
54 public: | |
55 SingleSplitViewListenerImpl() : count_(0) {} | |
56 | |
57 bool SplitHandleMoved(views::SingleSplitView* sender) override { | |
58 ++count_; | |
59 return false; | |
60 } | |
61 | |
62 int count() const { return count_; } | |
63 | |
64 private: | |
65 int count_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(SingleSplitViewListenerImpl); | |
68 }; | |
69 | |
70 class MinimumSizedView: public views::View { | |
71 public: | |
72 MinimumSizedView(gfx::Size min_size) : min_size_(min_size) {} | |
73 | |
74 private: | |
75 gfx::Size min_size_; | |
76 gfx::Size GetMinimumSize() const override; | |
77 }; | |
78 | |
79 gfx::Size MinimumSizedView::GetMinimumSize() const { | |
80 return min_size_; | |
81 } | |
82 | |
83 } // namespace | |
84 | |
85 namespace views { | |
86 | |
87 TEST(SingleSplitViewTest, Resize) { | |
88 // Test cases to iterate through for horizontal and vertical split views. | |
89 struct TestCase { | |
90 // Split view resize policy for this test case. | |
91 bool resize_leading_on_bounds_change; | |
92 // Split view size to set. | |
93 int primary_axis_size; | |
94 int secondary_axis_size; | |
95 // Expected divider offset. | |
96 int divider_offset; | |
97 } test_cases[] = { | |
98 // The initial split size is 100x100, divider at 33. | |
99 { true, 100, 100, 33 }, | |
100 // Grow the split view, leading view should grow. | |
101 { true, 1000, 100, 933 }, | |
102 // Shrink the split view, leading view should shrink. | |
103 { true, 200, 100, 133 }, | |
104 // Minimize the split view, divider should not move. | |
105 { true, 0, 0, 133 }, | |
106 // Restore the split view, divider should not move. | |
107 { false, 500, 100, 133 }, | |
108 // Resize the split view by secondary axis, divider should not move. | |
109 { false, 500, 600, 133 } | |
110 }; | |
111 | |
112 SingleSplitView::Orientation orientations[] = { | |
113 SingleSplitView::HORIZONTAL_SPLIT, | |
114 SingleSplitView::VERTICAL_SPLIT | |
115 }; | |
116 | |
117 int borders[][4] = { | |
118 {0, 0, 0, 0}, {5, 5, 5, 5}, {10, 5, 5, 0}, | |
119 }; | |
120 | |
121 for (size_t orientation = 0; orientation < arraysize(orientations); | |
122 ++orientation) { | |
123 for (size_t border = 0; border < arraysize(borders); ++border) { | |
124 // Create a split view. | |
125 SingleSplitView split(new View(), new View(), orientations[orientation], | |
126 NULL); | |
127 | |
128 // Set initial size and divider offset. | |
129 EXPECT_EQ(test_cases[0].primary_axis_size, | |
130 test_cases[0].secondary_axis_size); | |
131 split.SetBounds(0, 0, test_cases[0].primary_axis_size, | |
132 test_cases[0].secondary_axis_size); | |
133 | |
134 if (border != 0) | |
135 split.SetBorder( | |
136 Border::CreateEmptyBorder(borders[border][0], borders[border][1], | |
137 borders[border][2], borders[border][3])); | |
138 | |
139 split.set_divider_offset(test_cases[0].divider_offset); | |
140 split.Layout(); | |
141 | |
142 // Run all test cases. | |
143 for (size_t i = 0; i < arraysize(test_cases); ++i) { | |
144 split.set_resize_leading_on_bounds_change( | |
145 test_cases[i].resize_leading_on_bounds_change); | |
146 if (split.orientation() == SingleSplitView::HORIZONTAL_SPLIT) { | |
147 split.SetBounds(0, 0, test_cases[i].primary_axis_size, | |
148 test_cases[i].secondary_axis_size); | |
149 } else { | |
150 split.SetBounds(0, 0, test_cases[i].secondary_axis_size, | |
151 test_cases[i].primary_axis_size); | |
152 } | |
153 | |
154 EXPECT_EQ(test_cases[i].divider_offset, split.divider_offset()); | |
155 VerifySplitViewLayout(split); | |
156 } | |
157 | |
158 // Special cases, one of the child views is hidden. | |
159 split.child_at(0)->SetVisible(false); | |
160 split.Layout(); | |
161 | |
162 EXPECT_EQ(split.GetContentsBounds().size(), split.child_at(1)->size()); | |
163 | |
164 split.child_at(0)->SetVisible(true); | |
165 split.child_at(1)->SetVisible(false); | |
166 split.Layout(); | |
167 | |
168 EXPECT_EQ(split.GetContentsBounds().size(), split.child_at(0)->size()); | |
169 } | |
170 } | |
171 } | |
172 | |
173 TEST(SingleSplitViewTest, MouseDrag) { | |
174 const int kMinimumChildSize = 25; | |
175 MinimumSizedView *child0 = | |
176 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); | |
177 MinimumSizedView *child1 = | |
178 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); | |
179 SingleSplitViewListenerImpl listener; | |
180 SingleSplitView split( | |
181 child0, child1, SingleSplitView::VERTICAL_SPLIT, &listener); | |
182 | |
183 const int kTotalSplitSize = 100; | |
184 split.SetBounds(0, 0, 10, kTotalSplitSize); | |
185 const int kInitialDividerOffset = 33; | |
186 const int kMouseOffset = 2; // Mouse offset in the divider. | |
187 const int kMouseMoveDelta = 7; | |
188 split.set_divider_offset(kInitialDividerOffset); | |
189 split.Layout(); | |
190 | |
191 gfx::Point press_point(7, kInitialDividerOffset + kMouseOffset); | |
192 ui::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, press_point, press_point, | |
193 ui::EventTimeForNow(), 0, 0); | |
194 ASSERT_TRUE(split.OnMousePressed(mouse_pressed)); | |
195 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); | |
196 EXPECT_EQ(0, listener.count()); | |
197 | |
198 // Drag divider to the bottom. | |
199 gfx::Point drag_1_point( | |
200 5, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta); | |
201 ui::MouseEvent mouse_dragged_1(ui::ET_MOUSE_DRAGGED, drag_1_point, | |
202 drag_1_point, ui::EventTimeForNow(), 0, 0); | |
203 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_1)); | |
204 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta, split.divider_offset()); | |
205 EXPECT_EQ(1, listener.count()); | |
206 | |
207 // Drag divider to the top, beyond first child minimum size. | |
208 gfx::Point drag_2_point( | |
209 7, kMinimumChildSize - 5); | |
210 ui::MouseEvent mouse_dragged_2(ui::ET_MOUSE_DRAGGED, drag_2_point, | |
211 drag_2_point, ui::EventTimeForNow(), 0, 0); | |
212 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_2)); | |
213 EXPECT_EQ(kMinimumChildSize, split.divider_offset()); | |
214 EXPECT_EQ(2, listener.count()); | |
215 | |
216 // Drag divider to the bottom, beyond second child minimum size. | |
217 gfx::Point drag_3_point( | |
218 7, kTotalSplitSize - kMinimumChildSize + 5); | |
219 ui::MouseEvent mouse_dragged_3(ui::ET_MOUSE_DRAGGED, drag_3_point, | |
220 drag_3_point, ui::EventTimeForNow(), 0, 0); | |
221 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_3)); | |
222 EXPECT_EQ(kTotalSplitSize - kMinimumChildSize - split.GetDividerSize(), | |
223 split.divider_offset()); | |
224 EXPECT_EQ(3, listener.count()); | |
225 | |
226 // Drag divider between childs' minimum sizes. | |
227 gfx::Point drag_4_point( | |
228 6, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); | |
229 ui::MouseEvent mouse_dragged_4(ui::ET_MOUSE_DRAGGED, drag_4_point, | |
230 drag_4_point, ui::EventTimeForNow(), 0, 0); | |
231 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_4)); | |
232 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, | |
233 split.divider_offset()); | |
234 EXPECT_EQ(4, listener.count()); | |
235 | |
236 gfx::Point release_point( | |
237 7, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); | |
238 ui::MouseEvent mouse_released(ui::ET_MOUSE_RELEASED, release_point, | |
239 release_point, ui::EventTimeForNow(), 0, 0); | |
240 split.OnMouseReleased(mouse_released); | |
241 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, | |
242 split.divider_offset()); | |
243 | |
244 // Expect intial offset after a system/user gesture cancels the drag. | |
245 // This shouldn't occur after mouse release, but it's sufficient for testing. | |
246 split.OnMouseCaptureLost(); | |
247 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); | |
248 EXPECT_EQ(5, listener.count()); | |
249 } | |
250 | |
251 } // namespace views | |
OLD | NEW |