OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/controls/single_split_view.h" |
| 6 |
5 #include "base/logging.h" | 7 #include "base/logging.h" |
6 #include "testing/gmock/include/gmock/gmock.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/views/controls/single_split_view.h" | |
9 #include "ui/views/controls/single_split_view_listener.h" | 9 #include "ui/views/controls/single_split_view_listener.h" |
10 | 10 |
11 using ::testing::_; | |
12 using ::testing::Return; | |
13 | |
14 namespace { | 11 namespace { |
15 | 12 |
16 static void VerifySplitViewLayout(const views::SingleSplitView& split) { | 13 static void VerifySplitViewLayout(const views::SingleSplitView& split) { |
17 ASSERT_EQ(2, split.child_count()); | 14 ASSERT_EQ(2, split.child_count()); |
18 | 15 |
19 const views::View* leading = split.child_at(0); | 16 const views::View* leading = split.child_at(0); |
20 const views::View* trailing = split.child_at(1); | 17 const views::View* trailing = split.child_at(1); |
21 | 18 |
22 if (split.bounds().IsEmpty()) { | 19 if (split.bounds().IsEmpty()) { |
23 EXPECT_TRUE(leading->bounds().IsEmpty()); | 20 EXPECT_TRUE(leading->bounds().IsEmpty()); |
(...skipping 13 matching lines...) Expand all Loading... |
37 } else if (split.orientation() == views::SingleSplitView::VERTICAL_SPLIT) { | 34 } else if (split.orientation() == views::SingleSplitView::VERTICAL_SPLIT) { |
38 EXPECT_EQ(leading->bounds().width(), split.bounds().width()); | 35 EXPECT_EQ(leading->bounds().width(), split.bounds().width()); |
39 EXPECT_EQ(trailing->bounds().width(), split.bounds().width()); | 36 EXPECT_EQ(trailing->bounds().width(), split.bounds().width()); |
40 EXPECT_LT(leading->bounds().height() + trailing->bounds().height(), | 37 EXPECT_LT(leading->bounds().height() + trailing->bounds().height(), |
41 split.bounds().height()); | 38 split.bounds().height()); |
42 } else { | 39 } else { |
43 NOTREACHED(); | 40 NOTREACHED(); |
44 } | 41 } |
45 } | 42 } |
46 | 43 |
47 class MockObserver : public views::SingleSplitViewListener { | 44 class SingleSplitViewListenerImpl : public views::SingleSplitViewListener { |
48 public: | 45 public: |
49 MOCK_METHOD1(SplitHandleMoved, bool(views::SingleSplitView*)); | 46 SingleSplitViewListenerImpl() : count_(0) {} |
| 47 |
| 48 virtual bool SplitHandleMoved(views::SingleSplitView* sender) OVERRIDE { |
| 49 ++count_; |
| 50 return false; |
| 51 } |
| 52 |
| 53 int count() const { return count_; } |
| 54 |
| 55 private: |
| 56 int count_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(SingleSplitViewListenerImpl); |
50 }; | 59 }; |
51 | 60 |
52 class MinimumSizedView: public views::View { | 61 class MinimumSizedView: public views::View { |
53 public: | 62 public: |
54 MinimumSizedView(gfx::Size min_size) : min_size_(min_size) {} | 63 MinimumSizedView(gfx::Size min_size) : min_size_(min_size) {} |
55 | 64 |
56 private: | 65 private: |
57 gfx::Size min_size_; | 66 gfx::Size min_size_; |
58 virtual gfx::Size GetMinimumSize() OVERRIDE; | 67 virtual gfx::Size GetMinimumSize() OVERRIDE; |
59 }; | 68 }; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 143 |
135 split.child_at(0)->SetVisible(true); | 144 split.child_at(0)->SetVisible(true); |
136 split.child_at(1)->SetVisible(false); | 145 split.child_at(1)->SetVisible(false); |
137 split.Layout(); | 146 split.Layout(); |
138 | 147 |
139 EXPECT_EQ(split.size(), split.child_at(0)->size()); | 148 EXPECT_EQ(split.size(), split.child_at(0)->size()); |
140 } | 149 } |
141 } | 150 } |
142 | 151 |
143 TEST(SingleSplitViewTest, MouseDrag) { | 152 TEST(SingleSplitViewTest, MouseDrag) { |
144 MockObserver observer; | |
145 const int kMinimumChildSize = 25; | 153 const int kMinimumChildSize = 25; |
146 MinimumSizedView *child0 = | 154 MinimumSizedView *child0 = |
147 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); | 155 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); |
148 MinimumSizedView *child1 = | 156 MinimumSizedView *child1 = |
149 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); | 157 new MinimumSizedView(gfx::Size(5, kMinimumChildSize)); |
| 158 SingleSplitViewListenerImpl listener; |
150 SingleSplitView split( | 159 SingleSplitView split( |
151 child0, child1, SingleSplitView::VERTICAL_SPLIT, &observer); | 160 child0, child1, SingleSplitView::VERTICAL_SPLIT, &listener); |
152 | |
153 ON_CALL(observer, SplitHandleMoved(_)) | |
154 .WillByDefault(Return(true)); | |
155 // SplitHandleMoved is called for two mouse moves and one mouse capture loss. | |
156 EXPECT_CALL(observer, SplitHandleMoved(_)) | |
157 .Times(5); | |
158 | 161 |
159 const int kTotalSplitSize = 100; | 162 const int kTotalSplitSize = 100; |
160 split.SetBounds(0, 0, 10, kTotalSplitSize); | 163 split.SetBounds(0, 0, 10, kTotalSplitSize); |
161 const int kInitialDividerOffset = 33; | 164 const int kInitialDividerOffset = 33; |
162 const int kMouseOffset = 2; // Mouse offset in the divider. | 165 const int kMouseOffset = 2; // Mouse offset in the divider. |
163 const int kMouseMoveDelta = 7; | 166 const int kMouseMoveDelta = 7; |
164 split.set_divider_offset(kInitialDividerOffset); | 167 split.set_divider_offset(kInitialDividerOffset); |
165 split.Layout(); | 168 split.Layout(); |
166 | 169 |
167 gfx::Point press_point(7, kInitialDividerOffset + kMouseOffset); | 170 gfx::Point press_point(7, kInitialDividerOffset + kMouseOffset); |
168 ui::MouseEvent mouse_pressed( | 171 ui::MouseEvent mouse_pressed( |
169 ui::ET_MOUSE_PRESSED, press_point, press_point, 0); | 172 ui::ET_MOUSE_PRESSED, press_point, press_point, 0); |
170 ASSERT_TRUE(split.OnMousePressed(mouse_pressed)); | 173 ASSERT_TRUE(split.OnMousePressed(mouse_pressed)); |
171 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); | 174 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); |
| 175 EXPECT_EQ(0, listener.count()); |
172 | 176 |
173 // Drag divider to the bottom. | 177 // Drag divider to the bottom. |
174 gfx::Point drag_1_point( | 178 gfx::Point drag_1_point( |
175 5, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta); | 179 5, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta); |
176 ui::MouseEvent mouse_dragged_1( | 180 ui::MouseEvent mouse_dragged_1( |
177 ui::ET_MOUSE_DRAGGED, drag_1_point, drag_1_point, 0); | 181 ui::ET_MOUSE_DRAGGED, drag_1_point, drag_1_point, 0); |
178 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_1)); | 182 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_1)); |
179 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta, split.divider_offset()); | 183 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta, split.divider_offset()); |
| 184 EXPECT_EQ(1, listener.count()); |
180 | 185 |
181 // Drag divider to the top, beyond first child minimum size. | 186 // Drag divider to the top, beyond first child minimum size. |
182 gfx::Point drag_2_point( | 187 gfx::Point drag_2_point( |
183 7, kMinimumChildSize - 5); | 188 7, kMinimumChildSize - 5); |
184 ui::MouseEvent mouse_dragged_2( | 189 ui::MouseEvent mouse_dragged_2( |
185 ui::ET_MOUSE_DRAGGED, drag_2_point, drag_2_point, 0); | 190 ui::ET_MOUSE_DRAGGED, drag_2_point, drag_2_point, 0); |
186 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_2)); | 191 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_2)); |
187 EXPECT_EQ(kMinimumChildSize, | 192 EXPECT_EQ(kMinimumChildSize, split.divider_offset()); |
188 split.divider_offset()); | 193 EXPECT_EQ(2, listener.count()); |
189 | 194 |
190 // Drag divider to the bottom, beyond second child minimum size. | 195 // Drag divider to the bottom, beyond second child minimum size. |
191 gfx::Point drag_3_point( | 196 gfx::Point drag_3_point( |
192 7, kTotalSplitSize - kMinimumChildSize + 5); | 197 7, kTotalSplitSize - kMinimumChildSize + 5); |
193 ui::MouseEvent mouse_dragged_3( | 198 ui::MouseEvent mouse_dragged_3( |
194 ui::ET_MOUSE_DRAGGED, drag_3_point, drag_3_point, 0); | 199 ui::ET_MOUSE_DRAGGED, drag_3_point, drag_3_point, 0); |
195 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_3)); | 200 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_3)); |
196 EXPECT_EQ(kTotalSplitSize - kMinimumChildSize - split.GetDividerSize(), | 201 EXPECT_EQ(kTotalSplitSize - kMinimumChildSize - split.GetDividerSize(), |
197 split.divider_offset()); | 202 split.divider_offset()); |
| 203 EXPECT_EQ(3, listener.count()); |
198 | 204 |
199 // Drag divider between childs' minimum sizes. | 205 // Drag divider between childs' minimum sizes. |
200 gfx::Point drag_4_point( | 206 gfx::Point drag_4_point( |
201 6, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); | 207 6, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); |
202 ui::MouseEvent mouse_dragged_4( | 208 ui::MouseEvent mouse_dragged_4( |
203 ui::ET_MOUSE_DRAGGED, drag_4_point, drag_4_point, 0); | 209 ui::ET_MOUSE_DRAGGED, drag_4_point, drag_4_point, 0); |
204 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_4)); | 210 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_4)); |
205 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, | 211 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, |
206 split.divider_offset()); | 212 split.divider_offset()); |
| 213 EXPECT_EQ(4, listener.count()); |
207 | 214 |
208 gfx::Point release_point( | 215 gfx::Point release_point( |
209 7, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); | 216 7, kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2); |
210 ui::MouseEvent mouse_released( | 217 ui::MouseEvent mouse_released( |
211 ui::ET_MOUSE_RELEASED, release_point, release_point, 0); | 218 ui::ET_MOUSE_RELEASED, release_point, release_point, 0); |
212 split.OnMouseReleased(mouse_released); | 219 split.OnMouseReleased(mouse_released); |
213 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, | 220 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, |
214 split.divider_offset()); | 221 split.divider_offset()); |
215 | 222 |
216 // Expect intial offset after a system/user gesture cancels the drag. | 223 // Expect intial offset after a system/user gesture cancels the drag. |
217 // This shouldn't occur after mouse release, but it's sufficient for testing. | 224 // This shouldn't occur after mouse release, but it's sufficient for testing. |
218 split.OnMouseCaptureLost(); | 225 split.OnMouseCaptureLost(); |
219 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); | 226 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); |
| 227 EXPECT_EQ(5, listener.count()); |
220 } | 228 } |
221 | 229 |
222 } // namespace views | 230 } // namespace views |
OLD | NEW |