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