summaryrefslogtreecommitdiff
path: root/Tools/DumpRenderTree/wx/DumpRenderTreeWx.cpp
blob: 31add11a2eb74d1f4d8329479f419f107ffba88f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2008 Kevin Ollivier <kevino@theolliviers.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "DumpRenderTree.h"

#include "LayoutTestController.h"
#include "WorkQueue.h"
#include "WorkQueueItem.h"

#include <JavaScriptCore/JavaScript.h>

#include <wx/wx.h>
#include "WebView.h"
#include "WebFrame.h"
#include "WebBrowserShell.h"

#include <wtf/Assertions.h>

#include <cassert>
#include <stdlib.h>
#include <string.h>
#include <time.h>

volatile bool done = true;
volatile bool notified = false;
static bool printSeparators = true;
static int dumpPixels;
static int dumpTree = 1;
time_t startTime; // to detect timeouts / failed tests

using namespace std;
using namespace WebKit;

FILE* logOutput;

RefPtr<LayoutTestController> gLayoutTestController;
static WebView* webView;
static wxTimer* idleTimer;

const unsigned timeOut = 10;
const unsigned maxViewHeight = 600;
const unsigned maxViewWidth = 800;

class LayoutWebViewEventHandler : public wxEvtHandler {

public:
    LayoutWebViewEventHandler(WebView* webView)
        : m_webView(webView)
    {
    }
    
