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 {
BasicAdminReceiver()44         public BasicAdminReceiver() {}
45 
46         @Override
onTransferOwnershipComplete(Context context, PersistableBundle bundle)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
onBind(Intent intent)54         public IBinder onBind(Intent intent) {
55             return null;
56         }
57     }
58 
59     public static class BasicAdminReceiverNoMetadata extends DeviceAdminReceiver {
BasicAdminReceiverNoMetadata()60         public BasicAdminReceiverNoMetadata() {}
61     }
62 
63     private final static String SHARED_PREFERENCE_NAME = "shared-preference-name";
64     private final static String KEY_TRANSFER_COMPLETED_CALLED = "key-transfer-completed-called";
65     private final static String ARE_PARAMETERS_SAVED = "ARE_PARAMETERS_SAVED";
66 
67     protected Context mContext;
68     protected ComponentName mIncomingComponentName;
69     protected DevicePolicyManager mDevicePolicyManager;
70     protected boolean mHasSecureLockScreen;
71 
72     @Before
setUp()73     public void setUp() throws Exception {
74         mContext = InstrumentationRegistry.getTargetContext();
75         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
76         mIncomingComponentName = new ComponentName(mContext, BasicAdminReceiver.class.getName());
77         mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature(
78                 PackageManager.FEATURE_SECURE_LOCK_SCREEN);
79     }
80 
81     @Test
testTransferCompleteCallbackIsCalled()82     public void testTransferCompleteCallbackIsCalled() {
83         assertTrue(getBooleanPref(mContext, KEY_TRANSFER_COMPLETED_CALLED));
84     }
85 
86     @Test
testIsAffiliationId1()87     public void testIsAffiliationId1() {
88         assertEquals("id.number.1", getAffiliationId());
89     }
90 
getAffiliationId()91     private String getAffiliationId() {
92         ComponentName admin = mIncomingComponentName;
93         DevicePolicyManager dpm = (DevicePolicyManager)
94                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
95         Set<String> affiliationIds = dpm.getAffiliationIds(admin);
96         assertNotNull(affiliationIds);
97         assertEquals(1, affiliationIds.size());
98         return affiliationIds.iterator().next();
99     }
100 
101     @Test
testTransferOwnershipBundleLoaded()102     public void testTransferOwnershipBundleLoaded() throws Throwable {
103         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
104         assertNotNull(bundle);
105         assertTrue(bundle.getBoolean(ARE_PARAMETERS_SAVED));
106     }
107 
108     @Test
testTransferOwnershipEmptyBundleLoaded()109     public void testTransferOwnershipEmptyBundleLoaded() throws Throwable {
110         PersistableBundle bundle = mDevicePolicyManager.getTransferOwnershipBundle();
111         assertNotNull(bundle);
112         assertTrue(bundle.isEmpty());
113     }
114 
getPrefs(Context context)115     private static SharedPreferences getPrefs(Context context) {
116         return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
117     }
118 
putBooleanPref(Context context, String key, boolean value)119     private static void putBooleanPref(Context context, String key, boolean value) {
120         getPrefs(context).edit().putBoolean(key, value).apply();
121     }
122 
getBooleanPref(Context context, String key)123     protected static boolean getBooleanPref(Context context, String key) {
124         return getPrefs(context).getBoolean(key, false);
125     }
126 }
127