- add third_party src.
[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 "WebFrameClient.h"
37 #include "WebFrameImpl.h"
38 #include "WebSettings.h"
39 #include "WebViewClient.h"
40 #include "WebViewImpl.h"
41 #include "public/platform/Platform.h"
42 #include "public/platform/WebString.h"
43 #include "public/platform/WebThread.h"
44 #include "public/platform/WebURLRequest.h"
45 #include "public/platform/WebURLResponse.h"
46 #include "public/platform/WebUnitTestSupport.h"
47
48 namespace WebKit {
49 namespace FrameTestHelpers {
50
51 namespace {
52
53 class QuitTask : public WebThread::Task {
54 public:
55     virtual void run()
56     {
57         Platform::current()->currentThread()->exitRunLoop();
58     }
59 };
60
61 WebFrameClient* defaultWebFrameClient()
62 {
63     DEFINE_STATIC_LOCAL(WebFrameClient, client, ());
64     return &client;
65 }
66
67 WebViewClient* defaultWebViewClient()
68 {
69     DEFINE_STATIC_LOCAL(WebViewClient,  client, ());
70     return &client;
71 }
72
73 } // namespace
74
75 void loadFrame(WebFrame* frame, const std::string& url)
76 {
77     WebURLRequest urlRequest;
78     urlRequest.initialize();
79     urlRequest.setURL(URLTestHelpers::toKURL(url));
80     frame->loadRequest(urlRequest);
81 }
82
83 void runPendingTasks()
84 {
85     Platform::current()->currentThread()->postTask(new QuitTask);
86     Platform::current()->currentThread()->enterRunLoop();
87 }
88
89 WebViewHelper::WebViewHelper()
90     : m_mainFrame(0)
91     , m_webView(0)
92 {
93 }
94
95 WebViewHelper::~WebViewHelper()
96 {
97     reset();
98 }
99
100 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
101 {
102     reset();
103
104     if (!webFrameClient)
105         webFrameClient = defaultWebFrameClient();
106     if (!webViewClient)
107         webViewClient = defaultWebViewClient();
108     m_webView = WebViewImpl::create(webViewClient);
109     m_webView->settings()->setJavaScriptEnabled(enableJavascript);
110     if (updateSettingsFunc) {
111         updateSettingsFunc(m_webView->settings());
112     } else {
113         m_webView->settings()->setDeviceSupportsMouse(false);
114         m_webView->settings()->setForceCompositingMode(true);
115     }
116
117     m_mainFrame = WebFrameImpl::create(webFrameClient);
118     m_webView->setMainFrame(m_mainFrame);
119
120     return m_webView;
121 }
122
123 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enableJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*))
124 {
125     initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFunc);
126
127     loadFrame(webView()->mainFrame(), url);
128     Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
129
130     return webViewImpl();
131 }
132
133 void WebViewHelper::reset()
134 {
135     if (m_webView) {
136         m_webView->close();
137         m_webView = 0;
138     }
139     if (m_mainFrame) {
140         m_mainFrame->close();
141         m_mainFrame = 0;
142     }
143 }
144
145 } // namespace FrameTestHelpers
146 } // namespace WebKit