• 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 "texgine_font_manager.h"
20 #include "texgine_font_style_set.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 struct MockVars {
26     sk_sp<SkTypeface> skTypeface_ = SkTypeface::MakeDefault();
27     std::shared_ptr<SkFontStyle> skFontStyle_ = std::make_shared<SkFontStyle>();
28     std::shared_ptr<OHOS::Rosen::TextEngine::TexgineFontStyle> texgineFontStyle_ =
29         std::make_shared<OHOS::Rosen::TextEngine::TexgineFontStyle>();
30     std::shared_ptr<OHOS::Rosen::TextEngine::TexgineFontStyleSet> texgineFontStyleSet_ =
31         OHOS::Rosen::TextEngine::TexgineFontStyleSet::CreateEmpty();
32 } g_tfmMockvars;
33 
InitTfmMockVars(struct MockVars && vars)34 void InitTfmMockVars(struct MockVars &&vars)
35 {
36     g_tfmMockvars = std::move(vars);
37 }
38 
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const39 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
40                                                  const char* bcp47[], int bcp47Count,
41                                                  SkUnichar character) const
42 {
43     return g_tfmMockvars.skTypeface_.get();
44 }
45 
matchFamily(const char familyName[]) const46 SkFontStyleSet *SkFontMgr::matchFamily(const char familyName[]) const
47 {
48     return g_tfmMockvars.texgineFontStyleSet_->GetFontStyleSet();
49 }
50 
51 namespace OHOS {
52 namespace Rosen {
53 namespace TextEngine {
54 class TexgineFontManagerTest : public testing::Test {
55 };
56 
57 /**
58  * @tc.name:MatchFamilyStyleCharacter
59  * @tc.desc: Verify the MatchFamilyStyleCharacter
60  * @tc.type:FUNC
61  */
62 HWTEST_F(TexgineFontManagerTest, MatchFamilyStyleCharacter, TestSize.Level1)
63 {
64     InitTfmMockVars({});
65     std::string str = "";
66     std::shared_ptr<TexgineFontManager> tfm1 = std::make_shared<TexgineFontManager>();
67     tfm1->SetFontMgr(nullptr);
68     EXPECT_EQ(tfm1->MatchFamilyStyleCharacter(str, *g_tfmMockvars.texgineFontStyle_, nullptr, 0, 0), nullptr);
69 
70     std::shared_ptr<TexgineFontManager> tfm2 = TexgineFontManager::RefDefault();
71     g_tfmMockvars.texgineFontStyle_->SetFontStyle(nullptr);
72     EXPECT_EQ(tfm2->MatchFamilyStyleCharacter(str, *g_tfmMockvars.texgineFontStyle_, nullptr, 0, 0), nullptr);
73 
74     InitTfmMockVars({});
75     std::shared_ptr<TexgineFontManager> tfm3 = TexgineFontManager::RefDefault();
76     auto tf = tfm3->MatchFamilyStyleCharacter(str, *g_tfmMockvars.texgineFontStyle_, nullptr, 0, 0);
77     EXPECT_EQ(tf->GetTypeface().get(), g_tfmMockvars.skTypeface_.get());
78 }
79 
80 /**
81  * @tc.name:MatchFamily
82  * @tc.desc: Verify the MatchFamily
83  * @tc.type:FUNC
84  */
85 HWTEST_F(TexgineFontManagerTest, MatchFamily, TestSize.Level1)
86 {
87     InitTfmMockVars({});
88     std::string str = "";
89     std::shared_ptr<TexgineFontManager> tfm1 = std::make_shared<TexgineFontManager>();
90     tfm1->SetFontMgr(nullptr);
91     EXPECT_EQ(tfm1->MatchFamily(str), nullptr);
92 
93     InitTfmMockVars({});
94     std::shared_ptr<TexgineFontManager> tfm2 = TexgineFontManager::RefDefault();
95     auto tfss = tfm2->MatchFamily(str);
96     EXPECT_EQ(tfss->GetFontStyleSet(), g_tfmMockvars.texgineFontStyleSet_->GetFontStyleSet());
97 }
98 } // namespace TextEngine
99 } // namespace Rosen
100 } // namespace OHOS
101