OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/mus/example/wm/move_loop.h" |
| 6 |
| 7 #include "components/mus/public/cpp/tests/test_window.h" |
| 8 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/events/event.h" |
| 11 #include "ui/gfx/geometry/point.h" |
| 12 #include "ui/gfx/geometry/rect.h" |
| 13 |
| 14 using MoveLoopTest = testing::Test; |
| 15 |
| 16 namespace { |
| 17 |
| 18 // Sets the client area for |window|. Padding is provided on the top so that |
| 19 // there is a movable region. All other edges are sized to |
| 20 // |MoveLoop::kResizeSize|. |
| 21 void SetClientArea(mus::Window* window) { |
| 22 if (window->bounds().IsEmpty()) |
| 23 window->SetBounds(gfx::Rect(100, 200, 300, 400)); |
| 24 |
| 25 gfx::Rect client_area(window->bounds().size()); |
| 26 client_area.Inset(MoveLoop::kResizeSize, MoveLoop::kResizeSize + 10, |
| 27 MoveLoop::kResizeSize, MoveLoop::kResizeSize); |
| 28 window->SetClientArea(client_area); |
| 29 } |
| 30 |
| 31 mus::mojom::EventPtr CreatePointerDownEvent(const gfx::Point& location) { |
| 32 const ui::TouchEvent event(ui::ET_TOUCH_PRESSED, gfx::PointF(location), 1, |
| 33 base::TimeDelta()); |
| 34 return mus::mojom::Event::From(static_cast<const ui::Event&>(event)); |
| 35 } |
| 36 |
| 37 mus::mojom::EventPtr CreatePointerMove(const gfx::Point& location) { |
| 38 const ui::TouchEvent event(ui::ET_TOUCH_MOVED, gfx::PointF(location), 1, |
| 39 base::TimeDelta()); |
| 40 return mus::mojom::Event::From(static_cast<const ui::Event&>(event)); |
| 41 } |
| 42 |
| 43 enum class HorizontalLocation { |
| 44 LEFT, |
| 45 MIDDLE, |
| 46 RIGHT, |
| 47 }; |
| 48 |
| 49 enum class VerticalLocation { |
| 50 TOP_MOVE, |
| 51 TOP_RESIZE, |
| 52 MIDDLE, |
| 53 BOTTOM, |
| 54 }; |
| 55 |
| 56 int HorizontalLocationToPoint(const mus::Window* window, |
| 57 HorizontalLocation loc) { |
| 58 if (loc == HorizontalLocation::LEFT) |
| 59 return 0; |
| 60 return loc == HorizontalLocation::MIDDLE ? window->bounds().width() / 2 |
| 61 : window->bounds().width() - 1; |
| 62 } |
| 63 |
| 64 int VerticalLocationToPoint(const mus::Window* window, VerticalLocation loc) { |
| 65 switch (loc) { |
| 66 case VerticalLocation::TOP_MOVE: |
| 67 return MoveLoop::kResizeSize + 1; |
| 68 case VerticalLocation::TOP_RESIZE: |
| 69 return 0; |
| 70 case VerticalLocation::MIDDLE: |
| 71 return window->bounds().height() / 2; |
| 72 case VerticalLocation::BOTTOM: |
| 73 return window->bounds().height() - 1; |
| 74 } |
| 75 return 0; |
| 76 } |
| 77 |
| 78 gfx::Point LocationToPoint(const mus::Window* window, |
| 79 HorizontalLocation h_loc, |
| 80 VerticalLocation v_loc) { |
| 81 return gfx::Point(HorizontalLocationToPoint(window, h_loc), |
| 82 VerticalLocationToPoint(window, v_loc)); |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 87 TEST_F(MoveLoopTest, Move) { |
| 88 struct TestData { |
| 89 HorizontalLocation initial_h_loc; |
| 90 VerticalLocation initial_v_loc; |
| 91 // Number of values in delta_* that are valid. For example, if this is 1 |
| 92 // then only one move is generated with thye point |delta_x[0]|, |
| 93 // |delta_y[0]|. |
| 94 int delta_count; |
| 95 int delta_x[3]; |
| 96 int delta_y[3]; |
| 97 gfx::Rect expected_bounds; |
| 98 } data[] = { |
| 99 // Move. |
| 100 {HorizontalLocation::MIDDLE, |
| 101 VerticalLocation::TOP_MOVE, |
| 102 1, |
| 103 {1, 0, 0}, |
| 104 {2, 0, 0}, |
| 105 gfx::Rect(101, 202, 300, 400)}, |
| 106 |
| 107 // Resize from top, left. |
| 108 {HorizontalLocation::LEFT, |
| 109 VerticalLocation::TOP_RESIZE, |
| 110 1, |
| 111 {1, 0, 0}, |
| 112 {2, 0, 0}, |
| 113 gfx::Rect(101, 202, 299, 398)}, |
| 114 |
| 115 // Resize from left. |
| 116 {HorizontalLocation::LEFT, |
| 117 VerticalLocation::MIDDLE, |
| 118 1, |
| 119 {1, 0, 0}, |
| 120 {2, 0, 0}, |
| 121 gfx::Rect(101, 200, 299, 400)}, |
| 122 |
| 123 // Resize from bottom, left. |
| 124 {HorizontalLocation::LEFT, |
| 125 VerticalLocation::BOTTOM, |
| 126 1, |
| 127 {-1, 0, 0}, |
| 128 {-2, 0, 0}, |
| 129 gfx::Rect(99, 200, 301, 398)}, |
| 130 |
| 131 // Resize from bottom. |
| 132 {HorizontalLocation::MIDDLE, |
| 133 VerticalLocation::BOTTOM, |
| 134 1, |
| 135 {-1, 0, 0}, |
| 136 {4, 0, 0}, |
| 137 gfx::Rect(100, 200, 300, 404)}, |
| 138 |
| 139 // Resize from bottom, right. |
| 140 {HorizontalLocation::RIGHT, |
| 141 VerticalLocation::BOTTOM, |
| 142 1, |
| 143 {-3, 0, 0}, |
| 144 {4, 0, 0}, |
| 145 gfx::Rect(100, 200, 297, 404)}, |
| 146 |
| 147 // Resize from right. |
| 148 {HorizontalLocation::RIGHT, |
| 149 VerticalLocation::MIDDLE, |
| 150 1, |
| 151 {6, 0, 0}, |
| 152 {-4, 0, 0}, |
| 153 gfx::Rect(100, 200, 306, 400)}, |
| 154 |
| 155 // Resize from top, right. |
| 156 {HorizontalLocation::RIGHT, |
| 157 VerticalLocation::TOP_RESIZE, |
| 158 1, |
| 159 {6, 0, 0}, |
| 160 {5, 0, 0}, |
| 161 gfx::Rect(100, 205, 306, 395)}, |
| 162 }; |
| 163 |
| 164 for (size_t i = 0; i < arraysize(data); ++i) { |
| 165 mus::TestWindow window; |
| 166 SetClientArea(&window); |
| 167 gfx::Point pointer_location( |
| 168 LocationToPoint(&window, data[i].initial_h_loc, data[i].initial_v_loc)); |
| 169 scoped_ptr<MoveLoop> move_loop = |
| 170 MoveLoop::Create(&window, *CreatePointerDownEvent(pointer_location)); |
| 171 ASSERT_TRUE(move_loop.get()) << i; |
| 172 for (int j = 0; j < data[i].delta_count; ++j) { |
| 173 pointer_location.Offset(data[i].delta_x[j], data[i].delta_y[j]); |
| 174 ASSERT_EQ(MoveLoop::MoveResult::CONTINUE, |
| 175 move_loop->Move(*CreatePointerMove(pointer_location))) |
| 176 << i << " " << j; |
| 177 } |
| 178 ASSERT_EQ(data[i].expected_bounds, window.bounds()); |
| 179 } |
| 180 } |
OLD | NEW |