FileDocCategorySizeDatePackage
ComprehensiveCountryDetectorTest.javaAPI DocAndroid 5.1 API11748Thu Mar 12 22:22:42 GMT 2015com.android.server.location

ComprehensiveCountryDetectorTest

public class ComprehensiveCountryDetectorTest extends android.test.AndroidTestCase

Fields Summary
Constructors Summary
Methods Summary
private booleansameCountry(android.location.Country country1, android.location.Country country2)

        return country1 == null && country2 == null || country1 != null && country2 != null &&
        country1.getCountryIso().equalsIgnoreCase(country2.getCountryIso()) &&
        country1.getSource() == country2.getSource();
    
public voidtestAddRemoveListener()

        TestCountryDetector countryDetector = new TestCountryDetector();
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        assertTrue(countryDetector.isPhoneStateListenerAdded());
        assertTrue(countryDetector.locationBasedDetectorStarted());
        countryDetector.setCountryListener(null);
        assertFalse(countryDetector.isPhoneStateListenerAdded());
        assertTrue(countryDetector.locationBasedDetectorStopped());
    
public voidtestDetectLocationBasedCountry()

        final Country resultCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM);
        final Country locationBasedCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_LOCATION);
        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected Country getSimBasedCountry() {
                return resultCountry;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, resultCountry));
        assertTrue(countryDetector.locationBasedDetectorStarted());
        countryDetector.notifyLocationBasedListener(locationBasedCountry);
        assertTrue(listener.notified());
        assertTrue(sameCountry(listener.getCountry(), locationBasedCountry));
        assertTrue(countryDetector.locationBasedDetectorStopped());
        assertTrue(countryDetector.locationRefreshStarted());
        countryDetector.stop();
        assertTrue(countryDetector.locationRefreshCancelled());
    
public voidtestDetectNetworkBasedCountry()

        final Country resultCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_NETWORK);
        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected Country getNetworkBasedCountry() {
                return resultCountry;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, resultCountry));
        assertFalse(listener.notified());
        assertFalse(countryDetector.locationBasedDetectorStarted());
        assertFalse(countryDetector.locationRefreshStarted());
        countryDetector.stop();
    
public voidtestGeocoderNotImplemented()

        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected boolean isGeoCoderImplemented() {
                return false;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        assertTrue(countryDetector.isPhoneStateListenerAdded());
        assertFalse(countryDetector.locationBasedDetectorStarted());
        countryDetector.setCountryListener(null);
        assertFalse(countryDetector.isPhoneStateListenerAdded());
    
public voidtestLocaleBasedCountry()

        final Country resultCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_LOCALE);
        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected Country getLocaleCountry() {
                return resultCountry;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, resultCountry));
        assertFalse(listener.notified());
        assertTrue(countryDetector.locationBasedDetectorStarted());
        assertTrue(countryDetector.locationRefreshStarted());
        countryDetector.stop();
        assertTrue(countryDetector.locationRefreshCancelled());
    
public voidtestLocationBasedCountryNotFound()

        final Country resultCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM);
        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected Country getSimBasedCountry() {
                return resultCountry;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, resultCountry));
        assertTrue(countryDetector.locationBasedDetectorStarted());
        countryDetector.notifyLocationBasedListener(null);
        assertFalse(listener.notified());
        assertTrue(sameCountry(listener.getCountry(), null));
        assertTrue(countryDetector.locationBasedDetectorStopped());
        assertTrue(countryDetector.locationRefreshStarted());
        countryDetector.stop();
        assertTrue(countryDetector.locationRefreshCancelled());
    
public voidtestNoCountryFound()

        TestCountryDetector countryDetector = new TestCountryDetector();
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, null));
        assertTrue(countryDetector.locationBasedDetectorStarted());
        countryDetector.notifyLocationBasedListener(null);
        assertFalse(listener.notified());
        assertTrue(sameCountry(listener.getCountry(), null));
        assertTrue(countryDetector.locationBasedDetectorStopped());
        assertTrue(countryDetector.locationRefreshStarted());
        countryDetector.stop();
        assertTrue(countryDetector.locationRefreshCancelled());
    
public voidtestStoppingDetector()

        // Test stopping detector when LocationBasedCountryDetector was started
        final Country resultCountry = new Country(
                TestCountryDetector.COUNTRY_ISO, Country.COUNTRY_SOURCE_SIM);
        TestCountryDetector countryDetector = new TestCountryDetector() {
            @Override
            protected Country getSimBasedCountry() {
                return resultCountry;
            }
        };
        CountryListenerImpl listener = new CountryListenerImpl();
        countryDetector.setCountryListener(listener);
        Country country = countryDetector.detectCountry();
        assertTrue(sameCountry(country, resultCountry));
        assertTrue(countryDetector.locationBasedDetectorStarted());
        countryDetector.stop();
        // The LocationBasedDetector should be stopped.
        assertTrue(countryDetector.locationBasedDetectorStopped());
        // The location refresh should not running.
        assertTrue(countryDetector.locationRefreshCancelled());