OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/mac/scoped_nsobject.h" | |
6 #import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h" | |
7 #import "ios/chrome/browser/geolocation/location_manager+Testing.h" | |
8 #import "ios/chrome/browser/geolocation/location_manager.h" | |
sdefresne
2015/04/28 08:34:39
nit: shouldn't this #import be above the others (s
| |
9 #import "ios/public/provider/chrome/browser/geolocation_updater_provider.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "testing/gtest_mac.h" | |
12 #include "testing/platform_test.h" | |
13 #import "third_party/ocmock/OCMock/OCMock.h" | |
14 #import "third_party/ocmock/gtest_support.h" | |
15 | |
16 namespace { | |
17 | |
18 class LocationManagerTest : public PlatformTest { | |
19 public: | |
20 LocationManagerTest() {} | |
21 | |
22 protected: | |
23 void SetUp() override { | |
24 PlatformTest::SetUp(); | |
25 | |
26 mock_geolocation_updater_.reset( | |
27 [[OCMockObject mockForProtocol:@protocol(GeolocationUpdater)] retain]); | |
28 | |
29 // Set up LocationManager with a mock GeolocationUpdater. | |
30 location_manager_.reset([[LocationManager alloc] init]); | |
31 [location_manager_ setGeolocationUpdater:mock_geolocation_updater_.get()]; | |
32 } | |
33 | |
34 void TearDown() override { | |
35 [location_manager_ setGeolocationUpdater:nil]; | |
36 | |
37 PlatformTest::TearDown(); | |
38 } | |
39 | |
40 base::scoped_nsobject<id> mock_geolocation_updater_; | |
41 base::scoped_nsobject<LocationManager> location_manager_; | |
42 }; | |
43 | |
44 // Verifies that -[LocationManager startUpdatingLocation] calls | |
45 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater does not yet have | |
46 // a current location. | |
47 TEST_F(LocationManagerTest, StartUpdatingLocationNilCurrentLocation) { | |
48 [[[mock_geolocation_updater_ expect] andReturn:nil] currentLocation]; | |
49 | |
50 // Also expect the call to -[GeolocationUpdater isEnabled]; | |
51 BOOL no = NO; | |
52 [[[mock_geolocation_updater_ expect] | |
53 andReturnValue:OCMOCK_VALUE(no)] isEnabled]; | |
54 [[mock_geolocation_updater_ expect] requestWhenInUseAuthorization]; | |
55 [[mock_geolocation_updater_ expect] setEnabled:YES]; | |
56 | |
57 [location_manager_ startUpdatingLocation]; | |
58 EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get()); | |
59 } | |
60 | |
61 // Verifies that -[LocationManager startUpdatingLocation] calls | |
62 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater | |
63 // |currentLocation| is stale. | |
64 TEST_F(LocationManagerTest, StartUpdatingLocationStaleCurrentLocation) { | |
65 // Set up to return a stale mock CLLocation from -[GeolocationUpdater | |
66 // currentLocation]. | |
67 base::scoped_nsobject<id> mock_location( | |
68 [[OCMockObject mockForClass:[CLLocation class]] retain]); | |
69 BOOL yes = YES; | |
70 [[[mock_location expect] andReturnValue:OCMOCK_VALUE(yes)] cr_shouldRefresh]; | |
71 | |
72 [[[mock_geolocation_updater_ expect] | |
73 andReturn:mock_location.get()] currentLocation]; | |
74 | |
75 // Also expect the call to -[GeolocationUpdater isEnabled]; | |
76 BOOL no = NO; | |
77 [[[mock_geolocation_updater_ expect] | |
78 andReturnValue:OCMOCK_VALUE(no)] isEnabled]; | |
79 [[mock_geolocation_updater_ expect] requestWhenInUseAuthorization]; | |
80 [[mock_geolocation_updater_ expect] setEnabled:YES]; | |
81 | |
82 [location_manager_ startUpdatingLocation]; | |
83 EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get()); | |
84 EXPECT_OCMOCK_VERIFY(mock_location.get()); | |
85 } | |
86 | |
87 // Verifies that -[LocationManager startUpdatingLocation] does not call | |
88 // -[GeolocationUpdater setEnabled:] when GeolocationUpdater's | |
89 // |currentLocation| is fresh. | |
90 TEST_F(LocationManagerTest, StartUpdatingLocationFreshCurrentLocation) { | |
91 // Set up to return a fresh mock CLLocation from -[GeolocationUpdater | |
92 // currentLocation]. | |
93 base::scoped_nsobject<id> mock_location( | |
94 [[OCMockObject mockForClass:[CLLocation class]] retain]); | |
95 BOOL no = NO; | |
96 [[[mock_location expect] andReturnValue:OCMOCK_VALUE(no)] cr_shouldRefresh]; | |
97 | |
98 [[[mock_geolocation_updater_ expect] | |
99 andReturn:mock_location.get()] currentLocation]; | |
100 | |
101 [location_manager_ startUpdatingLocation]; | |
102 EXPECT_OCMOCK_VERIFY(mock_geolocation_updater_.get()); | |
103 EXPECT_OCMOCK_VERIFY(mock_location.get()); | |
104 } | |
105 | |
106 } // namespace | |
OLD | NEW |