• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.libraries.mobiledatadownload.populator;
17 
18 import static android.Manifest.permission.ACCESS_FINE_LOCATION;
19 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.when;
22 import static org.robolectric.Shadows.shadowOf;
23 
24 import android.Manifest.permission;
25 import android.app.Application;
26 import android.location.Location;
27 import android.location.LocationManager;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnit;
34 import org.mockito.junit.MockitoRule;
35 import org.robolectric.RobolectricTestRunner;
36 
37 /** Tests for {@link LocationProviderImpl}. */
38 @RunWith(RobolectricTestRunner.class)
39 public class LocationProviderImplTest {
40 
41   @Rule public final MockitoRule mocks = MockitoJUnit.rule();
42 
43   @Mock private LocationManager locationManager;
44   private LocationProvider locationProvider;
45   private Location locationWithAccuracy;
46   private Location locationWithoutAccuracy;
47 
48   @Before
setUp()49   public void setUp() {
50     locationProvider = new LocationProviderImpl(getApplicationContext(), locationManager);
51     locationWithAccuracy = new Location("provider");
52     locationWithAccuracy.setLatitude(40.7);
53     locationWithAccuracy.setLongitude(-74.0);
54     locationWithoutAccuracy = new Location(locationWithAccuracy);
55     locationWithAccuracy.setAccuracy(10);
56   }
57 
58   @Test
getLocation_whenPermissionsDenied_returnsAbsentLocation()59   public void getLocation_whenPermissionsDenied_returnsAbsentLocation() {
60     shadowOf((Application) getApplicationContext())
61         .denyPermissions(permission.ACCESS_FINE_LOCATION, permission.ACCESS_COARSE_LOCATION);
62     assertThat(locationProvider.get()).isAbsent();
63   }
64 
65   @Test
getLocation_whenEitherAccessLocationPermissionGranted_returnsLocation()66   public void getLocation_whenEitherAccessLocationPermissionGranted_returnsLocation() {
67     when(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER))
68         .thenReturn(locationWithAccuracy);
69     shadowOf((Application) getApplicationContext())
70         .denyPermissions(permission.ACCESS_COARSE_LOCATION);
71     shadowOf((Application) getApplicationContext())
72         .grantPermissions(permission.ACCESS_FINE_LOCATION);
73 
74     assertThat(locationProvider.get()).hasValue(locationWithAccuracy);
75 
76     shadowOf((Application) getApplicationContext())
77         .denyPermissions(permission.ACCESS_FINE_LOCATION);
78     shadowOf((Application) getApplicationContext())
79         .grantPermissions(permission.ACCESS_COARSE_LOCATION);
80 
81     assertThat(locationProvider.get()).hasValue(locationWithAccuracy);
82 
83     shadowOf((Application) getApplicationContext())
84         .grantPermissions(permission.ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION);
85     assertThat(locationProvider.get()).hasValue(locationWithAccuracy);
86   }
87 
88   @Test
getLocation_whenNetworkLocationHasNoAccuracy_returnsGpsLocation()89   public void getLocation_whenNetworkLocationHasNoAccuracy_returnsGpsLocation() {
90     shadowOf((Application) getApplicationContext())
91         .grantPermissions(permission.ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION);
92 
93     when(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER))
94         .thenReturn(locationWithoutAccuracy);
95     when(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER))
96         .thenReturn(locationWithAccuracy);
97     assertThat(locationProvider.get()).hasValue(locationWithAccuracy);
98   }
99 
100   @Test
getLocation_whenBothLocationsHaveNoAccuracy_returnsAbsentLocation()101   public void getLocation_whenBothLocationsHaveNoAccuracy_returnsAbsentLocation() {
102     shadowOf((Application) getApplicationContext())
103         .grantPermissions(permission.ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION);
104 
105     when(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER))
106         .thenReturn(locationWithoutAccuracy);
107     when(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER))
108         .thenReturn(locationWithoutAccuracy);
109     assertThat(locationProvider.get()).isAbsent();
110   }
111 }
112