• 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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 
19 #include "font_collection.h"
20 #include "texgine/font_providers.h"
21 #include "texgine/system_font_provider.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace TextEngine {
29 class MockFontProvider : public IFontProvider {
30 public:
31     MOCK_METHOD(std::shared_ptr<VariantFontStyleSet>, MatchFamily, (const std::string &familyName), (noexcept(true)));
32 };
33 
34 struct MockVars {
35     std::shared_ptr<VariantFontStyleSet> systemFontProviderMatchFamilyRetval;
36     std::vector<std::shared_ptr<VariantFontStyleSet>> catchedFontStyleSets;
37 } g_fpMockvars;
38 
InitFpMockVars(struct MockVars && vars)39 void InitFpMockVars(struct MockVars &&vars)
40 {
41     g_fpMockvars = std::move(vars);
42 }
43 
FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> && fontStyleSets)44 FontCollection::FontCollection(std::vector<std::shared_ptr<VariantFontStyleSet>> &&fontStyleSets)
45 {
46     g_fpMockvars.catchedFontStyleSets = std::move(fontStyleSets);
47 }
48 
MatchFamily(const std::string & familyName)49 std::shared_ptr<VariantFontStyleSet> SystemFontProvider::MatchFamily(const std::string &familyName) noexcept(true)
50 {
51     return g_fpMockvars.systemFontProviderMatchFamilyRetval;
52 }
53 
54 class FontProvidersTest : public testing::Test {
55 public:
56     std::shared_ptr<FontProviders> fontProviders_ = FontProviders::Create();
57     std::shared_ptr<MockFontProvider> msfp1_ = std::make_shared<MockFontProvider>();
58     std::shared_ptr<MockFontProvider> msfp2_ = std::make_shared<MockFontProvider>();
59     std::shared_ptr<VariantFontStyleSet> fontStyleSet1_ = std::make_shared<VariantFontStyleSet>(nullptr);
60     std::shared_ptr<VariantFontStyleSet> fontStyleSet2_ = std::make_shared<VariantFontStyleSet>(nullptr);
61 };
62 
63 /**
64  * @tc.name: CreateAndSystemOnly
65  * @tc.desc: Verify the CreateAndSystemOnly
66  * @tc.type:FUNC
67  */
68 HWTEST_F(FontProvidersTest, CreateAndSystemOnly, TestSize.Level1)
69 {
70     std::shared_ptr<VariantFontStyleSet> fss1 = std::make_shared<VariantFontStyleSet>(nullptr);
71 
72     // Create
73     InitFpMockVars({});
74     auto fp1 = FontProviders::Create();
75     ASSERT_NE(fp1, nullptr);
76     auto fc1 = fp1->GenerateFontCollection({"Create"});
77     ASSERT_NE(fc1, nullptr);
78     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 0u);
79 
80     // SystemFontOnly
81     InitFpMockVars({.systemFontProviderMatchFamilyRetval = fss1});
82     auto fp2 = FontProviders::SystemFontOnly();
83     ASSERT_NE(fp2, nullptr);
84     auto fc2 = fp2->GenerateFontCollection({"SystemFontOnly"});
85     ASSERT_NE(fc2, nullptr);
86     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 1u);
87     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss1);
88 }
89 
90 /**
91  * @tc.name: AppendFontProvider1
92  * @tc.desc: Verify the AppendFontProvider
93  * @tc.type:FUNC
94  */
95 HWTEST_F(FontProvidersTest, AppendFontProvider1, TestSize.Level1)
96 {
97     // AppendFontProvider nullptr
98     InitFpMockVars({});
99     auto fp1 = FontProviders::Create();
100     ASSERT_NE(fp1, nullptr);
101     fp1->AppendFontProvider(nullptr);
102     auto fc1 = fp1->GenerateFontCollection({"AppendFontProvider1"});
103     ASSERT_NE(fc1, nullptr);
104     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 0u);
105 }
106 
107 /**
108  * @tc.name: AppendFontProvider2
109  * @tc.desc: Verify the AppendFontProvider
110  * @tc.type:FUNC
111  */
112 HWTEST_F(FontProvidersTest, AppendFontProvider2, TestSize.Level1)
113 {
114     std::shared_ptr<VariantFontStyleSet> fss1 = std::make_shared<VariantFontStyleSet>(nullptr);
115 
116     // AppendFontProvider mock1 once
117     InitFpMockVars({});
118     auto fp2 = FontProviders::Create();
119     ASSERT_NE(fp2, nullptr);
120     auto mfp1 = std::make_shared<MockFontProvider>();
121     EXPECT_CALL(*mfp1, MatchFamily("AppendFontProvider2")).Times(1).WillOnce(testing::Return(fss1));
122     fp2->AppendFontProvider(mfp1);
123     auto fc2 = fp2->GenerateFontCollection({"AppendFontProvider2"});
124     ASSERT_NE(fc2, nullptr);
125     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 1u);
126     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss1);
127 }
128 
129 /**
130  * @tc.name: AppendFontProvider3
131  * @tc.desc: Verify the AppendFontProvider
132  * @tc.type:FUNC
133  */
134 HWTEST_F(FontProvidersTest, AppendFontProvider3, TestSize.Level1)
135 {
136     std::shared_ptr<VariantFontStyleSet> fss1 = std::make_shared<VariantFontStyleSet>(nullptr);
137     std::shared_ptr<VariantFontStyleSet> fss2 = std::make_shared<VariantFontStyleSet>(nullptr);
138 
139     // AppendFontProvider mock1 mock2
140     InitFpMockVars({});
141     auto fp4 = FontProviders::Create();
142     ASSERT_NE(fp4, nullptr);
143     auto mfp3 = std::make_shared<MockFontProvider>();
144     EXPECT_CALL(*mfp3, MatchFamily)
145         .Times(2)
146         .WillOnce(testing::Return(fss1))
147         .WillOnce(testing::Return(nullptr)); // for mfp4
148     auto mfp4 = std::make_shared<MockFontProvider>();
149     EXPECT_CALL(*mfp4, MatchFamily("AppendFontProvider5"))
150         .Times(1)
151         .WillOnce(testing::Return(fss2));
152     fp4->AppendFontProvider(mfp3);
153     fp4->AppendFontProvider(mfp4);
154     auto fc4 = fp4->GenerateFontCollection({"AppendFontProvider4", "AppendFontProvider5"});
155     ASSERT_NE(fc4, nullptr);
156     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 2u);
157     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss1);
158     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[1], fss2);
159 }
160 
161 /**
162  * @tc.name: GenerateFontCollection1
163  * @tc.desc: Verify the GenerateFontCollection
164  * @tc.type:FUNC
165  */
166 HWTEST_F(FontProvidersTest, GenerateFontCollection1, TestSize.Level1)
167 {
168     // GenerateFontCollection set=nullptr
169     InitFpMockVars({});
170     auto fp1 = FontProviders::Create();
171     ASSERT_NE(fp1, nullptr);
172     auto mfp1 = std::make_shared<MockFontProvider>();
173     EXPECT_CALL(*mfp1, MatchFamily("GenerateFontCollection1")).Times(1).WillOnce(testing::Return(nullptr));
174     fp1->AppendFontProvider(mfp1);
175     auto fc1 = fp1->GenerateFontCollection({"GenerateFontCollection1"});
176     ASSERT_NE(fc1, nullptr);
177     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 0u);
178 }
179 
180 /**
181  * @tc.name: GenerateFontCollection2
182  * @tc.desc: Verify the GenerateFontCollection
183  * @tc.type:FUNC
184  */
185 HWTEST_F(FontProvidersTest, GenerateFontCollection2, TestSize.Level1)
186 {
187     std::shared_ptr<VariantFontStyleSet> fss1 = std::make_shared<VariantFontStyleSet>(nullptr);
188 
189     // GenerateFontCollection use cache
190     InitFpMockVars({});
191     auto fp2 = FontProviders::Create();
192     ASSERT_NE(fp2, nullptr);
193     auto mfp2 = std::make_shared<MockFontProvider>();
194     EXPECT_CALL(*mfp2, MatchFamily("GenerateFontCollection2")).Times(1).WillOnce(testing::Return(fss1));
195     fp2->AppendFontProvider(mfp2);
196     auto fc21 = fp2->GenerateFontCollection({"GenerateFontCollection2"});
197     ASSERT_NE(fc21, nullptr);
198     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 1u);
199     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss1);
200     auto fc22 = fp2->GenerateFontCollection({"GenerateFontCollection2"});
201     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 1u);
202     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss1);
203 }
204 
205 /**
206  * @tc.name: GenerateFontCollection3
207  * @tc.desc: Verify the GenerateFontCollection
208  * @tc.type:FUNC
209  */
210 HWTEST_F(FontProvidersTest, GenerateFontCollection3, TestSize.Level1)
211 {
212     std::shared_ptr<VariantFontStyleSet> fss2 = std::make_shared<VariantFontStyleSet>(nullptr);
213 
214     // GenerateFontCollection first failed, second success
215     InitFpMockVars({});
216     auto fp3 = FontProviders::Create();
217     ASSERT_NE(fp3, nullptr);
218     auto mfp3 = std::make_shared<MockFontProvider>();
219     EXPECT_CALL(*mfp3, MatchFamily("GenerateFontCollection3")).Times(1).WillOnce(testing::Return(nullptr));
220     fp3->AppendFontProvider(mfp3);
221     auto mfp4 = std::make_shared<MockFontProvider>();
222     EXPECT_CALL(*mfp4, MatchFamily("GenerateFontCollection3")).Times(1).WillOnce(testing::Return(fss2));
223     fp3->AppendFontProvider(mfp4);
224     auto fc3 = fp3->GenerateFontCollection({"GenerateFontCollection3"});
225     ASSERT_NE(fc3, nullptr);
226     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets.size(), 1u);
227     ASSERT_EQ(g_fpMockvars.catchedFontStyleSets[0], fss2);
228 }
229 } // namespace TextEngine
230 } // namespace Rosen
231 } // namespace OHOS
232