[Win] Various DumpRenderTree Fixes.
[WebKit-https.git] / Tools / DumpRenderTree / win / HistoryDelegate.cpp
1 /*
2  * Copyright (C) 2009, 2014 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "HistoryDelegate.h"
28
29 #include "DumpRenderTree.h"
30 #include "DumpRenderTreeWin.h"
31 #include "TestRunner.h"
32 #include <comutil.h>
33 #include <string>
34 #include <WebKit/WebKit.h>
35
36 using std::wstring;
37
38 static inline wstring wstringFromBSTR(BSTR str)
39 {
40     return wstring(str, ::SysStringLen(str));
41 }
42
43 HistoryDelegate::HistoryDelegate()
44     : m_refCount(1)
45 {
46 }
47
48 HistoryDelegate::~HistoryDelegate()
49 {
50 }
51
52     // IUnknown
53 HRESULT HistoryDelegate::QueryInterface(REFIID riid, void** ppvObject)
54 {
55     *ppvObject = 0;
56     if (IsEqualGUID(riid, IID_IUnknown))
57         *ppvObject = static_cast<IWebHistoryDelegate*>(this);
58     else if (IsEqualGUID(riid, IID_IWebHistoryDelegate))
59         *ppvObject = static_cast<IWebHistoryDelegate*>(this);
60     else
61         return E_NOINTERFACE;
62
63     AddRef();
64     return S_OK;
65 }
66
67 ULONG HistoryDelegate::AddRef(void)
68 {
69     return ++m_refCount;
70 }
71
72 ULONG HistoryDelegate::Release(void)
73 {
74     ULONG newRef = --m_refCount;
75     if (!newRef)
76         delete(this);
77
78     return newRef;
79 }
80
81 // IWebHistoryDelegate
82 HRESULT HistoryDelegate::didNavigateWithNavigationData(IWebView* webView, IWebNavigationData* navigationData, IWebFrame* webFrame)
83 {
84     if (!gTestRunner->dumpHistoryDelegateCallbacks())
85         return S_OK;
86
87     _bstr_t urlBSTR;
88     if (FAILED(navigationData->url(&urlBSTR.GetBSTR())))
89         return E_FAIL;
90     wstring url;
91     if (urlBSTR.length())
92         url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
93
94     _bstr_t titleBSTR;
95     if (FAILED(navigationData->title(&titleBSTR.GetBSTR())))
96         return E_FAIL;
97
98     COMPtr<IWebURLRequest> request;
99     if (FAILED(navigationData->originalRequest(&request)))
100         return E_FAIL;
101
102     _bstr_t httpMethodBSTR;
103     if (FAILED(request->HTTPMethod(&httpMethodBSTR.GetBSTR())))
104         return E_FAIL;
105
106     COMPtr<IWebURLResponse> response;
107     if (FAILED(navigationData->response(&response)))
108         return E_FAIL;
109
110     COMPtr<IWebHTTPURLResponse> httpResponse;
111     if (FAILED(response->QueryInterface(&httpResponse)))
112         return E_FAIL;
113
114     int statusCode = 0;
115     if (FAILED(httpResponse->statusCode(&statusCode)))
116         return E_FAIL;
117
118     BOOL hasSubstituteData;
119     if (FAILED(navigationData->hasSubstituteData(&hasSubstituteData)))
120         return E_FAIL;
121
122     _bstr_t clientRedirectSourceBSTR;
123     if (FAILED(navigationData->clientRedirectSource(&clientRedirectSourceBSTR.GetBSTR())))
124         return E_FAIL;
125     bool hasClientRedirect = clientRedirectSourceBSTR.length();
126     wstring redirectSource;
127     if (clientRedirectSourceBSTR.length())
128         redirectSource = urlSuitableForTestResult(wstringFromBSTR(clientRedirectSourceBSTR));
129
130     bool wasFailure = hasSubstituteData || (httpResponse && statusCode >= 400);
131         
132     printf("WebView navigated to url \"%S\" with title \"%S\" with HTTP equivalent method \"%S\".  The navigation was %s and was %s%S.\n", 
133         url.c_str(), 
134         static_cast<wchar_t*>(titleBSTR),
135         static_cast<wchar_t*>(httpMethodBSTR),
136         wasFailure ? "a failure" : "successful", 
137         hasClientRedirect ? "a client redirect from " : "not a client redirect", 
138         redirectSource.c_str());
139
140     return S_OK;
141 }
142
143 HRESULT HistoryDelegate::didPerformClientRedirectFromURL(IWebView*, BSTR sourceURL, BSTR destinationURL, IWebFrame*)
144 {
145     if (!gTestRunner->dumpHistoryDelegateCallbacks())
146         return S_OK;
147
148     wstring source;
149     if (sourceURL)
150         source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
151     
152     wstring destination;
153     if (destinationURL)
154         destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
155
156     printf("WebView performed a client redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
157     return S_OK;
158 }
159     
160 HRESULT HistoryDelegate::didPerformServerRedirectFromURL(IWebView* webView, BSTR sourceURL, BSTR destinationURL, IWebFrame* webFrame)
161 {
162     if (!gTestRunner->dumpHistoryDelegateCallbacks())
163         return S_OK;
164
165     wstring source;
166     if (sourceURL)
167         source = urlSuitableForTestResult(wstringFromBSTR(sourceURL));
168     
169     wstring destination;
170     if (destinationURL)
171         destination = urlSuitableForTestResult(wstringFromBSTR(destinationURL));
172
173     printf("WebView performed a server redirect from \"%S\" to \"%S\".\n", source.c_str(), destination.c_str());
174     return S_OK;
175 }
176
177 HRESULT HistoryDelegate::updateHistoryTitle(IWebView* webView, BSTR titleBSTR, BSTR urlBSTR)
178 {
179     if (!gTestRunner->dumpHistoryDelegateCallbacks())
180         return S_OK;
181     
182     wstring url;
183     if (urlBSTR)
184         url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
185
186     printf("WebView updated the title for history URL \"%S\" to \"%S\".\n", url.c_str(), titleBSTR ? titleBSTR : L"");
187     return S_OK;
188 }
189     
190 HRESULT HistoryDelegate::populateVisitedLinksForWebView(IWebView* webView)
191 {
192     if (!gTestRunner->dumpHistoryDelegateCallbacks())
193         return S_OK;
194
195     _bstr_t urlBSTR;
196     if (FAILED(webView->mainFrameURL(&urlBSTR.GetBSTR())))
197         return E_FAIL;
198
199     wstring url;
200     if (urlBSTR.length())
201         url = urlSuitableForTestResult(wstringFromBSTR(urlBSTR));
202
203     if (gTestRunner->dumpVisitedLinksCallback())
204         printf("Asked to populate visited links for WebView \"%S\"\n", url.c_str());
205
206     return S_OK;
207 }