| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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/views/test/native_widget_factory.h" | |
| 6 | |
| 7 #if defined(USE_AURA) | |
| 8 #include "ui/views/widget/native_widget_aura.h" | |
| 9 #if !defined(OS_CHROMEOS) | |
| 10 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" | |
| 11 #endif | |
| 12 #elif defined(OS_MACOSX) | |
| 13 #include "ui/views/widget/native_widget_mac.h" | |
| 14 #endif | |
| 15 | |
| 16 namespace views { | |
| 17 namespace test { | |
| 18 | |
| 19 #if defined(USE_AURA) | |
| 20 using PlatformNativeWidget = NativeWidgetAura; | |
| 21 #elif defined(OS_MACOSX) | |
| 22 using PlatformNativeWidget = NativeWidgetMac; | |
| 23 #endif | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // NativeWidget implementation that adds the following: | |
| 28 // . capture can be mocked. | |
| 29 // . a boolean is set when the NativeWiget is destroyed. | |
| 30 class TestPlatformNativeWidget : public PlatformNativeWidget { | |
| 31 public: | |
| 32 TestPlatformNativeWidget(internal::NativeWidgetDelegate* delegate, | |
| 33 bool mock_capture, | |
| 34 bool* destroyed); | |
| 35 ~TestPlatformNativeWidget() override; | |
| 36 | |
| 37 void SetCapture() override; | |
| 38 void ReleaseCapture() override; | |
| 39 bool HasCapture() const override; | |
| 40 | |
| 41 private: | |
| 42 bool mouse_capture_; | |
| 43 const bool mock_capture_; | |
| 44 bool* destroyed_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(TestPlatformNativeWidget); | |
| 47 }; | |
| 48 | |
| 49 // A widget that assumes mouse capture always works. It won't in testing, so we | |
| 50 // mock it. | |
| 51 TestPlatformNativeWidget::TestPlatformNativeWidget( | |
| 52 internal::NativeWidgetDelegate* delegate, | |
| 53 bool mock_capture, | |
| 54 bool* destroyed) | |
| 55 : PlatformNativeWidget(delegate), | |
| 56 mouse_capture_(false), | |
| 57 mock_capture_(mock_capture), | |
| 58 destroyed_(destroyed) {} | |
| 59 | |
| 60 TestPlatformNativeWidget::~TestPlatformNativeWidget() { | |
| 61 if (destroyed_) | |
| 62 *destroyed_ = true; | |
| 63 } | |
| 64 | |
| 65 void TestPlatformNativeWidget::SetCapture() { | |
| 66 if (mock_capture_) | |
| 67 mouse_capture_ = true; | |
| 68 else | |
| 69 PlatformNativeWidget::SetCapture(); | |
| 70 } | |
| 71 | |
| 72 void TestPlatformNativeWidget::ReleaseCapture() { | |
| 73 if (mock_capture_) { | |
| 74 if (mouse_capture_) | |
| 75 delegate()->OnMouseCaptureLost(); | |
| 76 mouse_capture_ = false; | |
| 77 } else { | |
| 78 PlatformNativeWidget::ReleaseCapture(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 bool TestPlatformNativeWidget::HasCapture() const { | |
| 83 return mock_capture_ ? mouse_capture_ : PlatformNativeWidget::HasCapture(); | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 // Factory methods ------------------------------------------------------------ | |
| 89 | |
| 90 NativeWidget* CreatePlatformNativeWidgetImpl( | |
| 91 const Widget::InitParams& init_params, | |
| 92 Widget* widget, | |
| 93 uint32_t type, | |
| 94 bool* destroyed) { | |
| 95 return new TestPlatformNativeWidget(widget, type == kStubCapture, destroyed); | |
| 96 } | |
| 97 | |
| 98 } // namespace test | |
| 99 } // namespace views | |
| OLD | NEW |