/*
 * 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 com.android.managedprovisioning.task;

import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE;
import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.test.AndroidTestCase;

import androidx.test.filters.SmallTest;

import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
import com.android.managedprovisioning.common.Utils;
import com.android.managedprovisioning.model.ProvisioningParams;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class SetDeviceOwnerPolicyTaskTest extends AndroidTestCase {
    private static final String ADMIN_PACKAGE_NAME = "com.admin.test";
    private static final String ADMIN_RECEIVER_NAME = ADMIN_PACKAGE_NAME + ".AdminReceiver";
    private static final ComponentName ADMIN_COMPONENT_NAME = new ComponentName(ADMIN_PACKAGE_NAME,
            ADMIN_RECEIVER_NAME);
    private static final int TEST_USER_ID = 123;

    @Mock private Context mContext;
    @Mock private PackageManager mPackageManager;
    @Mock private DevicePolicyManager mDevicePolicyManager;
    @Mock private AbstractProvisioningTask.Callback mCallback;
    @Mock private Utils mUtils;

    private SetDeviceOwnerPolicyTask mTask;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // This is necessary for mockito to work
        System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
        MockitoAnnotations.initMocks(this);

        when(mContext.getPackageManager()).thenReturn(mPackageManager);
        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
                .thenReturn(mDevicePolicyManager);
        when(mContext.getResources()).thenReturn(getContext().getResources());

        when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        when(mUtils.getCurrentDeviceOwnerComponentName(mDevicePolicyManager)).thenReturn(null);
        when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID))
                .thenReturn(true);
        when(mUtils.findDeviceAdmin(null, ADMIN_COMPONENT_NAME, mContext, TEST_USER_ID))
                .thenReturn(ADMIN_COMPONENT_NAME);
    }

    @SmallTest
    public void testEnableDevicePolicyApp_DefaultToDefault() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);
        // GIVEN that the management app is currently the manifest default
        when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN the management app should still be default
        verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
                anyInt(), anyInt());
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testEnableDevicePolicyApp_DisabledToDefault() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);
        // GIVEN that the management app is currently disabled
        when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN the management app should have been enabled
        verify(mPackageManager).setApplicationEnabledSetting(ADMIN_PACKAGE_NAME,
                PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                PackageManager.DONT_KILL_APP);
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testEnableDevicePolicyApp_EnabledToEnabled() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);
        // GIVEN that the management app is currently enabled
        when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
                .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN the management app should have been untouched
        verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
                anyInt(), anyInt());
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testEnableDevicePolicyApp_PackageNotFound() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);
        // GIVEN that the management app is not present on the device
        when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
                .thenThrow(new IllegalArgumentException());

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN an error should be returned
        verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetActiveAdmin() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN the management app should have been set as active admin
        verify(mDevicePolicyManager).setActiveAdmin(ADMIN_COMPONENT_NAME, true, TEST_USER_ID);
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetDeviceOwner() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN the management app should have been set as device owner
        verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetDeviceOwner_PreconditionsNotMet() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);

        // GIVEN that setting device owner is not currently allowed
        when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME,
                TEST_USER_ID)).thenThrow(new IllegalStateException());

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN an error should be returned
        verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetDeviceOwner_ReturnFalse() {
        // GIVEN that we are provisioning device owner
        createTask(ACTION_PROVISION_MANAGED_DEVICE);

        // GIVEN that setting device owner fails
        when(mDevicePolicyManager.setDeviceOwner(
                ADMIN_COMPONENT_NAME, TEST_USER_ID)).thenReturn(false);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN an error should be returned
        verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice() {
        // GIVEN that we are provisioning a financed device.
        createTask(ACTION_PROVISION_FINANCED_DEVICE);

        // WHEN running the task.
        mTask.run(TEST_USER_ID);

        // THEN the device owner type should have been set as financed when there is a device
        // owner.
        verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
        verify(mDevicePolicyManager).setDeviceOwnerType(
                ADMIN_COMPONENT_NAME, DEVICE_OWNER_TYPE_FINANCED);
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    @SmallTest
    public void testSetDeviceOwnerType_asDeviceOwner_notCalledWhenProvisioningManagedDevice() {
        // GIVEN that we are provisioning a managed device.
        createTask(ACTION_PROVISION_MANAGED_DEVICE);

        // WHEN running the task
        mTask.run(TEST_USER_ID);

        // THEN setting the device owner type should not have been called.
        verify(mDevicePolicyManager, never()).setDeviceOwnerType(any(), anyInt());
        verify(mCallback).onSuccess(mTask);
        verifyNoMoreInteractions(mCallback);
    }

    private void createTask(String action) {
        ProvisioningParams params = new ProvisioningParams.Builder()
                .setDeviceAdminComponentName(ADMIN_COMPONENT_NAME)
                .setProvisioningAction(action)
                .build();
        mTask = new SetDeviceOwnerPolicyTask(mUtils, mContext, params, mCallback,
                mock(ProvisioningAnalyticsTracker.class));
    }
}
