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.
32 #include "FrameTestHelpers.h"
34 #include "URLTestHelpers.h"
35 #include "wtf/StdLibExtras.h"
36 #include "WebFrameImpl.h"
37 #include "WebSettings.h"
38 #include "WebViewClient.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebString.h"
41 #include "public/platform/WebThread.h"
42 #include "public/platform/WebURLRequest.h"
43 #include "public/platform/WebURLResponse.h"
44 #include "public/platform/WebUnitTestSupport.h"
47 namespace FrameTestHelpers {
51 class QuitTask : public WebThread::Task {
53 void PostThis(WebCore::Timer<QuitTask>*)
55 // We don't just quit here because the SharedTimer may be part-way
56 // through the current queue of tasks when runPendingTasks was called,
57 // and we can't miss the tasks that were behind it.
58 // Takes ownership of |this|.
59 Platform::current()->currentThread()->postTask(this);
64 Platform::current()->currentThread()->exitRunLoop();
68 WebFrameClient* defaultWebFrameClient()
70 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ());
74 WebViewClient* defaultWebViewClient()
76 DEFINE_STATIC_LOCAL(WebViewClient, client, ());
82 void loadFrame(WebFrame* frame, const std::string& url)
84 WebURLRequest urlRequest;
85 urlRequest.initialize();
86 urlRequest.setURL(URLTestHelpers::toKURL(url));
87 frame->loadRequest(urlRequest);
90 void runPendingTasks()
92 // Pending tasks include Timers that have been scheduled.
93 WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis);
94 quitOnTimeout.startOneShot(0);
95 Platform::current()->currentThread()->enterRunLoop();
98 WebViewHelper::WebViewHelper()
103 WebViewHelper::~WebViewHelper()
108 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
113 webFrameClient = defaultWebFrameClient();
115 webViewClient = defaultWebViewClient();
116 m_webView = WebViewImpl::create(webViewClient);
117 m_webView->settings()->setJavaScriptEnabled(enableJavascript);
118 if (updateSettingsFunc) {
119 updateSettingsFunc(m_webView->settings());
121 m_webView->settings()->setDeviceSupportsMouse(false);
122 m_webView->settings()->setForceCompositingMode(true);
125 m_webView->setMainFrame(WebFrameImpl::create(webFrameClient));
130 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
132 initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFunc);
134 loadFrame(webView()->mainFrame(), url);
135 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
137 return webViewImpl();
140 void WebViewHelper::reset()
148 WebFrame* TestWebFrameClient::createChildFrame(WebFrame* parent, const WebString& frameName)
150 return WebFrame::create(this);
153 void TestWebFrameClient::frameDetached(WebFrame* frame)
159 } // namespace FrameTestHelpers