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