• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <iostream>
17 #include <vector>
18 
19 #include <gtest/gtest.h>
20 #include <hb.h>
21 
22 #include "opentype_parser/cmap_parser.h"
23 #include "param_test_macros.h"
24 #include "texgine_exception.h"
25 #include "typeface.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 struct hb_blob_t {};
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace TextEngine {
35 struct Mockvars {
36     const char *name = "";
37     int sizeIndex = 0;
38     int lengthIndex = 0;
39     int idIndex = 0;
40     std::vector<int> tableSize = {1};
41     std::vector<int> dataLength = {1};
42     int parseRetval = 0;
43     std::vector<int> glyphId = {0};
44     std::shared_ptr<TextEngine::TexgineTypeface> texgineTypeface = std::make_shared<TextEngine::TexgineTypeface>();
45     std::unique_ptr<hb_blob_t> blob = std::make_unique<hb_blob_t>();
46     std::unique_ptr<TextEngine::Typeface> typeface = nullptr;
47 } g_typefaceMockvars;
48 
InitMyMockVars(Mockvars vars)49 void InitMyMockVars(Mockvars vars)
50 {
51     g_typefaceMockvars = std::move(vars);
52     g_typefaceMockvars.typeface = std::make_unique<TextEngine::Typeface>(g_typefaceMockvars.texgineTypeface);
53 }
54 
55 #ifndef USE_ROSEN_DRAWING
MakeFromFile(const std::string & path,int index)56 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromFile(const std::string &path, int index)
57 {
58     return g_typefaceMockvars.texgineTypeface;
59 }
60 #endif
61 
62 #ifndef USE_ROSEN_DRAWING
GetTableSize(uint32_t tag) const63 size_t TexgineTypeface::GetTableSize(uint32_t tag) const
64 {
65     assert(g_typefaceMockvars.sizeIndex < g_typefaceMockvars.tableSize.size());
66     return g_typefaceMockvars.tableSize[g_typefaceMockvars.sizeIndex++];
67 }
68 #endif
69 
70 #ifndef USE_ROSEN_DRAWING
GetTableData(uint32_t tag,size_t offset,size_t length,void * data) const71 size_t TexgineTypeface::GetTableData(uint32_t tag, size_t offset, size_t length, void* data) const
72 {
73     return g_typefaceMockvars.dataLength[g_typefaceMockvars.lengthIndex++];
74 }
75 #endif
76 
77 #ifndef USE_ROSEN_DRAWING
GetFamilyName(TexgineString * name) const78 void TexgineTypeface::GetFamilyName(TexgineString* name) const
79 {
80     name->SetString(std::make_shared<SkString>(g_typefaceMockvars.name));
81 }
82 #endif
83 
84 extern "C" {
hb_blob_create(const char * data,unsigned int length,hb_memory_mode_t mode,void * user_data,hb_destroy_func_t destroy)85 hb_blob_t* hb_blob_create(const char* data, unsigned int length, hb_memory_mode_t mode,
86     void* user_data, hb_destroy_func_t destroy)
87 {
88     return g_typefaceMockvars.blob.get();
89 }
90 
hb_blob_destroy(hb_blob_t *)91 void hb_blob_destroy(hb_blob_t*)
92 {
93 }
94 }
95 
Parse(const char * data,int32_t size)96 int CmapParser::Parse(const char* data, int32_t size)
97 {
98     return g_typefaceMockvars.parseRetval;
99 }
100 
GetGlyphId(int32_t codepoint) const101 int32_t CmapParser::GetGlyphId(int32_t codepoint) const
102 {
103     return g_typefaceMockvars.glyphId[g_typefaceMockvars.idIndex++];
104 }
105 
106 class TypefaceTest : public testing::Test {
107 public:
108 };
109 
110 /**
111  * @tc.name: MakeFromFile1
112  * @tc.desc: Verify the MakeFromFile
113  * @tc.type:FUNC
114  */
115 HWTEST_F(TypefaceTest, MakeFromFile1, TestSize.Level1)
116 {
117     InitMyMockVars({ .name = "cxt" });
118     ASSERT_EXCEPTION(ExceptionType::API_FAILED, Typeface::MakeFromFile("aaa"));
119 }
120 
121 /**
122  * @tc.name: MakeFromFile2
123  * @tc.desc: Verify the MakeFromFile
124  * @tc.type:FUNC
125  */
126 HWTEST_F(TypefaceTest, MakeFromFile2, TestSize.Level1)
127 {
128     InitMyMockVars({ .name = "cxxt", .texgineTypeface = nullptr });
129     ASSERT_EXCEPTION(ExceptionType::API_FAILED, Typeface::MakeFromFile("aaa"));
130     EXPECT_NE(g_typefaceMockvars.typeface->GetName(), "cxxt");
131 }
132 
133 /**
134  * @tc.name: Has1
135  * @tc.desc: Verify the Has
136  * @tc.type:FUNC
137  */
138 HWTEST_F(TypefaceTest, Has1, TestSize.Level1)
139 {
140     InitMyMockVars({ .name = "one", .texgineTypeface = nullptr });
141     // 0x0006 is codepoint
142     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), false);
143 }
144 
145 /**
146  * @tc.name: Has2
147  * @tc.desc: Verify the Has
148  * @tc.type:FUNC
149  */
150 HWTEST_F(TypefaceTest, Has2, TestSize.Level1)
151 {
152     InitMyMockVars({ .name = "two", .tableSize = {0} });
153     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
154 }
155 
156 /**
157  * @tc.name: Has3
158  * @tc.desc: Verify the Has
159  * @tc.type:FUNC
160  */
161 HWTEST_F(TypefaceTest, Has3, TestSize.Level1)
162 {
163     InitMyMockVars({ .name = "three", .tableSize = {3}, .dataLength = {2} });
164     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
165 }
166 
167 /**
168  * @tc.name: Has4
169  * @tc.desc: Verify the Has
170  * @tc.type:FUNC
171  */
172 HWTEST_F(TypefaceTest, Has4, TestSize.Level1)
173 {
174     InitMyMockVars({ .name = "four", .blob = nullptr });
175     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
176 }
177 
178 /**
179  * @tc.name: Has5
180  * @tc.desc: Verify the Has
181  * @tc.type:FUNC
182  */
183 HWTEST_F(TypefaceTest, Has5, TestSize.Level1)
184 {
185     InitMyMockVars({ .name = "five", .parseRetval = -1 });
186     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
187 }
188 
189 /**
190  * @tc.name: Has6
191  * @tc.desc: Verify the Has
192  * @tc.type:FUNC
193  */
194 HWTEST_F(TypefaceTest, Has6, TestSize.Level1)
195 {
196     InitMyMockVars({ .name = "six" });
197     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
198 }
199 
200 /**
201  * @tc.name: Has7
202  * @tc.desc: Verify the Has
203  * @tc.type:FUNC
204  */
205 HWTEST_F(TypefaceTest, Has7, TestSize.Level1)
206 {
207     InitMyMockVars({ .name = "seven", .tableSize = {1, 1}, .dataLength = {1, 1}, .glyphId = {0, -1} });
208     auto bool1 = g_typefaceMockvars.typeface->Has(0x0006);
209     auto bool2 = g_typefaceMockvars.typeface->Has(0x0006);
210     EXPECT_EQ(bool1, true);
211     EXPECT_EQ(bool2, false);
212     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 0);
213 }
214 
215 /**
216  * @tc.name: Has8
217  * @tc.desc: Verify the Has
218  * @tc.type:FUNC
219  */
220 HWTEST_F(TypefaceTest, Has8, TestSize.Level1)
221 {
222     InitMyMockVars({ .name = "eight", .tableSize = {2, 1}, .dataLength = {1, 1} });
223     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
224     auto bool1 = g_typefaceMockvars.typeface->Has(0x0006);
225     EXPECT_EQ(bool1, true);
226     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 0);
227 }
228 
229 /**
230  * @tc.name: Has9
231  * @tc.desc: Verify the Has
232  * @tc.type:FUNC
233  */
234 HWTEST_F(TypefaceTest, Has9, TestSize.Level1)
235 {
236     InitMyMockVars({ .name = "nine", .tableSize = {0, 0} });
237     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
238     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 0);
239 }
240 } // namespace TextEngine
241 } // namespace Rosen
242 } // namespace OHOS
243