1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "TestShell.h"
33
34 #include "SkTypeface.h"
35 #include "WebView.h"
36 #include "webkit/support/webkit_support.h"
37
38 #if !OS(ANDROID)
39 #include <fontconfig/fontconfig.h>
40 #endif
41
42 #if USE(GTK)
43 #include <gtk/gtk.h>
44 #endif
45 #include <signal.h>
46 #include <unistd.h>
47
48 static void AlarmHandler(int)
49 {
50 // If the alarm alarmed, kill the process since we have a really bad hang.
51 puts("\n#TEST_TIMED_OUT\n");
52 puts("#EOF\n");
53 fflush(stdout);
54 exit(0);
55 }
56
57 void TestShell::waitTestFinished()
58 {
59 ASSERT(!m_testIsPending);
60 m_testIsPending = true;
61
62 // Install an alarm signal handler that will kill us if we time out.
63 struct sigaction alarmAction;
64 alarmAction.sa_handler = AlarmHandler;
65 sigemptyset(&alarmAction.sa_mask);
66 alarmAction.sa_flags = 0;
67
68 struct sigaction oldAction;
69 sigaction(SIGALRM, &alarmAction, &oldAction);
70 alarm(layoutTestTimeoutForWatchDog() / 1000);
71
72 // TestFinished() will post a quit message to break this loop when the page
73 // finishes loading.
74 while (m_testIsPending)
75 webkit_support::RunMessageLoop();
76
77 // Remove the alarm.
78 alarm(0);
79 sigaction(SIGALRM, &oldAction, 0);
80 }
81
82 #if !OS(ANDROID)
83 static bool checkAndLoadFontFile(FcConfig* fontcfg, const char* path1, const char* path2)
84 {
85 const char* font = path1;
86 if (access(font, R_OK) < 0) {
87 font = path2;
88 if (access(font, R_OK) < 0) {
89 fprintf(stderr, "You are missing %s or %s. Without this, some layout tests may fail. "
90 "See http://code.google.com/p/chromium/wiki/LayoutTestsLinux "
91 "for more.\n", path1, path2);
92 return false;
93 }
94 }
95 if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) font)) {
96 fprintf(stderr, "Failed to load font %s\n", font);
97 return false;
98 }
99 return true;
100 }
101
102 static void setupFontconfig()
103 {
104 // We wish to make the layout tests reproducable with respect to fonts. Skia
105 // uses fontconfig to resolve font family names from WebKit into actual font
106 // files found on the current system. This means that fonts vary based on the
107 // system and also on the fontconfig configuration.
108 //
109 // To avoid this we initialise fontconfig here and install a configuration
110 // which only knows about a few, select, fonts.
111
112 // We have fontconfig parse a config file from our resources file. This
113 // sets a number of aliases ("sans"->"Arial" etc), but doesn't include any
114 // font directories.
115 FcInit();
116
117 char drtPath[PATH_MAX + 1];
118 int drtPathSize = readlink("/proc/self/exe", drtPath, PATH_MAX);
119 if (drtPathSize < 0 || drtPathSize > PATH_MAX) {
120 fputs("Unable to resolve /proc/self/exe.", stderr);
121 exit(1);
122 }
123 drtPath[drtPathSize] = 0;
124 std::string drtDirPath(drtPath);
125 size_t lastPathPos = drtDirPath.rfind("/");
126 ASSERT(lastPathPos != std::string::npos);
127 drtDirPath.erase(lastPathPos + 1);
128
129 FcConfig* fontcfg = FcConfigCreate();
130 std::string fontconfigPath = drtDirPath + "fonts.conf";
131 if (!FcConfigParseAndLoad(fontcfg, reinterpret_cast<const FcChar8*>(fontconfigPath.c_str()), true)) {
132 fputs("Failed to parse fontconfig config file\n", stderr);
133 exit(1);
134 }
135
136 // This is the list of fonts that fontconfig will know about. It
137 // will try its best to match based only on the fonts here in. The
138 // paths are where these fonts are found on our Ubuntu boxes.
139 static const char *const fonts[] = {
140 "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
141 "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
142 "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
143 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
144 "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
145 "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
146 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
147 "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
148 "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
149 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
150 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
151 "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
152 "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
153 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
154 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
155 "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
156 "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
157 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
158 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
159 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
160 "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
161 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
162 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
163 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
164 "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
165 "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
166 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
167 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
168 "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
169 // The DejaVuSans font is used by the css2.1 tests.
170 "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
171 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf",
172 "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
173 "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
174 };
175 for (size_t i = 0; i < arraysize(fonts); ++i) {
176 if (access(fonts[i], R_OK)) {
177 fprintf(stderr, "You are missing %s. Try re-running build/install-build-deps.sh. Also see "
178 "http://code.google.com/p/chromium/wiki/LayoutTestsLinux",
179 fonts[i]);
180 exit(1);
181 }
182 if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) fonts[i])) {
183 fprintf(stderr, "Failed to load font %s\n", fonts[i]);
184 exit(1);
185 }
186 }
187
188 if (!checkAndLoadFontFile(fontcfg, "/usr/share/fonts/truetype/thai/Garuda.ttf",
189 "/usr/share/fonts/truetype/tlwg/Garuda.ttf"))
190 exit(1);
191
192 // We special case these fonts because they're only needed in a
193 // few layout tests.
194 checkAndLoadFontFile(fontcfg, "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_pa.ttf",
195 "/usr/share/fonts/truetype/ttf-punjabi-fonts/lohit_pa.ttf");
196
197 // Also load the layout-test-specific "Ahem" font.
198 std::string ahemPath = drtDirPath + "AHEM____.TTF";
199 if (!FcConfigAppFontAddFile(fontcfg, reinterpret_cast<const FcChar8*>(ahemPath.c_str()))) {
200 fprintf(stderr, "Failed to load font %s\n", ahemPath.c_str());
201 exit(1);
202 }
203
204 if (!FcConfigSetCurrent(fontcfg)) {
205 fputs("Failed to set the default font configuration\n", stderr);
206 exit(1);
207 }
208 }
209 #endif // !OS(ANDROID)
210
211 void platformInit(int* argc, char*** argv)
212 {
213 // FIXME: It's better call gtk_init() only when we run plugin tests.
214 // See http://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/633ea167cde196ca#
215 #if USE(GTK)
216 gtk_init(argc, argv);
217 #endif
218
219 #if !OS(ANDROID)
220 setupFontconfig();
221 #endif
222 }
223