| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/aura/mus/window_tree_client.h" |
| 6 |
| 7 #include "components/viz/client/hit_test_data_provider.h" |
| 8 #include "ui/aura/client/aura_constants.h" |
| 9 #include "ui/aura/mus/window_mus.h" |
| 10 #include "ui/aura/mus/window_port_mus.h" |
| 11 #include "ui/aura/test/aura_mus_test_base.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_targeter.h" |
| 14 #include "ui/gfx/geometry/rect.h" |
| 15 |
| 16 namespace aura { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const int kMouseInset = -5; |
| 21 const int kTouchInset = -10; |
| 22 |
| 23 Id server_id(Window* window) { |
| 24 return WindowMus::Get(window)->server_id(); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 // Creates a root window and child windows. Maintains a cc:LayerTreeFrameSink |
| 30 // to help exercise its viz::HitTestDataProvider. |
| 31 class HitTestDataProviderMusTest : public test::AuraTestBaseMus { |
| 32 public: |
| 33 HitTestDataProviderMusTest() {} |
| 34 ~HitTestDataProviderMusTest() override {} |
| 35 |
| 36 void SetUp() override { |
| 37 test::AuraTestBaseMus::SetUp(); |
| 38 |
| 39 root_ = base::MakeUnique<Window>(nullptr); |
| 40 root_->Init(ui::LAYER_NOT_DRAWN); |
| 41 root_->SetEventTargeter(base::MakeUnique<WindowTargeter>()); |
| 42 root_->SetBounds(gfx::Rect(0, 0, 300, 200)); |
| 43 |
| 44 window2_ = new Window(nullptr); |
| 45 window2_->Init(ui::LAYER_TEXTURED); |
| 46 window2_->SetBounds(gfx::Rect(20, 30, 40, 60)); |
| 47 |
| 48 window3_ = new Window(nullptr); |
| 49 window3_->Init(ui::LAYER_TEXTURED); |
| 50 window3_->SetEventTargeter(base::MakeUnique<WindowTargeter>()); |
| 51 window3_->SetBounds(gfx::Rect(50, 60, 100, 40)); |
| 52 |
| 53 window4_ = new Window(nullptr); |
| 54 window4_->Init(ui::LAYER_TEXTURED); |
| 55 window4_->SetBounds(gfx::Rect(25, 10, 50, 20)); |
| 56 |
| 57 window3_->AddChild(window4_); |
| 58 root_->AddChild(window2_); |
| 59 root_->AddChild(window3_); |
| 60 |
| 61 WindowPortMus* port = WindowPortMus::Get(root_.get()); |
| 62 sink_ = port->CreateLayerTreeFrameSink(); |
| 63 } |
| 64 |
| 65 protected: |
| 66 viz::HitTestDataProvider* hit_test_provider() { |
| 67 WindowPortMus* port = WindowPortMus::Get(root_.get()); |
| 68 return port->local_layer_tree_frame_sink_->hit_test_data_provider_.get(); |
| 69 } |
| 70 |
| 71 Window* root() { return root_.get(); } |
| 72 Window* window2() { return window2_; } |
| 73 Window* window3() { return window3_; } |
| 74 Window* window4() { return window4_; } |
| 75 |
| 76 private: |
| 77 std::unique_ptr<cc::LayerTreeFrameSink> sink_; |
| 78 std::unique_ptr<Window> root_; |
| 79 Window* window2_; |
| 80 Window* window3_; |
| 81 Window* window4_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(HitTestDataProviderMusTest); |
| 84 }; |
| 85 |
| 86 // Custom WindowTargeter that expands hit-test regions of child windows. |
| 87 class TestWindowTargeter : public WindowTargeter { |
| 88 public: |
| 89 TestWindowTargeter() {} |
| 90 ~TestWindowTargeter() override {} |
| 91 |
| 92 protected: |
| 93 // WindowTargeter: |
| 94 bool GetHitTestRects(aura::Window* window, |
| 95 gfx::Rect* rect_mouse, |
| 96 gfx::Rect* rect_touch) const override { |
| 97 if (rect_mouse) { |
| 98 *rect_mouse = gfx::Rect(window->bounds()); |
| 99 rect_mouse->Inset(gfx::Insets(kMouseInset)); |
| 100 } |
| 101 if (rect_touch) { |
| 102 *rect_touch = gfx::Rect(window->bounds()); |
| 103 rect_touch->Inset(gfx::Insets(kTouchInset)); |
| 104 } |
| 105 return true; |
| 106 } |
| 107 |
| 108 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(TestWindowTargeter); |
| 110 }; |
| 111 |
| 112 // Tests that the order of reported hit-test regions matches windows Z-order. |
| 113 TEST_F(HitTestDataProviderMusTest, Stacking) { |
| 114 std::unique_ptr<viz::HitTestDataProvider::HitTestRegionList> hit_test_data = |
| 115 hit_test_provider()->GetHitTestData(); |
| 116 |
| 117 Window* expected_order_1[] = {window3(), window4(), window2()}; |
| 118 EXPECT_EQ(arraysize(expected_order_1), hit_test_data->size()); |
| 119 int i = 0; |
| 120 for (const auto& region : *hit_test_data) { |
| 121 EXPECT_EQ(0x03U, region.flags); |
| 122 EXPECT_EQ(WindowPortMus::Get(expected_order_1[i])->frame_sink_id(), |
| 123 region.frame_sink_id); |
| 124 EXPECT_EQ(server_id(expected_order_1[i]), region.window_id); |
| 125 EXPECT_EQ(expected_order_1[i]->bounds().ToString(), region.rect.ToString()); |
| 126 i++; |
| 127 } |
| 128 |
| 129 root()->StackChildAbove(window2(), window3()); |
| 130 hit_test_data = hit_test_provider()->GetHitTestData(); |
| 131 |
| 132 Window* expected_order_2[] = {window2(), window3(), window4()}; |
| 133 EXPECT_EQ(arraysize(expected_order_2), hit_test_data->size()); |
| 134 i = 0; |
| 135 for (const auto& region : *hit_test_data) { |
| 136 EXPECT_EQ(0x03U, region.flags); |
| 137 EXPECT_EQ(WindowPortMus::Get(expected_order_2[i])->frame_sink_id(), |
| 138 region.frame_sink_id); |
| 139 EXPECT_EQ(server_id(expected_order_2[i]), region.window_id); |
| 140 EXPECT_EQ(expected_order_2[i]->bounds().ToString(), region.rect.ToString()); |
| 141 i++; |
| 142 } |
| 143 } |
| 144 |
| 145 // Tests that the hit-test regions get expanded with a custom event targeter. |
| 146 TEST_F(HitTestDataProviderMusTest, CustomTargeter) { |
| 147 window3()->SetEventTargeter(base::MakeUnique<TestWindowTargeter>()); |
| 148 std::unique_ptr<viz::HitTestDataProvider::HitTestRegionList> hit_test_data = |
| 149 hit_test_provider()->GetHitTestData(); |
| 150 |
| 151 // Children of a container that has the custom targeter installed will get |
| 152 // reported twice, once with hit-test bounds optimized for mouse events and |
| 153 // another time with bounds expanded more for touch input. |
| 154 Window* expected_windows[] = {window3(), window4(), window4(), window2()}; |
| 155 uint32_t expected_flags[] = {0x03U, 0x01U, 0x02U, 0x03U}; |
| 156 int expected_insets[] = {0, kMouseInset, kTouchInset, 0}; |
| 157 ASSERT_EQ(arraysize(expected_windows), hit_test_data->size()); |
| 158 ASSERT_EQ(arraysize(expected_flags), hit_test_data->size()); |
| 159 ASSERT_EQ(arraysize(expected_insets), hit_test_data->size()); |
| 160 int i = 0; |
| 161 for (const auto& region : *hit_test_data) { |
| 162 EXPECT_EQ(WindowPortMus::Get(expected_windows[i])->frame_sink_id(), |
| 163 region.frame_sink_id); |
| 164 EXPECT_EQ(server_id(expected_windows[i]), region.window_id); |
| 165 EXPECT_EQ(expected_flags[i], region.flags); |
| 166 gfx::Rect expected_bounds = expected_windows[i]->bounds(); |
| 167 expected_bounds.Inset(gfx::Insets(expected_insets[i])); |
| 168 EXPECT_EQ(expected_bounds.ToString(), region.rect.ToString()); |
| 169 i++; |
| 170 } |
| 171 } |
| 172 |
| 173 } // namespace aura |
| OLD | NEW |