1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "HbFontCache.h"
18
19 #include <android/log.h>
20 #include <gtest/gtest.h>
21 #include <utils/Mutex.h>
22
23 #include <memory>
24
25 #include <hb.h>
26
27 #include "MinikinInternal.h"
28 #include "MinikinFontForTest.h"
29 #include <minikin/MinikinFont.h>
30
31 namespace minikin {
32
33 class HbFontCacheTest : public testing::Test {
34 public:
TearDown()35 virtual void TearDown() {
36 android::AutoMutex _l(gMinikinLock);
37 purgeHbFontCacheLocked();
38 }
39 };
40
TEST_F(HbFontCacheTest,getHbFontLockedTest)41 TEST_F(HbFontCacheTest, getHbFontLockedTest) {
42 std::shared_ptr<MinikinFontForTest> fontA(
43 new MinikinFontForTest(kTestFontDir "Regular.ttf"));
44
45 std::shared_ptr<MinikinFontForTest> fontB(
46 new MinikinFontForTest(kTestFontDir "Bold.ttf"));
47
48 std::shared_ptr<MinikinFontForTest> fontC(
49 new MinikinFontForTest(kTestFontDir "BoldItalic.ttf"));
50
51 android::AutoMutex _l(gMinikinLock);
52 // Never return NULL.
53 EXPECT_NE(nullptr, getHbFontLocked(fontA.get()));
54 EXPECT_NE(nullptr, getHbFontLocked(fontB.get()));
55 EXPECT_NE(nullptr, getHbFontLocked(fontC.get()));
56
57 EXPECT_NE(nullptr, getHbFontLocked(nullptr));
58
59 // Must return same object if same font object is passed.
60 EXPECT_EQ(getHbFontLocked(fontA.get()), getHbFontLocked(fontA.get()));
61 EXPECT_EQ(getHbFontLocked(fontB.get()), getHbFontLocked(fontB.get()));
62 EXPECT_EQ(getHbFontLocked(fontC.get()), getHbFontLocked(fontC.get()));
63
64 // Different object must be returned if the passed minikinFont has different ID.
65 EXPECT_NE(getHbFontLocked(fontA.get()), getHbFontLocked(fontB.get()));
66 EXPECT_NE(getHbFontLocked(fontA.get()), getHbFontLocked(fontC.get()));
67 }
68
TEST_F(HbFontCacheTest,purgeCacheTest)69 TEST_F(HbFontCacheTest, purgeCacheTest) {
70 std::shared_ptr<MinikinFontForTest> minikinFont(
71 new MinikinFontForTest(kTestFontDir "Regular.ttf"));
72
73 android::AutoMutex _l(gMinikinLock);
74 hb_font_t* font = getHbFontLocked(minikinFont.get());
75 ASSERT_NE(nullptr, font);
76
77 // Set user data to identify the font object.
78 hb_user_data_key_t key;
79 void* data = (void*)0xdeadbeef;
80 hb_font_set_user_data(font, &key, data, NULL, false);
81 ASSERT_EQ(data, hb_font_get_user_data(font, &key));
82
83 purgeHbFontCacheLocked();
84
85 // By checking user data, confirm that the object after purge is different from previously
86 // created one. Do not compare the returned pointer here since memory allocator may assign
87 // same region for new object.
88 font = getHbFontLocked(minikinFont.get());
89 EXPECT_EQ(nullptr, hb_font_get_user_data(font, &key));
90 }
91
92 } // namespace minikin
93