1 /*
2 * Copyright (c) 2022 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
16 #include "shader_cache.h"
17
18 #include <cstring>
19
20 #include <gtest/gtest.h>
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 class ShaderCacheTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp() override;
32 void TearDown() override;
33 };
34
SetUpTestCase()35 void ShaderCacheTest::SetUpTestCase() {}
TearDownTestCase()36 void ShaderCacheTest::TearDownTestCase() {}
SetUp()37 void ShaderCacheTest::SetUp() {}
TearDown()38 void ShaderCacheTest::TearDown() {}
39
40 /**
41 * @tc.name: instance_test_001
42 * @tc.desc: Verify the instanciation of ShaderCache
43 * @tc.type: FUNC
44 * @tc.require:
45 * @tc.author:
46 */
47 HWTEST_F(ShaderCacheTest, instance_test_001, TestSize.Level1)
48 {
49 #ifdef ACE_ENABLE_GL
50 GTEST_LOG_(INFO) << "ShaderCacheTest instance_test_001 start";
51 /**
52 * @tc.steps: step1. initialize a shader cache with empty cache directory
53 */
54 auto &cache = ShaderCache::Instance();
55 std::string testedFileDir = "";
56 cache.SetFilePath(testedFileDir);
57 const char* identity = nullptr;
58 sk_sp<SkData> fakeData = SkData::MakeEmpty();
59 /**
60 * @tc.steps: step2. test initialization function
61 */
62 cache.InitShaderCache(identity, 0, false);
63 EXPECT_EQ(nullptr, cache.load(*fakeData));
64 #endif
65 }
66
67 /**
68 * @tc.name: initialization_test_001
69 * @tc.desc: Verify the initialization function of shader cache
70 * @tc.type: FUNC
71 * @tc.require:
72 * @tc.author:
73 */
74 HWTEST_F(ShaderCacheTest, initialization_test_001, TestSize.Level1)
75 {
76 #ifdef ACE_ENABLE_GL
77 GTEST_LOG_(INFO) << "ShaderCacheTest initialization_test_001 start";
78 /**
79 * @tc.steps: step1. initialize a shader cache instance and prepare parameters
80 */
81 auto &cache = ShaderCache::Instance();
82 const char* identity = nullptr;
83 sk_sp<SkData> fakeData = SkData::MakeEmpty();
84 /**
85 * @tc.steps: step2. test initialization function
86 */
87 cache.InitShaderCache(identity, 0, false);
88 EXPECT_EQ(nullptr, cache.load(*fakeData));
89 #endif
90 }
91
92
93 /**
94 * @tc.name: initialization_test_002
95 * @tc.desc: Verify the file directory setting function of shader cache
96 * @tc.type: FUNC
97 * @tc.require:
98 * @tc.author:
99 */
100 HWTEST_F(ShaderCacheTest, initialization_test_002, TestSize.Level1)
101 {
102 #ifdef ACE_ENABLE_GL
103 GTEST_LOG_(INFO) << "ShaderCacheTest initialization_test_002 start";
104 /**
105 * @tc.steps: step1. initialize a shader cache instance and set the file dir
106 */
107 auto &cache = ShaderCache::Instance();
108 std::string testedFileDir = "TemporalFilePath";
109 cache.SetFilePath(testedFileDir);
110 const char* identity = nullptr;
111 const char* fakeBuffer = "testStr";
112 sk_sp<SkData> fakeData = SkData::MakeWithCopy(fakeBuffer, 8);
113 /**
114 * @tc.steps: step2. test if the file direction is correctly set
115 */
116 cache.InitShaderCache(identity, 0, false);
117 EXPECT_EQ(nullptr, cache.load(*fakeData));
118 #endif
119 }
120
121 /**
122 * @tc.name: store_test_001
123 * @tc.desc: Verify the store function of shader cache with a zero-length
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author:
127 */
128 HWTEST_F(ShaderCacheTest, store_test_001, TestSize.Level1)
129 {
130 #ifdef ACE_ENABLE_GL
131 GTEST_LOG_(INFO) << "ShaderCacheTest store_test_001 start";
132 /**
133 * @tc.steps: step1. initialize a shader cache instance and prepare parameters
134 */
135 auto &cache = ShaderCache::Instance();
136 std::string testedFileDir = "TemporalFilePath";
137 cache.SetFilePath(testedFileDir);
138 const char* identity = nullptr;
139 sk_sp<SkData> fakeKey = SkData::MakeEmpty();
140 sk_sp<SkData> fakeData = SkData::MakeEmpty();
141 /**
142 * @tc.steps: step2. test the store function with given parameters
143 */
144 cache.InitShaderCache(identity, 0, false);
145 cache.store(*fakeKey, *fakeData);
146 EXPECT_EQ(nullptr, cache.load(*fakeKey));
147 #endif
148 }
149
150 /**
151 * @tc.name: store_test_002
152 * @tc.desc: Verify the store function of shader cache with uninitialized status
153 * @tc.type: FUNC
154 * @tc.require:
155 * @tc.author:
156 */
157 HWTEST_F(ShaderCacheTest, store_test_002, TestSize.Level1)
158 {
159 #ifdef ACE_ENABLE_GL
160 GTEST_LOG_(INFO) << "ShaderCacheTest store_test_002 start";
161 /**
162 * @tc.steps: step1. initialize a shader cache instance and prepare parameters
163 */
164 auto &cache = ShaderCache::Instance();
165 std::string testedFileDir = "";
166 cache.SetFilePath(testedFileDir);
167 const char* identity = nullptr;
168 sk_sp<SkData> fakeKey = SkData::MakeEmpty();
169 sk_sp<SkData> fakeData = SkData::MakeEmpty();
170 /**
171 * @tc.steps: step2. test the store function with given parameters
172 */
173 cache.InitShaderCache(identity, 0, false);
174 cache.store(*fakeKey, *fakeData);
175 EXPECT_EQ(nullptr, cache.load(*fakeKey));
176 #endif
177 }
178
179 /**
180 * @tc.name: writing_test_001
181 * @tc.desc: Verify the writeToDisk function with a zero-length hashID
182 * @tc.type: FUNC
183 * @tc.require:
184 * @tc.author:
185 */
186 HWTEST_F(ShaderCacheTest, writing_test_001, TestSize.Level1)
187 {
188 #ifdef ACE_ENABLE_GL
189 GTEST_LOG_(INFO) << "ShaderCacheTest writing_test_001 start";
190 /**
191 * @tc.steps: step1. initialize a shader cache instance and prepare parameters
192 */
193 auto &cache = ShaderCache::Instance();
194 std::string testedFileDir = "TemporalFilePath";
195 cache.SetFilePath(testedFileDir);
196 const char* identity = nullptr;
197 const char* fakeBuffer = "testStr";
198 sk_sp<SkData> fakeKey = SkData::MakeWithCopy(fakeBuffer, 8);
199 sk_sp<SkData> fakeData = SkData::MakeWithCopy(fakeBuffer, 8);
200 /**
201 * @tc.steps: step2. verify the empty result of writeToDisk function
202 */
203 cache.InitShaderCache(identity, 0, false);
204 cache.store(*fakeKey, *fakeData);
205 EXPECT_EQ(fakeData->size(), cache.load(*fakeKey)->size());
206
207 #endif
208 }
209
210 /**
211 * @tc.name: writing_test_002
212 * @tc.desc: Verify the writeToDisk function with a non-zero-length hashID
213 * @tc.type: FUNC
214 * @tc.require:
215 * @tc.author:
216 */
217 HWTEST_F(ShaderCacheTest, writing_test_002, TestSize.Level1)
218 {
219 #ifdef ACE_ENABLE_GL
220 GTEST_LOG_(INFO) << "ShaderCacheTest writing_test_002 start";
221 /**
222 * @tc.steps: step1. initialize a shader cache instance and prepare parameters
223 */
224 auto &cache = ShaderCache::Instance();
225 std::string testedFileDir = "TemporalFilePath";
226 cache.SetFilePath(testedFileDir);
227 const char* identity = "testIdentity";
228 const char* fakeBuffer = "testStr";
229 sk_sp<SkData> fakeKey = SkData::MakeWithCopy(fakeBuffer, 8);
230 sk_sp<SkData> fakeData = SkData::MakeWithCopy(fakeBuffer, 8);
231 /**
232 * @tc.steps: step2. verify the non-empty result of writeToDisk function
233 */
234 cache.InitShaderCache(identity, 16, false);
235 cache.store(*fakeKey, *fakeData);
236 EXPECT_EQ(fakeData->size(), cache.load(*fakeKey)->size());
237 #endif
238 }
239 } // namespace Rosen
240 } // namespace OHOS