tizen beta release
[profile/ivi/webkit-efl.git] / Tools / DumpRenderTree / chromium / TestShellLinux.cpp
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 void setupFontconfig()
84 {
85     // We wish to make the layout tests reproducable with respect to fonts. Skia
86     // uses fontconfig to resolve font family names from WebKit into actual font
87     // files found on the current system. This means that fonts vary based on the
88     // system and also on the fontconfig configuration.
89     //
90     // To avoid this we initialise fontconfig here and install a configuration
91     // which only knows about a few, select, fonts.
92
93     // We have fontconfig parse a config file from our resources file. This
94     // sets a number of aliases ("sans"->"Arial" etc), but doesn't include any
95     // font directories.
96     FcInit();
97
98     char drtPath[PATH_MAX + 1];
99     int drtPathSize = readlink("/proc/self/exe", drtPath, PATH_MAX);
100     if (drtPathSize < 0 || drtPathSize > PATH_MAX) {
101         fputs("Unable to resolve /proc/self/exe.", stderr);
102         exit(1);
103     }
104     drtPath[drtPathSize] = 0;
105     std::string drtDirPath(drtPath);
106     size_t lastPathPos = drtDirPath.rfind("/");
107     ASSERT(lastPathPos != std::string::npos);
108     drtDirPath.erase(lastPathPos + 1);
109
110     FcConfig* fontcfg = FcConfigCreate();
111     std::string fontconfigPath = drtDirPath + "fonts.conf";
112     if (!FcConfigParseAndLoad(fontcfg, reinterpret_cast<const FcChar8*>(fontconfigPath.c_str()), true)) {
113         fputs("Failed to parse fontconfig config file\n", stderr);
114         exit(1);
115     }
116
117     // This is the list of fonts that fontconfig will know about. It
118     // will try its best to match based only on the fonts here in. The
119     // paths are where these fonts are found on our Ubuntu boxes.
120     static const char *const fonts[] = {
121         "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
122         "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
123         "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
124         "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
125         "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
126         "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
127         "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
128         "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
129         "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
130         "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
131         "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
132         "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
133         "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
134         "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
135         "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
136         "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
137         "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
138         "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
139         "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
140         "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
141         "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
142         "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
143         "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
144         "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
145         "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
146         "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
147         "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
148         "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
149         "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
150         "/usr/share/fonts/truetype/thai/Garuda.ttf",
151         // The DejaVuSans font is used by the css2.1 tests.
152         "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
153         "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_hi.ttf",
154         "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
155         "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
156     };
157     for (size_t i = 0; i < arraysize(fonts); ++i) {
158         if (access(fonts[i], R_OK)) {
159             fprintf(stderr, "You are missing %s. Try re-running build/install-build-deps.sh. Also see "
160                             "http://code.google.com/p/chromium/wiki/LayoutTestsLinux",
161                             fonts[i]);
162             exit(1);
163         }
164         if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) fonts[i])) {
165             fprintf(stderr, "Failed to load font %s\n", fonts[i]);
166             exit(1);
167         }
168     }
169
170     // We special case these fonts because they're only needed in a
171     // few layout tests.
172     static const char* const optionalFonts[] = {
173         "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_pa.ttf",
174     };
175     for (size_t i = 0; i < arraysize(optionalFonts); ++i) {
176         const char* font = optionalFonts[i];
177
178         // This font changed paths across Ubuntu releases, so try checking in both locations.
179         if (!strcmp(font, "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_pa.ttf")
180             && access(font, R_OK) < 0)
181             font = "/usr/share/fonts/truetype/ttf-punjabi-fonts/lohit_pa.ttf";
182
183         if (access(font, R_OK) < 0) {
184             fprintf(stderr, "You are missing %s. Without this, some layout tests may fail. "
185                             "See http://code.google.com/p/chromium/wiki/LayoutTestsLinux "
186                             "for more.\n", font);
187         } else if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) font)) {
188             fprintf(stderr, "Failed to load font %s\n", font);
189             exit(1);
190         }
191     }
192
193     // Also load the layout-test-specific "Ahem" font.
194     std::string ahemPath = drtDirPath + "AHEM____.TTF";
195     if (!FcConfigAppFontAddFile(fontcfg, reinterpret_cast<const FcChar8*>(ahemPath.c_str()))) {
196         fprintf(stderr, "Failed to load font %s\n", ahemPath.c_str());
197         exit(1);
198     }
199
200     if (!FcConfigSetCurrent(fontcfg)) {
201         fputs("Failed to set the default font configuration\n", stderr);
202         exit(1);
203     }
204 }
205 #endif // !OS(ANDROID)
206
207 void platformInit(int* argc, char*** argv)
208 {
209     // FIXME: It's better call gtk_init() only when we run plugin tests.
210     // See http://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/633ea167cde196ca#
211 #if USE(GTK)
212     gtk_init(argc, argv);
213 #endif
214
215 #if !OS(ANDROID)
216     setupFontconfig();
217 #endif
218 }
219