diff options
author | Peter Visontay <pvisontay@google.com> | 2011-02-11 18:36:18 +0000 |
---|---|---|
committer | Peter Visontay <pvisontay@google.com> | 2011-02-25 12:03:19 +0000 |
commit | 7089c89c9ec21f28326ac6a9e9e91344749a02e0 (patch) | |
tree | 205f615b265a66bf30933bc14afc3265c93264bb /tests | |
parent | d995cd1f165074ebf14b9b884486770f2c219499 (diff) | |
download | ApplicationsProvider-7089c89c9ec21f28326ac6a9e9e91344749a02e0.tar.gz |
Made ApplicationsProvider fetch launch counts from a remote service.
Bug: 3431684
Change-Id: Ie350ef440a7a49e5fdae5723414bfbab11f09793
Diffstat (limited to 'tests')
4 files changed, 91 insertions, 60 deletions
diff --git a/tests/runtests b/tests/runtests index 184a278..be1c779 100644 --- a/tests/runtests +++ b/tests/runtests @@ -1,4 +1,5 @@ mmm packages/providers/ApplicationsProvider || return +mmm packages/providers/ApplicationsProvider/tests || return adb install -r ${OUT}/system/app/ApplicationsProvider.apk || return adb install -r ${OUT}/data/app/ApplicationsProviderTests.apk || return adb shell am instrument -w -e class com.android.providers.applications.ApplicationsProviderTest com.android.providers.applications.tests/android.test.InstrumentationTestRunner || return diff --git a/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java b/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java index ea91fd0..fa8645c 100644 --- a/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java +++ b/tests/src/com/android/providers/applications/ApplicationsProviderForTesting.java @@ -18,6 +18,8 @@ package com.android.providers.applications; import android.content.pm.PackageManager; +import java.util.Map; + /** * An extension of {@link ApplicationsProvider} that makes its testing easier. */ @@ -25,6 +27,8 @@ public class ApplicationsProviderForTesting extends ApplicationsProvider { private PackageManager mMockPackageManager; + private MockActivityManager mMockActivityManager; + private boolean mCanRankByLaunchCount; @Override @@ -37,8 +41,12 @@ public class ApplicationsProviderForTesting extends ApplicationsProvider { } @Override - protected void enforcePermissionForLaunchCountIncrease() { - // Tests are allowed to do this. + protected Map<String, Integer> fetchLaunchCounts() { + return mMockActivityManager.getAllPackageLaunchCounts(); + } + + protected void setMockActivityManager(MockActivityManager mockActivityManager) { + mMockActivityManager = mockActivityManager; } protected void setCanRankByLaunchCount(boolean canRankByLaunchCount) { @@ -49,4 +57,8 @@ public class ApplicationsProviderForTesting extends ApplicationsProvider { protected boolean canRankByLaunchCount() { return mCanRankByLaunchCount; } + + @Override + protected void scheduleRegularLaunchCountUpdates() { + } } diff --git a/tests/src/com/android/providers/applications/ApplicationsProviderTest.java b/tests/src/com/android/providers/applications/ApplicationsProviderTest.java index 9d07bb2..77bc064 100644 --- a/tests/src/com/android/providers/applications/ApplicationsProviderTest.java +++ b/tests/src/com/android/providers/applications/ApplicationsProviderTest.java @@ -17,17 +17,13 @@ package com.android.providers.applications; import android.content.ComponentName; -import android.content.ContentValues; 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; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.FutureTask; -import java.util.concurrent.TimeUnit; /** @@ -43,6 +39,8 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv private ApplicationsProviderForTesting mProvider; + private MockActivityManager mMockActivityManager; + public ApplicationsProviderTest() { super(ApplicationsProviderForTesting.class, Applications.AUTHORITY); } @@ -51,6 +49,7 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv protected void setUp() throws Exception { super.setUp(); mProvider = getProvider(); + mMockActivityManager = new MockActivityManager(); initProvider(mProvider); } @@ -62,6 +61,7 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv MockPackageManager mockPackageManager = new MockPackageManager(); addDefaultTestPackages(mockPackageManager); provider.setMockPackageManager(mockPackageManager); + provider.setMockActivityManager(mMockActivityManager); // We need to wait for the applications database to be updated (it's // updated with a slight delay by a separate thread) before we can use @@ -118,14 +118,37 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv "Ebay", "Email", "Fakeapp"); } - public void testSearch_appsAreRankedByLaunchCount() { + public void testSearch_appsAreRankedByLaunchCountOnStartup() throws Exception { + mMockActivityManager.addLaunchCount("d", 3); + mMockActivityManager.addLaunchCount("b", 1); + // Missing launch count for "a". + mMockActivityManager.addLaunchCount("c", 0); + + // Launch count database is populated on startup. + mProvider = createNewProvider(getMockContext()); + mProvider.setCanRankByLaunchCount(true); + initProvider(mProvider); + + // Override the previous provider with the new instance in the + // ContentResolver. + getMockContentResolver().addProvider(Applications.AUTHORITY, mProvider); + + // 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"); + } + + public void testSearch_appsAreRankedByLaunchCountAfterScheduledUpdate() { mProvider.setCanRankByLaunchCount(true); - // Original ranking: A, B, C, D (alphabetic; all launch counts are 0 - // by default). - increaseLaunchCount(new ComponentName("b", "b.BView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); + mMockActivityManager.addLaunchCount("d", 3); + mMockActivityManager.addLaunchCount("b", 1); + // Missing launch count for "a". + mMockActivityManager.addLaunchCount("c", 0); + + // Fetch new data from launch count provider (in the real instance this + // is scheduled). + mProvider.updateLaunchCounts(); // New ranking: D, B, A, C (first by launch count, then // - if the launch counts of two apps are equal - alphabetically) @@ -141,52 +164,19 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv // Simulate non-privileged calling application. mProvider.setCanRankByLaunchCount(false); - // Original ranking: A, B, C, D (alphabetic; all launch counts are 0 - // by default). - increaseLaunchCount(new ComponentName("b", "b.BView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); + mMockActivityManager.addLaunchCount("d", 3); + mMockActivityManager.addLaunchCount("b", 1); + mMockActivityManager.addLaunchCount("a", 0); + mMockActivityManager.addLaunchCount("c", 0); + + // Fetch new data from launch count provider. + mProvider.updateLaunchCounts(); // Launch count information mustn't be leaked - ranking is still // alphabetic. testSearch("alphabetic", "AlphabeticA", "AlphabeticB", "AlphabeticC", "AlphabeticD"); } - /** - * Tests that the launch count values are persisted even if the - * ApplicationsProvider is restarted. - */ - public void testSearch_launchCountInformationIsPersistent() throws Exception { - mProvider.setCanRankByLaunchCount(true); - - // Original ranking: A, B, C, D (alphabetic; all launch counts are 0 - // by default). - increaseLaunchCount(new ComponentName("b", "b.BView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); - increaseLaunchCount(new ComponentName("d", "d.DView")); - - // 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"); - - // Now we'll create a new ApplicationsProvider instance (the provider - // may be killed by Android at any time) and verify that it has access - // to the same launch count information as the original provider instance. - // The new instance will use the same IsolatedContext as the previous one. - ApplicationsProviderForTesting newProviderInstance = createNewProvider(mProvider.getContext()); - newProviderInstance.setCanRankByLaunchCount(true); - assertNotSame(newProviderInstance, mProvider); - - // Override the previous provider with the new instance in the - // ContentResolver. - getMockContentResolver().addProvider(Applications.AUTHORITY, newProviderInstance); - - initProvider(newProviderInstance); - - // Verify that the launch-count-dependent ordering is still correct. - testSearch("alphabetic", "AlphabeticD", "AlphabeticB", "AlphabeticA", "AlphabeticC"); - } - private void testSearch(String searchQuery, String... expectedResultsInOrder) { Cursor cursor = Applications.search(getMockContentResolver(), searchQuery); @@ -215,14 +205,6 @@ public class ApplicationsProviderTest extends ProviderTestCase2<ApplicationsProv } } - /** - * Makes the ApplicationsProvider increase the launch count of this - * application stored in its database. - */ - private void increaseLaunchCount(ComponentName componentName) { - Applications.increaseLaunchCount(getMockContentResolver(), componentName); - } - 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 new file mode 100644 index 0000000..63deacf --- /dev/null +++ b/tests/src/com/android/providers/applications/MockActivityManager.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.providers.applications; + +import java.util.HashMap; +import java.util.Map; + +/** + * The real ActivityManager is difficult to mock out (has a package visibility + * constructor), so this doesn't extend it. + */ +public class MockActivityManager { + + private Map<String, Integer> mPackageLaunchCounts = new HashMap<String, Integer>(); + + public void addLaunchCount(String packageName, int launchCount) { + mPackageLaunchCounts.put(packageName, launchCount); + } + + public Map<String, Integer> getAllPackageLaunchCounts() { + return mPackageLaunchCounts; + } +} |