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 "services/ui/ws/platform_display_default.h" |
| 6 |
| 7 #include "base/time/time.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/base/cursor/image_cursors.h" |
| 10 #include "ui/display/types/native_display_delegate.h" |
| 11 #include "ui/events/event.h" |
| 12 #include "ui/events/event_sink.h" |
| 13 #include "ui/gfx/geometry/point.h" |
| 14 #include "ui/ozone/public/ozone_platform.h" |
| 15 #include "ui/ozone/public/system_input_injector.h" |
| 16 #include "ui/platform_window/platform_window.h" |
| 17 #include "ui/platform_window/stub/stub_window.h" |
| 18 |
| 19 namespace ui { |
| 20 namespace ws { |
| 21 namespace { |
| 22 |
| 23 // An EventSink that records events sent to it. |
| 24 class TestEventSink : public EventSink { |
| 25 public: |
| 26 TestEventSink() { Reset(); } |
| 27 ~TestEventSink() override = default; |
| 28 |
| 29 void Reset() { |
| 30 count_ = 0; |
| 31 last_event_type_ = ET_UNKNOWN; |
| 32 } |
| 33 |
| 34 // EventSink: |
| 35 EventDispatchDetails OnEventFromSource(Event* event) override { |
| 36 count_++; |
| 37 last_event_type_ = event->type(); |
| 38 return EventDispatchDetails(); |
| 39 } |
| 40 |
| 41 int count_; |
| 42 EventType last_event_type_; |
| 43 }; |
| 44 |
| 45 // A PlatformDisplayDelegate to connect the PlatformDisplay to a TestEventSink. |
| 46 class TestPlatformDisplayDelegate : public PlatformDisplayDelegate { |
| 47 public: |
| 48 TestPlatformDisplayDelegate(TestEventSink* sink, OzonePlatform* platform) |
| 49 : event_sink_(sink), ozone_platform_(platform) {} |
| 50 ~TestPlatformDisplayDelegate() override = default; |
| 51 |
| 52 // PlatformDisplayDelegate: |
| 53 const display::Display& GetDisplay() override { return stub_display_; } |
| 54 ServerWindow* GetRootWindow() override { return nullptr; } |
| 55 EventSink* GetEventSink() override { return event_sink_; } |
| 56 void OnAcceleratedWidgetAvailable() override {} |
| 57 void OnNativeCaptureLost() override {} |
| 58 OzonePlatform* GetOzonePlatform() override { return ozone_platform_; } |
| 59 |
| 60 private: |
| 61 TestEventSink* event_sink_; |
| 62 OzonePlatform* ozone_platform_; |
| 63 display::Display stub_display_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(TestPlatformDisplayDelegate); |
| 66 }; |
| 67 |
| 68 // An OzonePlatform that creates StubWindows. |
| 69 class TestOzonePlatform : public OzonePlatform { |
| 70 public: |
| 71 TestOzonePlatform() = default; |
| 72 ~TestOzonePlatform() override = default; |
| 73 |
| 74 // OzonePlatform: |
| 75 ui::SurfaceFactoryOzone* GetSurfaceFactoryOzone() override { return nullptr; } |
| 76 ui::OverlayManagerOzone* GetOverlayManager() override { return nullptr; } |
| 77 ui::CursorFactoryOzone* GetCursorFactoryOzone() override { return nullptr; } |
| 78 ui::InputController* GetInputController() override { return nullptr; } |
| 79 ui::GpuPlatformSupportHost* GetGpuPlatformSupportHost() override { |
| 80 return nullptr; |
| 81 } |
| 82 std::unique_ptr<SystemInputInjector> CreateSystemInputInjector() override { |
| 83 return nullptr; |
| 84 } |
| 85 std::unique_ptr<PlatformWindow> CreatePlatformWindow( |
| 86 PlatformWindowDelegate* delegate, |
| 87 const gfx::Rect& bounds) override { |
| 88 return base::MakeUnique<StubWindow>( |
| 89 delegate, false /* use_default_accelerated_widget */); |
| 90 } |
| 91 std::unique_ptr<display::NativeDisplayDelegate> CreateNativeDisplayDelegate() |
| 92 override { |
| 93 return nullptr; |
| 94 } |
| 95 void InitializeUI(const InitParams& params) override {} |
| 96 void InitializeGPU(const InitParams& params) override {} |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(TestOzonePlatform); |
| 100 }; |
| 101 |
| 102 TEST(PlatformDisplayDefaultTest, EventDispatch) { |
| 103 // Setup ozone so the display can be initialized. |
| 104 TestOzonePlatform platform; |
| 105 |
| 106 // Create the display. |
| 107 display::ViewportMetrics metrics; |
| 108 metrics.bounds_in_pixels = gfx::Rect(1024, 768); |
| 109 metrics.device_scale_factor = 1.f; |
| 110 metrics.ui_scale_factor = 1.f; |
| 111 PlatformDisplayDefault display(nullptr, metrics, |
| 112 std::unique_ptr<ImageCursors>()); |
| 113 |
| 114 // Initialize the display with a test EventSink so we can sense events. |
| 115 TestEventSink event_sink; |
| 116 TestPlatformDisplayDelegate delegate(&event_sink, &platform); |
| 117 display.Init(&delegate); |
| 118 |
| 119 // Event dispatch is handled at the PlatformWindowDelegate level. |
| 120 PlatformWindowDelegate* display_for_dispatch = |
| 121 static_cast<PlatformWindowDelegate*>(&display); |
| 122 |
| 123 // Mouse events are converted to pointer events. |
| 124 MouseEvent mouse(ET_MOUSE_PRESSED, gfx::Point(1, 2), gfx::Point(1, 2), |
| 125 base::TimeTicks(), EF_NONE, 0); |
| 126 display_for_dispatch->DispatchEvent(&mouse); |
| 127 EXPECT_EQ(ET_POINTER_DOWN, event_sink.last_event_type_); |
| 128 event_sink.Reset(); |
| 129 |
| 130 // Touch events are converted to pointer events. |
| 131 TouchEvent touch(ET_TOUCH_PRESSED, gfx::Point(3, 4), base::TimeTicks(), |
| 132 PointerDetails(EventPointerType::POINTER_TYPE_TOUCH, 0)); |
| 133 display_for_dispatch->DispatchEvent(&touch); |
| 134 EXPECT_EQ(ET_POINTER_DOWN, event_sink.last_event_type_); |
| 135 event_sink.Reset(); |
| 136 |
| 137 // Pressing a key dispatches exactly one event. |
| 138 KeyEvent key_pressed(ET_KEY_PRESSED, VKEY_A, EF_NONE); |
| 139 display_for_dispatch->DispatchEvent(&key_pressed); |
| 140 EXPECT_EQ(1, event_sink.count_); |
| 141 EXPECT_EQ(ET_KEY_PRESSED, event_sink.last_event_type_); |
| 142 event_sink.Reset(); |
| 143 |
| 144 // Releasing the key dispatches exactly one event. |
| 145 KeyEvent key_released(ET_KEY_RELEASED, VKEY_A, EF_NONE); |
| 146 display_for_dispatch->DispatchEvent(&key_released); |
| 147 EXPECT_EQ(1, event_sink.count_); |
| 148 EXPECT_EQ(ET_KEY_RELEASED, event_sink.last_event_type_); |
| 149 } |
| 150 |
| 151 } // namespace |
| 152 } // namespace ws |
| 153 } // namespace ui |
OLD | NEW |