summaryrefslogtreecommitdiffstats
path: root/tests/CoreTests/android/location/LocationManagerProximityTest.java
blob: f2bff4d19b970aa3a069b14b3ce7f38b3414385a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.location;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.Log;

/**
 * Tests for LocationManager.addProximityAlert
 * 
 * TODO: add tests for more scenarios
 * 
 * To run:
 *  adb shell am instrument -e class com.google.android.mapstests.api.LocationProximityTest \
 *     -w com.google.android.mapstests/.MapInstrumentationTestRunner
 * 
 */
@MediumTest
public class LocationManagerProximityTest extends AndroidTestCase {

    private static final int UPDATE_LOCATION_WAIT_TIME = 1000;
    private static final int PROXIMITY_WAIT_TIME = 2000;

    private LocationManager mLocationManager;
    private PendingIntent mPendingIntent;
    private TestIntentReceiver mIntentReceiver;
    private String mOriginalAllowedProviders;

    private static final String LOG_TAG = "LocationProximityTest";

    // use network provider as mock location provider, because:
    //  - proximity alert is hardcoded to listen to only network or gps
    //  - 'network' provider is not installed in emulator, so can mock it 
    //    using test provider APIs
    private static final String PROVIDER_NAME = LocationManager.NETWORK_PROVIDER;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mOriginalAllowedProviders = 
            android.provider.Settings.Secure.getString(
                    getContext().getContentResolver(), 
                    android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        
        // ensure 'only' the mock provider is enabled
        // need to do this so the proximity listener does not ignore the mock 
        // updates in favor of gps updates
        android.provider.Settings.Secure.putString(
                getContext().getContentResolver(), 
                android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED, 
                PROVIDER_NAME);

        mLocationManager = (LocationManager) getContext().
                getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.getProvider(PROVIDER_NAME) != null) {
            mLocationManager.removeTestProvider(PROVIDER_NAME);
        }
        
