Upstream version 8.37.180.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_hasError(false)
43     , m_controllerTimer(this, &GeolocationClientMock::controllerTimerFired)
44     , m_permissionTimer(this, &GeolocationClientMock::permissionTimerFired)
45     , m_isActive(false)
46     , m_permissionState(PermissionStateUnset)
47 {
48 }
49
50 GeolocationClientMock::~GeolocationClientMock()
51 {
52     ASSERT(!m_isActive);
53 }
54
55 void GeolocationClientMock::setPosition(GeolocationPosition* position)
56 {
57     m_lastPosition = position;
58     clearError();
59     asyncUpdateController();
60 }
61
62 void GeolocationClientMock::setPositionUnavailableError(const String& errorMessage)
63 {
64     m_hasError = true;
65     m_errorMessage = errorMessage;
66     m_lastPosition = nullptr;
67     asyncUpdateController();
68 }
69
70 void GeolocationClientMock::setPermission(bool allowed)
71 {
72     m_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
73     asyncUpdatePermission();
74 }
75
76 int GeolocationClientMock::numberOfPendingPermissionRequests() const
77 {
78     return m_pendingPermissions.size();
79 }
80
81 void GeolocationClientMock::requestPermission(Geolocation* geolocation)
82 {
83     m_pendingPermissions.add(geolocation);
84     if (m_permissionState != PermissionStateUnset)
85         asyncUpdatePermission();
86 }
87
88 void GeolocationClientMock::cancelPermissionRequest(Geolocation* geolocation)
89 {
90     // Called from Geolocation::disconnectFrame() in response to LocalFrame destruction.
91     m_pendingPermissions.remove(geolocation);
92     if (m_pendingPermissions.isEmpty() && m_permissionTimer.isActive())
93         m_permissionTimer.stop();
94 }
95
96 void GeolocationClientMock::controllerForTestAdded(GeolocationController* controller)
97 {
98     m_controllers.add(controller);
99 }
100
101 void GeolocationClientMock::controllerForTestRemoved(GeolocationController* controller)
102 {
103     m_controllers.remove(controller);
104 }
105
106 void GeolocationClientMock::asyncUpdatePermission()
107 {
108     ASSERT(m_permissionState != PermissionStateUnset);
109     if (!m_permissionTimer.isActive())
110         m_permissionTimer.startOneShot(0, FROM_HERE);
111 }
112
113 void GeolocationClientMock::permissionTimerFired(Timer<GeolocationClientMock>* timer)
114 {
115     ASSERT_UNUSED(timer, timer == &m_permissionTimer);
116     ASSERT(m_permissionState != PermissionStateUnset);
117     bool allowed = m_permissionState == PermissionStateAllowed;
118     GeolocationSet::iterator end = m_pendingPermissions.end();
119
120     // Once permission has been set (or denied) on a Geolocation object, there can be
121     // no further requests for permission to the mock. Consequently the callbacks
122     // which fire synchronously from Geolocation::setIsAllowed() cannot reentrantly modify
123     // m_pendingPermissions.
124     for (GeolocationSet::iterator it = m_pendingPermissions.begin(); it != end; ++it)
125         (*it)->setIsAllowed(allowed);
126     m_pendingPermissions.clear();
127 }
128
129 void GeolocationClientMock::geolocationDestroyed()
130 {
131     ASSERT(!m_isActive);
132 }
133
134 void GeolocationClientMock::startUpdating()
135 {
136     ASSERT(!m_isActive);
137     m_isActive = true;
138     asyncUpdateController();
139 }
140
141 void GeolocationClientMock::stopUpdating()
142 {
143     ASSERT(m_isActive);
144     m_isActive = false;
145     m_controllerTimer.stop();
146 }
147
148 void GeolocationClientMock::setEnableHighAccuracy(bool)
149 {
150     // FIXME: We need to add some tests regarding "high accuracy" mode.
151     // See https://bugs.webkit.org/show_bug.cgi?id=49438
152 }
153
154 GeolocationPosition* GeolocationClientMock::lastPosition()
155 {
156     return m_lastPosition.get();
157 }
158
159 void GeolocationClientMock::asyncUpdateController()
160 {
161     if (m_isActive && !m_controllerTimer.isActive())
162         m_controllerTimer.startOneShot(0, FROM_HERE);
163 }
164
165 void GeolocationClientMock::controllerTimerFired(Timer<GeolocationClientMock>* timer)
166 {
167     ASSERT_UNUSED(timer, timer == &m_controllerTimer);
168
169     // Make a copy of the set of controllers since it might be modified while iterating.
170     GeolocationControllers controllers = m_controllers;
171     if (m_lastPosition.get()) {
172         ASSERT(!m_hasError);
173         for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
174             (*it)->positionChanged(m_lastPosition.get());
175     } else if (m_hasError) {
176         for (GeolocationControllers::iterator it = controllers.begin(); it != controllers.end(); ++it)
177             (*it)->errorOccurred(GeolocationError::create(GeolocationError::PositionUnavailable, m_errorMessage));
178     }
179 }
180
181 void GeolocationClientMock::clearError()
182 {
183     m_hasError = false;
184     m_errorMessage = String();
185 }
186
187 } // WebCore