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