diff options
author | Mark Brophy <mbrophy@google.com> | 2011-06-21 15:40:08 +0100 |
---|---|---|
committer | Mark Brophy <mbrophy@google.com> | 2011-07-12 11:39:25 +0100 |
commit | 01ff60258654b87d12c219035a637746cf1fdbf7 (patch) | |
tree | d04f54d650d69359403438417329531cf005f73a /tests | |
parent | 42227d505a6fabb1374f84fc7d6f3e2469903584 (diff) | |
download | ApplicationsProvider-01ff60258654b87d12c219035a637746cf1fdbf7.tar.gz |
Return the last access time of apps to QSB.
When we get a request for results from global search, results will be
sorted by last access time, and the new field
SearchManager.SUGGEST_COLUMN_LAST_ACCESS_HINT will be set.
The launch times are no longer updated every 6 hours. Now we
update the in-memory database when the request from QSB sets a
"refresh" parameter.
DEPENDS ON: I80e9b127410ed0d528515d2256787f30a953e9b0
Change-Id: Ie45e4fa52ffe43d4e7f3fa06221f3c9e039f2bb1
Diffstat (limited to 'tests')
3 files changed, 119 insertions, 41 deletions
diff --git a/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java b/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java index fa8645c..90b6711 100644 --- a/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java +++ b/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java @@ -17,7 +17,9 @@ package com.android.providers.applications; import android.content.pm.PackageManager; +import com.android.internal.os.PkgUsageStats; +import java.util.HashMap; import java.util.Map; /** @@ -29,7 +31,7 @@ public class ApplicationsProviderForTesting extends ApplicationsProvider { private MockActivityManager mMockActivityManager; - private boolean mCanRankByLaunchCount; + private boolean mHasGlobalSearchPermission; @Override protected PackageManager getPackageManager() { @@ -41,24 +43,24 @@ public class ApplicationsProviderForTesting extends ApplicationsProvider { } @Override - protected Map<String, Integer> fetchLaunchCounts() { - return mMockActivityManager.getAllPackageLaunchCounts(); + protected Map<String, PkgUsageStats> fetchUsageStats() { + Map<String, PkgUsageStats> stats = new HashMap<String, PkgUsageStats>(); + for (PkgUsageStats pus : mMockActivityManager.getAllPackageUsageStats()) { + stats.put(pus.packageName, pus); + } + return stats; } protected void setMockActivityManager(MockActivityManager mockActivityManager) { mMockActivityManager = mockActivityManager; } - protected void setCanRankByLaunchCount(boolean canRankByLaunchCount) { - mCanRankByLaunchCount = canRankByLaunchCount; + protected void setHasGlobalSearchPermission(boolean hasGlobalSearchPermission) { + mHasGlobalSearchPermission = hasGlobalSearchPermission; } @Override - protected boolean canRankByLaunchCount() { - return mCanRankByLaunchCount; - } - - @Override - protected void scheduleRegularLaunchCountUpdates() { + protected boolean hasGlobalSearchPermission() { + return mHasGlobalSearchPermission; } } diff --git a/tests/src/com/android/providers/applications/ApplicationsProviderTest.java b/tests/src/com/android/providers/applications/ApplicationsProviderTest.java index 77bc064..d512590 100644 --- a/tests/src/com/android/providers/applications/ApplicationsProviderTest.java +++ b/tests/src/com/android/providers/applications/ApplicationsProviderTest.java @@ -16,9 +16,11 @@ package com.android.providers.applications; +import android.app.SearchManager; import android.content.ComponentName; import android.content.Context; import android.database.Cursor; +import android.net.Uri; import android.provider.Applications; import android.test.ProviderTestCase2; import android.test.suitebuilder.annotation.LargeTest; @@ -94,6 +96,7 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv mockPackageManager.addPackage("AlphabeticB", new ComponentName("b", "b.BView")); mockPackageManager.addPackage("AlphabeticC", new ComponentName("c", "c.CView")); mockPackageManager.addPackage("AlphabeticD", new ComponentName("d", "d.DView")); + mockPackageManager.addPackage("AlphabeticD2", new ComponentName("d", "d.DView2")); } public void testSearch_singleResult() { @@ -109,24 +112,25 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv } public void testSearch_orderingIsAlphabeticByDefault() { - testSearch("alphabetic", "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD"); + testSearch("alphabetic", "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD", + "AlphabeticD2"); } public void testSearch_emptySearchQueryReturnsEverything() { testSearch("", - "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD", + "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD", "AlphabeticD2", "Ebay", "Email", "Fakeapp"); } - public void testSearch_appsAreRankedByLaunchCountOnStartup() throws Exception { - mMockActivityManager.addLaunchCount("d", 3); - mMockActivityManager.addLaunchCount("b", 1); - // Missing launch count for "a". - mMockActivityManager.addLaunchCount("c", 0); + public void testSearch_appsAreRankedByUsageTimeOnStartup() throws Exception { + mMockActivityManager.addLastResumeTime("d", "d.DView", 3); + mMockActivityManager.addLastResumeTime("b", "b.BView", 1); + // Missing usage time for "a". + mMockActivityManager.addLastResumeTime("c", "c.CView", 0); // Launch count database is populated on startup. mProvider = createNewProvider(getMockContext()); - mProvider.setCanRankByLaunchCount(true); + mProvider.setHasGlobalSearchPermission(true); initProvider(mProvider); // Override the previous provider with the new instance in the @@ -135,24 +139,56 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv // New ranking: D, B, A, C (first by launch count, then // - if the launch counts of two apps are equal - alphabetically) - testSearch("alphabetic", "AlphabeticD", "AlphabeticB", "AlphabeticA", "AlphabeticC"); + testSearch("alphabetic", "AlphabeticD", "AlphabeticB", "AlphabeticA", "AlphabeticC", + "AlphabeticD2"); } - public void testSearch_appsAreRankedByLaunchCountAfterScheduledUpdate() { - mProvider.setCanRankByLaunchCount(true); + public void testSearch_appsAreRankedByResumeTimeAfterUpdate() { + mProvider.setHasGlobalSearchPermission(true); - mMockActivityManager.addLaunchCount("d", 3); - mMockActivityManager.addLaunchCount("b", 1); + mMockActivityManager.addLastResumeTime("d", "d.DView", 3); + mMockActivityManager.addLastResumeTime("b", "b.BView", 1); // Missing launch count for "a". - mMockActivityManager.addLaunchCount("c", 0); + mMockActivityManager.addLastResumeTime("c", "c.CView", 0); - // Fetch new data from launch count provider (in the real instance this - // is scheduled). - mProvider.updateLaunchCounts(); + // Fetch new data from usage stat provider (in the real instance this + // is triggered by a zero-query from global search). + mProvider.updateUsageStats(); // New ranking: D, B, A, C (first by launch count, then // - if the launch counts of two apps are equal - alphabetically) - testSearch("alphabetic", "AlphabeticD", "AlphabeticB", "AlphabeticA", "AlphabeticC"); + testSearch("alphabetic", "AlphabeticD", "AlphabeticB", "AlphabeticA", "AlphabeticC", + "AlphabeticD2"); + } + + public void testSearch_noLastAccessTimesWithoutPermission() { + mProvider.setHasGlobalSearchPermission(false); + mMockActivityManager.addLastResumeTime("d", "d.DView", 1); + mMockActivityManager.addLastResumeTime("b", "b.BView", 2); + mMockActivityManager.addLastResumeTime("c", "c.CView", 3); + + assertNull(getGlobalSearchCursor("", false)); + + Cursor cursor = getGlobalSearchCursor("alphabetic", false); + assertEquals(-1, cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_LAST_ACCESS_HINT)); + } + + public void testSearch_lastAccessTimes() { + mProvider.setHasGlobalSearchPermission(true); + mMockActivityManager.addLastResumeTime("d", "d.DView", 1); + mMockActivityManager.addLastResumeTime("b", "b.BView", 2); + mMockActivityManager.addLastResumeTime("c", "c.CView", 3); + + testLastAccessTimes("", true, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0); + + testLastAccessTimes("alphabetic", true, 3, 2, 1, 0, 0); + + // Update the last resume time of "c". + mMockActivityManager.addLastResumeTime("c", "c.CView", 5); + // Without a refresh, we should see the same results as before. + testLastAccessTimes("alphabetic", false, 3, 2, 1, 0, 0); + // If we refresh, we should see the change. + testLastAccessTimes("alphabetic", true, 5, 2, 1, 0, 0); } /** @@ -162,19 +198,20 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv */ public void testSearch_notAllowedToRankByLaunchCount() { // Simulate non-privileged calling application. - mProvider.setCanRankByLaunchCount(false); + mProvider.setHasGlobalSearchPermission(false); - mMockActivityManager.addLaunchCount("d", 3); - mMockActivityManager.addLaunchCount("b", 1); - mMockActivityManager.addLaunchCount("a", 0); - mMockActivityManager.addLaunchCount("c", 0); + mMockActivityManager.addLastResumeTime("d", "d.DView", 3); + mMockActivityManager.addLastResumeTime("b", "b.BView", 1); + mMockActivityManager.addLastResumeTime("a", "a.AView", 0); + mMockActivityManager.addLastResumeTime("c", "c.CView", 0); // Fetch new data from launch count provider. - mProvider.updateLaunchCounts(); + mProvider.updateUsageStats(); // Launch count information mustn't be leaked - ranking is still // alphabetic. - testSearch("alphabetic", "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD"); + testSearch("alphabetic", "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD", + "AlphabeticD2"); } private void testSearch(String searchQuery, String... expectedResultsInOrder) { @@ -205,6 +242,39 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv } } + private Cursor getGlobalSearchCursor(String searchQuery, boolean refresh) { + Uri.Builder uriBuilder = Applications.CONTENT_URI.buildUpon(); + uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath(searchQuery); + if (refresh) { + uriBuilder.appendQueryParameter(ApplicationsProvider.REFRESH_STATS, ""); + } + return getMockContentResolver().query(uriBuilder.build(), null, null, null, null); + } + + private void testLastAccessTimes(String searchQuery, boolean refresh, + int... expectedLastAccessTimesInOrder) { + Cursor cursor = getGlobalSearchCursor(searchQuery, refresh); + + assertNotNull(cursor); + assertFalse(cursor.isClosed()); + + verifyLastAccessTimes(cursor, expectedLastAccessTimesInOrder); + + cursor.close(); + } + + private void verifyLastAccessTimes(Cursor cursor, int... expectedLastAccessTimesInOrder) { + cursor.moveToFirst(); + int lastAccessTimeIndex = cursor.getColumnIndex( + SearchManager.SUGGEST_COLUMN_LAST_ACCESS_HINT); + // Verify that the actual results match the expected ones. + for (int i = 0; i < cursor.getCount(); i++) { + assertEquals("Wrong last-access time at position " + i, + expectedLastAccessTimesInOrder[i], cursor.getInt(lastAccessTimeIndex)); + cursor.moveToNext(); + } + } + private ApplicationsProviderForTesting createNewProvider(Context context) throws Exception { ApplicationsProviderForTesting newProviderInstance = ApplicationsProviderForTesting.class.newInstance(); diff --git a/tests/src/com/android/providers/applications/MockActivityManager.java b/tests/src/com/android/providers/applications/MockActivityManager.java index 63deacf..5ac5cf3 100644 --- a/tests/src/com/android/providers/applications/MockActivityManager.java +++ b/tests/src/com/android/providers/applications/MockActivityManager.java @@ -15,6 +15,8 @@ */ package com.android.providers.applications; +import com.android.internal.os.PkgUsageStats; + import java.util.HashMap; import java.util.Map; @@ -24,13 +26,17 @@ import java.util.Map; */ public class MockActivityManager { - private Map<String, Integer> mPackageLaunchCounts = new HashMap<String, Integer>(); + private Map<String, PkgUsageStats> mPackageUsageStats = new HashMap<String, PkgUsageStats>(); - public void addLaunchCount(String packageName, int launchCount) { - mPackageLaunchCounts.put(packageName, launchCount); + public void addLastResumeTime(String packageName, String componentName, long lastResumeTime) { + if (!mPackageUsageStats.containsKey(packageName)) { + PkgUsageStats stats = new PkgUsageStats(packageName, 1, 0, new HashMap<String, Long>()); + mPackageUsageStats.put(packageName, stats); + } + mPackageUsageStats.get(packageName).componentResumeTimes.put(componentName, lastResumeTime); } - public Map<String, Integer> getAllPackageLaunchCounts() { - return mPackageLaunchCounts; + public PkgUsageStats[] getAllPackageUsageStats() { + return mPackageUsageStats.values().toArray(new PkgUsageStats[mPackageUsageStats.size()]); } } |