Home | History | Annotate | Download | only in tests
      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 <gtest/gtest.h>
     18 
     19 #include "HbFontCache.h"
     20 
     21 #include <cutils/log.h>
     22 #include <hb.h>
     23 #include <utils/Mutex.h>
     24 
     25 #include "MinikinInternal.h"
     26 #include "MinikinFontForTest.h"
     27 #include <minikin/MinikinFont.h>
     28 
     29 namespace android {
     30 namespace {
     31 
     32 class HbFontCacheTest : public testing::Test {
     33 public:
     34     virtual void TearDown() {
     35         AutoMutex _l(gMinikinLock);
     36         purgeHbFontCacheLocked();
     37     }
     38 };
     39 
     40 TEST_F(HbFontCacheTest, getHbFontLockedTest) {
     41     AutoMutex _l(gMinikinLock);
     42 
     43     MinikinFontForTest fontA(kTestFontDir "Regular.ttf");
     44     MinikinFontForTest fontB(kTestFontDir "Bold.ttf");
     45     MinikinFontForTest fontC(kTestFontDir "BoldItalic.ttf");
     46 
     47     // Never return NULL.
     48     EXPECT_NE(nullptr, getHbFontLocked(&fontA));
     49     EXPECT_NE(nullptr, getHbFontLocked(&fontB));
     50     EXPECT_NE(nullptr, getHbFontLocked(&fontC));
     51 
     52     EXPECT_NE(nullptr, getHbFontLocked(nullptr));
     53 
     54     // Must return same object if same font object is passed.
     55     EXPECT_EQ(getHbFontLocked(&fontA), getHbFontLocked(&fontA));
     56     EXPECT_EQ(getHbFontLocked(&fontB), getHbFontLocked(&fontB));
     57     EXPECT_EQ(getHbFontLocked(&fontC), getHbFontLocked(&fontC));
     58 
     59     // Different object must be returned if the passed minikinFont has different ID.
     60     EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontB));
     61     EXPECT_NE(getHbFontLocked(&fontA), getHbFontLocked(&fontC));
     62 }
     63 
     64 TEST_F(HbFontCacheTest, purgeCacheTest) {
     65     AutoMutex _l(gMinikinLock);
     66     MinikinFontForTest minikinFont(kTestFontDir "Regular.ttf");
     67 
     68     hb_font_t* font = getHbFontLocked(&minikinFont);
     69     ASSERT_NE(nullptr, font);
     70 
     71     // Set user data to identify the font object.
     72     hb_user_data_key_t key;
     73     void* data = (void*)0xdeadbeef;
     74     hb_font_set_user_data(font, &key, data, NULL, false);
     75     ASSERT_EQ(data, hb_font_get_user_data(font, &key));
     76 
     77     purgeHbFontCacheLocked();
     78 
     79     // By checking user data, confirm that the object after purge is different from previously
     80     // created one. Do not compare the returned pointer here since memory allocator may assign
     81     // same region for new object.
     82     font = getHbFontLocked(&minikinFont);
     83     EXPECT_EQ(nullptr, hb_font_get_user_data(font, &key));
     84 }
     85 
     86 }  // namespace
     87 }  // namespace android
     88