1/*
2 * Copyright (C) 2011 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#ifndef FrameTestHelpers_h
32#define FrameTestHelpers_h
33
34#include "public/platform/WebURLRequest.h"
35#include "public/web/WebFrameClient.h"
36#include "public/web/WebHistoryItem.h"
37#include "public/web/WebRemoteFrameClient.h"
38#include "public/web/WebViewClient.h"
39#include "web/WebViewImpl.h"
40#include "wtf/PassOwnPtr.h"
41#include <string>
42
43namespace blink {
44
45class WebLocalFrameImpl;
46class WebSettings;
47
48namespace FrameTestHelpers {
49
50class TestWebFrameClient;
51
52// Loads a url into the specified WebFrame for testing purposes. Pumps any
53// pending resource requests, as well as waiting for the threaded parser to
54// finish, before returning.
55void loadFrame(WebFrame*, const std::string& url);
56// Same as above, but for WebFrame::loadHTMLString().
57void loadHTMLString(WebFrame*, const std::string& html, const WebURL& baseURL);
58// Same as above, but for WebFrame::loadHistoryItem().
59void loadHistoryItem(WebFrame*, const WebHistoryItem&, WebHistoryLoadType, WebURLRequest::CachePolicy);
60// Same as above, but for WebFrame::reload().
61void reloadFrame(WebFrame*);
62void reloadFrameIgnoringCache(WebFrame*);
63
64// Pumps pending resource requests while waiting for a frame to load. Don't use
65// this. Use one of the above helpers.
66void pumpPendingRequestsDoNotUse(WebFrame*);
67
68void runPendingTasks();
69
70// Convenience class for handling the lifetime of a WebView and its associated mainframe in tests.
71class WebViewHelper {
72    WTF_MAKE_NONCOPYABLE(WebViewHelper);
73public:
74    WebViewHelper();
75    ~WebViewHelper();
76
77    // Creates and initializes the WebView. Implicitly calls reset() first. IF a
78    // WebFrameClient or a WebViewClient are passed in, they must outlive the
79    // WebViewHelper.
80    WebViewImpl* initialize(bool enableJavascript = false, TestWebFrameClient* = 0, WebViewClient* = 0, void (*updateSettingsFunc)(WebSettings*) = 0);
81
82    // Same as initialize() but also performs the initial load of the url. Only
83    // returns once the load is complete.
84    WebViewImpl* initializeAndLoad(const std::string& url, bool enableJavascript = false, TestWebFrameClient* = 0, WebViewClient* = 0, void (*updateSettingsFunc)(WebSettings*) = 0);
85
86    void reset();
87
88    WebView* webView() const { return m_webView; }
89    WebViewImpl* webViewImpl() const { return m_webView; }
90
91private:
92    WebViewImpl* m_webView;
93};
94
95// Minimal implementation of WebFrameClient needed for unit tests that load frames. Tests that load
96// frames and need further specialization of WebFrameClient behavior should subclass this.
97class TestWebFrameClient : public WebFrameClient {
98public:
99    TestWebFrameClient();
100
101    virtual WebFrame* createChildFrame(WebLocalFrame* parent, const WebString& frameName) OVERRIDE;
102    virtual void frameDetached(WebFrame*) OVERRIDE;
103    virtual void didStartLoading(bool) OVERRIDE;
104    virtual void didStopLoading() OVERRIDE;
105
106    bool isLoading() { return m_loadsInProgress > 0; }
107
108private:
109    int m_loadsInProgress;
110};
111
112// Minimal implementation of WebRemoteFrameClient needed for unit tests that load remote frames. Tests that load
113// frames and need further specialization of WebFrameClient behavior should subclass this.
114class TestWebRemoteFrameClient : public WebRemoteFrameClient {
115public:
116    // Notifies the embedder that a postMessage was issued to a remote frame.
117    virtual void postMessageEvent(
118        WebLocalFrame* sourceFrame,
119        WebRemoteFrame* targetFrame,
120        WebSecurityOrigin targetOrigin,
121        WebDOMMessageEvent) { }
122};
123
124class TestWebViewClient : public WebViewClient {
125public:
126    virtual ~TestWebViewClient() { }
127    virtual void initializeLayerTreeView() OVERRIDE;
128    virtual WebLayerTreeView* layerTreeView() OVERRIDE { return m_layerTreeView.get(); }
129
130private:
131    OwnPtr<WebLayerTreeView> m_layerTreeView;
132};
133
134} // namespace FrameTestHelpers
135} // namespace blink
136
137#endif // FrameTestHelpers_h
138