1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_DEVICE_PUBLIC_CPP_TEST_SCOPED_GEOLOCATION_OVERRIDER_H_
6 #define SERVICES_DEVICE_PUBLIC_CPP_TEST_SCOPED_GEOLOCATION_OVERRIDER_H_
7 
8 #include "base/bind.h"
9 #include "services/device/public/mojom/geoposition.mojom.h"
10 
11 namespace device {
12 
13 // A helper class which owns a FakeGeolocationContext by which the geolocation
14 // is overriden to a given position or latitude and longitude values.
15 // The FakeGeolocationContext overrides the binder of Device Service by
16 // service_manager::ServiceContext::SetGlobalBinderForTesting().
17 // The override of the geolocation implementation will be in effect for the
18 // duration of this object's lifetime.
19 //
20 // Note that for this override to work properly, it must be constructed in the
21 // same process that runs the Device Service implementation.
22 class ScopedGeolocationOverrider {
23  public:
24   explicit ScopedGeolocationOverrider(mojom::GeopositionPtr position);
25   ScopedGeolocationOverrider(double latitude, double longitude);
26   ~ScopedGeolocationOverrider();
27   void OverrideGeolocation(mojom::GeopositionPtr position);
28   void UpdateLocation(mojom::GeopositionPtr position);
29   void UpdateLocation(double latitude, double longitude);
30 
31   // Pause resolving Geolocation queries to keep request inflight.
32   // After |Pause()| call, Geolocation::QueryNextPosition does not respond,
33   // allowing us to test behavior in the middle of the request.
34   void Pause();
35 
36   // Resume resolving Geolocation queries.
37   // Send the paused Geolocation::QueryNextPosition response.
38   void Resume();
39 
40   // Count number of active FakeGeolocation instances, which is equal to the
41   // number of active consumer Remote<Geolocation>s.
42   // This is used to verify if consumers properly close the connections when
43   // they should no longer be listening.
44   size_t GetGeolocationInstanceCount() const;
45 
46   // Register callback to be notified when a Remote<Geolocation> is cleared and
47   // the corresponding fake Geolocation instance is disposed.
48   void SetGeolocationCloseCallback(base::RepeatingClosure closure);
49 
50  private:
51   class FakeGeolocation;
52   class FakeGeolocationContext;
53   std::unique_ptr<FakeGeolocationContext> geolocation_context_;
54 };
55 
56 }  // namespace device
57 
58 #endif  // SERVICES_DEVICE_PUBLIC_CPP_TEST_SCOPED_GEOLOCATION_OVERRIDER_H_
59