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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "font_collection.h"
20 #include "texgine_font_manager.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace TextEngine {
28 struct MockVars {
29 int catchedSize = -1;
30 bool hasRetval = false;
31 int hasCount = 0;
32 std::shared_ptr<TexgineFontManager> fontMgr = std::make_shared<TexgineFontManager>();
33 std::shared_ptr<TexgineTypeface> SCRetvalTypeface = std::make_shared<TexgineTypeface>();
34 std::shared_ptr<TexgineTypeface> styleRetvalTypeface = std::make_shared<TexgineTypeface>();
35 std::vector<std::shared_ptr<VariantFontStyleSet>> fontStyleSets;
36 std::shared_ptr<TextEngine::FontCollection> fontCollection;
37 } g_fcMockVars;
38
39 #ifndef USE_ROSEN_DRAWING
RefDefault()40 std::shared_ptr<TexgineFontManager> TexgineFontManager::RefDefault()
41 {
42 return g_fcMockVars.fontMgr;
43 }
44
MatchFamilyStyleCharacter(const std::string & familyName,const TexgineFontStyle & style,const char * bcp47[],int bcp47Count,int32_t character)45 std::shared_ptr<TexgineTypeface> TexgineFontManager::MatchFamilyStyleCharacter(const std::string &familyName,
46 const TexgineFontStyle &style, const char *bcp47[], int bcp47Count, int32_t character)
47 {
48 g_fcMockVars.catchedSize = bcp47Count;
49 return g_fcMockVars.SCRetvalTypeface;
50 }
51 #endif
52
MatchStyle(std::shared_ptr<TexgineFontStyle> pattern)53 std::shared_ptr<TexgineTypeface> VariantFontStyleSet::MatchStyle(std::shared_ptr<TexgineFontStyle> pattern)
54 {
55 return g_fcMockVars.styleRetvalTypeface;
56 }
57
Has(uint32_t ch)58 bool Typeface::Has(uint32_t ch)
59 {
60 g_fcMockVars.hasCount++;
61 return g_fcMockVars.hasRetval;
62 }
63
CreateSets()64 std::vector<std::shared_ptr<VariantFontStyleSet>> CreateSets()
65 {
66 std::vector<std::shared_ptr<VariantFontStyleSet>> sets;
67 auto tfss = std::make_shared<TexgineFontStyleSet>(nullptr);
68 sets.push_back(std::make_shared<VariantFontStyleSet>(tfss));
69 return sets;
70 }
71
InitFcMockVars(MockVars vars)72 void InitFcMockVars(MockVars vars)
73 {
74 g_fcMockVars = vars;
75 g_fcMockVars.fontCollection = std::make_shared<FontCollection>(std::move(g_fcMockVars.fontStyleSets));
76 }
77
78 class FontCollectionTest : public testing::Test {
79 };
80
81 /**
82 * @tc.name: FontCollection
83 * @tc.desc: Verify the FontCollection
84 * @tc.type:FUNC
85 * @tc.require: issueI6TIIY
86 */
87 HWTEST_F(FontCollectionTest, FontCollection, TestSize.Level1)
88 {
89 InitFcMockVars({});
90 EXPECT_NO_THROW(FontCollection fc(std::move(g_fcMockVars.fontStyleSets)));
91 }
92
93 /**
94 * @tc.name: GetTypefaceForChar1
95 * @tc.desc: Verify the GetTypefaceForChar
96 * @tc.type:FUNC
97 * @tc.require: issueI6TIIY
98 */
99 HWTEST_F(FontCollectionTest, GetTypefaceForChar1, TestSize.Level1)
100 {
101 InitFcMockVars({});
102 FontStyles style;
103 auto tf = g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "");
104 EXPECT_NE(tf, nullptr);
105 EXPECT_NE(tf->Get(), g_fcMockVars.SCRetvalTypeface);
106 EXPECT_EQ(g_fcMockVars.catchedSize, -1);
107 }
108
109 /**
110 * @tc.name: GetTypefaceForChar2
111 * @tc.desc: Verify the GetTypefaceForChar
112 * @tc.type:FUNC
113 * @tc.require: issueI6TIIY
114 */
115 HWTEST_F(FontCollectionTest, GetTypefaceForChar2, TestSize.Level1)
116 {
117 InitFcMockVars({.fontMgr = nullptr});
118 FontStyles style;
119 EXPECT_NE(g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "zh_CN", ""), nullptr);
120 EXPECT_EQ(g_fcMockVars.catchedSize, -1);
121 }
122
123 /**
124 * @tc.name: GetTypefaceForChar3
125 * @tc.desc: Verify the GetTypefaceForChar
126 * @tc.type:FUNC
127 * @tc.require: issueI6TIIY
128 */
129 HWTEST_F(FontCollectionTest, GetTypefaceForChar3, TestSize.Level1)
130 {
131 InitFcMockVars({.SCRetvalTypeface = nullptr});
132 FontStyles style;
133 EXPECT_NE(g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "zh_CN"), nullptr);
134 EXPECT_NE(g_fcMockVars.catchedSize, 0);
135 }
136
137 /**
138 * @tc.name: GetTypefaceForChar4
139 * @tc.desc: Verify the GetTypefaceForChar
140 * @tc.type:FUNC
141 * @tc.require: issueI6TIIY
142 */
143 HWTEST_F(FontCollectionTest, GetTypefaceForChar4, TestSize.Level1)
144 {
145 InitFcMockVars({});
146 FontStyles style;
147 auto tf1 = g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "zh_HK");
148 EXPECT_NE(tf1, nullptr);
149 EXPECT_NE(g_fcMockVars.catchedSize, 0);
150 InitFcMockVars({});
151 auto tf2 = g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "zh_HK");
152 EXPECT_NE(tf2, nullptr);
153 EXPECT_EQ(g_fcMockVars.catchedSize, -1);
154 EXPECT_NE(tf1->Get(), tf2->Get());
155 }
156
157 /**
158 * @tc.name: GetTypefaceForChar5
159 * @tc.desc: Verify the GetTypefaceForChar
160 * @tc.type:FUNC
161 * @tc.require: issueI6TIIY
162 */
163 HWTEST_F(FontCollectionTest, GetTypefaceForChar5, TestSize.Level1)
164 {
165 InitFcMockVars({.fontMgr = nullptr, .styleRetvalTypeface = nullptr, .fontStyleSets = CreateSets()});
166 FontStyles style;
167 EXPECT_NE(g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "en_US"), nullptr);
168 }
169
170 /**
171 * @tc.name: GetTypefaceForChar6
172 * @tc.desc: Verify the GetTypefaceForChar
173 * @tc.type:FUNC
174 * @tc.require: issueI6TIIY
175 */
176 HWTEST_F(FontCollectionTest, GetTypefaceForChar6, TestSize.Level1)
177 {
178 InitFcMockVars({.fontMgr = nullptr, .fontStyleSets = CreateSets()});
179 FontStyles style;
180 EXPECT_NE(g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "en_US"), nullptr);
181 }
182
183 /**
184 * @tc.name: GetTypefaceForChar7
185 * @tc.desc: Verify the GetTypefaceForCha
186 * @tc.type:FUNC
187 * @tc.require: issueI6TIIY
188 */
189 HWTEST_F(FontCollectionTest, GetTypefaceForChar7, TestSize.Level1)
190 {
191 InitFcMockVars({.hasRetval = true, .fontMgr = nullptr, .fontStyleSets = CreateSets()});
192 FontStyles style;
193 auto tf1 = g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "locale1");
194 auto tf2 = g_fcMockVars.fontCollection->GetTypefaceForChar('a', style, "", "locale1");
195 EXPECT_NE(tf1, nullptr);
196 EXPECT_NE(tf2, nullptr);
197 EXPECT_EQ(g_fcMockVars.hasCount, 2);
198 EXPECT_EQ(tf1->Get(), tf2->Get());
199 }
200 } // namespace TextEngine
201 } // namespace Rosen
202 } // namespace OHOS
203