2a4a9fcc9216db172d3ee4caa625757b4c699874
[WebKit-https.git] / Source / WebKit / efl / WebCoreSupport / DumpRenderTreeSupportEfl.cpp
1 /*
2     Copyright (C) 2011 ProFUSION embedded systems
3     Copyright (C) 2011 Samsung Electronics
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "DumpRenderTreeSupportEfl.h"
23
24 #include "FrameLoaderClientEfl.h"
25 #include "ewk_private.h"
26
27 #include <AnimationController.h>
28 #include <DocumentLoader.h>
29 #include <Eina.h>
30 #include <Evas.h>
31 #include <FindOptions.h>
32 #include <FloatSize.h>
33 #include <FrameView.h>
34 #include <IntRect.h>
35 #include <PrintContext.h>
36 #include <RenderTreeAsText.h>
37 #include <Settings.h>
38 #include <bindings/js/GCController.h>
39 #include <history/HistoryItem.h>
40 #include <workers/WorkerThread.h>
41 #include <wtf/text/AtomicString.h>
42
43 unsigned DumpRenderTreeSupportEfl::activeAnimationsCount(const Evas_Object* ewkFrame)
44 {
45     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
46
47     if (!frame)
48         return 0;
49
50     WebCore::AnimationController* animationController = frame->animation();
51
52     if (!animationController)
53         return 0;
54
55     return animationController->numberOfActiveAnimations(frame->document());
56 }
57
58 void DumpRenderTreeSupportEfl::clearFrameName(Evas_Object* ewkFrame)
59 {
60     if (WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame))
61         frame->tree()->clearName();
62 }
63
64 void DumpRenderTreeSupportEfl::clearOpener(Evas_Object* ewkFrame)
65 {
66     if (WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame))
67         frame->loader()->setOpener(0);
68 }
69
70 String DumpRenderTreeSupportEfl::counterValueByElementId(const Evas_Object* ewkFrame, const char* elementId)
71 {
72     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
73
74     if (!frame)
75         return String();
76
77     WebCore::Element* element = frame->document()->getElementById(elementId);
78
79     if (!element)
80         return String();
81
82     return WebCore::counterValueForElement(element);
83 }
84
85 Eina_List* DumpRenderTreeSupportEfl::frameChildren(const Evas_Object* ewkFrame)
86 {
87     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
88
89     if (!frame)
90         return 0;
91
92     Eina_List* childFrames = 0;
93
94     for (unsigned index = 0; index < frame->tree()->childCount(); index++) {
95         WebCore::Frame *childFrame = frame->tree()->child(index);
96         WebCore::FrameLoaderClientEfl *client = static_cast<WebCore::FrameLoaderClientEfl*>(childFrame->loader()->client());
97
98         if (!client)
99             continue;
100
101         childFrames = eina_list_append(childFrames, client->webFrame());
102     }
103
104     return childFrames;
105 }
106
107 WebCore::Frame* DumpRenderTreeSupportEfl::frameParent(const Evas_Object* ewkFrame)
108 {
109     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
110
111     if (!frame)
112         return 0;
113
114     return frame->tree()->parent();
115 }
116
117 void DumpRenderTreeSupportEfl::layoutFrame(Evas_Object* ewkFrame)
118 {
119     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
120
121     if (!frame)
122         return;
123
124     WebCore::FrameView* frameView = frame->view();
125
126     if (!frameView)
127         return;
128
129     frameView->layout();
130 }
131
132 int DumpRenderTreeSupportEfl::numberOfPages(const Evas_Object* ewkFrame, float pageWidth, float pageHeight)
133 {
134     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
135
136     if (!frame)
137         return 0;
138
139     return WebCore::PrintContext::numberOfPages(frame, WebCore::FloatSize(pageWidth, pageHeight));
140 }
141
142 int DumpRenderTreeSupportEfl::numberOfPagesForElementId(const Evas_Object* ewkFrame, const char* elementId, float pageWidth, float pageHeight)
143 {
144     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
145
146     if (!frame)
147         return 0;
148
149     WebCore::Element *element = frame->document()->getElementById(elementId);
150
151     if (!element)
152         return 0;
153
154     return WebCore::PrintContext::pageNumberForElement(element, WebCore::FloatSize(pageWidth, pageHeight));
155 }
156
157 bool DumpRenderTreeSupportEfl::pauseAnimation(Evas_Object* ewkFrame, const char* name, const char* elementId, double time)
158 {
159     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
160
161     if (!frame)
162         return false;
163
164     WebCore::Element* element = frame->document()->getElementById(elementId);
165
166     if (!element || !element->renderer())
167         return false;
168
169     return frame->animation()->pauseAnimationAtTime(element->renderer(), name, time);
170 }
171
172 bool DumpRenderTreeSupportEfl::pauseTransition(Evas_Object* ewkFrame, const char* name, const char* elementId, double time)
173 {
174     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
175
176     if (!frame)
177         return false;
178
179     WebCore::Element* element = frame->document()->getElementById(elementId);
180
181     if (!element || !element->renderer())
182         return false;
183
184     return frame->animation()->pauseTransitionAtTime(element->renderer(), name, time);
185 }
186
187 unsigned DumpRenderTreeSupportEfl::pendingUnloadEventCount(const Evas_Object* ewkFrame)
188 {
189     if (WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame))
190         return frame->domWindow()->pendingUnloadEventListeners();
191
192     return 0;
193 }
194
195 String DumpRenderTreeSupportEfl::renderTreeDump(Evas_Object* ewkFrame)
196 {
197     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
198
199     if (!frame)
200         return String();
201
202     WebCore::FrameView *frameView = frame->view();
203
204     if (frameView && frameView->layoutPending())
205         frameView->layout();
206
207     return WebCore::externalRepresentation(frame);
208 }
209
210 String DumpRenderTreeSupportEfl::responseMimeType(const Evas_Object* ewkFrame)
211 {
212     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
213
214     if (!frame)
215         return String();
216
217     WebCore::DocumentLoader *documentLoader = frame->loader()->documentLoader();
218
219     if (!documentLoader)
220         return String();
221
222     return documentLoader->responseMIMEType();
223 }
224
225 void DumpRenderTreeSupportEfl::resumeAnimations(Evas_Object* ewkFrame)
226 {
227     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
228
229     if (!frame)
230         return;
231
232     WebCore::AnimationController *animationController = frame->animation();
233     if (animationController)
234         animationController->resumeAnimations();
235 }
236
237 WebCore::IntRect DumpRenderTreeSupportEfl::selectionRectangle(const Evas_Object* ewkFrame)
238 {
239     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
240
241     if (!frame)
242         return WebCore::IntRect();
243
244     return enclosingIntRect(frame->selection()->bounds());
245 }
246
247 // Compare with "WebKit/Tools/DumpRenderTree/mac/FrameLoadDelegate.mm
248 String DumpRenderTreeSupportEfl::suitableDRTFrameName(const Evas_Object* ewkFrame)
249 {
250     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
251
252     if (!frame)
253         return String();
254
255     const String frameName(ewk_frame_name_get(ewkFrame));
256
257     if (ewkFrame == ewk_view_frame_main_get(ewk_frame_view_get(ewkFrame))) {
258         if (!frameName.isEmpty())
259             return String("main frame \"") + frameName + String("\"");
260
261         return String("main frame");
262     }
263
264     if (!frameName.isEmpty())
265         return String("frame \"") + frameName + String("\"");
266
267     return String("frame (anonymous)");
268 }
269
270 void DumpRenderTreeSupportEfl::suspendAnimations(Evas_Object* ewkFrame)
271 {
272     WebCore::Frame* frame = EWKPrivate::coreFrame(ewkFrame);
273
274     if (!frame)
275         return;
276
277     WebCore::AnimationController *animationController = frame->animation();
278     if (animationController)
279         animationController->suspendAnimations();
280 }
281
282 bool DumpRenderTreeSupportEfl::findString(const Evas_Object* ewkView, const char* text, WebCore::FindOptions options)
283 {
284     WebCore::Page* page = EWKPrivate::corePage(ewkView);
285
286     if (!page)
287         return false;
288
289     return page->findString(String::fromUTF8(text), options);
290 }
291
292 void DumpRenderTreeSupportEfl::garbageCollectorCollect()
293 {
294     WebCore::gcController().garbageCollectNow();
295 }
296
297 void DumpRenderTreeSupportEfl::garbageCollectorCollectOnAlternateThread(bool waitUntilDone)
298 {
299     WebCore::gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
300 }
301
302 size_t DumpRenderTreeSupportEfl::javaScriptObjectsCount()
303 {
304     return WebCore::JSDOMWindow::commonJSGlobalData()->heap.objectCount();
305 }
306
307 unsigned DumpRenderTreeSupportEfl::workerThreadCount()
308 {
309 #if ENABLE(WORKERS)
310     return WebCore::WorkerThread::workerThreadCount();
311 #else
312     return 0;
313 #endif
314 }
315
316 HistoryItemChildrenVector DumpRenderTreeSupportEfl::childHistoryItems(const Ewk_History_Item* ewkHistoryItem)
317 {
318     WebCore::HistoryItem* historyItem = EWKPrivate::coreHistoryItem(ewkHistoryItem);
319     HistoryItemChildrenVector kids;
320
321     if (!historyItem)
322         return kids;
323
324     const WebCore::HistoryItemVector& children = historyItem->children();
325     const unsigned size = children.size();
326
327     for (unsigned i = 0; i < size; ++i) {
328         Ewk_History_Item* kid = ewk_history_item_new_from_core(children[i].get());
329         kids.append(kid);
330     }
331
332     return kids;
333 }
334
335 String DumpRenderTreeSupportEfl::historyItemTarget(const Ewk_History_Item* ewkHistoryItem)
336 {
337     WebCore::HistoryItem* historyItem = EWKPrivate::coreHistoryItem(ewkHistoryItem);
338
339     if (!historyItem)
340         return String();
341
342     return historyItem->target();
343 }
344
345 bool DumpRenderTreeSupportEfl::isTargetItem(const Ewk_History_Item* ewkHistoryItem)
346 {
347     WebCore::HistoryItem* historyItem = EWKPrivate::coreHistoryItem(ewkHistoryItem);
348
349     if (!historyItem)
350         return false;
351
352     return historyItem->isTargetItem();
353 }
354
355 void DumpRenderTreeSupportEfl::setMockScrollbarsEnabled(bool enable)
356 {
357     WebCore::Settings::setMockScrollbarsEnabled(enable);
358 }
359
360 void DumpRenderTreeSupportEfl::dumpConfigurationForViewport(Evas_Object* ewkView, int deviceDPI, const WebCore::IntSize& deviceSize, const WebCore::IntSize& availableSize)
361 {
362     WebCore::Page* page = EWKPrivate::corePage(ewkView);
363
364     if (!page)
365         return;
366     WebCore::ViewportArguments arguments = page->mainFrame()->document()->viewportArguments();
367     WebCore::ViewportAttributes attributes = computeViewportAttributes(arguments,
368             /* default layout width for non-mobile pages */ 980,
369             deviceSize.width(), deviceSize.height(),
370             deviceDPI,
371             availableSize);
372     restrictMinimumScaleFactorToViewportSize(attributes, availableSize);
373     restrictScaleFactorToInitialScaleIfNotUserScalable(attributes);
374     fprintf(stdout, "viewport size %dx%d scale %f with limits [%f, %f] and userScalable %f\n", attributes.layoutSize.width(), attributes.layoutSize.height(), attributes.initialScale, attributes.minimumScale, attributes.maximumScale, attributes.userScalable);
375 }