Add CTS tests for maxSdkVersion on <uses-permission>

Tests:

  * Verify that when maxSdkVersion names a version in the past, the
    requested permission is not granted

  * Verify that when maxSdkVersion names a *future* version, the
    requested permission is granted.

Bug 11294332

Change-Id: Ie877f740ecc6225a47a5c4b257783bb03a75a430
diff --git a/tests/tests/permission2/AndroidManifest.xml b/tests/tests/permission2/AndroidManifest.xml
index 01ccb97..1c1a0d8 100755
--- a/tests/tests/permission2/AndroidManifest.xml
+++ b/tests/tests/permission2/AndroidManifest.xml
@@ -45,6 +45,17 @@
     <!-- need app that has RECORD_AUDIO but not CAPTURE_AUDIO_OUTPUT -->
     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
 
+    <!-- need a permission that would ordinarily be granted, but has a maxSdkVersion that
+         causes it to be withheld under the current API level -->
+    <uses-permission
+            android:name="android.permission.VIBRATE"
+            android:maxSdkVersion="18" />
+
+    <!-- need a permission that will be granted -->
+    <uses-permission
+            android:name="android.permission.FLASHLIGHT"
+            android:maxSdkVersion="9000" />
+
     <instrumentation android:name="android.test.InstrumentationCtsTestRunner"
                      android:targetPackage="com.android.cts.permission2"
                      android:label="More CTS tests for permissions"/>
diff --git a/tests/tests/permission2/src/android/permission2/cts/PermissionMaxSdkVersionTest.java b/tests/tests/permission2/src/android/permission2/cts/PermissionMaxSdkVersionTest.java
new file mode 100644
index 0000000..e6026e4
--- /dev/null
+++ b/tests/tests/permission2/src/android/permission2/cts/PermissionMaxSdkVersionTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2013 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.permission.cts;
+
+import android.content.pm.PackageManager;
+import android.os.Process;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+/**
+ * Verify permission behaviors with android:maxSdkVersion
+ */
+public class PermissionMaxSdkVersionTest extends AndroidTestCase {
+    // These two permission names must match the corresponding <uses-permission>
+    // declarations in the test app manifest.
+    static final String UNGRANTABLE_PERMISSION = "android.permission.VIBRATE";
+    static final String GRANTABLE_PERMISSION = "android.permission.FLASHLIGHT";
+
+    /**
+     * Verify that with android:maxSdkVersion set to a previous API level,
+     * the permission is not being granted.
+     */
+    @SmallTest
+    public void testMaxSdkInPast() {
+        int result = mContext.checkPermission(UNGRANTABLE_PERMISSION,
+                Process.myPid(), Process.myUid());
+        assertEquals("Permissions with maxSdkVersion in the past should not be granted",
+                result,
+                PackageManager.PERMISSION_DENIED);
+    }
+
+    /**
+     * Verify that with android:maxSdkVersion set to a future API level,
+     * the permission is being granted.
+     */
+    @SmallTest
+    public void testMaxSdkInFuture() {
+        int result = mContext.checkPermission(GRANTABLE_PERMISSION,
+                Process.myPid(), Process.myUid());
+        assertEquals("Permissions with maxSdkVersion in the future should be granted",
+                result,
+                PackageManager.PERMISSION_GRANTED);
+    }
+}