1 /* 2 * Copyright (C) 2010 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 17 package com.android.server.location; 18 19 import android.location.Country; 20 import android.location.CountryListener; 21 import android.test.AndroidTestCase; 22 23 public class ComprehensiveCountryDetectorTest extends AndroidTestCase { 24 private class TestCountryDetector extends ComprehensiveCountryDetector { 25 public final static String COUNTRY_ISO = "us"; 26 private boolean mLocationBasedDetectorStarted; 27 private boolean mLocationBasedDetectorStopped; 28 protected boolean mNotified; 29 private boolean listenerAdded = false; 30 31 private Country mNotifiedCountry; 32 public TestCountryDetector() { 33 super(getContext()); 34 } 35 36 public void notifyLocationBasedListener(Country country) { 37 mNotified = true; 38 mNotifiedCountry = country; 39 mLocationBasedCountryDetector.notifyListener(country); 40 } 41 42 public boolean locationBasedDetectorStarted() { 43 return mLocationBasedCountryDetector != null && mLocationBasedDetectorStarted; 44 } 45 46 public boolean locationBasedDetectorStopped() { 47 return mLocationBasedCountryDetector == null && mLocationBasedDetectorStopped; 48 } 49 50 public boolean locationRefreshStarted() { 51 return mLocationRefreshTimer != null; 52 } 53 54 public boolean locationRefreshCancelled() { 55 return mLocationRefreshTimer == null; 56 } 57 58 @Override 59 protected CountryDetectorBase createLocationBasedCountryDetector() { 60 return new CountryDetectorBase(mContext) { 61 @Override 62 public Country detectCountry() { 63 mLocationBasedDetectorStarted = true; 64 return null; 65 } 66 67 @Override 68 public void stop() { 69 mLocationBasedDetectorStopped = true; 70 } 71 }; 72 } 73 74 @Override 75 protected Country getNetworkBasedCountry() { 76 return null; 77 } 78 79 @Override 80 protected Country getLastKnownLocationBasedCountry() { 81 return mNotifiedCountry; 82 } 83 84 @Override 85 protected Country getSimBasedCountry() { 86 return null; 87 } 88 89 @Override 90 protected Country getLocaleCountry() { 91 return null; 92 } 93 94 @Override 95 protected void runAfterDetectionAsync(final Country country, final Country detectedCountry, 96 final boolean notifyChange, final boolean startLocationBasedDetection) { 97 runAfterDetection(country, detectedCountry, notifyChange, startLocationBasedDetection); 98 }; 99 100 @Override 101 protected boolean isAirplaneModeOff() { 102 return true; 103 } 104 105 @Override 106 protected synchronized void addPhoneStateListener() { 107 listenerAdded = true; 108 } 109 110 @Override 111 protected synchronized void removePhoneStateListener() { 112 listenerAdded = false; 113 } 114 115 @Override 116 protected boolean isGeoCoderImplemented() { 117 return true; 118 } 119 120 public boolean isPhoneStateListenerAdded() { 121 return listenerAdded; 122 } 123 } 124 125 private class CountryListenerImpl implements CountryListener { 126 private boolean mNotified; 127 private Country mCountry; 128 129 public void onCountryDetected(Country country) { 130 mNotified = true; 131 mCountry = country; 132 } 133 134 public boolean notified() { 135 return mNotified; 136 } 137 138 public Country getCountry() { 139 return mCountry; 140 } 141 } 142 143 public void testDetectNetworkBasedCountry() { 144 final Country resultCountry = new Country( 145 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_NETWORK); 146 TestCountryDetector countryDetector = new TestCountryDetector() { 147 @Override 148 protected Country getNetworkBasedCountry() { 149 return resultCountry; 150 } 151 }; 152 CountryListenerImpl listener = new CountryListenerImpl(); 153 countryDetector.setCountryListener(listener); 154 Country country = countryDetector.detectCountry(); 155 assertTrue(sameCountry(country, resultCountry)); 156 assertFalse(listener.notified()); 157 assertFalse(countryDetector.locationBasedDetectorStarted()); 158 assertFalse(countryDetector.locationRefreshStarted()); 159 countryDetector.stop(); 160 } 161 162 public void testDetectLocationBasedCountry() { 163 final Country resultCountry = new Country( 164 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM); 165 final Country locationBasedCountry = new Country( 166 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_LOCATION); 167 TestCountryDetector countryDetector = new TestCountryDetector() { 168 @Override 169 protected Country getSimBasedCountry() { 170 return resultCountry; 171 } 172 }; 173 CountryListenerImpl listener = new CountryListenerImpl(); 174 countryDetector.setCountryListener(listener); 175 Country country = countryDetector.detectCountry(); 176 assertTrue(sameCountry(country, resultCountry)); 177 assertTrue(countryDetector.locationBasedDetectorStarted()); 178 countryDetector.notifyLocationBasedListener(locationBasedCountry); 179 assertTrue(listener.notified()); 180 assertTrue(sameCountry(listener.getCountry(), locationBasedCountry)); 181 assertTrue(countryDetector.locationBasedDetectorStopped()); 182 assertTrue(countryDetector.locationRefreshStarted()); 183 countryDetector.stop(); 184 assertTrue(countryDetector.locationRefreshCancelled()); 185 } 186 187 public void testLocaleBasedCountry() { 188 final Country resultCountry = new Country( 189 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_LOCALE); 190 TestCountryDetector countryDetector = new TestCountryDetector() { 191 @Override 192 protected Country getLocaleCountry() { 193 return resultCountry; 194 } 195 }; 196 CountryListenerImpl listener = new CountryListenerImpl(); 197 countryDetector.setCountryListener(listener); 198 Country country = countryDetector.detectCountry(); 199 assertTrue(sameCountry(country, resultCountry)); 200 assertFalse(listener.notified()); 201 assertTrue(countryDetector.locationBasedDetectorStarted()); 202 assertTrue(countryDetector.locationRefreshStarted()); 203 countryDetector.stop(); 204 assertTrue(countryDetector.locationRefreshCancelled()); 205 } 206 207 public void testStoppingDetector() { 208 // Test stopping detector when LocationBasedCountryDetector was started 209 final Country resultCountry = new Country( 210 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM); 211 TestCountryDetector countryDetector = new TestCountryDetector() { 212 @Override 213 protected Country getSimBasedCountry() { 214 return resultCountry; 215 } 216 }; 217 CountryListenerImpl listener = new CountryListenerImpl(); 218 countryDetector.setCountryListener(listener); 219 Country country = countryDetector.detectCountry(); 220 assertTrue(sameCountry(country, resultCountry)); 221 assertTrue(countryDetector.locationBasedDetectorStarted()); 222 countryDetector.stop(); 223 // The LocationBasedDetector should be stopped. 224 assertTrue(countryDetector.locationBasedDetectorStopped()); 225 // The location refresh should not running. 226 assertTrue(countryDetector.locationRefreshCancelled()); 227 } 228 229 public void testLocationBasedCountryNotFound() { 230 final Country resultCountry = new Country( 231 TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM); 232 TestCountryDetector countryDetector = new TestCountryDetector() { 233 @Override 234 protected Country getSimBasedCountry() { 235 return resultCountry; 236 } 237 }; 238 CountryListenerImpl listener = new CountryListenerImpl(); 239 countryDetector.setCountryListener(listener); 240 Country country = countryDetector.detectCountry(); 241 assertTrue(sameCountry(country, resultCountry)); 242 assertTrue(countryDetector.locationBasedDetectorStarted()); 243 countryDetector.notifyLocationBasedListener(null); 244 assertFalse(listener.notified()); 245 assertTrue(sameCountry(listener.getCountry(), null)); 246 assertTrue(countryDetector.locationBasedDetectorStopped()); 247 assertTrue(countryDetector.locationRefreshStarted()); 248 countryDetector.stop(); 249 assertTrue(countryDetector.locationRefreshCancelled()); 250 } 251 252 public void testNoCountryFound() { 253 TestCountryDetector countryDetector = new TestCountryDetector(); 254 CountryListenerImpl listener = new CountryListenerImpl(); 255 countryDetector.setCountryListener(listener); 256 Country country = countryDetector.detectCountry(); 257 assertTrue(sameCountry(country, null)); 258 assertTrue(countryDetector.locationBasedDetectorStarted()); 259 countryDetector.notifyLocationBasedListener(null); 260 assertFalse(listener.notified()); 261 assertTrue(sameCountry(listener.getCountry(), null)); 262 assertTrue(countryDetector.locationBasedDetectorStopped()); 263 assertTrue(countryDetector.locationRefreshStarted()); 264 countryDetector.stop(); 265 assertTrue(countryDetector.locationRefreshCancelled()); 266 } 267 268 public void testAddRemoveListener() { 269 TestCountryDetector countryDetector = new TestCountryDetector(); 270 CountryListenerImpl listener = new CountryListenerImpl(); 271 countryDetector.setCountryListener(listener); 272 assertTrue(countryDetector.isPhoneStateListenerAdded()); 273 assertTrue(countryDetector.locationBasedDetectorStarted()); 274 countryDetector.setCountryListener(null); 275 assertFalse(countryDetector.isPhoneStateListenerAdded()); 276 assertTrue(countryDetector.locationBasedDetectorStopped()); 277 } 278 279 public void testGeocoderNotImplemented() { 280 TestCountryDetector countryDetector = new TestCountryDetector() { 281 @Override 282 protected boolean isGeoCoderImplemented() { 283 return false; 284 } 285 }; 286 CountryListenerImpl listener = new CountryListenerImpl(); 287 countryDetector.setCountryListener(listener); 288 assertTrue(countryDetector.isPhoneStateListenerAdded()); 289 assertFalse(countryDetector.locationBasedDetectorStarted()); 290 countryDetector.setCountryListener(null); 291 assertFalse(countryDetector.isPhoneStateListenerAdded()); 292 } 293 294 private boolean sameCountry(Country country1, Country country2) { 295 return country1 == null && country2 == null || country1 != null && country2 != null && 296 country1.getCountryIso().equalsIgnoreCase(country2.getCountryIso()) && 297 country1.getSource() == country2.getSource(); 298 } 299 } 300