1 package com.android.server.location; 2 3 import com.android.server.location.LocationRequestStatistics.PackageProviderKey; 4 import com.android.server.location.LocationRequestStatistics.PackageStatistics; 5 6 import android.os.SystemClock; 7 import android.test.AndroidTestCase; 8 9 /** 10 * Unit tests for {@link LocationRequestStatistics}. 11 */ 12 public class LocationRequestStatisticsTest extends AndroidTestCase { 13 private static final String PACKAGE1 = "package1"; 14 private static final String PACKAGE2 = "package2"; 15 private static final String PROVIDER1 = "provider1"; 16 private static final String PROVIDER2 = "provider2"; 17 private static final long INTERVAL1 = 5000; 18 private static final long INTERVAL2 = 100000; 19 20 private LocationRequestStatistics mStatistics; 21 private long mStartElapsedRealtimeMs; 22 23 @Override setUp()24 public void setUp() { 25 mStatistics = new LocationRequestStatistics(); 26 mStartElapsedRealtimeMs = SystemClock.elapsedRealtime(); 27 } 28 29 /** 30 * Tests that adding a single package works correctly. 31 */ testSinglePackage()32 public void testSinglePackage() { 33 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 34 35 assertEquals(1, mStatistics.statistics.size()); 36 PackageProviderKey key = mStatistics.statistics.keySet().iterator().next(); 37 assertEquals(PACKAGE1, key.packageName); 38 assertEquals(PROVIDER1, key.providerName); 39 PackageStatistics stats = mStatistics.statistics.values().iterator().next(); 40 verifyStatisticsTimes(stats); 41 assertEquals(INTERVAL1, stats.getFastestIntervalMs()); 42 assertEquals(INTERVAL1, stats.getSlowestIntervalMs()); 43 assertTrue(stats.isActive()); 44 } 45 46 /** 47 * Tests that adding a single package works correctly when it is stopped and restarted. 48 */ testSinglePackage_stopAndRestart()49 public void testSinglePackage_stopAndRestart() { 50 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 51 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 52 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 53 54 assertEquals(1, mStatistics.statistics.size()); 55 PackageProviderKey key = mStatistics.statistics.keySet().iterator().next(); 56 assertEquals(PACKAGE1, key.packageName); 57 assertEquals(PROVIDER1, key.providerName); 58 PackageStatistics stats = mStatistics.statistics.values().iterator().next(); 59 verifyStatisticsTimes(stats); 60 assertEquals(INTERVAL1, stats.getFastestIntervalMs()); 61 assertEquals(INTERVAL1, stats.getSlowestIntervalMs()); 62 assertTrue(stats.isActive()); 63 64 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 65 assertFalse(stats.isActive()); 66 } 67 68 /** 69 * Tests that adding a single package works correctly when multiple intervals are used. 70 */ testSinglePackage_multipleIntervals()71 public void testSinglePackage_multipleIntervals() { 72 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 73 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL2); 74 75 assertEquals(1, mStatistics.statistics.size()); 76 PackageProviderKey key = mStatistics.statistics.keySet().iterator().next(); 77 assertEquals(PACKAGE1, key.packageName); 78 assertEquals(PROVIDER1, key.providerName); 79 PackageStatistics stats = mStatistics.statistics.values().iterator().next(); 80 verifyStatisticsTimes(stats); 81 assertEquals(INTERVAL1, stats.getFastestIntervalMs()); 82 assertTrue(stats.isActive()); 83 84 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 85 assertTrue(stats.isActive()); 86 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 87 assertFalse(stats.isActive()); 88 } 89 90 /** 91 * Tests that adding a single package works correctly when multiple providers are used. 92 */ testSinglePackage_multipleProviders()93 public void testSinglePackage_multipleProviders() { 94 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 95 mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL2); 96 97 assertEquals(2, mStatistics.statistics.size()); 98 PackageProviderKey key1 = new PackageProviderKey(PACKAGE1, PROVIDER1); 99 PackageStatistics stats1 = mStatistics.statistics.get(key1); 100 verifyStatisticsTimes(stats1); 101 assertEquals(INTERVAL1, stats1.getSlowestIntervalMs()); 102 assertEquals(INTERVAL1, stats1.getFastestIntervalMs()); 103 assertTrue(stats1.isActive()); 104 PackageProviderKey key2 = new PackageProviderKey(PACKAGE1, PROVIDER2); 105 PackageStatistics stats2 = mStatistics.statistics.get(key2); 106 verifyStatisticsTimes(stats2); 107 assertEquals(INTERVAL2, stats2.getSlowestIntervalMs()); 108 assertEquals(INTERVAL2, stats2.getFastestIntervalMs()); 109 assertTrue(stats2.isActive()); 110 111 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 112 assertFalse(stats1.isActive()); 113 assertTrue(stats2.isActive()); 114 mStatistics.stopRequesting(PACKAGE1, PROVIDER2); 115 assertFalse(stats1.isActive()); 116 assertFalse(stats2.isActive()); 117 } 118 119 /** 120 * Tests that adding multiple packages works correctly. 121 */ testMultiplePackages()122 public void testMultiplePackages() { 123 mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1); 124 mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL1); 125 mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL2); 126 mStatistics.startRequesting(PACKAGE2, PROVIDER1, INTERVAL1); 127 128 assertEquals(3, mStatistics.statistics.size()); 129 PackageProviderKey key1 = new PackageProviderKey(PACKAGE1, PROVIDER1); 130 PackageStatistics stats1 = mStatistics.statistics.get(key1); 131 verifyStatisticsTimes(stats1); 132 assertEquals(INTERVAL1, stats1.getSlowestIntervalMs()); 133 assertEquals(INTERVAL1, stats1.getFastestIntervalMs()); 134 assertTrue(stats1.isActive()); 135 136 PackageProviderKey key2 = new PackageProviderKey(PACKAGE1, PROVIDER2); 137 PackageStatistics stats2 = mStatistics.statistics.get(key2); 138 verifyStatisticsTimes(stats2); 139 assertEquals(INTERVAL2, stats2.getSlowestIntervalMs()); 140 assertEquals(INTERVAL1, stats2.getFastestIntervalMs()); 141 assertTrue(stats2.isActive()); 142 143 PackageProviderKey key3 = new PackageProviderKey(PACKAGE2, PROVIDER1); 144 PackageStatistics stats3 = mStatistics.statistics.get(key3); 145 verifyStatisticsTimes(stats3); 146 assertEquals(INTERVAL1, stats3.getSlowestIntervalMs()); 147 assertEquals(INTERVAL1, stats3.getFastestIntervalMs()); 148 assertTrue(stats3.isActive()); 149 150 mStatistics.stopRequesting(PACKAGE1, PROVIDER1); 151 assertFalse(stats1.isActive()); 152 assertTrue(stats2.isActive()); 153 assertTrue(stats3.isActive()); 154 155 mStatistics.stopRequesting(PACKAGE1, PROVIDER2); 156 assertFalse(stats1.isActive()); 157 assertTrue(stats2.isActive()); 158 assertTrue(stats3.isActive()); 159 mStatistics.stopRequesting(PACKAGE1, PROVIDER2); 160 assertFalse(stats2.isActive()); 161 162 mStatistics.stopRequesting(PACKAGE2, PROVIDER1); 163 assertFalse(stats1.isActive()); 164 assertFalse(stats2.isActive()); 165 assertFalse(stats3.isActive()); 166 } 167 verifyStatisticsTimes(PackageStatistics stats)168 private void verifyStatisticsTimes(PackageStatistics stats) { 169 long durationMs = stats.getDurationMs(); 170 long timeSinceFirstRequestMs = stats.getTimeSinceFirstRequestMs(); 171 long maxDeltaMs = SystemClock.elapsedRealtime() - mStartElapsedRealtimeMs; 172 assertTrue("Duration is too large", durationMs <= maxDeltaMs); 173 assertTrue("Time since first request is too large", timeSinceFirstRequestMs <= maxDeltaMs); 174 } 175 } 176