Home | History | Annotate | Download | only in transferowner
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.cts.transferowner;
     17 
     18 import static junit.framework.Assert.assertEquals;
     19 import static junit.framework.Assert.assertNotNull;
     20 import static junit.framework.Assert.assertTrue;
     21 
     22 import android.app.admin.DeviceAdminReceiver;
     23 import android.app.admin.DevicePolicyManager;
     24 import android.content.ComponentName;
     25 import android.content.Context;
     26 import android.content.SharedPreferences;
     27 import android.os.PersistableBundle;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.support.test.filters.SmallTest;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 
     34 import java.util.Set;
     35 
     36 @SmallTest
     37 public class DeviceAndProfileOwnerTransferIncomingTest {
     38     public static class BasicAdminReceiver extends DeviceAdminReceiver {
     39         public BasicAdminReceiver() {}
     40 
     41         @Override
     42         public void onTransferOwnershipComplete(Context context, PersistableBundle bundle) {
     43             putBooleanPref(context, KEY_TRANSFER_COMPLETED_CALLED, true);
     44         }
     45     }
     46 
     47     public static class BasicAdminReceiverNoMetadata extends DeviceAdminReceiver {
     48         public BasicAdminReceiverNoMetadata() {}
     49     }
     50 
     51     private final static String SHARED_PREFERENCE_NAME = "shared-preference-name";
     52     private final static String KEY_TRANSFER_COMPLETED_CALLED = "key-transfer-completed-called";
     53     private final static String ARE_PARAMETERS_SAVED = "ARE_PARAMETERS_SAVED";
     54 
     55     protected Context mContext;
     56     protected ComponentName mIncomingComponentName;
     57     protected DevicePolicyManager mDevicePolicyManager;
     58 
     59     @Before
     60     public void setUp() throws Exception {
     61         mContext = InstrumentationRegistry.getTargetContext();
     62         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
     63         mIncomingComponentName = new ComponentName(mContext, BasicAdminReceiver.class.getName());
     64     }
     65 
     66     @Test
     67     public void testTransferCompleteCallbackIsCalled() {
     68         assertTrue(getBooleanPref(mContext, KEY_TRANSFER_COMPLETED_CALLED));
     69     }
     70 
     71     @Test
     72     public void testIsAffiliationId1() {
     73         assertEquals("id.number.1", getAffiliationId());
     74     }
     75 
     76     private String getAffiliationId() {
     77         ComponentName admin = mIncomingComponentName;
     78         DevicePolicyManager dpm = (DevicePolicyManager)
     79                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
     80         Set<String> affiliationIds = dpm.getAffiliationIds(admin);
     81         assertNotNull(affiliationIds);
     82         assertEquals(1, affiliationIds.size());
     83         return affiliationIds.iterator().next();
     84     }
     85 
     86     @Test
     87     public void testTransferOwnershipBundleLoaded() throws Throwable {
     88         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
     89         assertNotNull(bundle);
     90         assertTrue(bundle.getBoolean(ARE_PARAMETERS_SAVED));
     91     }
     92 
     93     @Test
     94     public void testTransferOwnershipEmptyBundleLoaded() throws Throwable {
     95         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
     96         assertNotNull(bundle);
     97         assertTrue(bundle.isEmpty());
     98     }
     99 
    100     private static SharedPreferences getPrefs(Context context) {
    101         return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
    102     }
    103 
    104     private static void putBooleanPref(Context context, String key, boolean value) {
    105         getPrefs(context).edit().putBoolean(key, value).apply();
    106     }
    107 
    108     protected static boolean getBooleanPref(Context context, String key) {
    109         return getPrefs(context).getBoolean(key, false);
    110     }
    111 }
    112