1/*
2 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
3 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include "config.h"
21#include "DumpRenderTreeSupport.h"
22
23#include "DeviceOrientationClientMock.h"
24#include "DeviceOrientationController.h"
25#include "DeviceOrientationData.h"
26#include "Frame.h"
27#include "GeolocationClientMock.h"
28#include "GeolocationController.h"
29#include "GeolocationError.h"
30#include "GeolocationPosition.h"
31#include "JSCSSStyleDeclaration.h"
32#include "JSElement.h"
33#include "Page.h"
34#include "RuntimeEnabledFeatures.h"
35#include "WebPage_p.h"
36#include "bindings/js/GCController.h"
37#include <JavaScriptCore/APICast.h>
38#include <wtf/CurrentTime.h>
39
40using namespace BlackBerry::WebKit;
41using namespace WebCore;
42using namespace JSC;
43
44bool DumpRenderTreeSupport::s_linksIncludedInTabChain = true;
45
46GeolocationClientMock* toGeolocationClientMock(GeolocationClient* client)
47{
48    ASSERT(isRunningDrt());
49    return static_cast<GeolocationClientMock*>(client);
50}
51
52DumpRenderTreeSupport::DumpRenderTreeSupport()
53{
54}
55
56DumpRenderTreeSupport::~DumpRenderTreeSupport()
57{
58}
59
60Page* DumpRenderTreeSupport::corePage(WebPage* webPage)
61{
62    return WebPagePrivate::core(webPage);
63}
64
65int DumpRenderTreeSupport::javaScriptObjectsCount()
66{
67    return JSDOMWindowBase::commonVM()->heap.globalObjectCount();
68}
69
70void DumpRenderTreeSupport::garbageCollectorCollect()
71{
72    gcController().garbageCollectNow();
73}
74
75void DumpRenderTreeSupport::garbageCollectorCollectOnAlternateThread(bool waitUntilDone)
76{
77    gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
78}
79
80void DumpRenderTreeSupport::setLinksIncludedInFocusChain(bool enabled)
81{
82    s_linksIncludedInTabChain = enabled;
83}
84
85bool DumpRenderTreeSupport::linksIncludedInFocusChain()
86{
87    return s_linksIncludedInTabChain;
88}
89
90int DumpRenderTreeSupport::numberOfPendingGeolocationPermissionRequests(WebPage* webPage)
91{
92    GeolocationClientMock* mockClient = toGeolocationClientMock(GeolocationController::from(corePage(webPage))->client());
93    return mockClient->numberOfPendingPermissionRequests();
94}
95
96void DumpRenderTreeSupport::resetGeolocationMock(WebPage* webPage)
97{
98    GeolocationClientMock* mockClient = toGeolocationClientMock(GeolocationController::from(corePage(webPage))->client());
99    mockClient->reset();
100}
101
102void DumpRenderTreeSupport::setMockGeolocationPositionUnavailableError(WebPage* webPage, const String message)
103{
104    GeolocationClientMock* mockClient = static_cast<GeolocationClientMock*>(GeolocationController::from(corePage(webPage))->client());
105    mockClient->setPositionUnavailableError(message);
106}
107
108void DumpRenderTreeSupport::setMockGeolocationPermission(WebPage* webPage, bool allowed)
109{
110    GeolocationClientMock* mockClient = toGeolocationClientMock(GeolocationController::from(corePage(webPage))->client());
111    mockClient->setPermission(allowed);
112}
113
114void DumpRenderTreeSupport::setMockGeolocationPosition(WebPage* webPage, double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
115{
116    GeolocationClientMock* mockClient = toGeolocationClientMock(GeolocationController::from(corePage(webPage))->client());
117    mockClient->setPosition(GeolocationPosition::create(currentTime(), latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed));
118}
119
120void DumpRenderTreeSupport::scalePageBy(WebPage* webPage, float scaleFactor, float x, float y)
121{
122    corePage(webPage)->setPageScaleFactor(scaleFactor, IntPoint(x, y));
123}
124
125#if ENABLE(STYLE_SCOPED)
126void DumpRenderTreeSupport::setStyleScopedEnabled(bool enabled)
127{
128    RuntimeEnabledFeatures::setStyleScopedEnabled(enabled);
129}
130#endif
131
132#if ENABLE(DEVICE_ORIENTATION)
133DeviceOrientationClientMock* toDeviceOrientationClientMock(DeviceOrientationClient* client)
134{
135    return static_cast<DeviceOrientationClientMock*>(client);
136}
137#endif
138
139void DumpRenderTreeSupport::setMockDeviceOrientation(BlackBerry::WebKit::WebPage* webPage, bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
140{
141#if ENABLE(DEVICE_ORIENTATION)
142    Page* page = corePage(webPage);
143    DeviceOrientationClientMock* mockClient = toDeviceOrientationClientMock(DeviceOrientationController::from(page)->deviceOrientationClient());
144    mockClient->setOrientation(DeviceOrientationData::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma));
145#endif
146}
147