1/*
2 * Copyright (C) 2006 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
17package android.location;
18
19import android.content.Context;
20import android.location.Criteria;
21import android.location.Location;
22import android.location.LocationManager;
23import android.location.LocationProvider;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.Suppress;
26import android.util.Log;
27
28@Suppress
29public class LocationManagerTest extends AndroidTestCase {
30    private static final String LOG_TAG = "LocationManagerTest";
31
32    private LocationManager manager;
33
34    @Override
35    public void setUp() throws Exception {
36        super.setUp();
37        manager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
38        assertNotNull(manager);
39    }
40
41    public void testGetBogusProvider() {
42        LocationProvider p = manager.getProvider("bogus");
43        assertNull(p);
44    }
45
46    public void testGetNetworkProvider() {
47        LocationProvider p = manager.getProvider("network");
48        assertNotNull(p);
49    }
50
51    public void testGetGpsProvider() {
52        LocationProvider p = manager.getProvider("gps");
53        assertNotNull(p);
54    }
55
56    public void testGetBestProviderEmptyCriteria() {
57        String p = manager.getBestProvider(new Criteria(), true);
58        assertNotNull(p);
59    }
60
61    public void testGetBestProviderPowerCriteria() {
62        Criteria c = new Criteria();
63        c.setPowerRequirement(Criteria.POWER_HIGH);
64        String p = manager.getBestProvider(c, true);
65        assertNotNull(p);
66
67        c.setPowerRequirement(Criteria.POWER_MEDIUM);
68        p = manager.getBestProvider(c, true);
69        assertNotNull(p);
70
71        c.setPowerRequirement(Criteria.POWER_LOW);
72        p = manager.getBestProvider(c, true);
73        assertNotNull(p);
74
75        c.setPowerRequirement(Criteria.NO_REQUIREMENT);
76        p = manager.getBestProvider(c, true);
77        assertNotNull(p);
78    }
79
80    public void testGpsTracklog() {
81        LocationProvider p = manager.getProvider("gps");
82        assertNotNull(p);
83
84        // TODO: test requestUpdates method
85    }
86
87    public void testLocationConversions() {
88        String loc1 = Location.convert(-80.075, Location.FORMAT_DEGREES);
89        Log.i(LOG_TAG, "Input = " + (-80.075) + ", output = " + loc1);
90        assertEquals("-80.075", loc1);
91
92        String loc1b = Location.convert(-80.0, Location.FORMAT_DEGREES);
93        Log.i(LOG_TAG, "Input = " + (-80.0) + ", output = " + loc1b);
94        assertEquals("-80", loc1b);
95
96        String loc2 = Location.convert(-80.085, Location.FORMAT_DEGREES);
97        Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc2);
98        assertEquals("-80.085", loc2);
99
100        String loc3 = Location.convert(-80.085, Location.FORMAT_MINUTES);
101        Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc3);
102        assertEquals("-80:5.1", loc3);
103
104        String loc4 = Location.convert(-80.085, Location.FORMAT_SECONDS);
105        Log.i(LOG_TAG, "Input = " + (-80.085) + ", output = " + loc4);
106        assertEquals("-80:5:6", loc4);
107
108        String loc5 = Location.convert(5 + 0.5f / 60.0f, Location.FORMAT_MINUTES);
109        Log.i(LOG_TAG, "Input = 5:0.5, output = " + loc5);
110        int index = loc5.indexOf(':');
111        String loc5a = loc5.substring(0, index);
112        Log.i(LOG_TAG, "loc5a = " + loc5a);
113        assertTrue(loc5a.equals("5"));
114        String loc5b = loc5.substring(index + 1);
115        Log.i(LOG_TAG, "loc5b = " + loc5b);
116        double minutes = Double.parseDouble(loc5b);
117        Log.i(LOG_TAG, "minutes = " + minutes);
118        assertTrue(Math.abs(minutes - 0.5) < 0.0001);
119
120        String loc6 = Location.convert(0.1, Location.FORMAT_DEGREES);
121        Log.i(LOG_TAG, "loc6 = " + loc6);
122        assertEquals(loc6, "0.1");
123
124        String loc7 = Location.convert(0.1, Location.FORMAT_MINUTES);
125        Log.i(LOG_TAG, "loc7 = " + loc7);
126        assertEquals(loc7, "0:6");
127
128        String loc8 = Location.convert(0.1, Location.FORMAT_SECONDS);
129        Log.i(LOG_TAG, "loc8 = " + loc8);
130        assertEquals(loc8, "0:6:0");
131    }
132}
133