summaryrefslogtreecommitdiffstats
path: root/core/tests
diff options
context:
space:
mode:
authorJeff Davidson <jpd@google.com>2014-04-14 15:14:30 -0700
committerJeff Davidson <jpd@google.com>2014-04-16 14:17:01 -0700
commitdd6fd1e62b1792d1f96824b2fefb16d894b43197 (patch)
tree166a5b67eea88d0b1d2b7c18fff33687b54006da /core/tests
parent34de7a83033c65f28626aaa9762a91a1646d2486 (diff)
downloadframeworks_base-dd6fd1e62b1792d1f96824b2fefb16d894b43197.zip
frameworks_base-dd6fd1e62b1792d1f96824b2fefb16d894b43197.tar.gz
frameworks_base-dd6fd1e62b1792d1f96824b2fefb16d894b43197.tar.bz2
Class for managing the active scorer application.
Allows listing all possible scorer apps, getting the active scorer app, setting the active scorer app, and determining whether a given UID is for the active scorer. Note that setting a default scorer with a system image will be handled separately - the plan here is to add a read-only build property containing a package name to use as the scorer out of the box. When NetworkScorerService is initialized, it will check a provisioning bit; if that bit isn't set, it will set the current scorer to the package specified in this build property (if any) and set the provisioning bit to true. Bug: 13769362 Change-Id: I18c0b2ba2aceccc88b70c3611a49149e0bf17ecf
Diffstat (limited to 'core/tests')
-rw-r--r--core/tests/coretests/Android.mk2
-rw-r--r--core/tests/coretests/src/android/net/NetworkScorerApplicationTest.java101
2 files changed, 102 insertions, 1 deletions
diff --git a/core/tests/coretests/Android.mk b/core/tests/coretests/Android.mk
index 73a53cb..6bdeaf0 100644
--- a/core/tests/coretests/Android.mk
+++ b/core/tests/coretests/Android.mk
@@ -23,7 +23,7 @@ LOCAL_SRC_FILES := \
LOCAL_DX_FLAGS := --core-library
LOCAL_AAPT_FLAGS = -0 dat -0 gld
-LOCAL_STATIC_JAVA_LIBRARIES := core-tests-support android-common frameworks-core-util-lib mockwebserver guava littlemock
+LOCAL_STATIC_JAVA_LIBRARIES := core-tests-support android-common frameworks-core-util-lib mockwebserver guava littlemock mockito-target
LOCAL_JAVA_LIBRARIES := android.test.runner conscrypt telephony-common
LOCAL_PACKAGE_NAME := FrameworksCoreTests
diff --git a/core/tests/coretests/src/android/net/NetworkScorerApplicationTest.java b/core/tests/coretests/src/android/net/NetworkScorerApplicationTest.java
new file mode 100644
index 0000000..6d5ede8
--- /dev/null
+++ b/core/tests/coretests/src/android/net/NetworkScorerApplicationTest.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2014 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 android.net;
+
+import android.Manifest.permission;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.test.InstrumentationTestCase;
+
+import com.google.android.collect.Lists;
+
+import org.mockito.ArgumentMatcher;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import java.util.Iterator;
+
+public class NetworkScorerApplicationTest extends InstrumentationTestCase {
+ @Mock private Context mMockContext;
+ @Mock private PackageManager mMockPm;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ // Configuration needed to make mockito/dexcache work.
+ System.setProperty("dexmaker.dexcache",
+ getInstrumentation().getTargetContext().getCacheDir().getPath());
+ ClassLoader newClassLoader = getInstrumentation().getClass().getClassLoader();
+ Thread.currentThread().setContextClassLoader(newClassLoader);
+
+ MockitoAnnotations.initMocks(this);
+ Mockito.when(mMockContext.getPackageManager()).thenReturn(mMockPm);
+ }
+
+ public void testGetAllValidScorers() throws Exception {
+ // Package 1 - Valid scorer.
+ ResolveInfo package1 = buildResolveInfo("package1", true, true);
+
+ // Package 2 - Receiver does not have BROADCAST_SCORE_NETWORKS permission.
+ ResolveInfo package2 = buildResolveInfo("package2", false, true);
+
+ // Package 3 - App does not have SCORE_NETWORKS permission.
+ ResolveInfo package3 = buildResolveInfo("package3", true, false);
+
+ setScorers(package1, package2, package3);
+
+ Iterator<String> result =
+ NetworkScorerApplication.getAllValidScorers(mMockContext).iterator();
+
+ assertTrue(result.hasNext());
+ assertEquals("package1", result.next());
+
+ assertFalse(result.hasNext());
+ }
+
+ private void setScorers(ResolveInfo... scorers) {
+ Mockito.when(mMockPm.queryBroadcastReceivers(
+ Mockito.argThat(new ArgumentMatcher<Intent>() {
+ @Override
+ public boolean matches(Object object) {
+ Intent intent = (Intent) object;
+ return NetworkScoreManager.ACTION_SCORE_NETWORKS.equals(intent.getAction());
+ }
+ }), Mockito.eq(0)))
+ .thenReturn(Lists.newArrayList(scorers));
+ }
+
+ private ResolveInfo buildResolveInfo(String packageName,
+ boolean hasReceiverPermission, boolean hasScorePermission) throws Exception {
+ Mockito.when(mMockPm.checkPermission(permission.SCORE_NETWORKS, packageName))
+ .thenReturn(hasScorePermission ?
+ PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED);
+
+ ResolveInfo resolveInfo = new ResolveInfo();
+ resolveInfo.activityInfo = new ActivityInfo();
+ resolveInfo.activityInfo.packageName = packageName;
+ if (hasReceiverPermission) {
+ resolveInfo.activityInfo.permission = permission.BROADCAST_SCORE_NETWORKS;
+ }
+ return resolveInfo;
+ }
+}