• 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 
MakeFromFile(const std::string & path,int index)55 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromFile(const std::string &path, int index)
56 {
57     return g_typefaceMockvars.texgineTypeface;
58 }
59 
GetTableSize(uint32_t tag) const60 size_t TexgineTypeface::GetTableSize(uint32_t tag) const
61 {
62     assert(g_typefaceMockvars.sizeIndex < g_typefaceMockvars.tableSize.size());
63     return g_typefaceMockvars.tableSize[g_typefaceMockvars.sizeIndex++];
64 }
65 
GetTableData(uint32_t tag,size_t offset,size_t length,void * data) const66 size_t TexgineTypeface::GetTableData(uint32_t tag, size_t offset, size_t length,void* data) const
67 {
68     return g_typefaceMockvars.dataLength[g_typefaceMockvars.lengthIndex++];
69 }
70 
GetFamilyName(TexgineString * name) const71 void TexgineTypeface::GetFamilyName(TexgineString* name) const
72 {
73     name->SetString(g_typefaceMockvars.name);
74 }
75 
76 extern "C" {
hb_blob_create(const char * data,unsigned int length,hb_memory_mode_t mode,void * user_data,hb_destroy_func_t destroy)77 hb_blob_t* hb_blob_create(const char* data, unsigned int length, hb_memory_mode_t mode,
78     void* user_data, hb_destroy_func_t destroy)
79 {
80     return g_typefaceMockvars.blob.get();
81 }
82 
hb_blob_destroy(hb_blob_t *)83 void hb_blob_destroy(hb_blob_t*)
84 {
85 }
86 }
87 
Parse(const char * data,int32_t size)88 int CmapParser::Parse(const char* data, int32_t size)
89 {
90     return g_typefaceMockvars.parseRetval;
91 }
92 
GetGlyphId(int32_t codepoint) const93 int32_t CmapParser::GetGlyphId(int32_t codepoint) const
94 {
95     return g_typefaceMockvars.glyphId[g_typefaceMockvars.idIndex++];
96 }
97 
98 class TypefaceTest : public testing::Test {
99 public:
100 };
101 
102 /**
103  * @tc.name: MakeFromFile1
104  * @tc.desc: Verify the MakeFromFile
105  * @tc.type:FUNC
106  */
107 HWTEST_F(TypefaceTest, MakeFromFile1, TestSize.Level1)
108 {
109     InitMyMockVars({ .name = "cxt" });
110     auto typeface = Typeface::MakeFromFile("aaa");
111     EXPECT_NE(typeface.get(), nullptr);
112     EXPECT_EQ(typeface->GetName(), "cxt");
113 }
114 
115 /**
116  * @tc.name: MakeFromFile2
117  * @tc.desc: Verify the MakeFromFile
118  * @tc.type:FUNC
119  */
120 HWTEST_F(TypefaceTest, MakeFromFile2, TestSize.Level1)
121 {
122     InitMyMockVars({ .name = "cxxt", .texgineTypeface = nullptr });
123     ASSERT_EXCEPTION(ExceptionType::API_FAILED, Typeface::MakeFromFile("aaa"));
124     EXPECT_NE(g_typefaceMockvars.typeface->GetName(), "cxxt");
125 }
126 
127 /**
128  * @tc.name: Has1
129  * @tc.desc: Verify the Has
130  * @tc.type:FUNC
131  */
132 HWTEST_F(TypefaceTest, Has1, TestSize.Level1)
133 {
134     InitMyMockVars({ .name = "one", .texgineTypeface = nullptr });
135     // 0x0006 is codepoint
136     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), false);
137 }
138 
139 /**
140  * @tc.name: Has2
141  * @tc.desc: Verify the Has
142  * @tc.type:FUNC
143  */
144 HWTEST_F(TypefaceTest, Has2, TestSize.Level1)
145 {
146     InitMyMockVars({ .name = "two", .tableSize = {0} });
147     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
148 }
149 
150 /**
151  * @tc.name: Has3
152  * @tc.desc: Verify the Has
153  * @tc.type:FUNC
154  */
155 HWTEST_F(TypefaceTest, Has3, TestSize.Level1)
156 {
157     InitMyMockVars({ .name = "three", .tableSize = {3}, .dataLength = {2} });
158     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
159 }
160 
161 /**
162  * @tc.name: Has4
163  * @tc.desc: Verify the Has
164  * @tc.type:FUNC
165  */
166 HWTEST_F(TypefaceTest, Has4, TestSize.Level1)
167 {
168     InitMyMockVars({ .name = "four", .blob = nullptr });
169     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
170 }
171 
172 /**
173  * @tc.name: Has5
174  * @tc.desc: Verify the Has
175  * @tc.type:FUNC
176  */
177 HWTEST_F(TypefaceTest, Has5, TestSize.Level1)
178 {
179     InitMyMockVars({ .name = "five", .parseRetval = -1 });
180     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), false);
181 }
182 
183 /**
184  * @tc.name: Has6
185  * @tc.desc: Verify the Has
186  * @tc.type:FUNC
187  */
188 HWTEST_F(TypefaceTest, Has6, TestSize.Level1)
189 {
190     InitMyMockVars({ .name = "six" });
191     EXPECT_EQ(g_typefaceMockvars.typeface->Has(0x0006), true);
192 }
193 
194 /**
195  * @tc.name: Has7
196  * @tc.desc: Verify the Has
197  * @tc.type:FUNC
198  */
199 HWTEST_F(TypefaceTest, Has7, TestSize.Level1)
200 {
201     InitMyMockVars({ .name = "seven", .tableSize = {1, 1}, .dataLength = {1, 1}, .glyphId = {0, -1} });
202     auto bool1 = g_typefaceMockvars.typeface->Has(0x0006);
203     auto bool2 = g_typefaceMockvars.typeface->Has(0x0006);
204     EXPECT_EQ(bool1, true);
205     EXPECT_EQ(bool2, false);
206     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 1);
207 }
208 
209 /**
210  * @tc.name: Has8
211  * @tc.desc: Verify the Has
212  * @tc.type:FUNC
213  */
214 HWTEST_F(TypefaceTest, Has8, TestSize.Level1)
215 {
216     InitMyMockVars({ .name = "eight", .tableSize = {2, 1}, .dataLength = {1, 1} });
217     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
218     auto bool1 = g_typefaceMockvars.typeface->Has(0x0006);
219     EXPECT_EQ(bool1, true);
220     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 2);
221 }
222 
223 /**
224  * @tc.name: Has9
225  * @tc.desc: Verify the Has
226  * @tc.type:FUNC
227  */
228 HWTEST_F(TypefaceTest, Has9, TestSize.Level1)
229 {
230     InitMyMockVars({ .name = "nine", .tableSize = {0, 0} });
231     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
232     ASSERT_EXCEPTION(ExceptionType::API_FAILED, g_typefaceMockvars.typeface->Has(0x0006));
233     EXPECT_EQ(g_typefaceMockvars.sizeIndex, 2);
234 }
235 } // namespace TextEngine
236 } // namespace Rosen
237 } // namespace OHOS
238