Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / FrameTestHelpers.h
blob6800cf78faca681a30bbc927e60d5c98ff977b4e
1 /*
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
6 * met:
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.
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>
46 #include <string>
48 namespace blink {
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 {
71 public:
72 virtual void overrideSettings(WebSettings*) = 0;
75 // Convenience class for handling the lifetime of a WebView and its associated mainframe in tests.
76 class WebViewHelper {
77 WTF_MAKE_NONCOPYABLE(WebViewHelper);
78 public:
79 WebViewHelper(SettingOverrider* = 0);
80 ~WebViewHelper();
82 // Creates and initializes the WebView. Implicitly calls reset() first. IF a
83 // WebFrameClient or a WebViewClient are passed in, they must outlive the
84 // WebViewHelper.
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);
91 void reset();
93 WebView* webView() const { return m_webView; }
94 WebViewImpl* webViewImpl() const { return m_webView; }
96 private:
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 {
104 public:
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();
115 private:
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 {
122 public:
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 { }
135 private:
136 WebRemoteFrame* const m_frame;
139 class TestWebViewClient : public WebViewClient {
140 public:
141 virtual ~TestWebViewClient() { }
142 void initializeLayerTreeView() override;
143 WebLayerTreeView* layerTreeView() override { return m_layerTreeView.get(); }
145 private:
146 OwnPtr<WebLayerTreeView> m_layerTreeView;
149 class UseMockScrollbarSettings {
150 public:
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
166 } // namespace blink
168 #endif // FrameTestHelpers_h