OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "base/logging.h" |
| 6 #include "testing/gmock/include/gmock/gmock.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "views/controls/single_split_view.h" |
| 9 |
| 10 using ::testing::_; |
| 11 using ::testing::Return; |
| 12 |
| 13 namespace { |
| 14 |
| 15 static void VerifySplitViewLayout(const views::SingleSplitView& split) { |
| 16 ASSERT_EQ(2, split.GetChildViewCount()); |
| 17 |
| 18 views::View* leading = split.GetChildViewAt(0); |
| 19 views::View* trailing = split.GetChildViewAt(1); |
| 20 |
| 21 if (split.bounds().IsEmpty()) { |
| 22 EXPECT_TRUE(leading->bounds().IsEmpty()); |
| 23 EXPECT_TRUE(trailing->bounds().IsEmpty()); |
| 24 return; |
| 25 } |
| 26 |
| 27 EXPECT_FALSE(leading->bounds().IsEmpty()); |
| 28 EXPECT_FALSE(trailing->bounds().IsEmpty()); |
| 29 EXPECT_FALSE(leading->bounds().Intersects(trailing->bounds())); |
| 30 |
| 31 if (split.orientation() == views::SingleSplitView::HORIZONTAL_SPLIT) { |
| 32 EXPECT_EQ(leading->bounds().height(), split.bounds().height()); |
| 33 EXPECT_EQ(trailing->bounds().height(), split.bounds().height()); |
| 34 EXPECT_LT(leading->bounds().width() + trailing->bounds().width(), |
| 35 split.bounds().width()); |
| 36 } else if (split.orientation() == views::SingleSplitView::VERTICAL_SPLIT) { |
| 37 EXPECT_EQ(leading->bounds().width(), split.bounds().width()); |
| 38 EXPECT_EQ(trailing->bounds().width(), split.bounds().width()); |
| 39 EXPECT_LT(leading->bounds().height() + trailing->bounds().height(), |
| 40 split.bounds().height()); |
| 41 } else { |
| 42 NOTREACHED(); |
| 43 } |
| 44 } |
| 45 |
| 46 class MockObserver : public views::SingleSplitView::Observer { |
| 47 public: |
| 48 MOCK_METHOD1(SplitHandleMoved, bool(views::SingleSplitView*)); |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 namespace views { |
| 54 |
| 55 TEST(SingleSplitViewTest, Resize) { |
| 56 // Test cases to iterate through for horizontal and vertical split views. |
| 57 struct TestCase { |
| 58 // Split view resize policy for this test case. |
| 59 bool resize_leading_on_bounds_change; |
| 60 // Split view size to set. |
| 61 int primary_axis_size; |
| 62 int secondary_axis_size; |
| 63 // Expected divider offset. |
| 64 int divider_offset; |
| 65 } test_cases[] = { |
| 66 // The initial split size is 100x100, divider at 33. |
| 67 { true, 100, 100, 33 }, |
| 68 // Grow the split view, leading view should grow. |
| 69 { true, 1000, 100, 933 }, |
| 70 // Shrink the split view, leading view should shrink. |
| 71 { true, 200, 100, 133 }, |
| 72 // Minimize the split view, divider should not move. |
| 73 { true, 0, 0, 133 }, |
| 74 // Restore the split view, divider should not move. |
| 75 { false, 500, 100, 133 }, |
| 76 // Resize the split view by secondary axis, divider should not move. |
| 77 { false, 500, 600, 133 } |
| 78 }; |
| 79 |
| 80 SingleSplitView::Orientation orientations[] = { |
| 81 SingleSplitView::HORIZONTAL_SPLIT, |
| 82 SingleSplitView::VERTICAL_SPLIT |
| 83 }; |
| 84 |
| 85 for (size_t orientation = 0; orientation < arraysize(orientations); |
| 86 ++orientation) { |
| 87 // Create a split view. |
| 88 SingleSplitView split( |
| 89 new View(), new View(), orientations[orientation], NULL); |
| 90 |
| 91 // Set initial size and divider offset. |
| 92 EXPECT_EQ(test_cases[0].primary_axis_size, |
| 93 test_cases[0].secondary_axis_size); |
| 94 split.SetBounds(0, 0, test_cases[0].primary_axis_size, |
| 95 test_cases[0].secondary_axis_size); |
| 96 split.set_divider_offset(test_cases[0].divider_offset); |
| 97 split.Layout(); |
| 98 |
| 99 // Run all test cases. |
| 100 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 101 split.set_resize_leading_on_bounds_change( |
| 102 test_cases[i].resize_leading_on_bounds_change); |
| 103 if (split.orientation() == SingleSplitView::HORIZONTAL_SPLIT) { |
| 104 split.SetBounds(0, 0, test_cases[i].primary_axis_size, |
| 105 test_cases[i].secondary_axis_size); |
| 106 } else { |
| 107 split.SetBounds(0, 0, test_cases[i].secondary_axis_size, |
| 108 test_cases[i].primary_axis_size); |
| 109 } |
| 110 |
| 111 EXPECT_EQ(test_cases[i].divider_offset, split.divider_offset()); |
| 112 VerifySplitViewLayout(split); |
| 113 } |
| 114 |
| 115 // Special cases, one of the child views is hidden. |
| 116 split.GetChildViewAt(0)->SetVisible(false); |
| 117 split.Layout(); |
| 118 |
| 119 EXPECT_EQ(split.bounds().size(), |
| 120 split.GetChildViewAt(1)->bounds().size()); |
| 121 |
| 122 split.GetChildViewAt(0)->SetVisible(true); |
| 123 split.GetChildViewAt(1)->SetVisible(false); |
| 124 split.Layout(); |
| 125 |
| 126 EXPECT_EQ(split.bounds().size(), |
| 127 split.GetChildViewAt(0)->bounds().size()); |
| 128 } |
| 129 } |
| 130 |
| 131 TEST(SingleSplitViewTest, MouseDrag) { |
| 132 MockObserver observer; |
| 133 SingleSplitView split( |
| 134 new View(), new View(), SingleSplitView::VERTICAL_SPLIT, &observer); |
| 135 |
| 136 ON_CALL(observer, SplitHandleMoved(_)) |
| 137 .WillByDefault(Return(true)); |
| 138 // SplitHandleMoved is expected to be called once for every mouse move. |
| 139 EXPECT_CALL(observer, SplitHandleMoved(_)) |
| 140 .Times(2); |
| 141 |
| 142 split.SetBounds(0, 0, 10, 100); |
| 143 const int kInitialDividerOffset = 33; |
| 144 const int kMouseOffset = 2; // Mouse offset in the divider. |
| 145 const int kMouseMoveDelta = 7; |
| 146 split.set_divider_offset(kInitialDividerOffset); |
| 147 split.Layout(); |
| 148 |
| 149 // Drag divider to the right, in 2 steps. |
| 150 MouseEvent mouse_pressed( |
| 151 Event::ET_MOUSE_PRESSED, 7, kInitialDividerOffset + kMouseOffset, 0); |
| 152 ASSERT_TRUE(split.OnMousePressed(mouse_pressed)); |
| 153 EXPECT_EQ(kInitialDividerOffset, split.divider_offset()); |
| 154 |
| 155 MouseEvent mouse_dragged_1( |
| 156 Event::ET_MOUSE_DRAGGED, 5, |
| 157 kInitialDividerOffset + kMouseOffset + kMouseMoveDelta, 0); |
| 158 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_1)); |
| 159 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta, split.divider_offset()); |
| 160 |
| 161 MouseEvent mouse_dragged_2( |
| 162 Event::ET_MOUSE_DRAGGED, 6, |
| 163 kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2, 0); |
| 164 ASSERT_TRUE(split.OnMouseDragged(mouse_dragged_2)); |
| 165 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, |
| 166 split.divider_offset()); |
| 167 |
| 168 MouseEvent mouse_released( |
| 169 Event::ET_MOUSE_RELEASED, 7, |
| 170 kInitialDividerOffset + kMouseOffset + kMouseMoveDelta * 2, 0); |
| 171 split.OnMouseReleased(mouse_released, false); |
| 172 EXPECT_EQ(kInitialDividerOffset + kMouseMoveDelta * 2, |
| 173 split.divider_offset()); |
| 174 } |
| 175 |
| 176 } // namespace views |
OLD | NEW |