Fix CTS for DPM.resetPassword().

The issue is that DAs are no longer allowed to clear the password.
Because the test originally only had DAs, resetPassword("") in setUp()
was broken.

Now we register DO too so DPM will happily accept resetPassword("").

The resetPassowrd() API doesn't take "who" unlike other APIs --
it'll just allow it if the caller APK has DO.

Bug 25740005

Change-Id: Ie9139bab28090b8e1d85a361b92dd42ef6b1813f
diff --git a/tests/admin/AndroidTest.xml b/tests/admin/AndroidTest.xml
index 80b5b7b..0e8d35d 100644
--- a/tests/admin/AndroidTest.xml
+++ b/tests/admin/AndroidTest.xml
@@ -23,6 +23,7 @@
     <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
         <option name="run-command" value="dpm set-active-admin android.admin.app/.CtsDeviceAdminReceiver" />
         <option name="run-command" value="dpm set-active-admin android.admin.app/.CtsDeviceAdminReceiver2" />
+        <option name="run-command" value="dpm set-device-owner android.admin.app/.CtsDeviceAdminDeviceOwner" />
     </target_preparer>
 
     <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
diff --git a/tests/admin/app/AndroidManifest.xml b/tests/admin/app/AndroidManifest.xml
index 4f63bdc..0834579 100644
--- a/tests/admin/app/AndroidManifest.xml
+++ b/tests/admin/app/AndroidManifest.xml
@@ -22,6 +22,15 @@
 
         <uses-library android:name="android.test.runner"/>
 
+        <receiver android:name="android.admin.app.CtsDeviceAdminDeviceOwner"
+            android:permission="android.permission.BIND_DEVICE_ADMIN">
+            <meta-data android:name="android.app.device_admin"
+                android:resource="@xml/device_admin" />
+            <intent-filter>
+                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
+            </intent-filter>
+        </receiver>
+
         <receiver android:name="android.admin.app.CtsDeviceAdminReceiver"
                 android:permission="android.permission.BIND_DEVICE_ADMIN">
             <meta-data android:name="android.app.device_admin"
diff --git a/tests/admin/app/src/android/admin/app/CtsDeviceAdminDeviceOwner.java b/tests/admin/app/src/android/admin/app/CtsDeviceAdminDeviceOwner.java
new file mode 100644
index 0000000..619f700
--- /dev/null
+++ b/tests/admin/app/src/android/admin/app/CtsDeviceAdminDeviceOwner.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2015 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.admin.app;
+
+import android.app.admin.DeviceAdminReceiver;
+
+public class CtsDeviceAdminDeviceOwner extends DeviceAdminReceiver {
+
+}
diff --git a/tests/admin/app/src/android/admin/app/DeactivationTest.java b/tests/admin/app/src/android/admin/app/DeactivationTest.java
index 03c4375..d6a7bf8 100644
--- a/tests/admin/app/src/android/admin/app/DeactivationTest.java
+++ b/tests/admin/app/src/android/admin/app/DeactivationTest.java
@@ -31,12 +31,22 @@
     private static final ComponentName RECEIVER2 = new ComponentName(PACKAGE,
             CtsDeviceAdminReceiver2.class.getName());
 
+    // Device admin we use to reset password.  Note profile owner can do this too,
+    // but DPM.clearProfileOwner() is hidden, so we just use a device owner.
+    private static final ComponentName DEVICE_OWNER = new ComponentName(PACKAGE,
+            CtsDeviceAdminDeviceOwner.class.getName());
+
     public void testDeactivateAdmins() throws Exception {
         DevicePolicyManager manager = (DevicePolicyManager)
                 getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
         assertNotNull(manager);
+
+        manager.clearDeviceOwnerApp(PACKAGE);
+
         manager.removeActiveAdmin(RECEIVER1);
         manager.removeActiveAdmin(RECEIVER2);
+        manager.removeActiveAdmin(DEVICE_OWNER);
+
         for (int i = 0; i < 1000 && isActive(manager); i++) {
             try {
                 Thread.sleep(100);
@@ -50,6 +60,6 @@
     private boolean isActive(DevicePolicyManager manager) {
         return manager.isAdminActive(RECEIVER1) ||
                 manager.isAdminActive(RECEIVER2) ||
-                manager.isDeviceOwnerApp(PACKAGE);
+                manager.isAdminActive(DEVICE_OWNER);
     }
 }
\ No newline at end of file
diff --git a/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java b/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
index ca1b340..a571da5 100644
--- a/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
+++ b/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
@@ -42,6 +42,11 @@
                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
     }
 
+    static ComponentName getDeviceOwnerReceiverComponent() {
+        return new ComponentName("android.admin.app",
+                "android.admin.app.CtsDeviceAdminProfileOwner");
+    }
+
     static ComponentName getReceiverComponent() {
         return new ComponentName("android.admin.app", "android.admin.app.CtsDeviceAdminReceiver");
     }
diff --git a/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java b/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
index 432cce5..648aed7 100644
--- a/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
+++ b/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
@@ -29,6 +29,7 @@
 import android.os.UserManager;
 import android.provider.Settings;
 import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.Suppress;
 import android.util.Log;
 
 import java.util.List;
@@ -45,6 +46,7 @@
     private static final String TAG = DevicePolicyManagerTest.class.getSimpleName();
 
     private DevicePolicyManager mDevicePolicyManager;
+    private ComponentName mDeviceOwnerComponent;
     private ComponentName mComponent;
     private ComponentName mSecondComponent;
     private boolean mDeviceAdmin;
@@ -75,6 +77,7 @@
         super.setUp();
         mDevicePolicyManager = (DevicePolicyManager)
                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        mDeviceOwnerComponent = DeviceAdminInfoTest.getDeviceOwnerReceiverComponent();
         mComponent = DeviceAdminInfoTest.getReceiverComponent();
         mPackageManager = mContext.getPackageManager();
         mSecondComponent = DeviceAdminInfoTest.getSecondReceiverComponent();
@@ -98,6 +101,8 @@
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
         mDevicePolicyManager.setPasswordMinimumLength(mComponent, 0);
+
+        // Note resetPassword() doesn't take "who", but because the caller package has
         assertTrue(mDevicePolicyManager.resetPassword("", 0));
     }
 
@@ -674,6 +679,9 @@
         }
     }
 
+    // This test registers itself as DO, so this is no longer testable.  We do a positive test
+    // for clearDeviceOwnerApp()
+    @Suppress
     public void testClearDeviceOwnerApp_failIfNotDeviceOwner() {
         if (!mDeviceAdmin) {
             Log.w(TAG, "Skipping testClearDeviceOwnerApp_failIfNotDeviceOwner");