MockWidget.h (2953B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef GTEST_MOCKWIDGET_H 8 #define GTEST_MOCKWIDGET_H 9 10 #include "mozilla/gfx/Point.h" 11 #include "mozilla/widget/InProcessCompositorWidget.h" 12 #include "nsIWidget.h" 13 #include "GLContext.h" 14 #include "GLContextProvider.h" 15 16 using mozilla::gl::CreateContextFlags; 17 using mozilla::gl::GLContext; 18 using mozilla::gl::GLContextProvider; 19 20 using mozilla::gfx::IntSize; 21 22 class MockWidget : public nsIWidget { 23 public: 24 MockWidget() : mCompWidth(0), mCompHeight(0) {} 25 MockWidget(int aWidth, int aHeight) 26 : mCompWidth(aWidth), mCompHeight(aHeight) {} 27 NS_DECL_ISUPPORTS_INHERITED 28 29 LayoutDeviceIntRect GetClientBounds() override { 30 return LayoutDeviceIntRect(0, 0, mCompWidth, mCompHeight); 31 } 32 LayoutDeviceIntRect GetBounds() override { return GetClientBounds(); } 33 34 void* GetNativeData(uint32_t aDataType) override { 35 if (aDataType == NS_NATIVE_OPENGL_CONTEXT) { 36 nsCString discardFailureId; 37 RefPtr<GLContext> context = GLContextProvider::CreateHeadless( 38 {CreateContextFlags::REQUIRE_COMPAT_PROFILE}, &discardFailureId); 39 if (!context) { 40 return nullptr; 41 } 42 if (!context->CreateOffscreenDefaultFb({mCompWidth, mCompHeight})) { 43 return nullptr; 44 } 45 return context.forget().take(); 46 } 47 return nullptr; 48 } 49 50 nsresult Create(nsIWidget* aParent, const LayoutDeviceIntRect&, 51 const InitData&) override { 52 return NS_OK; 53 } 54 nsresult Create(nsIWidget* aParent, const DesktopIntRect&, 55 const InitData&) override { 56 return NS_OK; 57 } 58 void Show(bool aState) override {} 59 bool IsVisible() const override { return true; } 60 void Move(const DesktopPoint&) override {} 61 void Resize(const DesktopSize&, bool aRepaint) override {} 62 void Resize(const DesktopRect&, bool aRepaint) override {} 63 64 void Enable(bool aState) override {} 65 bool IsEnabled() const override { return true; } 66 67 nsSizeMode SizeMode() override { return mSizeMode; } 68 void SetSizeMode(nsSizeMode aMode) override { mSizeMode = aMode; } 69 70 void SetFocus(Raise, mozilla::dom::CallerType aCallerType) override {} 71 void Invalidate(const LayoutDeviceIntRect& aRect) override {} 72 nsresult SetTitle(const nsAString& title) override { return NS_OK; } 73 LayoutDeviceIntPoint WidgetToScreenOffset() override { 74 return LayoutDeviceIntPoint(); 75 } 76 void SetInputContext(const InputContext& aContext, 77 const InputContextAction& aAction) override {} 78 InputContext GetInputContext() override { abort(); } 79 80 private: 81 ~MockWidget() = default; 82 83 nsSizeMode mSizeMode = nsSizeMode_Normal; 84 85 int mCompWidth; 86 int mCompHeight; 87 }; 88 89 #endif