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.Service;
     23 import android.app.admin.DeviceAdminReceiver;
     24 import android.app.admin.DevicePolicyManager;
     25 import android.content.ComponentName;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.SharedPreferences;
     29 import android.content.pm.PackageManager;
     30 import android.os.IBinder;
     31 import android.os.PersistableBundle;
     32 
     33 import androidx.test.InstrumentationRegistry;
     34 import androidx.test.filters.SmallTest;
     35 
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 
     39 import java.util.Set;
     40 
     41 @SmallTest
     42 public class DeviceAndProfileOwnerTransferIncomingTest {
     43     public static class BasicAdminReceiver extends DeviceAdminReceiver {
     44         public BasicAdminReceiver() {}
     45 
     46         @Override
     47         public void onTransferOwnershipComplete(Context context, PersistableBundle bundle) {
     48             putBooleanPref(context, KEY_TRANSFER_COMPLETED_CALLED, true);
     49         }
     50     }
     51 
     52     public static class BasicAdminService extends Service {
     53         @Override
     54         public void onCreate() {
     55             super.onCreate();
     56             putBooleanPref(getApplicationContext(), KEY_TRANSFER_ADMIN_SERVICE_BOUND, true);
     57         }
     58 
     59         @Override
     60         public IBinder onBind(Intent intent) {
     61             return null;
     62         }
     63     }
     64 
     65     public static class BasicAdminReceiverNoMetadata extends DeviceAdminReceiver {
     66         public BasicAdminReceiverNoMetadata() {}
     67     }
     68 
     69     private final static String SHARED_PREFERENCE_NAME = "shared-preference-name";
     70     private final static String KEY_TRANSFER_COMPLETED_CALLED = "key-transfer-completed-called";
     71     private final static String KEY_TRANSFER_ADMIN_SERVICE_BOUND =
     72         "key-transfer-admin-service-bound";
     73     private final static String ARE_PARAMETERS_SAVED = "ARE_PARAMETERS_SAVED";
     74 
     75     protected Context mContext;
     76     protected ComponentName mIncomingComponentName;
     77     protected DevicePolicyManager mDevicePolicyManager;
     78     protected boolean mHasSecureLockScreen;
     79 
     80     @Before
     81     public void setUp() throws Exception {
     82         mContext = InstrumentationRegistry.getTargetContext();
     83         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
     84         mIncomingComponentName = new ComponentName(mContext, BasicAdminReceiver.class.getName());
     85         mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature(
     86                 PackageManager.FEATURE_SECURE_LOCK_SCREEN);
     87     }
     88 
     89     @Test
     90     public void testTransferCompleteCallbackIsCalled() {
     91         assertTrue(getBooleanPref(mContext, KEY_TRANSFER_COMPLETED_CALLED));
     92     }
     93 
     94     @Test
     95     public void testIsAffiliationId1() {
     96         assertEquals("id.number.1", getAffiliationId());
     97     }
     98 
     99     private String getAffiliationId() {
    100         ComponentName admin = mIncomingComponentName;
    101         DevicePolicyManager dpm = (DevicePolicyManager)
    102                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
    103         Set<String> affiliationIds = dpm.getAffiliationIds(admin);
    104         assertNotNull(affiliationIds);
    105         assertEquals(1, affiliationIds.size());
    106         return affiliationIds.iterator().next();
    107     }
    108 
    109     @Test
    110     public void testTransferOwnershipBundleLoaded() throws Throwable {
    111         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
    112         assertNotNull(bundle);
    113         assertTrue(bundle.getBoolean(ARE_PARAMETERS_SAVED));
    114     }
    115 
    116     @Test
    117     public void testTransferOwnershipEmptyBundleLoaded() throws Throwable {
    118         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
    119         assertNotNull(bundle);
    120         assertTrue(bundle.isEmpty());
    121     }
    122 
    123     @Test
    124     public void testAdminServiceIsBound() {
    125         assertTrue(getBooleanPref(mContext, KEY_TRANSFER_ADMIN_SERVICE_BOUND));
    126     }
    127 
    128     private static SharedPreferences getPrefs(Context context) {
    129         return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
    130     }
    131 
    132     private static void putBooleanPref(Context context, String key, boolean value) {
    133         getPrefs(context).edit().putBoolean(key, value).apply();
    134     }
    135 
    136     protected static boolean getBooleanPref(Context context, String key) {
    137         return getPrefs(context).getBoolean(key, false);
    138     }
    139 }
    140