Home | History | Annotate | Download | only in finalization
      1 /*
      2  * Copyright 2016, 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 
     17 package com.android.managedprovisioning.finalization;
     18 
     19 import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE;
     20 import static android.app.admin.DevicePolicyManager.ACTION_MANAGED_PROFILE_PROVISIONED;
     21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
     22 import static org.mockito.Matchers.any;
     23 import static org.mockito.Mockito.doAnswer;
     24 import static org.mockito.Mockito.never;
     25 import static org.mockito.Mockito.verify;
     26 
     27 import android.accounts.Account;
     28 import android.content.Context;
     29 import android.content.Intent;
     30 import android.os.UserHandle;
     31 import android.test.AndroidTestCase;
     32 import android.test.suitebuilder.annotation.SmallTest;
     33 
     34 import com.android.managedprovisioning.common.Utils;
     35 
     36 import org.mockito.ArgumentCaptor;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.mockito.invocation.InvocationOnMock;
     40 
     41 import java.util.concurrent.Semaphore;
     42 import java.util.concurrent.TimeUnit;
     43 
     44 /**
     45  * Unit tests for {@link DpcReceivedSuccessReceiver}.
     46  */
     47 public class DpcReceivedSuccessReceiverTest extends AndroidTestCase {
     48     private static final int SEND_BROADCAST_TIMEOUT_SECONDS = 1;
     49     private static final String TEST_MDM_PACKAGE_NAME = "mdm.package.name";
     50     private static final Account TEST_ACCOUNT = new Account("test (at) account.com", "account.type");
     51     private static final Intent TEST_INTENT = new Intent(ACTION_PROFILE_PROVISIONING_COMPLETE);
     52     private static final UserHandle MANAGED_PROFILE_USER_HANDLE = UserHandle.of(123);
     53 
     54     @Mock private Context mContext;
     55     @Mock private Utils mUtils;
     56 
     57     @Override
     58     public void setUp() {
     59         // this is necessary for mockito to work
     60         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
     61         MockitoAnnotations.initMocks(this);
     62     }
     63 
     64     @SmallTest
     65     public void testNoAccountMigration() {
     66         // GIVEN that no account migration occurred during provisioning
     67         final DpcReceivedSuccessReceiver receiver = new DpcReceivedSuccessReceiver(null, false,
     68                 MANAGED_PROFILE_USER_HANDLE, TEST_MDM_PACKAGE_NAME, mUtils);
     69 
     70         // WHEN the profile provisioning complete intent was received by the DPC
     71         receiver.onReceive(mContext, TEST_INTENT);
     72 
     73         // THEN an intent should be sent to the primary user
     74         ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
     75         verify(mContext).sendBroadcast(intentCaptor.capture());
     76 
     77         // THEN the broadcast action is ACTION_MANAGED_PROFILE_PROVISIONED
     78         assertEquals(ACTION_MANAGED_PROFILE_PROVISIONED, intentCaptor.getValue().getAction());
     79 
     80         // THEN the receiver package is the DPC
     81         assertEquals(TEST_MDM_PACKAGE_NAME, intentCaptor.getValue().getPackage());
     82 
     83         // THEN the extra user handle should be of managed profile
     84         assertEquals(MANAGED_PROFILE_USER_HANDLE,
     85                 intentCaptor.getValue().getExtra(Intent.EXTRA_USER));
     86     }
     87 
     88     @SmallTest
     89     public void testAccountMigration() throws Exception {
     90         // GIVEN that account migration occurred during provisioning
     91         final DpcReceivedSuccessReceiver receiver = new DpcReceivedSuccessReceiver(TEST_ACCOUNT,
     92                 false /* keepAccountMigrated */, MANAGED_PROFILE_USER_HANDLE, TEST_MDM_PACKAGE_NAME,
     93                 mUtils);
     94 
     95         // WHEN receiver.onReceive is called
     96         invokeOnReceiveAndVerifyIntent(receiver);
     97 
     98         // THEN the account should have been removed from the primary user
     99         verify(mUtils).removeAccount(mContext, TEST_ACCOUNT);
    100     }
    101 
    102     @SmallTest
    103     public void testAccountCopy() throws Exception {
    104         // GIVEN that account copy occurred during provisioning
    105         final DpcReceivedSuccessReceiver receiver = new DpcReceivedSuccessReceiver(TEST_ACCOUNT,
    106                 true /* keepAccountMigrated */, MANAGED_PROFILE_USER_HANDLE, TEST_MDM_PACKAGE_NAME,
    107                 mUtils);
    108 
    109         // WHEN receiver.onReceive is called
    110         invokeOnReceiveAndVerifyIntent(receiver);
    111 
    112         // THEN the account is not removed from the primary user
    113         verify(mUtils, never()).removeAccount(mContext, TEST_ACCOUNT);
    114     }
    115 
    116     private void invokeOnReceiveAndVerifyIntent(final DpcReceivedSuccessReceiver receiver)
    117             throws InterruptedException {
    118         // prepare a semaphore to handle AsyncTask usage
    119         final Semaphore semaphore = new Semaphore(0);
    120         doAnswer((InvocationOnMock invocation) -> {
    121             semaphore.release(1);
    122             return null;
    123         }).when(mContext).sendBroadcast(any(Intent.class));
    124 
    125         // WHEN the profile provisioning complete intent was received by the DPC
    126         receiver.onReceive(mContext, TEST_INTENT);
    127 
    128         assertTrue(semaphore.tryAcquire(SEND_BROADCAST_TIMEOUT_SECONDS, TimeUnit.SECONDS));
    129 
    130         // THEN an intent should be sent to the primary user
    131         ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    132         verify(mContext).sendBroadcast(intentCaptor.capture());
    133 
    134         // THEN the broadcast action is ACTION_MANAGED_PROFILE_PROVISIONED
    135         assertEquals(ACTION_MANAGED_PROFILE_PROVISIONED, intentCaptor.getValue().getAction());
    136 
    137         // THEN the receiver package is the DPC
    138         assertEquals(TEST_MDM_PACKAGE_NAME, intentCaptor.getValue().getPackage());
    139 
    140         // THEN the extra user handle should be of managed profile
    141         assertEquals(MANAGED_PROFILE_USER_HANDLE,
    142                 intentCaptor.getValue().getExtra(Intent.EXTRA_USER));
    143 
    144         // THEN the account was added to the broadcast
    145         assertEquals(TEST_ACCOUNT, intentCaptor.getValue().getParcelableExtra(
    146                 EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE));
    147     }
    148 }
    149