Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / geolocation / location_manager_unittest.mm
blob6b7e7f112b8ae3ab7ee783d0115298e1fbfbe13b
1 // Copyright 2014 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.
5 #import "ios/chrome/browser/geolocation/location_manager.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h"
9 #import "ios/chrome/browser/geolocation/location_manager+Testing.h"
10 #import "ios/public/provider/chrome/browser/geolocation_updater_provider.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "testing/gtest_mac.h"
13 #include "testing/platform_test.h"
14 #import "third_party/ocmock/OCMock/OCMock.h"
15 #import "third_party/ocmock/gtest_support.h"
17 namespace {
19 class LocationManagerTest : public PlatformTest {
20  public:
21   LocationManagerTest() {}
23  protected:
24   void SetUp() override {
25     PlatformTest::SetUp();
27     mock_geolocation_updater_.reset(
28         [[OCMockObject mockForProtocol:@protocol(GeolocationUpdater)] retain]);
30     // Set up LocationManager with a mock GeolocationUpdater.
31     location_manager_.reset([[LocationManager alloc] init]);
32     [location_manager_ setGeolocationUpdater:mock_geolocation_updater_.get()];
33   }
35   void TearDown() override {
36     [location_manager_ setGeolocationUpdater:nil];
38     PlatformTest::TearDown();
39   }
41   base::scoped_nsobject<id> mock_geolocation_updater_;
42   base::scoped_nsobject<LocationManager> location_manager_;
45 // Verifies that -[LocationManager startUpdatingLocation] calls
46 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater does not yet have
47 // a current location.
48 TEST_F(LocationManagerTest, StartUpdatingLocationNilCurrentLocation) {
49   [[[mock_geolocation_updater_ expect] andReturn:nil] currentLocation];
51   // Also expect the call to -[GeolocationUpdater isEnabled];
52   BOOL no = NO;
53   [[[mock_geolocation_updater_ expect]
54       andReturnValue:OCMOCK_VALUE(no)] isEnabled];
55   [[mock_geolocation_updater_ expect] requestWhenInUseAuthorization];
56   [[mock_geolocation_updater_ expect] setEnabled:YES];
58   [location_manager_ startUpdatingLocation];
59   EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get());
62 // Verifies that -[LocationManager startUpdatingLocation] calls
63 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater
64 // |currentLocation| is stale.
65 TEST_F(LocationManagerTest, StartUpdatingLocationStaleCurrentLocation) {
66   // Set up to return a stale mock CLLocation from -[GeolocationUpdater
67   // currentLocation].
68   base::scoped_nsobject<id> mock_location(
69       [[OCMockObject mockForClass:[CLLocation class]] retain]);
70   BOOL yes = YES;
71   [[[mock_location expect] andReturnValue:OCMOCK_VALUE(yes)] cr_shouldRefresh];
73   [[[mock_geolocation_updater_ expect]
74       andReturn:mock_location.get()] currentLocation];
76   // Also expect the call to -[GeolocationUpdater isEnabled];
77   BOOL no = NO;
78   [[[mock_geolocation_updater_ expect]
79       andReturnValue:OCMOCK_VALUE(no)] isEnabled];
80   [[mock_geolocation_updater_ expect] requestWhenInUseAuthorization];
81   [[mock_geolocation_updater_ expect] setEnabled:YES];
83   [location_manager_ startUpdatingLocation];
84   EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get());
85   EXPECT_OCMOCK_VERIFY(mock_location.get());
88 // Verifies that -[LocationManager startUpdatingLocation] does not call
89 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater's
90 // |currentLocation| is fresh.
91 TEST_F(LocationManagerTest, StartUpdatingLocationFreshCurrentLocation) {
92   // Set up to return a fresh mock CLLocation from -[GeolocationUpdater
93   // currentLocation].
94   base::scoped_nsobject<id> mock_location(
95       [[OCMockObject mockForClass:[CLLocation class]] retain]);
96   BOOL no = NO;
97   [[[mock_location expect] andReturnValue:OCMOCK_VALUE(no)] cr_shouldRefresh];
99   [[[mock_geolocation_updater_ expect]
100       andReturn:mock_location.get()] currentLocation];
102   [location_manager_ startUpdatingLocation];
103   EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get());
104   EXPECT_OCMOCK_VERIFY(mock_location.get());
107 }  // namespace