Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / FrameTestHelpers.cpp
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 "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"
45
46 namespace blink {
47 namespace FrameTestHelpers {
48
49 namespace {
50
51 class QuitTask : public WebThread::Task {
52 public:
53     void PostThis(WebCore::Timer<QuitTask>*)
54     {
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);
60     }
61
62     virtual void run()
63     {
64         Platform::current()->currentThread()->exitRunLoop();
65     }
66 };
67
68 WebFrameClient* defaultWebFrameClient()
69 {
70     DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ());
71     return &client;
72 }
73
74 WebViewClient* defaultWebViewClient()
75 {
76     DEFINE_STATIC_LOCAL(WebViewClient,  client, ());
77     return &client;
78 }
79
80 } // namespace
81
82 void loadFrame(WebFrame* frame, const std::string& url)
83 {
84     WebURLRequest urlRequest;
85     urlRequest.initialize();
86     urlRequest.setURL(URLTestHelpers::toKURL(url));
87     frame->loadRequest(urlRequest);
88 }
89
90 void runPendingTasks()
91 {
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();
96 }
97
98 WebViewHelper::WebViewHelper()
99     : m_webView(0)
100 {
101 }
102
103 WebViewHelper::~WebViewHelper()
104 {
105     reset();
106 }
107
108 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
109 {
110     reset();
111
112     if (!webFrameClient)
113         webFrameClient = defaultWebFrameClient();
114     if (!webViewClient)
115         webViewClient = defaultWebViewClient();
116     m_webView = WebViewImpl::create(webViewClient);
117     m_webView->settings()->setJavaScriptEnabled(enableJavascript);
118     if (updateSettingsFunc) {
119         updateSettingsFunc(m_webView->settings());
120     } else {
121         m_webView->settings()->setDeviceSupportsMouse(false);
122         m_webView->settings()->setForceCompositingMode(true);
123     }
124
125     m_webView->setMainFrame(WebFrameImpl::create(webFrameClient));
126
127     return m_webView;
128 }
129
130 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
131 {
132     initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFunc);
133
134     loadFrame(webView()->mainFrame(), url);
135     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
136
137     return webViewImpl();
138 }
139
140 void WebViewHelper::reset()
141 {
142     if (m_webView) {
143         m_webView->close();
144         m_webView = 0;
145     }
146 }
147
148 WebFrame* TestWebFrameClient::createChildFrame(WebFrame* parent, const WebString& frameName)
149 {
150     return WebFrame::create(this);
151 }
152
153 void TestWebFrameClient::frameDetached(WebFrame* frame)
154 {
155     frame->close();
156 }
157
158
159 } // namespace FrameTestHelpers
160 } // namespace blink