15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2011 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef FrameTestHelpers_h
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define FrameTestHelpers_h
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "public/platform/WebURLRequest.h"
3509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "public/web/WebFrameClient.h"
36d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "public/web/WebHistoryItem.h"
377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "public/web/WebRemoteFrameClient.h"
38f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "public/web/WebViewClient.h"
39f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "web/WebViewImpl.h"
40591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/PassOwnPtr.h"
411e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include <string>
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)namespace blink {
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochclass WebLocalFrameImpl;
461e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class WebSettings;
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace FrameTestHelpers {
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
50d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)class TestWebFrameClient;
51d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
52d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// Loads a url into the specified WebFrame for testing purposes. Pumps any
53d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// pending resource requests, as well as waiting for the threaded parser to
54d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// finish, before returning.
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void loadFrame(WebFrame*, const std::string& url);
56d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// Same as above, but for WebFrame::loadHTMLString().
57d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void loadHTMLString(WebFrame*, const std::string& html, const WebURL& baseURL);
58d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// Same as above, but for WebFrame::loadHistoryItem().
59d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void loadHistoryItem(WebFrame*, const WebHistoryItem&, WebHistoryLoadType, WebURLRequest::CachePolicy);
60d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// Same as above, but for WebFrame::reload().
61d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void reloadFrame(WebFrame*);
62d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void reloadFrameIgnoringCache(WebFrame*);
63d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
64d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// Pumps pending resource requests while waiting for a frame to load. Don't use
65d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)// this. Use one of the above helpers.
66d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)void pumpPendingRequestsDoNotUse(WebFrame*);
67d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
681e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)void runPendingTasks();
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)// Convenience class for handling the lifetime of a WebView and its associated mainframe in tests.
711e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class WebViewHelper {
721e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(WebViewHelper);
731e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)public:
741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    WebViewHelper();
751e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    ~WebViewHelper();
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
771e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // Creates and initializes the WebView. Implicitly calls reset() first. IF a
781e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // WebFrameClient or a WebViewClient are passed in, they must outlive the
791e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    // WebViewHelper.
80d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    WebViewImpl* initialize(bool enableJavascript = false, TestWebFrameClient* = 0, WebViewClient* = 0, void (*updateSettingsFunc)(WebSettings*) = 0);
811e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
82d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // Same as initialize() but also performs the initial load of the url. Only
83d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    // returns once the load is complete.
84d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    WebViewImpl* initializeAndLoad(const std::string& url, bool enableJavascript = false, TestWebFrameClient* = 0, WebViewClient* = 0, void (*updateSettingsFunc)(WebSettings*) = 0);
851e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
861e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    void reset();
871e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
881e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    WebView* webView() const { return m_webView; }
891e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    WebViewImpl* webViewImpl() const { return m_webView; }
901e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
911e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)private:
921e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    WebViewImpl* m_webView;
931e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)};
94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// Minimal implementation of WebFrameClient needed for unit tests that load frames. Tests that load
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)// frames and need further specialization of WebFrameClient behavior should subclass this.
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class TestWebFrameClient : public WebFrameClient {
9809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)public:
99d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    TestWebFrameClient();
100d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
101a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    virtual WebFrame* createChildFrame(WebLocalFrame* parent, const WebString& frameName) OVERRIDE;
10209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void frameDetached(WebFrame*) OVERRIDE;
103d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    virtual void didStartLoading(bool) OVERRIDE;
104d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    virtual void didStopLoading() OVERRIDE;
105d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
106d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    bool isLoading() { return m_loadsInProgress > 0; }
107d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
108d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)private:
109d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    int m_loadsInProgress;
11009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)};
11109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
1127242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// Minimal implementation of WebRemoteFrameClient needed for unit tests that load remote frames. Tests that load
1137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci// frames and need further specialization of WebFrameClient behavior should subclass this.
1147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass TestWebRemoteFrameClient : public WebRemoteFrameClient {
1157242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccipublic:
1167242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // Notifies the embedder that a postMessage was issued to a remote frame.
1177242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    virtual void postMessageEvent(
1187242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        WebLocalFrame* sourceFrame,
1197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        WebRemoteFrame* targetFrame,
1207242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        WebSecurityOrigin targetOrigin,
1217242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        WebDOMMessageEvent) { }
1227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci};
1237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
124f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuclass TestWebViewClient : public WebViewClient {
125f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liupublic:
126f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    virtual ~TestWebViewClient() { }
127f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    virtual void initializeLayerTreeView() OVERRIDE;
128f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    virtual WebLayerTreeView* layerTreeView() OVERRIDE { return m_layerTreeView.get(); }
129f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
130f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuprivate:
131f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    OwnPtr<WebLayerTreeView> m_layerTreeView;
132f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu};
133f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace FrameTestHelpers
13551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)} // namespace blink
1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // FrameTestHelpers_h
138