blob: 744a41e5f2900c770ec4e6c1a620796beed53dbe [file] [log] [blame]
tomhudsonf79673b2014-08-05 06:36:11 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
9#include "SkFontConfigParser_android.h"
10#include "Test.h"
11
tomhudson2ed49a42014-08-13 07:53:48 -070012int CountFallbacks(SkTDArray<FontFamily*> fontFamilies) {
13 int countOfFallbackFonts = 0;
14 for (int i = 0; i < fontFamilies.count(); i++) {
15 if (fontFamilies[i]->fIsFallbackFont) {
16 countOfFallbackFonts++;
17 }
18 }
19 return countOfFallbackFonts;
20}
21
bungeman4b86bac2014-11-04 10:54:31 -080022void ValidateLoadedFonts(SkTDArray<FontFamily*> fontFamilies, const char* firstExpectedFile,
tomhudsonf79673b2014-08-05 06:36:11 -070023 skiatest::Reporter* reporter) {
24 REPORTER_ASSERT(reporter, fontFamilies[0]->fNames.count() == 5);
25 REPORTER_ASSERT(reporter, !strcmp(fontFamilies[0]->fNames[0].c_str(), "sans-serif"));
26 REPORTER_ASSERT(reporter,
bungeman4b86bac2014-11-04 10:54:31 -080027 !strcmp(fontFamilies[0]->fFonts[0].fFileName.c_str(), firstExpectedFile));
tomhudsonf79673b2014-08-05 06:36:11 -070028 REPORTER_ASSERT(reporter, !fontFamilies[0]->fIsFallbackFont);
tomhudsonf79673b2014-08-05 06:36:11 -070029}
30
31void DumpLoadedFonts(SkTDArray<FontFamily*> fontFamilies) {
32#if SK_DEBUG_FONTS
33 for (int i = 0; i < fontFamilies.count(); ++i) {
34 SkDebugf("Family %d:\n", i);
tomhudson07544752014-08-05 13:35:00 -070035 switch(fontFamilies[i]->fVariant) {
bungemana44e9b92015-01-30 19:58:19 -080036 case kElegant_FontVariant: SkDebugf(" elegant\n"); break;
37 case kCompact_FontVariant: SkDebugf(" compact\n"); break;
tomhudson07544752014-08-05 13:35:00 -070038 default: break;
39 }
bungeman7fa87cd2015-02-06 07:59:19 -080040 if (fontFamilies[i]->fBasePath) {
41 SkDebugf(" basePath %s\n", fontFamilies[i]->fBasePath);
42 }
tomhudson07544752014-08-05 13:35:00 -070043 if (!fontFamilies[i]->fLanguage.getTag().isEmpty()) {
bungemana44e9b92015-01-30 19:58:19 -080044 SkDebugf(" language %s\n", fontFamilies[i]->fLanguage.getTag().c_str());
tomhudson07544752014-08-05 13:35:00 -070045 }
tomhudsonf79673b2014-08-05 06:36:11 -070046 for (int j = 0; j < fontFamilies[i]->fNames.count(); ++j) {
47 SkDebugf(" name %s\n", fontFamilies[i]->fNames[j].c_str());
48 }
tomhudsond3ddea22014-08-11 11:28:00 -070049 for (int j = 0; j < fontFamilies[i]->fFonts.count(); ++j) {
50 const FontFileInfo& ffi = fontFamilies[i]->fFonts[j];
bungemana44e9b92015-01-30 19:58:19 -080051 SkDebugf(" file (%d) %s#%d\n", ffi.fWeight, ffi.fFileName.c_str(), ffi.fIndex);
tomhudson07544752014-08-05 13:35:00 -070052 }
tomhudsonf79673b2014-08-05 06:36:11 -070053 }
54#endif // SK_DEBUG_FONTS
55}
56
57DEF_TEST(FontConfigParserAndroid, reporter) {
58
tomhudson8aed3c12014-08-07 10:20:51 -070059 bool resourcesMissing = false;
60
tomhudsonf79673b2014-08-05 06:36:11 -070061 SkTDArray<FontFamily*> preV17FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -080062 SkFontConfigParser::GetCustomFontFamilies(preV17FontFamilies,
63 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -070064 GetResourcePath("android_fonts/pre_v17/system_fonts.xml").c_str(),
65 GetResourcePath("android_fonts/pre_v17/fallback_fonts.xml").c_str());
66
tomhudson8aed3c12014-08-07 10:20:51 -070067 if (preV17FontFamilies.count() > 0) {
68 REPORTER_ASSERT(reporter, preV17FontFamilies.count() == 14);
tomhudson2ed49a42014-08-13 07:53:48 -070069 REPORTER_ASSERT(reporter, CountFallbacks(preV17FontFamilies) == 10);
tomhudsonf79673b2014-08-05 06:36:11 -070070
tomhudson8aed3c12014-08-07 10:20:51 -070071 DumpLoadedFonts(preV17FontFamilies);
bungeman4b86bac2014-11-04 10:54:31 -080072 ValidateLoadedFonts(preV17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -070073 } else {
74 resourcesMissing = true;
75 }
tomhudsonf79673b2014-08-05 06:36:11 -070076
tomhudson07544752014-08-05 13:35:00 -070077
tomhudsonf79673b2014-08-05 06:36:11 -070078 SkTDArray<FontFamily*> v17FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -080079 SkFontConfigParser::GetCustomFontFamilies(v17FontFamilies,
80 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -070081 GetResourcePath("android_fonts/v17/system_fonts.xml").c_str(),
82 GetResourcePath("android_fonts/v17/fallback_fonts.xml").c_str());
83
tomhudson8aed3c12014-08-07 10:20:51 -070084 if (v17FontFamilies.count() > 0) {
85 REPORTER_ASSERT(reporter, v17FontFamilies.count() == 41);
tomhudson2ed49a42014-08-13 07:53:48 -070086 REPORTER_ASSERT(reporter, CountFallbacks(v17FontFamilies) == 31);
tomhudsonf79673b2014-08-05 06:36:11 -070087
tomhudson8aed3c12014-08-07 10:20:51 -070088 DumpLoadedFonts(v17FontFamilies);
bungeman4b86bac2014-11-04 10:54:31 -080089 ValidateLoadedFonts(v17FontFamilies, "Roboto-Regular.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -070090 } else {
91 resourcesMissing = true;
92 }
tomhudsonf79673b2014-08-05 06:36:11 -070093
tomhudson07544752014-08-05 13:35:00 -070094
tomhudsonf79673b2014-08-05 06:36:11 -070095 SkTDArray<FontFamily*> v22FontFamilies;
bungeman7fa87cd2015-02-06 07:59:19 -080096 SkFontConfigParser::GetCustomFontFamilies(v22FontFamilies,
97 SkString("/custom/font/path/"),
tomhudsonf79673b2014-08-05 06:36:11 -070098 GetResourcePath("android_fonts/v22/fonts.xml").c_str(),
99 NULL);
100
tomhudson8aed3c12014-08-07 10:20:51 -0700101 if (v22FontFamilies.count() > 0) {
102 REPORTER_ASSERT(reporter, v22FontFamilies.count() == 53);
tomhudson2ed49a42014-08-13 07:53:48 -0700103 REPORTER_ASSERT(reporter, CountFallbacks(v22FontFamilies) == 42);
tomhudsonf79673b2014-08-05 06:36:11 -0700104
tomhudson8aed3c12014-08-07 10:20:51 -0700105 DumpLoadedFonts(v22FontFamilies);
bungeman4b86bac2014-11-04 10:54:31 -0800106 ValidateLoadedFonts(v22FontFamilies, "Roboto-Thin.ttf", reporter);
tomhudson8aed3c12014-08-07 10:20:51 -0700107 } else {
108 resourcesMissing = true;
109 }
110
111 if (resourcesMissing) {
112 SkDebugf("---- Resource files missing for FontConfigParser test\n");
113 }
tomhudsonf79673b2014-08-05 06:36:11 -0700114}
115