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 "WebThemeEngineDRT.h" |
|
35 #include "webkit/support/webkit_support.h" |
|
36 #include <fcntl.h> |
|
37 #include <io.h> |
|
38 #include <process.h> |
|
39 #include <shlwapi.h> |
|
40 #include <sys/stat.h> |
|
41 |
|
42 // Theme engine |
|
43 static WebThemeEngineDRT themeEngine; |
|
44 |
|
45 // Thread main to run for the thread which just tests for timeout. |
|
46 unsigned int __stdcall watchDogThread(void* arg) |
|
47 { |
|
48 // If we're debugging a layout test, don't timeout. |
|
49 if (::IsDebuggerPresent()) |
|
50 return 0; |
|
51 |
|
52 TestShell* shell = static_cast<TestShell*>(arg); |
|
53 // FIXME: Do we need user-specified time settings as with the original |
|
54 // Chromium implementation? |
|
55 DWORD timeout = static_cast<DWORD>(shell->layoutTestTimeoutForWatchDog()); |
|
56 DWORD rv = WaitForSingleObject(shell->finishedEvent(), timeout); |
|
57 if (rv == WAIT_TIMEOUT) { |
|
58 // Print a warning to be caught by the layout-test script. |
|
59 // Note: the layout test driver may or may not recognize |
|
60 // this as a timeout. |
|
61 puts("\n#TEST_TIMED_OUT\n"); |
|
62 puts("#EOF\n"); |
|
63 fflush(stdout); |
|
64 TerminateProcess(GetCurrentProcess(), 0); |
|
65 } |
|
66 // Finished normally. |
|
67 return 0; |
|
68 } |
|
69 |
|
70 void TestShell::waitTestFinished() |
|
71 { |
|
72 DCHECK(!m_testIsPending) << "cannot be used recursively"; |
|
73 |
|
74 m_testIsPending = true; |
|
75 |
|
76 // Create a watchdog thread which just sets a timer and |
|
77 // kills the process if it times out. This catches really |
|
78 // bad hangs where the shell isn't coming back to the |
|
79 // message loop. If the watchdog is what catches a |
|
80 // timeout, it can't do anything except terminate the test |
|
81 // shell, which is unfortunate. |
|
82 m_finishedEvent = CreateEvent(0, TRUE, FALSE, 0); |
|
83 DCHECK(m_finishedEvent); |
|
84 |
|
85 HANDLE threadHandle = reinterpret_cast<HANDLE>(_beginthreadex( |
|
86 0, |
|
87 0, |
|
88 &watchDogThread, |
|
89 this, |
|
90 0, |
|
91 0)); |
|
92 DCHECK(threadHandle); |
|
93 |
|
94 // TestFinished() will post a quit message to break this loop when the page |
|
95 // finishes loading. |
|
96 while (m_testIsPending) |
|
97 webkit_support::RunMessageLoop(); |
|
98 |
|
99 // Tell the watchdog that we are finished. |
|
100 SetEvent(m_finishedEvent); |
|
101 |
|
102 // Wait to join the watchdog thread. (up to 1s, then quit) |
|
103 WaitForSingleObject(threadHandle, 1000); |
|
104 } |
|
105 |
|
106 void platformInit() |
|
107 { |
|
108 // Set stdout/stderr binary mode. |
|
109 _setmode(_fileno(stdout), _O_BINARY); |
|
110 _setmode(_fileno(stderr), _O_BINARY); |
|
111 |
|
112 // Set theme engine. |
|
113 webkit_support::SetThemeEngine(&themeEngine); |
|
114 |
|
115 // Load Ahem font. |
|
116 // AHEM____.TTF is copied to the directory of DumpRenderTree.exe by WebKit.gyp. |
|
117 WCHAR path[_MAX_PATH]; |
|
118 if (!::GetModuleFileName(0, path, _MAX_PATH)) { |
|
119 fprintf(stderr, "Can't get the module path.\n"); |
|
120 exit(1); |
|
121 } |
|
122 ::PathRemoveFileSpec(path); |
|
123 wcscat_s(path, _MAX_PATH, L"/AHEM____.TTF"); |
|
124 struct _stat ahemStat; |
|
125 if (_wstat(path, &ahemStat) == -1) { |
|
126 fprintf(stderr, "Can't access: '%S'\n", path); |
|
127 exit(1); |
|
128 } |
|
129 |
|
130 FILE* fp = _wfopen(path, L"rb"); |
|
131 if (!fp) { |
|
132 _wperror(path); |
|
133 exit(1); |
|
134 } |
|
135 size_t size = ahemStat.st_size; |
|
136 char* fontBuffer = new char[size]; |
|
137 if (fread(fontBuffer, 1, size, fp) != size) { |
|
138 fprintf(stderr, "Can't read the font: '%S'\n", path); |
|
139 fclose(fp); |
|
140 exit(1); |
|
141 } |
|
142 fclose(fp); |
|
143 DWORD numFonts = 1; |
|
144 HANDLE fontHandle = ::AddFontMemResourceEx(fontBuffer, size, 0, &numFonts); |
|
145 delete[] fontBuffer; // OS owns a copy of the buffer. |
|
146 if (!fontHandle) { |
|
147 fprintf(stderr, "Failed to register Ahem font: '%S'\n", path); |
|
148 exit(1); |
|
149 } |
|
150 // We don't need to release the font explicitly. |
|
151 } |
|