1 /*
2 * Copyright (c) 2024 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 "asset_font_manager.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypeface.h"
20 #include "typeface_font_asset_provider.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace txt {
26 class AssetFontManagerTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 static inline std::shared_ptr<AssetFontManager> assetFontManager_ = nullptr;
31 };
32
SetUpTestCase()33 void AssetFontManagerTest::SetUpTestCase()
34 {
35 std::unique_ptr<TypefaceFontAssetProvider> fontProvider = std::make_unique<TypefaceFontAssetProvider>();
36 ASSERT_NE(fontProvider, nullptr);
37 std::string familyName("test");
38 fontProvider->familyNames_.emplace_back(familyName);
39 assetFontManager_ = std::make_shared<AssetFontManager>(std::move(fontProvider));
40 ASSERT_NE(assetFontManager_, nullptr);
41 }
42
TearDownTestCase()43 void AssetFontManagerTest::TearDownTestCase()
44 {
45 }
46
47 /*
48 * @tc.name: AssetFontManagerTest001
49 * @tc.desc: test for onMatchFamily and some nullptr functions
50 * @tc.type: FUNC
51 */
52 HWTEST_F(AssetFontManagerTest, AssetFontManagerTest001, TestSize.Level0)
53 {
54 std::string familyName("test");
55 EXPECT_EQ(assetFontManager_->onMatchFamily(familyName.c_str()), nullptr);
56 SkString skFamilyName(familyName);
57 assetFontManager_->onGetFamilyName(0, &skFamilyName);
58 EXPECT_EQ(assetFontManager_->onCountFamilies(), 1);
59 EXPECT_EQ(assetFontManager_->onCreateStyleSet(0), nullptr);
60 SkFontStyle fontStyle;
61 EXPECT_EQ(assetFontManager_->onMatchFamilyStyle(familyName.c_str(), fontStyle), nullptr);
62 EXPECT_EQ(assetFontManager_->onMatchFamilyStyleCharacter(familyName.c_str(), fontStyle, nullptr, 0, 0), nullptr);
63 sk_sp<SkData> data = SkData::MakeEmpty();
64 EXPECT_EQ(assetFontManager_->onMakeFromData(data, 0), nullptr);
65 EXPECT_EQ(assetFontManager_->onMakeFromStreamIndex(nullptr, 0), nullptr);
66 SkFontArguments fontArguments;
67 EXPECT_EQ(assetFontManager_->onMakeFromStreamArgs(nullptr, fontArguments), nullptr);
68 EXPECT_EQ(assetFontManager_->onMakeFromFile(nullptr, 0), nullptr);
69 EXPECT_EQ(assetFontManager_->onLegacyMakeTypeface(nullptr, fontStyle), nullptr);
70 DynamicFontManager dynamicFontManager;
71 dynamicFontManager.font_provider();
72 EXPECT_NE(dynamicFontManager.fontProvider_, nullptr);
73 }
74
75 /*
76 * @tc.name: AssetFontManagerTest012
77 * @tc.desc: test for TestFontManager
78 * @tc.type: FUNC
79 */
80 HWTEST_F(AssetFontManagerTest, AssetFontManagerTest012, TestSize.Level0)
81 {
82 std::unique_ptr<FontAssetProvider> fontProvider = std::make_unique<TypefaceFontAssetProvider>();
83 EXPECT_NE(fontProvider, nullptr);
84 std::string familyName("test");
85 std::vector<std::string> familyNames;
86 familyNames.emplace_back(familyName);
87 std::shared_ptr<TestFontManager> testFontManager =
88 std::make_shared<TestFontManager>(std::move(fontProvider), familyNames);
89 EXPECT_NE(testFontManager, nullptr);
90 EXPECT_EQ(testFontManager->onMatchFamily(familyName.c_str()), nullptr);
91 }
92
93 /*
94 * @tc.name: AssetFontManagerTest013
95 * @tc.desc: test for match family and style
96 * @tc.type: FUNC
97 */
98 HWTEST_F(AssetFontManagerTest, AssetFontManagerTest013, TestSize.Level0)
99 {
100 std::unique_ptr<TypefaceFontAssetProvider> fontProvider = std::make_unique<TypefaceFontAssetProvider>();
101 std::string familyName("test");
102 fontProvider->familyNames_.emplace_back(familyName);
103 sk_sp<SkTypeface> skTypeface = SkTypeface::MakeDefault();
104 fontProvider->RegisterTypeface(skTypeface, familyName);
105 std::shared_ptr<AssetFontManager> assetFontManager =
106 std::make_shared<AssetFontManager>(std::move(fontProvider));
107
108 SkFontStyle fontStyle;
109 EXPECT_NE(assetFontManager->onMatchFamilyStyle(familyName.c_str(), fontStyle), nullptr);
110 }
111 } // namespace txt