1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <gtest/gtest.h>
16 #include <surface.h>
17 #include <surface_buffer_impl.h>
18 #include <buffer_manager.h>
19 #include <buffer_utils.h>
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS::Rosen {
25 class SurfaceBufferImplTest : public testing::Test {
26 public:
27 static void SetUpTestCase();
28 static void TearDownTestCase();
29
30 static inline BufferRequestConfig requestConfig = {
31 .width = 0x100,
32 .height = 0x100,
33 .strideAlignment = 0x8,
34 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
35 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
36 .timeout = 0,
37 .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3,
38 };
39 static inline sptr<SurfaceBuffer> buffer = nullptr;
40 static inline int32_t val32 = 0;
41 static inline int64_t val64 = 0;
42 };
43
SetUpTestCase()44 void SurfaceBufferImplTest::SetUpTestCase()
45 {
46 buffer = nullptr;
47 val32 = 0;
48 val64 = 0;
49 }
50
TearDownTestCase()51 void SurfaceBufferImplTest::TearDownTestCase()
52 {
53 buffer = nullptr;
54 }
55
56 /*
57 * Function: GetSeqNum
58 * Type: Function
59 * Rank: Important(2)
60 * EnvConditions: N/A
61 * CaseDescription: 1. new SurfaceBufferImpl and GetSeqNum
62 * 2. new SurfaceBufferImpl again and check GetSeqNum = oldSeq + 1
63 */
64 HWTEST_F(SurfaceBufferImplTest, NewSeqIncrease001, Function | MediumTest | Level2)
65 {
66 buffer = new SurfaceBufferImpl();
67 int oldSeq = buffer->GetSeqNum();
68
69 buffer = new SurfaceBufferImpl();
70 ASSERT_EQ(oldSeq + 1, buffer->GetSeqNum());
71 }
72
73 /*
74 * Function: check buffer state
75 * Type: Function
76 * Rank: Important(2)
77 * EnvConditions: N/A
78 * CaseDescription: 1. check buffer state, such as bufferhandle, virAddr, fileDescriptor and size
79 */
80 HWTEST_F(SurfaceBufferImplTest, State001, Function | MediumTest | Level2)
81 {
82 ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
83 ASSERT_EQ(buffer->GetVirAddr(), nullptr);
84 ASSERT_EQ(buffer->GetFileDescriptor(), -1);
85 ASSERT_EQ(buffer->GetSize(), 0u);
86 }
87
88 /*
89 * Function: check buffer state
90 * Type: Function
91 * Rank: Important(2)
92 * EnvConditions: N/A
93 * CaseDescription: 1. call GetBufferHandle and Alloc
94 * 2. check buffer state, such as bufferhandle, virAddr and size
95 * 3. call Free
96 * 4. check ret
97 */
98 HWTEST_F(SurfaceBufferImplTest, State002, Function | MediumTest | Level2)
99 {
100 ASSERT_EQ(buffer->GetBufferHandle(), nullptr);
101
102 GSError ret = buffer->Alloc(requestConfig);
103 ASSERT_EQ(ret, OHOS::GSERROR_OK);
104
105 ASSERT_NE(buffer->GetBufferHandle(), nullptr);
106 ASSERT_NE(buffer->GetVirAddr(), nullptr);
107 ASSERT_NE(buffer->GetSize(), 0u);
108 ASSERT_EQ(buffer->GetFormat(), GRAPHIC_PIXEL_FMT_RGBA_8888);
109 ASSERT_EQ(buffer->GetUsage(), BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA);
110 ASSERT_EQ(buffer->GetSurfaceBufferColorGamut(), GraphicColorGamut::GRAPHIC_COLOR_GAMUT_DCI_P3);
111
112 ret = BufferManager::GetInstance()->Free(buffer);
113 ASSERT_EQ(ret, OHOS::GSERROR_OK);
114 }
115
116 /*
117 * Function: parcel
118 * Type: Function
119 * Rank: Important(2)
120 * EnvConditions: N/A
121 * CaseDescription: 1. new SurfaceBufferImpl and Alloc
122 * 2. call Set data interface
123 * 3. call WriteSurfaceBufferImpl and ReadSurfaceBufferImpl
124 * 4. call Get data interface
125 * 5. check ret
126 */
127 HWTEST_F(SurfaceBufferImplTest, Parcel001, Function | MediumTest | Level2)
128 {
129 sptr<SurfaceBuffer> sbi = new SurfaceBufferImpl(0);
130 const auto &bm = BufferManager::GetInstance();
131 auto sret = bm->Alloc(requestConfig, sbi);
132 ASSERT_EQ(sret, OHOS::GSERROR_OK);
133
134 MessageParcel parcel;
135 WriteSurfaceBufferImpl(parcel, sbi->GetSeqNum(), sbi);
136
137 sptr<SurfaceBuffer> buffer = nullptr;
138 uint32_t seq;
139 ReadSurfaceBufferImpl(parcel, seq, buffer);
140 ASSERT_NE(buffer, nullptr);
141 }
142 }
143