Move ApplicationRestrictionsTest to DeviceAndProfileOwner

Because application restrictions apply to device and profile owner
situations, the test is moved to the DeviceAndProfileOwner package and
is now run in both situations.

Bug: 22709411
Change-Id: Ib72249241142d7d440f0283371a00b9440fc5d4a
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml
index 484dd6e..a9ce300 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml
@@ -30,6 +30,8 @@
                 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
             </intent-filter>
         </receiver>
+        <activity
+            android:name="com.android.cts.deviceandprofileowner.ApplicationRestrictionsActivity" />
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionActivity.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsActivity.java
similarity index 90%
rename from hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionActivity.java
rename to hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsActivity.java
index 4d8e4f2..ccf8222 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionActivity.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsActivity.java
@@ -13,8 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-package com.android.cts.deviceowner;
+package com.android.cts.deviceandprofileowner;
 
 import android.app.Activity;
 import android.content.BroadcastReceiver;
@@ -31,16 +30,16 @@
  * activity is to listen for the ACTION_APPLICATION_RESTRICTIONS_CHANGED broadcast
  * and relay the retrieved restriction bundle back to the test for validation.
  */
-public class ApplicationRestrictionActivity extends Activity {
+public class ApplicationRestrictionsActivity extends Activity {
 
     // Incoming intent type
     public static final String FINISH = "finishActivity";
 
     // Outgoing broadcast
     public static final String REGISTERED_ACTION =
-            "com.android.cts.deviceowner.APP_RESTRICTION_REGISTERED";
+            "com.android.cts.deviceandprofileowner.APP_RESTRICTION_REGISTERED";
     public static final String RESTRICTION_ACTION =
-            "com.android.cts.deviceowner.APP_RESTRICTION_VALUE";
+            "com.android.cts.deviceandprofileowner.APP_RESTRICTION_VALUE";
 
     private UserManager mUserManager;
 
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionsTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
similarity index 83%
rename from hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionsTest.java
rename to hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
index 13a2a6d..98ae202 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/ApplicationRestrictionsTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.cts.deviceowner;
+package com.android.cts.deviceandprofileowner;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -38,7 +38,7 @@
  * which is fired by the system whenever its restriction is modified,
  * and relays the value back to this test for verification.
  */
-public class ApplicationRestrictionsTest extends BaseDeviceOwnerTest {
+public class ApplicationRestrictionsTest extends BaseDeviceAdminTest {
 
     private static final String[] testStrings = new String[] {
             "<bad/>",
@@ -55,8 +55,8 @@
     protected void setUp() throws Exception {
         super.setUp();
         IntentFilter filter = new IntentFilter();
-        filter.addAction(ApplicationRestrictionActivity.REGISTERED_ACTION);
-        filter.addAction(ApplicationRestrictionActivity.RESTRICTION_ACTION);
+        filter.addAction(ApplicationRestrictionsActivity.REGISTERED_ACTION);
+        filter.addAction(ApplicationRestrictionsActivity.RESTRICTION_ACTION);
         mContext.registerReceiver(mReceiver, filter);
     }
 
@@ -76,30 +76,38 @@
         Bundle bundle1 = createBundle1();
 
         // Test setting restrictions
-        mDevicePolicyManager.setApplicationRestrictions(getWho(), CTS_PACKAGE, bundle0);
-        mDevicePolicyManager.setApplicationRestrictions(getWho(), OTHER_PACKAGE, bundle1);
+        mDevicePolicyManager.setApplicationRestrictions(ADMIN_RECEIVER_COMPONENT, CTS_PACKAGE,
+                bundle0);
+        mDevicePolicyManager.setApplicationRestrictions(ADMIN_RECEIVER_COMPONENT, OTHER_PACKAGE,
+                bundle1);
 
         // Retrieve restrictions locally and make sure they are what we put in.
-        assertBundle0(mDevicePolicyManager.getApplicationRestrictions(getWho(), CTS_PACKAGE));
-        assertBundle1(mDevicePolicyManager.getApplicationRestrictions(getWho(), OTHER_PACKAGE));
+        assertBundle0(mDevicePolicyManager.getApplicationRestrictions(ADMIN_RECEIVER_COMPONENT,
+                CTS_PACKAGE));
+        assertBundle1(mDevicePolicyManager.getApplicationRestrictions(ADMIN_RECEIVER_COMPONENT,
+                OTHER_PACKAGE));
 
         // The test activity should have received a change_restriction broadcast
         // and relay the value back to us.
         assertBundle0(waitForChangedRestriction());
 
         // Test overwriting
-        mDevicePolicyManager.setApplicationRestrictions(getWho(), CTS_PACKAGE, bundle1);
-        assertBundle1(mDevicePolicyManager.getApplicationRestrictions(getWho(), CTS_PACKAGE));
+        mDevicePolicyManager.setApplicationRestrictions(ADMIN_RECEIVER_COMPONENT, CTS_PACKAGE,
+                bundle1);
+        assertBundle1(mDevicePolicyManager.getApplicationRestrictions(ADMIN_RECEIVER_COMPONENT,
+                CTS_PACKAGE));
         assertBundle1(waitForChangedRestriction());
 
         // Cleanup
-        mDevicePolicyManager.setApplicationRestrictions(getWho(), CTS_PACKAGE, new Bundle());
-        assertTrue(
-                mDevicePolicyManager.getApplicationRestrictions(getWho(), CTS_PACKAGE).isEmpty());
+        mDevicePolicyManager.setApplicationRestrictions(ADMIN_RECEIVER_COMPONENT, CTS_PACKAGE,
+                new Bundle());
+        assertTrue(mDevicePolicyManager.getApplicationRestrictions(
+                ADMIN_RECEIVER_COMPONENT, CTS_PACKAGE).isEmpty());
         assertTrue(waitForChangedRestriction().isEmpty());
-        mDevicePolicyManager.setApplicationRestrictions(getWho(), OTHER_PACKAGE, new Bundle());
-        assertTrue(
-                mDevicePolicyManager.getApplicationRestrictions(getWho(), OTHER_PACKAGE).isEmpty());
+        mDevicePolicyManager.setApplicationRestrictions(ADMIN_RECEIVER_COMPONENT, OTHER_PACKAGE,
+                new Bundle());
+        assertTrue(mDevicePolicyManager.getApplicationRestrictions(
+                ADMIN_RECEIVER_COMPONENT, OTHER_PACKAGE).isEmpty());
 
         finish();
     }
@@ -185,9 +193,9 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
-            if (ApplicationRestrictionActivity.REGISTERED_ACTION.equals(action)) {
+            if (ApplicationRestrictionsActivity.REGISTERED_ACTION.equals(action)) {
                 mOnRegisteredSemaphore.release();
-            } else if (ApplicationRestrictionActivity.RESTRICTION_ACTION.equals(action)) {
+            } else if (ApplicationRestrictionsActivity.RESTRICTION_ACTION.equals(action)) {
                 mReceivedRestrictions = intent.getBundleExtra("value");
                 mOnRestrictionSemaphore.release();
             }
@@ -196,7 +204,7 @@
 
     private void startTestActivity(String command) {
         Intent intent = new Intent();
-        intent.setClassName(PACKAGE_NAME, ApplicationRestrictionActivity.class.getName());
+        intent.setClassName(PACKAGE_NAME, ApplicationRestrictionsActivity.class.getName());
         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
 
         if (command != null) {
@@ -227,7 +235,7 @@
     }
 
     private void finish() {
-        startTestActivity(ApplicationRestrictionActivity.FINISH);
+        startTestActivity(ApplicationRestrictionsActivity.FINISH);
     }
 
 }
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
index 64c7262..01ba3d4 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
@@ -62,8 +62,6 @@
             android:name="com.android.cts.deviceowner.LockTaskTest$IntentReceivingActivity"
             android:taskAffinity="com.android.cts.deviceowner.LockTaskTest.IntentReceivingActivity"
             />
-        <activity
-            android:name="com.android.cts.deviceowner.ApplicationRestrictionActivity" />
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
index b0918bc..c08fabd 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
@@ -52,6 +52,13 @@
         super.tearDown();
     }
 
+    public void testApplicationRestrictions() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".ApplicationRestrictionsTest", mUserId));
+    }
+
     public void testPermissionGrant() throws Exception {
         if (!mHasFeature) {
             return;
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
index 26d0d233..7fe0d7b 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
@@ -66,10 +66,6 @@
         super.tearDown();
     }
 
-    public void testApplicationRestrictions() throws Exception {
-        executeDeviceOwnerTest("ApplicationRestrictionsTest");
-    }
-
     public void testCaCertManagement() throws Exception {
         executeDeviceOwnerTest("CaCertManagementTest");
     }