1 /*
2  * Copyright (C) 2011 The Android Open Source Project
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.android.emulator.smoketests.gps;
17 
18 import android.content.Context;
19 import android.location.Location;
20 import android.location.LocationListener;
21 import android.location.LocationManager;
22 import android.os.Bundle;
23 import android.os.HandlerThread;
24 import android.test.AndroidTestCase;
25 
26 import junit.framework.Assert;
27 
28 /**
29  * GPS Location Test
30  *
31  * Test the GPS API by verifying the previously set location
32  */
33 public class GpsLocationTest extends AndroidTestCase implements LocationListener {
34 
35     private LocationManager locationManager;
36     private Location mLocation;
37     /**
38      * Prior to running this test the GPS location must be set to the following
39      * longitude and latitude coordinates via the geo fix command
40      */
41     private static final double LONGITUDE = -122.08345770835876;
42     private static final double LATITUDE = 37.41991859119417;
43     private static final int TIMEOUT = 5000;
44 
45     @Override
setUp()46     protected void setUp() throws Exception {
47         locationManager = (LocationManager) getContext().
48             getSystemService(Context.LOCATION_SERVICE);
49         assertNotNull(locationManager);
50     }
51 
52     /**
53      * verify that the last location equals to the location set
54      * via geo fix command
55      */
testCurrentLocationGivenLocation()56     public void testCurrentLocationGivenLocation(){
57         try{
58             synchronized ( this ){
59                 HandlerThread handlerThread = new HandlerThread("testLocationUpdates");
60                 handlerThread.start();
61                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this,
62                         handlerThread.getLooper());
63                 this.wait(TIMEOUT);
64             }
65         }catch ( InterruptedException ie){
66             ie.printStackTrace();
67             Assert.fail();
68         }
69         assertNotNull(mLocation);
70         assertEquals(new Float(LONGITUDE), new Float(mLocation.getLongitude()));
71         assertEquals(new Float(LATITUDE), new Float(mLocation.getLatitude()));
72         locationManager.removeUpdates(this);
73     }
74 
onLocationChanged(Location location)75     public void onLocationChanged(Location location) {
76         synchronized ( this ){
77             mLocation=location;
78             this.notify();
79         }
80     }
81 
onProviderDisabled(String arg0)82     public void onProviderDisabled(String arg0) {
83     }
84 
onProviderEnabled(String arg0)85     public void onProviderEnabled(String arg0) {
86     }
87 
onStatusChanged(String arg0, int arg1, Bundle arg2)88     public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
89     }
90 }
91