1/*
2 * Copyright (C) 2010 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 "webkit/support/webkit_support.h"
35#include <fontconfig/fontconfig.h>
36#include <gtk/gtk.h>
37#include <signal.h>
38
39static void AlarmHandler(int signatl)
40{
41    // If the alarm alarmed, kill the process since we have a really bad hang.
42    puts("\n#TEST_TIMED_OUT\n");
43    puts("#EOF\n");
44    fflush(stdout);
45    exit(0);
46}
47
48static void setupFontconfig()
49{
50    // We wish to make the layout tests reproducable with respect to fonts. Skia
51    // uses fontconfig to resolve font family names from WebKit into actual font
52    // files found on the current system. This means that fonts vary based on the
53    // system and also on the fontconfig configuration.
54    //
55    // To avoid this we initialise fontconfig here and install a configuration
56    // which only knows about a few, select, fonts.
57
58    // We have fontconfig parse a config file from our resources file. This
59    // sets a number of aliases ("sans"->"Arial" etc), but doesn't include any
60    // font directories.
61    FcInit();
62
63    char drtPath[PATH_MAX + 1];
64    int drtPathSize = readlink("/proc/self/exe", drtPath, PATH_MAX);
65    if (drtPathSize < 0 || drtPathSize > PATH_MAX) {
66        fputs("Unable to resolve /proc/self/exe.", stderr);
67        exit(1);
68    }
69    drtPath[drtPathSize] = 0;
70    std::string drtDirPath(drtPath);
71    size_t lastPathPos = drtDirPath.rfind("/");
72    ASSERT(lastPathPos != std::string::npos);
73    drtDirPath.erase(lastPathPos + 1);
74
75    FcConfig* fontcfg = FcConfigCreate();
76    std::string fontconfigPath = drtDirPath + "fonts.conf";
77    if (!FcConfigParseAndLoad(fontcfg, reinterpret_cast<const FcChar8*>(fontconfigPath.c_str()), true)) {
78        fputs("Failed to parse fontconfig config file\n", stderr);
79        exit(1);
80    }
81
82    // This is the list of fonts that fontconfig will know about. It
83    // will try its best to match based only on the fonts here in. The
84    // paths are where these fonts are found on our Ubuntu boxes.
85    static const char *const fonts[] = {
86        "/usr/share/fonts/truetype/kochi/kochi-gothic.ttf",
87        "/usr/share/fonts/truetype/kochi/kochi-mincho.ttf",
88        "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
89        "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf",
90        "/usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf",
91        "/usr/share/fonts/truetype/msttcorefonts/Arial_Italic.ttf",
92        "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf",
93        "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
94        "/usr/share/fonts/truetype/msttcorefonts/Courier_New.ttf",
95        "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold.ttf",
96        "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Bold_Italic.ttf",
97        "/usr/share/fonts/truetype/msttcorefonts/Courier_New_Italic.ttf",
98        "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf",
99        "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold.ttf",
100        "/usr/share/fonts/truetype/msttcorefonts/Georgia_Bold_Italic.ttf",
101        "/usr/share/fonts/truetype/msttcorefonts/Georgia_Italic.ttf",
102        "/usr/share/fonts/truetype/msttcorefonts/Impact.ttf",
103        "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS.ttf",
104        "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold.ttf",
105        "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Bold_Italic.ttf",
106        "/usr/share/fonts/truetype/msttcorefonts/Trebuchet_MS_Italic.ttf",
107        "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf",
108        "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold.ttf",
109        "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Bold_Italic.ttf",
110        "/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman_Italic.ttf",
111        "/usr/share/fonts/truetype/msttcorefonts/Verdana.ttf",
112        "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf",
113        "/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold_Italic.ttf",
114        "/usr/share/fonts/truetype/msttcorefonts/Verdana_Italic.ttf",
115        // The DejaVuSans font is used by the css2.1 tests.
116        "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf",
117        "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_ta.ttf",
118        "/usr/share/fonts/truetype/ttf-indic-fonts-core/MuktiNarrow.ttf",
119    };
120    for (size_t i = 0; i < arraysize(fonts); ++i) {
121        if (access(fonts[i], R_OK)) {
122            fprintf(stderr, "You are missing %s. Try installing msttcorefonts. Also see "
123                            "http://code.google.com/p/chromium/wiki/LinuxBuildInstructions",
124                            fonts[i]);
125            exit(1);
126        }
127        if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) fonts[i])) {
128            fprintf(stderr, "Failed to load font %s\n", fonts[i]);
129            exit(1);
130        }
131    }
132
133    // We special case these fonts because they're only needed in a
134    // few layout tests.
135    static const char* const optionalFonts[] = {
136        "/usr/share/fonts/truetype/ttf-lucida/LucidaSansRegular.ttf",
137        "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_pa.ttf",
138    };
139    for (size_t i = 0; i < arraysize(optionalFonts); ++i) {
140        const char* font = optionalFonts[i];
141
142        // This font changed paths across Ubuntu releases, so try checking in both locations.
143        if (!strcmp(font, "/usr/share/fonts/truetype/ttf-indic-fonts-core/lohit_pa.ttf")
144            && access(font, R_OK) < 0)
145            font = "/usr/share/fonts/truetype/ttf-punjabi-fonts/lohit_pa.ttf";
146
147        if (access(font, R_OK) < 0) {
148            fprintf(stderr, "You are missing %s. Without this, some layout tests may fail. "
149                            "See http://code.google.com/p/chromium/wiki/LinuxBuildInstructionsPrerequisites "
150                            "for more.\n", font);
151        } else if (!FcConfigAppFontAddFile(fontcfg, (FcChar8 *) font)) {
152            fprintf(stderr, "Failed to load font %s\n", font);
153            exit(1);
154        }
155    }
156
157    // Also load the layout-test-specific "Ahem" font.
158    std::string ahemPath = drtDirPath + "AHEM____.TTF";
159    if (!FcConfigAppFontAddFile(fontcfg, reinterpret_cast<const FcChar8*>(ahemPath.c_str()))) {
160        fprintf(stderr, "Failed to load font %s\n", ahemPath.c_str());
161        exit(1);
162    }
163
164    if (!FcConfigSetCurrent(fontcfg)) {
165        fputs("Failed to set the default font configuration\n", stderr);
166        exit(1);
167    }
168}
169
170void TestShell::waitTestFinished()
171{
172    ASSERT(!m_testIsPending);
173
174    m_testIsPending = true;
175
176    // Install an alarm signal handler that will kill us if we time out.
177    signal(SIGALRM, AlarmHandler);
178    alarm(layoutTestTimeoutForWatchDog() / 1000);
179
180    // TestFinished() will post a quit message to break this loop when the page
181    // finishes loading.
182    while (m_testIsPending)
183        webkit_support::RunMessageLoop();
184
185    // Remove the alarm.
186    alarm(0);
187    signal(SIGALRM, SIG_DFL);
188}
189
190void platformInit(int* argc, char*** argv)
191{
192    // FIXME: It's better call gtk_init() only when we run plugin tests.
193    // See http://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/633ea167cde196ca#
194    gtk_init(argc, argv);
195
196    setupFontconfig();
197}
198
199void openStartupDialog()
200{
201    GtkWidget* dialog = gtk_message_dialog_new(
202        0, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Attach to me?");
203    gtk_window_set_title(GTK_WINDOW(dialog), "DumpRenderTree");
204    gtk_dialog_run(GTK_DIALOG(dialog)); // Runs a nested message loop.
205    gtk_widget_destroy(dialog);
206}
207
208bool checkLayoutTestSystemDependencies()
209{
210    return true;
211}
212