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 "dynamic_font_provider.h"
17
18 #include <gtest/gtest.h>
19 #include <iostream>
20
21 #include "dynamic_font_style_set.h"
22 #include "param_test_macros.h"
23 #include "texgine_exception.h"
24 #include "texgine_font_style_set.h"
25 #include "texgine_stream.h"
26 #include "texgine_typeface.h"
27 #include "typeface.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Rosen {
34 namespace TextEngine {
35 std::unique_ptr<TexgineMemoryStream> memoryStream = nullptr;
36 std::shared_ptr<TexgineTypeface> typeface = nullptr;
37
38 struct Style {
39 std::unique_ptr<TexgineMemoryStream> memoryStream = std::make_unique<TexgineMemoryStream>();
40 std::shared_ptr<TexgineTypeface> typeface = std::make_shared<TexgineTypeface>();
41 };
42
InitMyMockVars(Style style)43 void InitMyMockVars(Style style)
44 {
45 memoryStream = std::move(style.memoryStream);
46 typeface = style.typeface;
47 }
48
49 #ifndef USE_ROSEN_DRAWING
MakeCopy(const void * data,size_t length)50 std::unique_ptr<TexgineMemoryStream> TexgineMemoryStream::MakeCopy(const void *data, size_t length)
51 {
52 return std::move(memoryStream);
53 }
54 #endif
55
56 #ifndef USE_ROSEN_DRAWING
MakeFromStream(std::unique_ptr<TexgineMemoryStream> stream,int index)57 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromStream(
58 std::unique_ptr<TexgineMemoryStream> stream, int index)
59 {
60 return typeface;
61 }
62 #endif
63
64 class DynamicFontProviderTest : public testing::Test {
65 public:
66 std::shared_ptr<DynamicFontProvider> dynamicFontProvider = DynamicFontProvider::Create();
67 };
68
69 /**
70 * @tc.name: Create
71 * @tc.desc: Verify the Create
72 * @tc.type:FUNC
73 * @tc.require: issueI6V6JP
74 */
75 HWTEST_F(DynamicFontProviderTest, Create, TestSize.Level1)
76 {
77 EXPECT_NE(DynamicFontProvider::Create(), nullptr);
78 }
79
80 /**
81 * @tc.name: LoadFont1
82 * @tc.desc: Verify the LoadFont
83 * @tc.type:FUNC
84 * @tc.require: issueI6V6JP
85 */
86 HWTEST_F(DynamicFontProviderTest, LoadFont1, TestSize.Level1)
87 {
88 InitMyMockVars({});
89 // 4 is data length, "LF1" length, 1 is PARAMETERERROR
90 EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF1", nullptr, 4), 1); });
91 }
92
93 /**
94 * @tc.name: LoadFont2
95 * @tc.desc: Verify the LoadFont
96 * @tc.type:FUNC
97 * @tc.require: issueI6V6JP
98 */
99 HWTEST_F(DynamicFontProviderTest, LoadFont2, TestSize.Level1)
100 {
101 InitMyMockVars({});
102 dynamicFontProvider = DynamicFontProvider::Create();
103 EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF2", this, 0), 1); });
104 }
105
106 /**
107 * @tc.name: LoadFont3
108 * @tc.desc: Verify the LoadFont
109 * @tc.type:FUNC
110 * @tc.require: issueI6V6JP
111 */
112 HWTEST_F(DynamicFontProviderTest, LoadFont3, TestSize.Level1)
113 {
114 InitMyMockVars({.memoryStream = nullptr});
115 // APIERROR
116 EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF3", this, 4), 0); });
117 }
118
119 /**
120 * @tc.name: LoadFont4
121 * @tc.desc: Verify the LoadFont
122 * @tc.type:FUNC
123 * @tc.require: issueI6V6JP
124 */
125 HWTEST_F(DynamicFontProviderTest, LoadFont4, TestSize.Level1)
126 {
127 InitMyMockVars({.typeface = nullptr});
128 EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF4", this, 4), 0); });
129 }
130
131 /**
132 * @tc.name: LoadFont5
133 * @tc.desc: Verify the LoadFont
134 * @tc.type:FUNC
135 * @tc.require: issueI6V6JP
136 */
137 HWTEST_F(DynamicFontProviderTest, LoadFont5, TestSize.Level1)
138 {
139 InitMyMockVars({});
140 EXPECT_NO_THROW({
141 auto ret = dynamicFontProvider->LoadFont("LF5", this, 4);
142 EXPECT_EQ(ret, 0);
143 });
144 }
145
146 /**
147 * @tc.name: MatchFamily1
148 * @tc.desc: Verify the MatchFamily
149 * @tc.type:FUNC
150 * @tc.require: issueI6V6JP
151 */
152 HWTEST_F(DynamicFontProviderTest, MatchFamily1, TestSize.Level1)
153 {
154 EXPECT_EQ(dynamicFontProvider->MatchFamily("aaa"), nullptr);
155 }
156
157 /**
158 * @tc.name: MatchFamily2
159 * @tc.desc: Verify the MatchFamily
160 * @tc.type:FUNC
161 * @tc.require: issueI6V6JP
162 */
163 HWTEST_F(DynamicFontProviderTest, MatchFamily2, TestSize.Level1)
164 {
165 InitMyMockVars({});
166 dynamicFontProvider->LoadFont("aaa", this, 4);
167 EXPECT_NE(dynamicFontProvider->MatchFamily("aaa"), nullptr);
168 }
169
170 /**
171 * @tc.name: MatchFamily3
172 * @tc.desc: Verify the MatchFamily
173 * @tc.type:FUNC
174 * @tc.require: issueI6V6JP
175 */
176 HWTEST_F(DynamicFontProviderTest, MatchFamily3, TestSize.Level1)
177 {
178 InitMyMockVars({});
179 dynamicFontProvider->LoadFont("aaa", this, 4);
180 EXPECT_EQ(dynamicFontProvider->MatchFamily("bbb"), nullptr);
181 }
182 } // namespace TextEngine
183 } // namespace Rosen
184 } // namespace OHOS
185