• 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 "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 
MakeCopy(const void * data,size_t length)49 std::unique_ptr<TexgineMemoryStream> TexgineMemoryStream::MakeCopy(const void *data, size_t length)
50 {
51     return std::move(memoryStream);
52 }
53 
MakeFromStream(std::unique_ptr<TexgineMemoryStream> stream,int index)54 std::shared_ptr<TexgineTypeface> TexgineTypeface::MakeFromStream(
55     std::unique_ptr<TexgineMemoryStream> stream, int index)
56 {
57     return typeface;
58 }
59 
60 class DynamicFontProviderTest : public testing::Test {
61 public:
62     std::shared_ptr<DynamicFontProvider> dynamicFontProvider = DynamicFontProvider::Create();
63 };
64 
65 /**
66  * @tc.name: Create
67  * @tc.desc: Verify the Create
68  * @tc.type:FUNC
69  * @tc.require: issueI6V6JP
70  */
71 HWTEST_F(DynamicFontProviderTest, Create, TestSize.Level1)
72 {
73     EXPECT_NE(DynamicFontProvider::Create(), nullptr);
74 }
75 
76 /**
77  * @tc.name: LoadFont1
78  * @tc.desc: Verify the LoadFont
79  * @tc.type:FUNC
80  * @tc.require: issueI6V6JP
81  */
82 HWTEST_F(DynamicFontProviderTest, LoadFont1, TestSize.Level1)
83 {
84     InitMyMockVars({});
85     // 4 is data length, "LF1" length, 1 is PARAMETERERROR
86     EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF1", nullptr, 4), 1); });
87 }
88 
89 /**
90  * @tc.name: LoadFont2
91  * @tc.desc: Verify the LoadFont
92  * @tc.type:FUNC
93  * @tc.require: issueI6V6JP
94  */
95 HWTEST_F(DynamicFontProviderTest, LoadFont2, TestSize.Level1)
96 {
97     InitMyMockVars({});
98     dynamicFontProvider = DynamicFontProvider::Create();
99     EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF2", this, 0), 1); });
100 }
101 
102 /**
103  * @tc.name: LoadFont3
104  * @tc.desc: Verify the LoadFont
105  * @tc.type:FUNC
106  * @tc.require: issueI6V6JP
107  */
108 HWTEST_F(DynamicFontProviderTest, LoadFont3, TestSize.Level1)
109 {
110     InitMyMockVars({.memoryStream = nullptr});
111     // APIERROR
112     EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF3", this, 4), 2); });
113 }
114 
115 /**
116  * @tc.name: LoadFont4
117  * @tc.desc: Verify the LoadFont
118  * @tc.type:FUNC
119  * @tc.require: issueI6V6JP
120  */
121 HWTEST_F(DynamicFontProviderTest, LoadFont4, TestSize.Level1)
122 {
123     InitMyMockVars({.typeface = nullptr});
124     EXPECT_NO_THROW({ EXPECT_EQ(dynamicFontProvider->LoadFont("LF4", this, 4), 2); });
125 }
126 
127 /**
128  * @tc.name: LoadFont5
129  * @tc.desc: Verify the LoadFont
130  * @tc.type:FUNC
131  * @tc.require: issueI6V6JP
132  */
133 HWTEST_F(DynamicFontProviderTest, LoadFont5, TestSize.Level1)
134 {
135     InitMyMockVars({});
136     EXPECT_NO_THROW({
137         auto ret = dynamicFontProvider->LoadFont("LF5", this, 4);
138         EXPECT_EQ(ret, 0);
139     });
140 }
141 
142 /**
143  * @tc.name: MatchFamily1
144  * @tc.desc: Verify the MatchFamily
145  * @tc.type:FUNC
146  * @tc.require: issueI6V6JP
147  */
148 HWTEST_F(DynamicFontProviderTest, MatchFamily1, TestSize.Level1)
149 {
150     EXPECT_EQ(dynamicFontProvider->MatchFamily("aaa"), nullptr);
151 }
152 
153 /**
154  * @tc.name: MatchFamily2
155  * @tc.desc: Verify the MatchFamily
156  * @tc.type:FUNC
157  * @tc.require: issueI6V6JP
158  */
159 HWTEST_F(DynamicFontProviderTest, MatchFamily2, TestSize.Level1)
160 {
161     InitMyMockVars({});
162     dynamicFontProvider->LoadFont("aaa", this, 4);
163     EXPECT_NE(dynamicFontProvider->MatchFamily("aaa"), nullptr);
164 }
165 
166 /**
167  * @tc.name: MatchFamily3
168  * @tc.desc: Verify the MatchFamily
169  * @tc.type:FUNC
170  * @tc.require: issueI6V6JP
171  */
172 HWTEST_F(DynamicFontProviderTest, MatchFamily3, TestSize.Level1)
173 {
174     InitMyMockVars({});
175     dynamicFontProvider->LoadFont("aaa", this, 4);
176     EXPECT_EQ(dynamicFontProvider->MatchFamily("bbb"), nullptr);
177 }
178 } // namespace TextEngine
179 } // namespace Rosen
180 } // namespace OHOS
181