Methods Summary |
---|
public void | testFindingCountry()
testFindingCountryCommon(null);
|
public void | testFindingCountryCancelled()
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 void | testFindingCountryCommon(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 void | testFindingCountryFailed()
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 void | testFindingCountryWithAcceptableProvider()
testFindingCountryCommon(new HashSet<String>(Arrays.asList("passive")));
|
public void | testFindingCountryWithLastKnownLocation()
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 void | testFindingLocationCancelled()
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 void | testFindingLocationFailed()
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.Thread | waitForQueryThreadLaunched(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 void | waitForThreadEnding(java.lang.Thread thread)
try {
thread.join(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
|
private void | waitForTimerReset(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);
|