summaryrefslogtreecommitdiffstats
path: root/services/tests
diff options
context:
space:
mode:
authorDavid Christie <dnchrist@google.com>2014-02-13 20:25:00 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2014-02-13 20:25:01 +0000
commitd63b4314b85e982a1d70d4064af59851f476dd36 (patch)
treef427ceb35b814b9395a1bf0a0a6c5b505e9c3b3a /services/tests
parent6b1a93fcb9551730583f1d3ea36dbfd5589859f3 (diff)
parent2ff96af24de2c22a21de9b56ea8543dccdbdcb9d (diff)
downloadframeworks_base-d63b4314b85e982a1d70d4064af59851f476dd36.zip
frameworks_base-d63b4314b85e982a1d70d4064af59851f476dd36.tar.gz
frameworks_base-d63b4314b85e982a1d70d4064af59851f476dd36.tar.bz2
Merge "Log history of location requests in LocationManager. -Assists with debugging power issues. Bug: 12824233"
Diffstat (limited to 'services/tests')
-rw-r--r--services/tests/servicestests/src/com/android/server/location/LocationRequestStatisticsTest.java175
1 files changed, 175 insertions, 0 deletions
diff --git a/services/tests/servicestests/src/com/android/server/location/LocationRequestStatisticsTest.java b/services/tests/servicestests/src/com/android/server/location/LocationRequestStatisticsTest.java
new file mode 100644
index 0000000..33f604d
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/location/LocationRequestStatisticsTest.java
@@ -0,0 +1,175 @@
+package com.android.server.location;
+
+import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
+import com.android.server.location.LocationRequestStatistics.PackageStatistics;
+
+import android.os.SystemClock;
+import android.test.AndroidTestCase;
+
+/**
+ * Unit tests for {@link LocationRequestStatistics}.
+ */
+public class LocationRequestStatisticsTest extends AndroidTestCase {
+ private static final String PACKAGE1 = "package1";
+ private static final String PACKAGE2 = "package2";
+ private static final String PROVIDER1 = "provider1";
+ private static final String PROVIDER2 = "provider2";
+ private static final long INTERVAL1 = 5000;
+ private static final long INTERVAL2 = 100000;
+
+ private LocationRequestStatistics mStatistics;
+ private long mStartElapsedRealtimeMs;
+
+ @Override
+ public void setUp() {
+ mStatistics = new LocationRequestStatistics();
+ mStartElapsedRealtimeMs = SystemClock.elapsedRealtime();
+ }
+
+ /**
+ * Tests that adding a single package works correctly.
+ */
+ public void testSinglePackage() {
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+
+ assertEquals(1, mStatistics.statistics.size());
+ PackageProviderKey key = mStatistics.statistics.keySet().iterator().next();
+ assertEquals(PACKAGE1, key.packageName);
+ assertEquals(PROVIDER1, key.providerName);
+ PackageStatistics stats = mStatistics.statistics.values().iterator().next();
+ verifyStatisticsTimes(stats);
+ assertEquals(INTERVAL1, stats.getFastestIntervalMs());
+ assertEquals(INTERVAL1, stats.getSlowestIntervalMs());
+ assertTrue(stats.isActive());
+ }
+
+ /**
+ * Tests that adding a single package works correctly when it is stopped and restarted.
+ */
+ public void testSinglePackage_stopAndRestart() {
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+
+ assertEquals(1, mStatistics.statistics.size());
+ PackageProviderKey key = mStatistics.statistics.keySet().iterator().next();
+ assertEquals(PACKAGE1, key.packageName);
+ assertEquals(PROVIDER1, key.providerName);
+ PackageStatistics stats = mStatistics.statistics.values().iterator().next();
+ verifyStatisticsTimes(stats);
+ assertEquals(INTERVAL1, stats.getFastestIntervalMs());
+ assertEquals(INTERVAL1, stats.getSlowestIntervalMs());
+ assertTrue(stats.isActive());
+
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ assertFalse(stats.isActive());
+ }
+
+ /**
+ * Tests that adding a single package works correctly when multiple intervals are used.
+ */
+ public void testSinglePackage_multipleIntervals() {
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL2);
+
+ assertEquals(1, mStatistics.statistics.size());
+ PackageProviderKey key = mStatistics.statistics.keySet().iterator().next();
+ assertEquals(PACKAGE1, key.packageName);
+ assertEquals(PROVIDER1, key.providerName);
+ PackageStatistics stats = mStatistics.statistics.values().iterator().next();
+ verifyStatisticsTimes(stats);
+ assertEquals(INTERVAL1, stats.getFastestIntervalMs());
+ assertTrue(stats.isActive());
+
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ assertTrue(stats.isActive());
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ assertFalse(stats.isActive());
+ }
+
+ /**
+ * Tests that adding a single package works correctly when multiple providers are used.
+ */
+ public void testSinglePackage_multipleProviders() {
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+ mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL2);
+
+ assertEquals(2, mStatistics.statistics.size());
+ PackageProviderKey key1 = new PackageProviderKey(PACKAGE1, PROVIDER1);
+ PackageStatistics stats1 = mStatistics.statistics.get(key1);
+ verifyStatisticsTimes(stats1);
+ assertEquals(INTERVAL1, stats1.getSlowestIntervalMs());
+ assertEquals(INTERVAL1, stats1.getFastestIntervalMs());
+ assertTrue(stats1.isActive());
+ PackageProviderKey key2 = new PackageProviderKey(PACKAGE1, PROVIDER2);
+ PackageStatistics stats2 = mStatistics.statistics.get(key2);
+ verifyStatisticsTimes(stats2);
+ assertEquals(INTERVAL2, stats2.getSlowestIntervalMs());
+ assertEquals(INTERVAL2, stats2.getFastestIntervalMs());
+ assertTrue(stats2.isActive());
+
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ assertFalse(stats1.isActive());
+ assertTrue(stats2.isActive());
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER2);
+ assertFalse(stats1.isActive());
+ assertFalse(stats2.isActive());
+ }
+
+ /**
+ * Tests that adding multiple packages works correctly.
+ */
+ public void testMultiplePackages() {
+ mStatistics.startRequesting(PACKAGE1, PROVIDER1, INTERVAL1);
+ mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL1);
+ mStatistics.startRequesting(PACKAGE1, PROVIDER2, INTERVAL2);
+ mStatistics.startRequesting(PACKAGE2, PROVIDER1, INTERVAL1);
+
+ assertEquals(3, mStatistics.statistics.size());
+ PackageProviderKey key1 = new PackageProviderKey(PACKAGE1, PROVIDER1);
+ PackageStatistics stats1 = mStatistics.statistics.get(key1);
+ verifyStatisticsTimes(stats1);
+ assertEquals(INTERVAL1, stats1.getSlowestIntervalMs());
+ assertEquals(INTERVAL1, stats1.getFastestIntervalMs());
+ assertTrue(stats1.isActive());
+
+ PackageProviderKey key2 = new PackageProviderKey(PACKAGE1, PROVIDER2);
+ PackageStatistics stats2 = mStatistics.statistics.get(key2);
+ verifyStatisticsTimes(stats2);
+ assertEquals(INTERVAL2, stats2.getSlowestIntervalMs());
+ assertEquals(INTERVAL1, stats2.getFastestIntervalMs());
+ assertTrue(stats2.isActive());
+
+ PackageProviderKey key3 = new PackageProviderKey(PACKAGE2, PROVIDER1);
+ PackageStatistics stats3 = mStatistics.statistics.get(key3);
+ verifyStatisticsTimes(stats3);
+ assertEquals(INTERVAL1, stats3.getSlowestIntervalMs());
+ assertEquals(INTERVAL1, stats3.getFastestIntervalMs());
+ assertTrue(stats3.isActive());
+
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER1);
+ assertFalse(stats1.isActive());
+ assertTrue(stats2.isActive());
+ assertTrue(stats3.isActive());
+
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER2);
+ assertFalse(stats1.isActive());
+ assertTrue(stats2.isActive());
+ assertTrue(stats3.isActive());
+ mStatistics.stopRequesting(PACKAGE1, PROVIDER2);
+ assertFalse(stats2.isActive());
+
+ mStatistics.stopRequesting(PACKAGE2, PROVIDER1);
+ assertFalse(stats1.isActive());
+ assertFalse(stats2.isActive());
+ assertFalse(stats3.isActive());
+ }
+
+ private void verifyStatisticsTimes(PackageStatistics stats) {
+ long durationMs = stats.getDurationMs();
+ long timeSinceFirstRequestMs = stats.getTimeSinceFirstRequestMs();
+ long maxDeltaMs = SystemClock.elapsedRealtime() - mStartElapsedRealtimeMs;
+ assertTrue("Duration is too large", durationMs <= maxDeltaMs);
+ assertTrue("Time since first request is too large", timeSinceFirstRequestMs <= maxDeltaMs);
+ }
+}