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

LocationBasedCountryDetectorTest

public class LocationBasedCountryDetectorTest extends android.test.AndroidTestCase

Fields Summary
private static final List
sEnabledProviders
Constructors Summary
Methods Summary
public voidtestFindingCountry()

        testFindingCountryCommon(null);
    
public voidtestFindingCountryCancelled()

        final String country = "us";
        final String provider = "Good";
        CountryListenerImpl countryListener = new CountryListenerImpl();
        TestCountryDetector detector = new TestCountryDetector(country, provider);
        detector.setCountryListener(countryListener);
        detector.detectCountry();
        assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        detector.notifyLocationFound();
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        // The time should be stopped
        assertNull(detector.getTimer());
        Thread queryThread = waitForQueryThreadLaunched(detector);
        detector.stop();
        // There is no way to stop the thread, let's test it could be stopped, after get country
        detector.notifyCountryFound();
        // Wait for query thread ending
        waitForThreadEnding(queryThread);
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        assertTrue(countryListener.notified());
        assertEquals("us", countryListener.getCountry().toLowerCase());
    
private voidtestFindingCountryCommon(java.util.Set acceptableProviders)

        final String country = "us";
        final String provider = "Good";
        CountryListenerImpl countryListener = new CountryListenerImpl();
        TestCountryDetector detector = new TestCountryDetector(country, provider);

        if (acceptableProviders != null) {
            detector.setAcceptableProvider(acceptableProviders);
        }

        detector.setCountryListener(countryListener);
        detector.detectCountry();

        if (acceptableProviders != null) {
            assertEquals(acceptableProviders.size(), detector.getListenersCount());
            Map<String, LocationListener> listeners = detector.getListeners();
            for (String acceptableProvider : acceptableProviders) {
                assertTrue(listeners.containsKey(acceptableProvider));
            }
        } else {
            assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        }

        detector.notifyLocationFound();
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        assertNull(detector.getTimer());
        Thread queryThread = waitForQueryThreadLaunched(detector);
        detector.notifyCountryFound();
        // Wait for query thread ending
        waitForThreadEnding(queryThread);
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        assertTrue(countryListener.notified());
        assertEquals("us", countryListener.getCountry().toLowerCase());
    
public voidtestFindingCountryFailed()

        final String country = "us";
        final String provider = "Good";
        TestCountryDetector detector = new TestCountryDetector(country, provider) {
            @Override
            protected String getCountryFromLocation(Location location) {
                synchronized (countryFoundLocker) {
                    if (! notifyCountry) {
                        try {
                            countryFoundLocker.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                }
                // We didn't find country.
                return null;
            }
        };
        CountryListenerImpl countryListener = new CountryListenerImpl();
        detector.setCountryListener(countryListener);
        detector.detectCountry();
        assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        detector.notifyLocationFound();
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        assertNull(detector.getTimer());
        Thread queryThread = waitForQueryThreadLaunched(detector);
        detector.notifyCountryFound();
        // Wait for query thread ending
        waitForThreadEnding(queryThread);
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        // CountryListener should be notified
        assertTrue(countryListener.notified());
        assertNull(countryListener.getCountry());
    
public voidtestFindingCountryWithAcceptableProvider()

        testFindingCountryCommon(new HashSet<String>(Arrays.asList("passive")));
    
public voidtestFindingCountryWithLastKnownLocation()

        final String country = "us";
        final String provider = "Good";
        long timeout = 1000;
        TestCountryDetector detector = new TestCountryDetector(country, provider, timeout);
        CountryListenerImpl countryListener = new CountryListenerImpl();
        detector.setCountryListener(countryListener);
        detector.detectCountry();
        assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        waitForTimerReset(detector);
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        Thread queryThread = waitForQueryThreadLaunched(detector);
        detector.notifyCountryFound();
        // Wait for query thread ending
        waitForThreadEnding(queryThread);
        // QueryThread should be set to NULL
        assertNull(detector.getQueryThread());
        // CountryListener should be notified
        assertTrue(countryListener.notified());
        assertEquals("us", countryListener.getCountry().toLowerCase());
    
public voidtestFindingLocationCancelled()

        final String country = "us";
        final String provider = "Good";
        CountryListenerImpl countryListener = new CountryListenerImpl();
        TestCountryDetector detector = new TestCountryDetector(country, provider);
        detector.setCountryListener(countryListener);
        detector.detectCountry();
        assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        detector.stop();
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        // The time should be stopped
        assertNull(detector.getTimer());
        // QueryThread should still be NULL
        assertNull(detector.getQueryThread());
        assertFalse(countryListener.notified());
    
public voidtestFindingLocationFailed()

        final String country = "us";
        final String provider = "Good";
        long timeout = 1000;
        TestCountryDetector detector = new TestCountryDetector(country, provider, timeout) {
            @Override
            protected Location getLastKnownLocation() {
                return null;
            }
        };
        CountryListenerImpl countryListener = new CountryListenerImpl();
        detector.setCountryListener(countryListener);
        detector.detectCountry();
        assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
        waitForTimerReset(detector);
        // All listeners should be unregistered
        assertEquals(0, detector.getListenersCount());
        // QueryThread should still be NULL
        assertNull(detector.getQueryThread());
        assertTrue(countryListener.notified());
        assertNull(countryListener.getCountry());
    
private java.lang.ThreadwaitForQueryThreadLaunched(com.android.server.location.LocationBasedCountryDetectorTest$TestCountryDetector detector)

        int count = 5;
        long interval = 1000;
        try {
            while (count-- > 0 && detector.getQueryThread() == null) {
                Thread.sleep(interval);
            }
        } catch (InterruptedException e) {
        }
        Thread thread = detector.getQueryThread();
        assertTrue(thread != null);
        return thread;
    
private voidwaitForThreadEnding(java.lang.Thread thread)

        try {
            thread.join(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    
private voidwaitForTimerReset(com.android.server.location.LocationBasedCountryDetectorTest$TestCountryDetector detector)

        int count = 5;
        long interval = 1000;
        try {
            while (count-- > 0 && detector.getTimer() != null) {
                Thread.sleep(interval);
            }
        } catch (InterruptedException e) {
        }
        Timer timer = detector.getTimer();
        assertTrue(timer == null);