• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "config.h"
32 #include "FrameTestHelpers.h"
33 
34 #include "URLTestHelpers.h"
35 #include "wtf/StdLibExtras.h"
36 #include "WebFrameClient.h"
37 #include "WebFrameImpl.h"
38 #include "WebSettings.h"
39 #include "WebViewClient.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebString.h"
42 #include "public/platform/WebThread.h"
43 #include "public/platform/WebURLRequest.h"
44 #include "public/platform/WebURLResponse.h"
45 #include "public/platform/WebUnitTestSupport.h"
46 
47 namespace blink {
48 namespace FrameTestHelpers {
49 
50 namespace {
51 
52 class QuitTask : public WebThread::Task {
53 public:
PostThis(WebCore::Timer<QuitTask> *)54     void PostThis(WebCore::Timer<QuitTask>*)
55     {
56         // We don't just quit here because the SharedTimer may be part-way
57         // through the current queue of tasks when runPendingTasks was called,
58         // and we can't miss the tasks that were behind it.
59         // Takes ownership of |this|.
60         Platform::current()->currentThread()->postTask(this);
61     }
62 
run()63     virtual void run()
64     {
65         Platform::current()->currentThread()->exitRunLoop();
66     }
67 };
68 
defaultWebFrameClient()69 WebFrameClient* defaultWebFrameClient()
70 {
71     DEFINE_STATIC_LOCAL(WebFrameClient, client, ());
72     return &client;
73 }
74 
defaultWebViewClient()75 WebViewClient* defaultWebViewClient()
76 {
77     DEFINE_STATIC_LOCAL(WebViewClient,  client, ());
78     return &client;
79 }
80 
81 } // namespace
82 
loadFrame(WebFrame * frame,const std::string & url)83 void loadFrame(WebFrame* frame, const std::string& url)
84 {
85     WebURLRequest urlRequest;
86     urlRequest.initialize();
87     urlRequest.setURL(URLTestHelpers::toKURL(url));
88     frame->loadRequest(urlRequest);
89 }
90 
runPendingTasks()91 void runPendingTasks()
92 {
93     // Pending tasks include Timers that have been scheduled.
94     WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis);
95     quitOnTimeout.startOneShot(0);
96     Platform::current()->currentThread()->enterRunLoop();
97 }
98 
WebViewHelper()99 WebViewHelper::WebViewHelper()
100     : m_mainFrame(0)
101     , m_webView(0)
102 {
103 }
104 
~WebViewHelper()105 WebViewHelper::~WebViewHelper()
106 {
107     reset();
108 }
109 
initialize(bool enableJavascript,WebFrameClient * webFrameClient,WebViewClient * webViewClient,void (* updateSettingsFunc)(WebSettings *))110 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
111 {
112     reset();
113 
114     if (!webFrameClient)
115         webFrameClient = defaultWebFrameClient();
116     if (!webViewClient)
117         webViewClient = defaultWebViewClient();
118     m_webView = WebViewImpl::create(webViewClient);
119     m_webView->settings()->setJavaScriptEnabled(enableJavascript);
120     if (updateSettingsFunc) {
121         updateSettingsFunc(m_webView->settings());
122     } else {
123         m_webView->settings()->setDeviceSupportsMouse(false);
124         m_webView->settings()->setForceCompositingMode(true);
125     }
126 
127     m_mainFrame = WebFrameImpl::create(webFrameClient);
128     m_webView->setMainFrame(m_mainFrame);
129 
130     return m_webView;
131 }
132 
initializeAndLoad(const std::string & url,bool enableJavascript,WebFrameClient * webFrameClient,WebViewClient * webViewClient,void (* updateSettingsFunc)(WebSettings *))133 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
134 {
135     initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFunc);
136 
137     loadFrame(webView()->mainFrame(), url);
138     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
139 
140     return webViewImpl();
141 }
142 
reset()143 void WebViewHelper::reset()
144 {
145     if (m_webView) {
146         m_webView->close();
147         m_webView = 0;
148     }
149     if (m_mainFrame) {
150         m_mainFrame->close();
151         m_mainFrame = 0;
152     }
153 }
154 
155 } // namespace FrameTestHelpers
156 } // namespace blink
157