Disabled touch adjust feature in case of SDK version.
[framework/web/webkit-efl.git] / Tools / DumpRenderTree / wx / DumpRenderTreeWx.cpp
1 /*
2  * Copyright (C) 2008 Kevin Ollivier <kevino@theolliviers.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "DumpRenderTree.h"
31
32 #include "LayoutTestController.h"
33 #include "WorkQueue.h"
34 #include "WorkQueueItem.h"
35
36 #include <JavaScriptCore/JavaScript.h>
37
38 #include <wx/wx.h>
39 #include "WebView.h"
40 #include "WebFrame.h"
41 #include "WebBrowserShell.h"
42
43 #include <wtf/Assertions.h>
44
45 #include <cassert>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49
50 volatile bool done = true;
51 volatile bool notified = false;
52 static bool printSeparators = true;
53 static int dumpPixels;
54 static int dumpTree = 1;
55 time_t startTime; // to detect timeouts / failed tests
56
57 using namespace std;
58 using namespace WebKit;
59
60 FILE* logOutput;
61
62 RefPtr<LayoutTestController> gLayoutTestController;
63 static WebView* webView;
64 static wxTimer* idleTimer;
65
66 const unsigned timeOut = 10;
67 const unsigned maxViewHeight = 600;
68 const unsigned maxViewWidth = 800;
69
70 class LayoutWebViewEventHandler : public wxEvtHandler {
71
72 public:
73     LayoutWebViewEventHandler(WebView* webView)
74         : m_webView(webView)
75     {
76     }
77     
78     void bindEvents() 
79     {
80         m_webView->Connect(wxEVT_WEBVIEW_LOAD, WebViewLoadEventHandler(LayoutWebViewEventHandler::OnLoadEvent), 0, this);
81         m_webView->Connect(wxEVT_WEBVIEW_JS_ALERT, WebViewAlertEventHandler(LayoutWebViewEventHandler::OnAlertEvent), 0, this);
82         m_webView->Connect(wxEVT_WEBVIEW_JS_CONFIRM, WebViewConfirmEventHandler(LayoutWebViewEventHandler::OnConfirmEvent), 0, this);
83         m_webView->Connect(wxEVT_WEBVIEW_JS_PROMPT, WebViewPromptEventHandler(LayoutWebViewEventHandler::OnPromptEvent), 0, this);
84         m_webView->Connect(wxEVT_WEBVIEW_CONSOLE_MESSAGE, WebViewConsoleMessageEventHandler(LayoutWebViewEventHandler::OnConsoleMessageEvent), 0, this);
85         m_webView->Connect(wxEVT_WEBVIEW_RECEIVED_TITLE, WebViewReceivedTitleEventHandler(LayoutWebViewEventHandler::OnReceivedTitleEvent), 0, this);
86         m_webView->Connect(wxEVT_WEBVIEW_WINDOW_OBJECT_CLEARED, WebViewWindowObjectClearedEventHandler(LayoutWebViewEventHandler::OnWindowObjectClearedEvent), 0, this);
87     }
88     
89     void OnLoadEvent(WebViewLoadEvent& event) 
90     {
91
92         if (event.GetState() == WEBVIEW_LOAD_FAILED || event.GetState() == WEBVIEW_LOAD_STOPPED)
93             done = true; 
94         
95         if (event.GetState() == WEBVIEW_LOAD_ONLOAD_HANDLED) {
96             done = true;
97             
98             if (!gLayoutTestController->waitToDump() || notified) {
99                 dump();
100             }
101         }
102     }
103     
104     void OnAlertEvent(WebViewAlertEvent& event)
105     {
106         wxFprintf(stdout, "ALERT: %S\n", event.GetMessage());
107     }
108     
109     void OnConfirmEvent(WebViewConfirmEvent& event)
110     {
111         wxFprintf(stdout, "CONFIRM: %S\n", event.GetMessage());
112         event.SetReturnCode(1);
113     }
114
115     void OnPromptEvent(WebViewPromptEvent& event)
116     {
117         wxFprintf(stdout, "PROMPT: %S, default text: %S\n", event.GetMessage(), event.GetResponse());
118         event.SetReturnCode(1);
119     }
120     
121     void OnConsoleMessageEvent(WebViewConsoleMessageEvent& event)
122     {
123         fprintf(stdout, "CONSOLE MESSAGE: ");
124         if (event.GetLineNumber())
125             fprintf(stdout, "line %d: ", event.GetLineNumber());
126         wxFprintf(stdout, "%S\n", event.GetMessage());
127     }
128     
129     void OnReceivedTitleEvent(WebViewReceivedTitleEvent& event)
130     {
131         if (gLayoutTestController->dumpTitleChanges() && !done)
132             wxFprintf(stdout, "TITLE CHANGED: %S\n", event.GetTitle());
133     }
134     
135     void OnWindowObjectClearedEvent(WebViewWindowObjectClearedEvent& event)
136     {
137         JSValueRef exception = 0;
138         gLayoutTestController->makeWindowObject(event.GetJSContext(), event.GetWindowObject(), &exception);
139     }
140     
141 private:
142     WebView* m_webView;
143
144 };
145
146 void notifyDoneFired() 
147 {
148     notified = true;
149     if (done)
150         dump();
151 }
152
153 LayoutWebViewEventHandler* eventHandler = 0;
154
155 static wxString dumpFramesAsText(WebFrame* frame)
156 {
157     // TODO: implement this. leaving this here so we don't forget this case.
158     if (gLayoutTestController->dumpChildFramesAsText()) {
159     }
160     
161     return frame->GetInnerText();
162 }
163
164 void dump()
165 {    
166     if (!done)
167         return;
168     
169     if (gLayoutTestController->waitToDump() && !notified)
170         return;
171         
172     if (dumpTree) {
173         const char* result = 0;
174
175         bool dumpAsText = gLayoutTestController->dumpAsText();
176         wxString str;
177         if (gLayoutTestController->dumpAsText())
178             str = dumpFramesAsText(webView->GetMainFrame());
179         else 
180             str = webView->GetMainFrame()->GetExternalRepresentation();
181
182         result = str.ToUTF8();
183         if (!result) {
184             const char* errorMessage;
185             if (gLayoutTestController->dumpAsText())
186                 errorMessage = "WebFrame::GetInnerText";
187             else
188                 errorMessage = "WebFrame::GetExternalRepresentation";
189             printf("ERROR: 0 result from %s", errorMessage);
190         } else {
191             printf("%s\n", result);
192         }
193
194         if (gLayoutTestController->dumpBackForwardList()) {
195             // FIXME: not implemented
196         }
197
198         if (printSeparators) {
199             puts("#EOF");
200             fputs("#EOF\n", stderr);
201             fflush(stdout);
202             fflush(stderr);
203         }
204     }
205
206     if (dumpPixels
207         && gLayoutTestController->generatePixelResults()
208         && !gLayoutTestController->dumpDOMAsWebArchive()
209         && !gLayoutTestController->dumpSourceAsWebArchive()) {
210         // FIXME: Add support for dumping pixels
211         fflush(stdout);
212     }
213
214     puts("#EOF");
215     fflush(stdout);
216     fflush(stderr);
217
218     gLayoutTestController.clear();
219 }
220
221 static void runTest(const wxString testPathOrURL)
222 {
223     done = false;
224     time(&startTime);
225     string pathOrURLString(testPathOrURL.char_str());
226     string pathOrURL(pathOrURLString);
227     string expectedPixelHash;
228
229     size_t separatorPos = pathOrURL.find("'");
230     if (separatorPos != string::npos) {
231         pathOrURL = string(pathOrURLString, 0, separatorPos);
232         expectedPixelHash = string(pathOrURLString, separatorPos + 1);
233     }
234     
235     // CURL isn't happy if we don't have a protocol.
236     size_t http = pathOrURL.find("http://");
237     if (http == string::npos)
238         pathOrURL.insert(0, "file://");
239     
240     gLayoutTestController = LayoutTestController::create(pathOrURL, expectedPixelHash);
241     if (!gLayoutTestController) {
242         wxTheApp->ExitMainLoop();
243     }
244
245     WorkQueue::shared()->clear();
246     WorkQueue::shared()->setFrozen(false);
247
248     webView->LoadURL(wxString(pathOrURL.c_str(), wxConvUTF8));
249     
250     // wait until load completes and the results are dumped
251     while (!done)
252         wxSafeYield();
253 }
254
255 class MyApp : public wxApp
256 {
257 public:
258
259     virtual bool OnInit();
260     
261 private:
262     wxLog* logger;
263 };
264
265
266 IMPLEMENT_APP(MyApp)
267
268 bool MyApp::OnInit()
269 {
270     logOutput = fopen("output.txt", "ab");
271     if (logOutput) {
272         logger = new wxLogStderr(logOutput);
273         wxLog::SetActiveTarget(logger);
274     }
275
276     wxLogMessage(wxT("Starting DumpRenderTool, %d args.\n"), argc);
277
278     for (int i = 1; i < argc; ++i) {
279         wxString option = wxString(argv[i]);
280         if (!option.CmpNoCase(_T("--notree"))) {
281             dumpTree = false;
282             continue;
283         }
284         
285         if (!option.CmpNoCase(_T("--pixel-tests"))) {
286             dumpPixels = true;
287             continue;
288         }
289         
290         if (!option.CmpNoCase(_T("--tree"))) {
291             dumpTree = true;
292             continue;
293         }
294     }
295     wxInitAllImageHandlers();
296         
297     // create the main application window
298     WebBrowserShell* webFrame = new WebBrowserShell(_T("wxWebKit DumpRenderTree App"), "about:blank");
299     SetTopWindow(webFrame);
300     webView = webFrame->webview;
301     webView->SetSize(wxSize(maxViewWidth, maxViewHeight));
302     
303     if (!eventHandler) {
304         eventHandler = new LayoutWebViewEventHandler(webView);
305         eventHandler->bindEvents();
306     }
307
308     int optind = 1;
309     time(&startTime);
310     wxString option_str = wxString(argv[optind]);
311     if (argc == optind+1 && option_str.Find(_T("-")) == 0) {
312         char filenameBuffer[2048];
313         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
314             wxString filename = wxString::FromUTF8(filenameBuffer);
315             char* newLineCharacter = strchr(filenameBuffer, '\n');
316             if (newLineCharacter)
317                 *newLineCharacter = '\0';
318
319             if (strlen(filenameBuffer) == 0)
320                 return 0;
321             wxLogMessage(wxT("Running test %S.\n"), filenameBuffer);
322             runTest(filename);
323         }
324     
325     } else {
326         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
327         for (int i = optind; i != argc; ++i) {
328             runTest(wxTheApp->argv[1]);
329         }
330     }
331     
332     webFrame->Close();
333     delete eventHandler;
334
335     wxLog::SetActiveTarget(0);
336     delete logger;
337     fclose(logOutput);
338     
339     // returning false shuts the app down
340     return false;
341 }