Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: device/geolocation/android/junit/src/org/chromium/device/geolocation/LocationProviderTest.java

Issue 2832983003: Revert of GeoLocation: add support for GmsCore location provider (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/geolocation/android/javatests/src/org/chromium/device/geolocation/MockLocationProvider.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 package org.chromium.device.geolocation;
6
7 import static org.junit.Assert.assertFalse;
8 import static org.junit.Assert.assertNotNull;
9 import static org.junit.Assert.assertTrue;
10 import static org.mockito.Mockito.doAnswer;
11
12 import android.content.Context;
13 import android.location.LocationManager;
14
15 import com.google.android.gms.common.api.GoogleApiClient;
16 import com.google.android.gms.location.FusedLocationProviderApi;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.robolectric.ParameterizedRobolectricTestRunner;
27 import org.robolectric.ParameterizedRobolectricTestRunner.Parameters;
28 import org.robolectric.Shadows;
29 import org.robolectric.annotation.Config;
30 import org.robolectric.internal.Shadow;
31 import org.robolectric.shadows.ShadowLocationManager;
32 import org.robolectric.shadows.ShadowLog; // remove me ?
33
34 import org.chromium.base.ThreadUtils;
35 import org.chromium.base.test.util.Feature;
36
37 import java.util.Arrays;
38 import java.util.Collection;
39
40 /**
41 * Test suite for Java Geolocation.
42 */
43 @RunWith(ParameterizedRobolectricTestRunner.class)
44 @Config(sdk = 21, manifest = Config.NONE)
45 public class LocationProviderTest {
46 public static enum LocationProviderType { MOCK, ANDROID, GMS_CORE }
47
48 @Parameters
49 public static Collection<Object[]> data() {
50 return Arrays.asList(new Object[][] {{LocationProviderType.MOCK},
51 {LocationProviderType.ANDROID}, {LocationProviderType.GMS_CORE}} );
52 }
53
54 @Mock
55 private Context mContext;
56
57 // Member variables for LocationProviderType.GMS_CORE case.
58 @Mock
59 private GoogleApiClient mGoogleApiClient;
60 private boolean mGoogleApiClientIsConnected;
61
62 // Member variables for LocationProviderType.ANDROID case.
63 private LocationManager mLocationManager;
64 private ShadowLocationManager mShadowLocationManager;
65
66 private LocationProviderAdapter mLocationProviderAdapter;
67
68 private final LocationProviderType mApi;
69
70 public LocationProviderTest(LocationProviderType api) {
71 mApi = api;
72 }
73
74 @Before
75 public void setUp() {
76 ShadowLog.stream = System.out;
77 MockitoAnnotations.initMocks(this);
78
79 mContext = Mockito.mock(Context.class);
80 }
81
82 /**
83 * Verify a normal start/stop call pair with the given LocationProvider.
84 */
85 @Test
86 @Feature({"Location"})
87 public void testStartStop() {
88 assertTrue("Should be on UI thread", ThreadUtils.runningOnUiThread());
89
90 setLocationProvider();
91
92 createLocationProviderAdapter();
93 startLocationProviderAdapter(false);
94 stopLocationProviderAdapter();
95 }
96
97 /**
98 * Verify a start/upgrade/stop call sequencewith the given LocationProvider.
99 */
100 @Test
101 @Feature({"Location"})
102 public void testStartUpgradeStop() {
103 assertTrue("Should be on UI thread", ThreadUtils.runningOnUiThread());
104
105 setLocationProvider();
106
107 createLocationProviderAdapter();
108 startLocationProviderAdapter(false);
109 startLocationProviderAdapter(true);
110 stopLocationProviderAdapter();
111 }
112
113 private void createLocationProviderAdapter() {
114 mLocationProviderAdapter = LocationProviderAdapter.create(mContext);
115 assertNotNull("LocationProvider", mLocationProviderAdapter);
116 }
117
118 private void setLocationProvider() {
119 if (mApi == LocationProviderType.MOCK) {
120 setLocationProviderMock();
121 } else if (mApi == LocationProviderType.ANDROID) {
122 setLocationProviderAndroid();
123 } else if (mApi == LocationProviderType.GMS_CORE) {
124 setLocationProviderGmsCore();
125 } else {
126 assert false;
127 }
128 }
129
130 private void setLocationProviderMock() {
131 LocationProviderFactory.setLocationProviderImpl(new MockLocationProvider ());
132 }
133
134 private void setLocationProviderAndroid() {
135 LocationProviderAndroid locationProviderAndroid = new LocationProviderAn droid(mContext);
136
137 // Robolectric has a ShadowLocationManager class that mocks the behaviou r of the real
138 // class very closely. Use it here.
139 mLocationManager = Shadow.newInstanceOf(LocationManager.class);
140 mShadowLocationManager = Shadows.shadowOf(mLocationManager);
141 locationProviderAndroid.setLocationManagerForTesting(mLocationManager);
142 LocationProviderFactory.setLocationProviderImpl(locationProviderAndroid) ;
143 }
144
145 private void setLocationProviderGmsCore() {
146 // Robolectric has a ShadowGoogleApiClientBuilder class that mocks the b ehaviour of the real
147 // class very closely, but it's not available in our build
148 mGoogleApiClient = Mockito.mock(GoogleApiClient.class);
149 mGoogleApiClientIsConnected = false;
150 doAnswer(new Answer<Boolean>() {
151 public Boolean answer(InvocationOnMock invocation) {
152 return mGoogleApiClientIsConnected;
153 }
154 })
155 .when(mGoogleApiClient)
156 .isConnected();
157
158 doAnswer(new Answer<Void>() {
159 public Void answer(InvocationOnMock invocation) {
160 mGoogleApiClientIsConnected = true;
161 return null;
162 }
163 })
164 .when(mGoogleApiClient)
165 .connect();
166
167 doAnswer(new Answer<Void>() {
168 public Void answer(InvocationOnMock invocation) {
169 mGoogleApiClientIsConnected = false;
170 return null;
171 }
172 })
173 .when(mGoogleApiClient)
174 .disconnect();
175
176 FusedLocationProviderApi fusedLocationProviderApi =
177 Mockito.mock(FusedLocationProviderApi.class);
178
179 LocationProviderGmsCore locationProviderGmsCore =
180 new LocationProviderGmsCore(mGoogleApiClient, fusedLocationProvi derApi);
181
182 LocationProviderFactory.setLocationProviderImpl(locationProviderGmsCore) ;
183 }
184
185 private void startLocationProviderAdapter(boolean highResolution) {
186 mLocationProviderAdapter.start(highResolution);
187 assertTrue("Should be running", mLocationProviderAdapter.isRunning());
188 }
189
190 private void stopLocationProviderAdapter() {
191 mLocationProviderAdapter.stop();
192 assertFalse("Should have stopped", mLocationProviderAdapter.isRunning()) ;
193 }
194 }
OLDNEW
« no previous file with comments | « device/geolocation/android/javatests/src/org/chromium/device/geolocation/MockLocationProvider.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698