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