Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / geolocation / testing / GeolocationClientMock.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2012 Apple Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "GeolocationClientMock.h"
34
35 #include "modules/geolocation/GeolocationController.h"
36 #include "modules/geolocation/GeolocationError.h"
37 #include "modules/geolocation/GeolocationPosition.h"
38
39 namespace WebCore {
40
41 GeolocationClientMock::GeolocationClientMock()
42     : m_controller(0)
43     , m_hasError(false)
44     , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired)
45     , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired)
46     , m_isActive(false)
47     , m_permissionState(PermissionStateUnset)
48 {
49 }
50
51 GeolocationClientMock::~GeolocationClientMock()
52 {
53     ASSERT(!m_isActive);
54 }
55
56 void GeolocationClientMock::setController(GeolocationController *controller)
57 {
58     ASSERT(controller && !m_controller);
59     m_controller = controller;
60 }
61
62 void GeolocationClientMock::setPosition(PassRefPtrWillBeRawPtr<GeolocationPosition> position)
63 {
64     m_lastPosition = position;
65     clearError();
66     asyncUpdateController();
67 }
68
69 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage)
70 {
71     m_hasError = true;
72     m_errorMessage = errorMessage;
73     m_lastPosition = nullptr;
74     asyncUpdateController();
75 }
76
77 void GeolocationClientMock::setPermission(bool allowed)
78 {
79     m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
80     asyncUpdatePermission();
81 }
82
83 int GeolocationClientMock::numberOfPendingPermissionRequests() const
84 {
85     return m_pendingPermissions.size();
86 }
87
88 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
89 {
90     m_pendingPermissions.add(geolocation);
91     if (m_permissionState != PermissionStateUnset)
92         asyncUpdatePermission();
93 }
94
95 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
96 {
97     // Called from Geolocation::disconnectFrame() in response to LocalFrame destruction.
98     m_pendingPermissions.remove(geolocation);
99     if (m_pendingPermissions.isEmpty() && m_permissionTimer.isActive())
100         m_permissionTimer.stop();
101 }
102
103 void GeolocationClientMock::asyncUpdatePermission()
104 {
105     ASSERT(m_permissionState != PermissionStateUnset);
106     if (!m_permissionTimer.isActive())
107         m_permissionTimer.startOneShot(0, FROM_HERE);
108 }
109
110 void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer)
111 {
112     ASSERT_UNUSED(timer, timer == &m_permissionTimer);
113     ASSERT(m_permissionState != PermissionStateUnset);
114     bool allowed = m_permissionState == PermissionStateAllowed;
115     GeolocationSet::iterator end = m_pendingPermissions.end();
116
117     // Once permission has been set (or denied) on a Geolocation object, there can be
118     // no further requests for permission to the mock. Consequently the callbacks
119     // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify
120     // m_pendingPermissions.
121     for (GeolocationSet::iterator it = m_pendingPermissions.begin(); it != end; ++it)
122         (*it)->setIsAllowed(allowed);
123     m_pendingPermissions.clear();
124 }
125
126 void GeolocationClientMock::geolocationDestroyed()
127 {
128     ASSERT(!m_isActive);
129 }
130
131 void GeolocationClientMock::startUpdating()
132 {
133     ASSERT(!m_isActive);
134     m_isActive = true;
135     asyncUpdateController();
136 }
137
138 void GeolocationClientMock::stopUpdating()
139 {
140     ASSERT(m_isActive);
141     m_isActive = false;
142     m_controllerTimer.stop();
143 }
144
145 void GeolocationClientMock::setEnableHighAccuracy(bool)
146 {
147     // FIXME: We need to add some tests regarding "high accuracy" mode.
148     // See https://bugs.webkit.org/show_bug.cgi?id=49438
149 }
150
151 GeolocationPosition* GeolocationClientMock::lastPosition()
152 {
153     return m_lastPosition.get();
154 }
155
156 void GeolocationClientMock::asyncUpdateController()
157 {
158     ASSERT(m_controller);
159     if (m_isActive && !m_controllerTimer.isActive())
160         m_controllerTimer.startOneShot(0, FROM_HERE);
161 }
162
163 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
164 {
165     ASSERT_UNUSED(timer, timer == &m_controllerTimer);
166     ASSERT(m_controller);
167
168     if (m_lastPosition.get()) {
169         ASSERT(!m_hasError);
170         m_controller->positionChanged(m_lastPosition.get());
171     } else if (m_hasError) {
172         m_controller->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage).get());
173     }
174 }
175
176 void GeolocationClientMock::clearError()
177 {
178     m_hasError = false;
179     m_errorMessage = String();
180 }
181
182 } // WebCore