        mLocationManager.addTestProvider(PROVIDER_NAME, true, //requiresNetwork,
                false, // requiresSatellite,
                true, // requiresCell,
                false, // hasMonetaryCost,
                false, // supportsAltitude,
                false, // supportsSpeed, s
                false, // upportsBearing,
                Criteria.POWER_MEDIUM, // powerRequirement
                Criteria.ACCURACY_FINE); // accuracy
    }

    @Override
    protected void tearDown() throws Exception {
        mLocationManager.removeTestProvider(PROVIDER_NAME);

        if (mPendingIntent != null) {
            mLocationManager.removeProximityAlert(mPendingIntent);
        }
        if (mIntentReceiver != null) {
            getContext().unregisterReceiver(mIntentReceiver);
        }
        
        if (mOriginalAllowedProviders != null) {
            // restore original settings
            android.provider.Settings.Secure.putString(
                    getContext().getContentResolver(), 
                    android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED, 
                    mOriginalAllowedProviders);
            mLocationManager.updateProviders();
        }    
    }

    /**
     * Tests basic proximity alert when entering proximity
     */
    public void testEnterProximity() throws Exception {
        doTestEnterProximity(10000);
    }

    /**
     * Tests proximity alert when entering proximity, with no expiration 
     */
    public void testEnterProximity_noexpire() throws Exception {
        doTestEnterProximity(-1);
    }

    /**
     * Helper variant for testing enter proximity scenario 
     * TODO: add additional parameters as more scenarios are added
     * 
     * @param expiration - expiry of proximity alert 
     */
    private void doTestEnterProximity(long expiration) throws Exception {
        // update location to outside proximity range
        synchronousSendLocation(30, 30);
        registerProximityListener(0, 0, 1000, expiration);
        sendLocation(0, 0);
        waitForAlert();
        assertProximityType(true);
    }

    /**
     * Tests basic proximity alert when exiting proximity
     */
    public void testExitProximity() throws Exception {
        // first do enter proximity scenario
        doTestEnterProximity(-1);

        // now update to trigger exit proximity proximity
        mIntentReceiver.clearReceivedIntents();
        sendLocation(20, 20);
        waitForAlert();
        assertProximityType(false);
    }

    /**
     * Registers the proximity intent receiver
     */
    private void registerProximityListener(double latitude, double longitude, 
            float radius, long expiration) {
        String intentKey = "testProximity";
        Intent proximityIntent = new Intent(intentKey);
        mPendingIntent = PendingIntent.getBroadcast(getContext(), 0, 
                proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        mIntentReceiver = new TestIntentReceiver(intentKey);

        mLocationManager.addProximityAlert(latitude, longitude, radius, 
                expiration, mPendingIntent);

        getContext().registerReceiver(mIntentReceiver, 
                mIntentReceiver.getFilter());

    }

    /**
     * Blocks until proximity intent notification is received
     * @throws InterruptedException
     */
    private void waitForAlert() throws InterruptedException {
        Log.d(LOG_TAG, "Waiting for proximity update");
        synchronized (mIntentReceiver) {
            mIntentReceiver.wait(PROXIMITY_WAIT_TIME);
        }

        assertNotNull("Did not receive proximity alert", 
                mIntentReceiver.getLastReceivedIntent());
    }

    /**
     * Asserts that the received intent had the enter proximity property set as
     * expected
     * @param expectedEnterProximity - true if enter proximity expected, false if
     *   exit expected
     */
    private void assertProximityType(boolean expectedEnterProximity) 
            throws Exception {
        boolean proximityTest = mIntentReceiver.getLastReceivedIntent().
                getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, 
                !expectedEnterProximity);
        assertEquals("proximity alert not set to expected enter proximity value",
                expectedEnterProximity, proximityTest);
    }

    /**
     * Synchronous variant of sendLocation
     */
    private void synchronousSendLocation(final double latitude, 
            final double longitude)
            throws InterruptedException {
        sendLocation(latitude, longitude, this);
        // wait for location to be set
        synchronized (this) {
            wait(UPDATE_LOCATION_WAIT_TIME);
        }
    }

    /**
     * Asynchronously update the mock location provider without notification
     */
    private void sendLocation(final double latitude, final double longitude) {
        sendLocation(latitude, longitude, null);
    }

    /**
     * Asynchronously update the mock location provider with given latitude and
     * longitude
     * 
     * @param latitude - update location
     * @param longitude - update location
     * @param observer - optionally, object to notify when update is sent.If
     *            null, no update will be sent
     */
    private void sendLocation(final double latitude, final double longitude, 
            final Object observer) {
        Thread locationUpdater = new Thread() {
            @Override
            public void run() {
                Location loc = new Location(PROVIDER_NAME);
                loc.setLatitude(latitude);
                loc.setLongitude(longitude);

                loc.setTime(java.lang.System.currentTimeMillis());
                Log.d(LOG_TAG, "Sending update for " + PROVIDER_NAME);
                mLocationManager.setTestProviderLocation(PROVIDER_NAME, loc);
                if (observer != null) {
                    synchronized (observer) {
                        observer.notify();
                    }
                }
            }
        };
        locationUpdater.start();

    }

    /**
     * Helper class that receives a proximity intent and notifies the main class
     * when received
     */
    private static class TestIntentReceiver extends BroadcastReceiver {

        private String mExpectedAction;
        private Intent mLastReceivedIntent;

        public TestIntentReceiver(String expectedAction) {
            mExpectedAction = expectedAction;
            mLastReceivedIntent = null;
        }

        public IntentFilter getFilter() {
            IntentFilter filter = new IntentFilter(mExpectedAction);
            return filter;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && mExpectedAction.equals(intent.getAction())) {
                Log.d(LOG_TAG, "Intent Received: " + intent.toString());
                mLastReceivedIntent = intent;
                synchronized (this) {
                    notify();
                }
            }
        }

        public Intent getLastReceivedIntent() {
            return mLastReceivedIntent;
        }
        
        public void clearReceivedIntents() {
            mLastReceivedIntent = null;
        }
    }
}