PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/services/tests/servicestests/src/com/android/server/location/LocationBasedCountryDetectorTest.java

https://bitbucket.org/Lloir/miragebase
Java | 379 lines | 303 code | 38 blank | 38 comment | 26 complexity | f4f43df1d465d8c8732e758e98a959bf MD5 | raw file
  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. package com.android.server.location;
  17. import android.location.Country;
  18. import android.location.CountryListener;
  19. import android.location.Location;
  20. import android.location.LocationListener;
  21. import android.location.LocationManager;
  22. import android.test.AndroidTestCase;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Map.Entry;
  29. import java.util.Set;
  30. import java.util.Timer;
  31. public class LocationBasedCountryDetectorTest extends AndroidTestCase {
  32. private static final List<String> sEnabledProviders = Arrays.asList(
  33. LocationManager.GPS_PROVIDER, LocationManager.PASSIVE_PROVIDER);
  34. private class TestCountryDetector extends LocationBasedCountryDetector {
  35. public static final int TOTAL_PROVIDERS = 2;
  36. protected Object countryFoundLocker = new Object();
  37. protected boolean notifyCountry = false;
  38. private final Location mLocation;
  39. private final String mCountry;
  40. private final long mQueryLocationTimeout;
  41. private Map<String, LocationListener> mListeners;
  42. public TestCountryDetector(String country, String provider) {
  43. this(country, provider, 1000 * 60 * 5);
  44. }
  45. public TestCountryDetector(String country, String provider, long queryLocationTimeout) {
  46. super(getContext());
  47. mCountry = country;
  48. mLocation = new Location(provider);
  49. mQueryLocationTimeout = queryLocationTimeout;
  50. mListeners = new HashMap<String, LocationListener>();
  51. }
  52. @Override
  53. protected String getCountryFromLocation(Location location) {
  54. synchronized (countryFoundLocker) {
  55. if (!notifyCountry) {
  56. try {
  57. countryFoundLocker.wait();
  58. } catch (InterruptedException e) {
  59. }
  60. }
  61. }
  62. if (mLocation.getProvider().endsWith(location.getProvider())) {
  63. return mCountry;
  64. } else {
  65. return null;
  66. }
  67. }
  68. @Override
  69. protected Location getLastKnownLocation() {
  70. return mLocation;
  71. }
  72. private Set<String> mAcceptableProviders;
  73. public void setAcceptableProvider(Set<String> acceptableProviders) {
  74. mAcceptableProviders = acceptableProviders;
  75. }
  76. @Override
  77. protected boolean isAcceptableProvider(String provider) {
  78. if (mAcceptableProviders != null) {
  79. return mAcceptableProviders.contains(provider);
  80. } else {
  81. return true;
  82. }
  83. }
  84. @Override
  85. protected void registerListener(String provider, LocationListener listener) {
  86. assertNotNull(provider);
  87. mListeners.put(provider, listener);
  88. }
  89. @Override
  90. protected void unregisterListener(LocationListener listener) {
  91. for (Entry<String, LocationListener> entry : mListeners.entrySet()) {
  92. if (entry.getValue().equals(listener)) {
  93. mListeners.remove(entry.getKey());
  94. return;
  95. }
  96. }
  97. fail("Not registered");
  98. }
  99. public Map<String, LocationListener> getListeners() {
  100. return mListeners;
  101. }
  102. @Override
  103. protected long getQueryLocationTimeout() {
  104. return mQueryLocationTimeout;
  105. }
  106. @Override
  107. protected List<String> getEnabledProviders() {
  108. return sEnabledProviders;
  109. }
  110. public void notifyLocationFound() {
  111. // Listener could be removed in the notification.
  112. LocationListener[] listeners = new LocationListener[mListeners.size()];
  113. mLocationListeners.toArray(listeners);
  114. for (LocationListener listener :listeners) {
  115. listener.onLocationChanged(mLocation);
  116. }
  117. }
  118. public int getListenersCount() {
  119. return mListeners.size();
  120. }
  121. public void notifyCountryFound() {
  122. synchronized (countryFoundLocker) {
  123. notifyCountry = true;
  124. countryFoundLocker.notify();
  125. }
  126. }
  127. public Timer getTimer() {
  128. return mTimer;
  129. }
  130. public Thread getQueryThread() {
  131. return mQueryThread;
  132. }
  133. }
  134. private class CountryListenerImpl implements CountryListener {
  135. private boolean mNotified;
  136. private String mCountryCode;
  137. public void onCountryDetected(Country country) {
  138. mNotified = true;
  139. if (country != null) {
  140. mCountryCode = country.getCountryIso();
  141. }
  142. }
  143. public boolean notified() {
  144. return mNotified;
  145. }
  146. public String getCountry() {
  147. return mCountryCode;
  148. }
  149. }
  150. public void testFindingCountry() {
  151. testFindingCountryCommon(null);
  152. }
  153. public void testFindingCountryWithAcceptableProvider() {
  154. testFindingCountryCommon(new HashSet<String>(Arrays.asList("passive")));
  155. }
  156. private void testFindingCountryCommon(Set<String> acceptableProviders) {
  157. final String country = "us";
  158. final String provider = "Good";
  159. CountryListenerImpl countryListener = new CountryListenerImpl();
  160. TestCountryDetector detector = new TestCountryDetector(country, provider);
  161. if (acceptableProviders != null) {
  162. detector.setAcceptableProvider(acceptableProviders);
  163. }
  164. detector.setCountryListener(countryListener);
  165. detector.detectCountry();
  166. if (acceptableProviders != null) {
  167. assertEquals(acceptableProviders.size(), detector.getListenersCount());
  168. Map<String, LocationListener> listeners = detector.getListeners();
  169. for (String acceptableProvider : acceptableProviders) {
  170. assertTrue(listeners.containsKey(acceptableProvider));
  171. }
  172. } else {
  173. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  174. }
  175. detector.notifyLocationFound();
  176. // All listeners should be unregistered
  177. assertEquals(0, detector.getListenersCount());
  178. assertNull(detector.getTimer());
  179. Thread queryThread = waitForQueryThreadLaunched(detector);
  180. detector.notifyCountryFound();
  181. // Wait for query thread ending
  182. waitForThreadEnding(queryThread);
  183. // QueryThread should be set to NULL
  184. assertNull(detector.getQueryThread());
  185. assertTrue(countryListener.notified());
  186. assertEquals("us", countryListener.getCountry().toLowerCase());
  187. }
  188. public void testFindingCountryCancelled() {
  189. final String country = "us";
  190. final String provider = "Good";
  191. CountryListenerImpl countryListener = new CountryListenerImpl();
  192. TestCountryDetector detector = new TestCountryDetector(country, provider);
  193. detector.setCountryListener(countryListener);
  194. detector.detectCountry();
  195. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  196. detector.notifyLocationFound();
  197. // All listeners should be unregistered
  198. assertEquals(0, detector.getListenersCount());
  199. // The time should be stopped
  200. assertNull(detector.getTimer());
  201. Thread queryThread = waitForQueryThreadLaunched(detector);
  202. detector.stop();
  203. // There is no way to stop the thread, let's test it could be stopped, after get country
  204. detector.notifyCountryFound();
  205. // Wait for query thread ending
  206. waitForThreadEnding(queryThread);
  207. // QueryThread should be set to NULL
  208. assertNull(detector.getQueryThread());
  209. assertTrue(countryListener.notified());
  210. assertEquals("us", countryListener.getCountry().toLowerCase());
  211. }
  212. public void testFindingLocationCancelled() {
  213. final String country = "us";
  214. final String provider = "Good";
  215. CountryListenerImpl countryListener = new CountryListenerImpl();
  216. TestCountryDetector detector = new TestCountryDetector(country, provider);
  217. detector.setCountryListener(countryListener);
  218. detector.detectCountry();
  219. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  220. detector.stop();
  221. // All listeners should be unregistered
  222. assertEquals(0, detector.getListenersCount());
  223. // The time should be stopped
  224. assertNull(detector.getTimer());
  225. // QueryThread should still be NULL
  226. assertNull(detector.getQueryThread());
  227. assertFalse(countryListener.notified());
  228. }
  229. public void testFindingLocationFailed() {
  230. final String country = "us";
  231. final String provider = "Good";
  232. long timeout = 1000;
  233. TestCountryDetector detector = new TestCountryDetector(country, provider, timeout) {
  234. @Override
  235. protected Location getLastKnownLocation() {
  236. return null;
  237. }
  238. };
  239. CountryListenerImpl countryListener = new CountryListenerImpl();
  240. detector.setCountryListener(countryListener);
  241. detector.detectCountry();
  242. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  243. waitForTimerReset(detector);
  244. // All listeners should be unregistered
  245. assertEquals(0, detector.getListenersCount());
  246. // QueryThread should still be NULL
  247. assertNull(detector.getQueryThread());
  248. assertTrue(countryListener.notified());
  249. assertNull(countryListener.getCountry());
  250. }
  251. public void testFindingCountryFailed() {
  252. final String country = "us";
  253. final String provider = "Good";
  254. TestCountryDetector detector = new TestCountryDetector(country, provider) {
  255. @Override
  256. protected String getCountryFromLocation(Location location) {
  257. synchronized (countryFoundLocker) {
  258. if (! notifyCountry) {
  259. try {
  260. countryFoundLocker.wait();
  261. } catch (InterruptedException e) {
  262. }
  263. }
  264. }
  265. // We didn't find country.
  266. return null;
  267. }
  268. };
  269. CountryListenerImpl countryListener = new CountryListenerImpl();
  270. detector.setCountryListener(countryListener);
  271. detector.detectCountry();
  272. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  273. detector.notifyLocationFound();
  274. // All listeners should be unregistered
  275. assertEquals(0, detector.getListenersCount());
  276. assertNull(detector.getTimer());
  277. Thread queryThread = waitForQueryThreadLaunched(detector);
  278. detector.notifyCountryFound();
  279. // Wait for query thread ending
  280. waitForThreadEnding(queryThread);
  281. // QueryThread should be set to NULL
  282. assertNull(detector.getQueryThread());
  283. // CountryListener should be notified
  284. assertTrue(countryListener.notified());
  285. assertNull(countryListener.getCountry());
  286. }
  287. public void testFindingCountryWithLastKnownLocation() {
  288. final String country = "us";
  289. final String provider = "Good";
  290. long timeout = 1000;
  291. TestCountryDetector detector = new TestCountryDetector(country, provider, timeout);
  292. CountryListenerImpl countryListener = new CountryListenerImpl();
  293. detector.setCountryListener(countryListener);
  294. detector.detectCountry();
  295. assertEquals(TestCountryDetector.TOTAL_PROVIDERS, detector.getListenersCount());
  296. waitForTimerReset(detector);
  297. // All listeners should be unregistered
  298. assertEquals(0, detector.getListenersCount());
  299. Thread queryThread = waitForQueryThreadLaunched(detector);
  300. detector.notifyCountryFound();
  301. // Wait for query thread ending
  302. waitForThreadEnding(queryThread);
  303. // QueryThread should be set to NULL
  304. assertNull(detector.getQueryThread());
  305. // CountryListener should be notified
  306. assertTrue(countryListener.notified());
  307. assertEquals("us", countryListener.getCountry().toLowerCase());
  308. }
  309. private void waitForTimerReset(TestCountryDetector detector) {
  310. int count = 5;
  311. long interval = 1000;
  312. try {
  313. while (count-- > 0 && detector.getTimer() != null) {
  314. Thread.sleep(interval);
  315. }
  316. } catch (InterruptedException e) {
  317. }
  318. Timer timer = detector.getTimer();
  319. assertTrue(timer == null);
  320. }
  321. private void waitForThreadEnding(Thread thread) {
  322. try {
  323. thread.join(5000);
  324. } catch (InterruptedException e) {
  325. e.printStackTrace();
  326. }
  327. }
  328. private Thread waitForQueryThreadLaunched(TestCountryDetector detector) {
  329. int count = 5;
  330. long interval = 1000;
  331. try {
  332. while (count-- > 0 && detector.getQueryThread() == null) {
  333. Thread.sleep(interval);
  334. }
  335. } catch (InterruptedException e) {
  336. }
  337. Thread thread = detector.getQueryThread();
  338. assertTrue(thread != null);
  339. return thread;
  340. }
  341. }