• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 "minikin/FontFamily.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include "minikin/LocaleList.h"
22 
23 #include "LocaleListCache.h"
24 #include "MinikinInternal.h"
25 
26 namespace minikin {
27 
TEST(LocaleListCacheTest,getId)28 TEST(LocaleListCacheTest, getId) {
29     EXPECT_NE(0UL, registerLocaleList("en"));
30     EXPECT_NE(0UL, registerLocaleList("jp"));
31     EXPECT_NE(0UL, registerLocaleList("en,zh-Hans"));
32 
33     EXPECT_EQ(0UL, LocaleListCache::getId(""));
34 
35     EXPECT_EQ(LocaleListCache::getId("en"), LocaleListCache::getId("en"));
36     EXPECT_NE(LocaleListCache::getId("en"), LocaleListCache::getId("jp"));
37 
38     EXPECT_EQ(LocaleListCache::getId("en,zh-Hans"), LocaleListCache::getId("en,zh-Hans"));
39     EXPECT_NE(LocaleListCache::getId("en,zh-Hans"), LocaleListCache::getId("zh-Hans,en"));
40     EXPECT_NE(LocaleListCache::getId("en,zh-Hans"), LocaleListCache::getId("jp"));
41     EXPECT_NE(LocaleListCache::getId("en,zh-Hans"), LocaleListCache::getId("en"));
42     EXPECT_NE(LocaleListCache::getId("en,zh-Hans"), LocaleListCache::getId("en,zh-Hant"));
43 }
44 
TEST(LocaleListCacheTest,getById)45 TEST(LocaleListCacheTest, getById) {
46     uint32_t enLangId = LocaleListCache::getId("en");
47     uint32_t jpLangId = LocaleListCache::getId("jp");
48     Locale english = LocaleListCache::getById(enLangId)[0];
49     Locale japanese = LocaleListCache::getById(jpLangId)[0];
50 
51     const LocaleList& defLangs = LocaleListCache::getById(0);
52     EXPECT_TRUE(defLangs.empty());
53 
54     const LocaleList& locales = LocaleListCache::getById(LocaleListCache::getId("en"));
55     ASSERT_EQ(1UL, locales.size());
56     EXPECT_EQ(english, locales[0]);
57 
58     const LocaleList& locales2 = LocaleListCache::getById(LocaleListCache::getId("en,jp"));
59     ASSERT_EQ(2UL, locales2.size());
60     EXPECT_EQ(english, locales2[0]);
61     EXPECT_EQ(japanese, locales2[1]);
62 }
63 
TEST(LocaleListCacheTest,buffer)64 TEST(LocaleListCacheTest, buffer) {
65     std::string locales[] = {"en", "jp", "en,zh-Hans"};
66     // Measure
67     BufferWriter fakeWriter(nullptr);
68     for (const std::string& locale : locales) {
69         LocaleListCache::writeTo(&fakeWriter, LocaleListCache::getId(locale));
70     }
71     // Write
72     std::vector<uint8_t> buffer(fakeWriter.size());
73     BufferWriter writer(buffer.data());
74     for (const std::string& locale : locales) {
75         LocaleListCache::writeTo(&writer, LocaleListCache::getId(locale));
76     }
77     // Read
78     BufferReader reader(buffer.data());
79     for (const std::string& locale : locales) {
80         EXPECT_EQ(LocaleListCache::getId(locale), LocaleListCache::readFrom(&reader))
81                 << "locale = " << locale;
82     }
83 }
84 
85 }  // namespace minikin
86