• 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 <iostream>
17 
18 #include <gtest/gtest.h>
19 
20 #include "mock.h"
21 #include "dynamic_file_font_provider.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace {
27 struct MockVars {
28     bool existsReturnValue = true;
29     std::error_code errorCode;
30     bool openReturnValue = true;
31     std::ios_base::iostate tellgState = std::ios_base::goodbit;
32     std::ios_base::iostate seekg1State = std::ios_base::goodbit;
33     std::ios_base::iostate seekg2State = std::ios_base::goodbit;
34     std::ios_base::iostate readState = std::ios_base::goodbit;
35 } g_mockVars;
36 
InitDffpMockVars(MockVars vars)37 void InitDffpMockVars(MockVars vars)
38 {
39     g_mockVars = vars;
40 }
41 } // namespace
42 
43 namespace OHOS {
44 namespace Rosen {
45 namespace TextEngine {
StdFilesystemExists(const std::string & p,std::error_code & ec)46 bool StdFilesystemExists(const std::string &p, std::error_code &ec)
47 {
48     ec = g_mockVars.errorCode;
49     return g_mockVars.existsReturnValue;
50 }
51 
StdFilesystemIsOpen() const52 bool MockIFStream::StdFilesystemIsOpen() const
53 {
54     return g_mockVars.openReturnValue;
55 }
56 
StdFilesystemSeekg(std::ifstream::off_type off,std::ios_base::seekdir dir)57 std::istream &MockIFStream::StdFilesystemSeekg(std::ifstream::off_type off, std::ios_base::seekdir dir)
58 {
59     this->clear();
60     this->setstate(g_mockVars.seekg2State);
61     return *this;
62 }
63 
StdFilesystemSeekg(pos_type pos)64 std::istream &MockIFStream::StdFilesystemSeekg(pos_type pos)
65 {
66     this->clear();
67     this->setstate(g_mockVars.seekg1State);
68     return *this;
69 }
70 
StdFilesystemTellg()71 std::ifstream::pos_type MockIFStream::StdFilesystemTellg()
72 {
73     this->clear();
74     this->setstate(g_mockVars.tellgState);
75     return {};
76 }
77 
StdFilesystemRead(char_type * s,std::streamsize count)78 std::istream &MockIFStream::StdFilesystemRead(char_type *s, std::streamsize count)
79 {
80     this->clear();
81     this->setstate(g_mockVars.readState);
82     return *this;
83 }
84 
LoadFont(const std::string & familyName,const void * data,size_t datalen)85 int DynamicFontProvider::LoadFont(const std::string &familyName,
86                                   const void *data, size_t datalen) noexcept(true)
87 {
88     return 0;
89 }
90 
91 class DynamicFileFontProviderTest : public testing::Test {
92 public:
93     std::shared_ptr<DynamicFileFontProvider> dynamicFileFontProvider = DynamicFileFontProvider::Create();
94 };
95 
96 /**
97  * @tc.name: Create
98  * @tc.desc: Verify the Create
99  * @tc.type:FUNC
100  * @tc.require: issueI6TIHE
101  */
102 HWTEST_F(DynamicFileFontProviderTest, Create, TestSize.Level1)
103 {
104     EXPECT_NE(DynamicFileFontProvider::Create(), nullptr);
105 }
106 
107 /**
108  * @tc.name: LoadFont1
109  * @tc.desc: Verify the LoadFont
110  * @tc.type:FUNC
111  * @tc.require: issueI6TIHE
112  */
113 HWTEST_F(DynamicFileFontProviderTest, LoadFont1, TestSize.Level1)
114 {
115     InitDffpMockVars({.errorCode = std::make_error_code(std::errc::file_exists)});
116     // 1 is data length
117     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF1", {}), 1);
118 }
119 
120 /**
121  * @tc.name: LoadFont2
122  * @tc.desc: Verify the LoadFont
123  * @tc.type:FUNC
124  * @tc.require: issueI6TIHE
125  */
126 HWTEST_F(DynamicFileFontProviderTest, LoadFont2, TestSize.Level1)
127 {
128     InitDffpMockVars({.existsReturnValue = false});
129     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF2", {}), 1);
130 }
131 
132 /**
133  * @tc.name: LoadFont3
134  * @tc.desc: Verify the LoadFont
135  * @tc.type:FUNC
136  * @tc.require: issueI6TIHE
137  */
138 HWTEST_F(DynamicFileFontProviderTest, LoadFont3, TestSize.Level1)
139 {
140     InitDffpMockVars({.openReturnValue = false});
141     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF3", {}), 1);
142 }
143 
144 /**
145  * @tc.name: LoadFont4
146  * @tc.desc: Verify the LoadFont
147  * @tc.type:FUNC
148  * @tc.require: issueI6TIHE
149  */
150 HWTEST_F(DynamicFileFontProviderTest, LoadFont4, TestSize.Level1)
151 {
152     InitDffpMockVars({.seekg2State = std::ios_base::badbit});
153     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF4", {}), 1);
154 }
155 
156 /**
157  * @tc.name: LoadFont5
158  * @tc.desc: Verify the LoadFont
159  * @tc.type:FUNC
160  * @tc.require: issueI6TIHE
161  */
162 HWTEST_F(DynamicFileFontProviderTest, LoadFont5, TestSize.Level1)
163 {
164     InitDffpMockVars({.tellgState = std::ios_base::failbit});
165     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF5", {}), 1);
166 }
167 
168 /**
169  * @tc.name: LoadFont6
170  * @tc.desc: Verify the LoadFont
171  * @tc.type:FUNC
172  * @tc.require: issueI6TIHE
173  */
174 HWTEST_F(DynamicFileFontProviderTest, LoadFont6, TestSize.Level1)
175 {
176     InitDffpMockVars({.seekg1State = std::ios_base::badbit});
177     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF6", {}), 1);
178 }
179 
180 /**
181  * @tc.name: LoadFont7
182  * @tc.desc: Verify the LoadFont
183  * @tc.type:FUNC
184  * @tc.require: issueI6TIHE
185  */
186 HWTEST_F(DynamicFileFontProviderTest, LoadFont7, TestSize.Level1)
187 {
188     InitDffpMockVars({.readState = std::ios_base::badbit});
189     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF7", {}), 1);
190 }
191 
192 /**
193  * @tc.name: LoadFont8
194  * @tc.desc: Verify the LoadFont
195  * @tc.type:FUNC
196  * @tc.require: issueI6TIHE
197  */
198 HWTEST_F(DynamicFileFontProviderTest, LoadFont8, TestSize.Level1)
199 {
200     InitDffpMockVars({});
201     EXPECT_EQ(dynamicFileFontProvider->LoadFont("LF8", {}), 0);
202 }
203 } // namespace TextEngine
204 } // namespace Rosen
205 } // namespace OHOS
206