xref: /aosp_15_r20/external/angle/src/libANGLE/Surface_unittest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "gmock/gmock.h"
8 #include "gtest/gtest.h"
9 
10 #include "libANGLE/AttributeMap.h"
11 #include "libANGLE/Config.h"
12 #include "libANGLE/State.h"
13 #include "libANGLE/Surface.h"
14 #include "libANGLE/angletypes.h"
15 #include "libANGLE/renderer/FramebufferImpl_mock.h"
16 #include "libANGLE/renderer/SurfaceImpl.h"
17 #include "tests/angle_unittests_utils.h"
18 
19 using namespace rx;
20 using namespace testing;
21 
22 namespace
23 {
24 
25 class MockSurfaceImpl : public rx::SurfaceImpl
26 {
27   public:
MockSurfaceImpl()28     MockSurfaceImpl() : SurfaceImpl(mockState), mockState({1}, nullptr, egl::AttributeMap()) {}
~MockSurfaceImpl()29     virtual ~MockSurfaceImpl() { destructor(); }
30 
31     MOCK_METHOD1(destroy, void(const egl::Display *));
32     MOCK_METHOD1(initialize, egl::Error(const egl::Display *));
33     MOCK_METHOD1(swap, egl::Error(const gl::Context *));
34     MOCK_METHOD3(swapWithDamage, egl::Error(const gl::Context *, const EGLint *, EGLint));
35     MOCK_METHOD5(postSubBuffer, egl::Error(const gl::Context *, EGLint, EGLint, EGLint, EGLint));
36     MOCK_METHOD2(querySurfacePointerANGLE, egl::Error(EGLint, void **));
37     MOCK_METHOD3(bindTexImage, egl::Error(const gl::Context *context, gl::Texture *, EGLint));
38     MOCK_METHOD2(releaseTexImage, egl::Error(const gl::Context *context, EGLint));
39     MOCK_METHOD3(getSyncValues, egl::Error(EGLuint64KHR *, EGLuint64KHR *, EGLuint64KHR *));
40     MOCK_METHOD2(getMscRate, egl::Error(EGLint *, EGLint *));
41     MOCK_METHOD2(setSwapInterval, void(const egl::Display *, EGLint));
42     MOCK_CONST_METHOD0(getWidth, EGLint());
43     MOCK_CONST_METHOD0(getHeight, EGLint());
44     MOCK_CONST_METHOD0(isPostSubBufferSupported, EGLint(void));
45     MOCK_CONST_METHOD0(getSwapBehavior, EGLint(void));
46     MOCK_METHOD5(getAttachmentRenderTarget,
47                  angle::Result(const gl::Context *,
48                                GLenum,
49                                const gl::ImageIndex &,
50                                GLsizei,
51                                rx::FramebufferAttachmentRenderTarget **));
52     MOCK_METHOD2(attachToFramebuffer, egl::Error(const gl::Context *, gl::Framebuffer *));
53     MOCK_METHOD2(detachFromFramebuffer, egl::Error(const gl::Context *, gl::Framebuffer *));
54     MOCK_METHOD0(destructor, void());
55 
56     egl::SurfaceState mockState;
57 };
58 
TEST(SurfaceTest,DestructionDeletesImpl)59 TEST(SurfaceTest, DestructionDeletesImpl)
60 {
61     NiceMock<MockEGLFactory> factory;
62 
63     MockSurfaceImpl *impl = new MockSurfaceImpl;
64     EXPECT_CALL(factory, createWindowSurface(_, _, _)).WillOnce(Return(impl));
65 
66     egl::Config config;
67     egl::Surface *surface = new egl::WindowSurface(
68         &factory, {1}, &config, static_cast<EGLNativeWindowType>(0), egl::AttributeMap(), false);
69 
70     EXPECT_CALL(*impl, destroy(_)).Times(1).RetiresOnSaturation();
71     EXPECT_CALL(*impl, destructor()).Times(1).RetiresOnSaturation();
72 
73     EXPECT_FALSE(surface->onDestroy(nullptr).isError());
74 
75     // Only needed because the mock is leaked if bugs are present,
76     // which logs an error, but does not cause the test to fail.
77     // Ordinarily mocks are verified when destroyed.
78     Mock::VerifyAndClear(impl);
79 }
80 
81 }  // namespace
82