    void bindEvents() 
    {
        m_webView->Connect(wxEVT_WEBVIEW_LOAD, WebViewLoadEventHandler(LayoutWebViewEventHandler::OnLoadEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_JS_ALERT, WebViewAlertEventHandler(LayoutWebViewEventHandler::OnAlertEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_JS_CONFIRM, WebViewConfirmEventHandler(LayoutWebViewEventHandler::OnConfirmEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_JS_PROMPT, WebViewPromptEventHandler(LayoutWebViewEventHandler::OnPromptEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_CONSOLE_MESSAGE, WebViewConsoleMessageEventHandler(LayoutWebViewEventHandler::OnConsoleMessageEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_RECEIVED_TITLE, WebViewReceivedTitleEventHandler(LayoutWebViewEventHandler::OnReceivedTitleEvent), 0, this);
        m_webView->Connect(wxEVT_WEBVIEW_WINDOW_OBJECT_CLEARED, WebViewWindowObjectClearedEventHandler(LayoutWebViewEventHandler::OnWindowObjectClearedEvent), 0, this);
    }
    
    void OnLoadEvent(WebViewLoadEvent& event) 
    {

        if (event.GetState() == WEBVIEW_LOAD_FAILED || event.GetState() == WEBVIEW_LOAD_STOPPED)
            done = true; 
        
        if (event.GetState() == WEBVIEW_LOAD_ONLOAD_HANDLED) {
            done = true;
            
            if (!gLayoutTestController->waitToDump() || notified) {
                dump();
            }
        }
    }
    
    void OnAlertEvent(WebViewAlertEvent& event)
    {
        wxFprintf(stdout, "ALERT: %S\n", event.GetMessage());
    }
    
    void OnConfirmEvent(WebViewConfirmEvent& event)
    {
        wxFprintf(stdout, "CONFIRM: %S\n", event.GetMessage());
        event.SetReturnCode(1);
    }

    void OnPromptEvent(WebViewPromptEvent& event)
    {
        wxFprintf(stdout, "PROMPT: %S, default text: %S\n", event.GetMessage(), event.GetResponse());
        event.SetReturnCode(1);
    }
    
    void OnConsoleMessageEvent(WebViewConsoleMessageEvent& event)
    {
        fprintf(stdout, "CONSOLE MESSAGE: ");
        if (event.GetLineNumber())
            fprintf(stdout, "line %d: ", event.GetLineNumber());
        wxFprintf(stdout, "%S\n", event.GetMessage());
    }
    
    void OnReceivedTitleEvent(WebViewReceivedTitleEvent& event)
    {
        if (gLayoutTestController->dumpTitleChanges() && !done)
            wxFprintf(stdout, "TITLE CHANGED: %S\n", event.GetTitle());
    }
    
    void OnWindowObjectClearedEvent(WebViewWindowObjectClearedEvent& event)
    {
        JSValueRef exception = 0;
        gLayoutTestController->makeWindowObject(event.GetJSContext(), event.GetWindowObject(), &exception);
    }
    
private:
    WebView* m_webView;

};

void notifyDoneFired() 
{
    notified = true;
    if (done)
        dump();
}

LayoutWebViewEventHandler* eventHandler = 0;

static wxString dumpFramesAsText(WebFrame* frame)
{
    // TODO: implement this. leaving this here so we don't forget this case.
    if (gLayoutTestController->dumpChildFramesAsText()) {
    }
    
    return frame->GetInnerText();
}

void dump()
{    
    if (!done)
        return;
    
    if (gLayoutTestController->waitToDump() && !notified)
        return;
        
    if (dumpTree) {
        const char* result = 0;

        bool dumpAsText = gLayoutTestController->dumpAsText();
        wxString str;
        if (gLayoutTestController->dumpAsText())
            str = dumpFramesAsText(webView->GetMainFrame());
        else 
            str = webView->GetMainFrame()->GetExternalRepresentation();

        result = str.ToUTF8();
        if (!result) {
            const char* errorMessage;
            if (gLayoutTestController->dumpAsText())
                errorMessage = "WebFrame::GetInnerText";
            else
                errorMessage = "WebFrame::GetExternalRepresentation";
            printf("ERROR: 0 result from %s", errorMessage);
        } else {
            printf("%s\n", result);
        }

        if (gLayoutTestController->dumpBackForwardList()) {
            // FIXME: not implemented
        }

        if (printSeparators) {
            puts("#EOF");
            fputs("#EOF\n", stderr);
            fflush(stdout);
            fflush(stderr);
        }
    }

    if (dumpPixels
        && gLayoutTestController->generatePixelResults()
        && !gLayoutTestController->dumpDOMAsWebArchive()
        && !gLayoutTestController->dumpSourceAsWebArchive()) {
        // FIXME: Add support for dumping pixels
        fflush(stdout);
    }

    puts("#EOF");
    fflush(stdout);
    fflush(stderr);

    gLayoutTestController.clear();
}

static void runTest(const wxString testPathOrURL)
{
    done = false;
    time(&startTime);
    string pathOrURLString(testPathOrURL.char_str());
    string pathOrURL(pathOrURLString);
    string expectedPixelHash;

    size_t separatorPos = pathOrURL.find("'");
    if (separatorPos != string::npos) {
        pathOrURL = string(pathOrURLString, 0, separatorPos);
        expectedPixelHash = string(pathOrURLString, separatorPos + 1);
    }
    
    // CURL isn't happy if we don't have a protocol.
    size_t http = pathOrURL.find("http://");
    if (http == string::npos)
        pathOrURL.insert(0, "file://");
    
    gLayoutTestController = LayoutTestController::create(pathOrURL, expectedPixelHash);
    if (!gLayoutTestController) {
        wxTheApp->ExitMainLoop();
    }

    WorkQueue::shared()->clear();
    WorkQueue::shared()->setFrozen(false);

    webView->LoadURL(wxString(pathOrURL.c_str(), wxConvUTF8));
    
    // wait until load completes and the results are dumped
    while (!done)
        wxSafeYield();
}

class MyApp : public wxApp
{
public:

    virtual bool OnInit();
    
private:
    wxLog* logger;
};


IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    logOutput = fopen("output.txt", "ab");
    if (logOutput) {
        logger = new wxLogStderr(logOutput);
        wxLog::SetActiveTarget(logger);
    }

    wxLogMessage(wxT("Starting DumpRenderTool, %d args.\n"), argc);

    for (int i = 1; i < argc; ++i) {
        wxString option = wxString(argv[i]);
        if (!option.CmpNoCase(_T("--notree"))) {
            dumpTree = false;
            continue;
        }
        
        if (!option.CmpNoCase(_T("--pixel-tests"))) {
            dumpPixels = true;
            continue;
        }
        
        if (!option.CmpNoCase(_T("--tree"))) {
            dumpTree = true;
            continue;
        }
    }
    wxInitAllImageHandlers();
        
    // create the main application window
    WebBrowserShell* webFrame = new WebBrowserShell(_T("wxWebKit DumpRenderTree App"), "about:blank");
    SetTopWindow(webFrame);
    webView = webFrame->webview;
    webView->SetSize(wxSize(maxViewWidth, maxViewHeight));
    
    if (!eventHandler) {
        eventHandler = new LayoutWebViewEventHandler(webView);
        eventHandler->bindEvents();
    }

    int optind = 1;
    time(&startTime);
    wxString option_str = wxString(argv[optind]);
    if (argc == optind+1 && option_str.Find(_T("-")) == 0) {
        char filenameBuffer[2048];
        while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
            wxString filename = wxString::FromUTF8(filenameBuffer);
            char* newLineCharacter = strchr(filenameBuffer, '\n');
            if (newLineCharacter)
                *newLineCharacter = '\0';

            if (strlen(filenameBuffer) == 0)
                return 0;
            wxLogMessage(wxT("Running test %S.\n"), filenameBuffer);
            runTest(filename);
        }
    
    } else {
        printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
        for (int i = optind; i != argc; ++i) {
            runTest(wxTheApp->argv[1]);
        }
    }
    
    webFrame->Close();
    delete eventHandler;

    wxLog::SetActiveTarget(0);
    delete logger;
    fclose(logOutput);
    
    // returning false shuts the app down
    return false;
}