2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef FrameTestHelpers_h
32 #define FrameTestHelpers_h
34 #include "core/frame/Settings.h"
35 #include "platform/RuntimeEnabledFeatures.h"
36 #include "platform/scroll/ScrollbarTheme.h"
37 #include "public/platform/WebURLRequest.h"
38 #include "public/web/WebFrameClient.h"
39 #include "public/web/WebHistoryItem.h"
40 #include "public/web/WebRemoteFrameClient.h"
41 #include "public/web/WebViewClient.h"
42 #include "web/WebViewImpl.h"
43 #include "wtf/PassOwnPtr.h"
44 #include <gmock/gmock.h>
45 #include <gtest/gtest.h>
50 namespace FrameTestHelpers
{
52 class TestWebFrameClient
;
54 // Loads a url into the specified WebFrame for testing purposes. Pumps any
55 // pending resource requests, as well as waiting for the threaded parser to
56 // finish, before returning.
57 void loadFrame(WebFrame
*, const std::string
& url
);
58 // Same as above, but for WebFrame::loadHTMLString().
59 void loadHTMLString(WebFrame
*, const std::string
& html
, const WebURL
& baseURL
);
60 // Same as above, but for WebFrame::loadHistoryItem().
61 void loadHistoryItem(WebFrame
*, const WebHistoryItem
&, WebHistoryLoadType
, WebURLRequest::CachePolicy
);
62 // Same as above, but for WebFrame::reload().
63 void reloadFrame(WebFrame
*);
64 void reloadFrameIgnoringCache(WebFrame
*);
66 // Pumps pending resource requests while waiting for a frame to load. Don't use
67 // this. Use one of the above helpers.
68 void pumpPendingRequestsDoNotUse(WebFrame
*);
70 class SettingOverrider
{
72 virtual void overrideSettings(WebSettings
*) = 0;
75 // Convenience class for handling the lifetime of a WebView and its associated mainframe in tests.
77 WTF_MAKE_NONCOPYABLE(WebViewHelper
);
79 WebViewHelper(SettingOverrider
* = 0);
82 // Creates and initializes the WebView. Implicitly calls reset() first. IF a
83 // WebFrameClient or a WebViewClient are passed in, they must outlive the
85 WebViewImpl
* initialize(bool enableJavascript
= false, TestWebFrameClient
* = 0, WebViewClient
* = 0, void (*updateSettingsFunc
)(WebSettings
*) = 0);
87 // Same as initialize() but also performs the initial load of the url. Only
88 // returns once the load is complete.
89 WebViewImpl
* initializeAndLoad(const std::string
& url
, bool enableJavascript
= false, TestWebFrameClient
* = 0, WebViewClient
* = 0, void (*updateSettingsFunc
)(WebSettings
*) = 0);
93 WebView
* webView() const { return m_webView
; }
94 WebViewImpl
* webViewImpl() const { return m_webView
; }
97 WebViewImpl
* m_webView
;
98 SettingOverrider
* m_settingOverrider
;
101 // Minimal implementation of WebFrameClient needed for unit tests that load frames. Tests that load
102 // frames and need further specialization of WebFrameClient behavior should subclass this.
103 class TestWebFrameClient
: public WebFrameClient
{
105 TestWebFrameClient();
107 WebFrame
* createChildFrame(WebLocalFrame
* parent
, WebTreeScopeType
, const WebString
& frameName
, WebSandboxFlags
) override
;
108 void frameDetached(WebFrame
*, DetachType
) override
;
109 void didStartLoading(bool) override
;
110 void didStopLoading() override
;
112 bool isLoading() { return m_loadsInProgress
> 0; }
113 void waitForLoadToComplete();
116 int m_loadsInProgress
;
119 // Minimal implementation of WebRemoteFrameClient needed for unit tests that load remote frames. Tests that load
120 // frames and need further specialization of WebFrameClient behavior should subclass this.
121 class TestWebRemoteFrameClient
: public WebRemoteFrameClient
{
123 TestWebRemoteFrameClient();
125 WebRemoteFrame
* frame() const { return m_frame
; }
127 // WebRemoteFrameClient overrides:
128 void frameDetached(DetachType
) override
;
129 void postMessageEvent(
130 WebLocalFrame
* sourceFrame
,
131 WebRemoteFrame
* targetFrame
,
132 WebSecurityOrigin targetOrigin
,
133 WebDOMMessageEvent
) override
{ }
136 WebRemoteFrame
* const m_frame
;
139 class TestWebViewClient
: public WebViewClient
{
141 virtual ~TestWebViewClient() { }
142 void initializeLayerTreeView() override
;
143 WebLayerTreeView
* layerTreeView() override
{ return m_layerTreeView
.get(); }
146 OwnPtr
<WebLayerTreeView
> m_layerTreeView
;
149 class UseMockScrollbarSettings
{
151 UseMockScrollbarSettings()
153 Settings::setMockScrollbarsEnabled(true);
154 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(true);
155 EXPECT_TRUE(ScrollbarTheme::theme()->usesOverlayScrollbars());
158 ~UseMockScrollbarSettings()
160 Settings::setMockScrollbarsEnabled(false);
161 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(false);
165 } // namespace FrameTestHelpers
168 #endif // FrameTestHelpers_h