1 /* 2 * Copyright (C) 2021 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.preprovisioning; 18 19 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_GETTING_PROVISIONING_MODE; 20 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_PREPROVISIONING_INITIALIZED; 21 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_PREPROVISIONING_INITIALIZING; 22 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_PROVISIONING_FINALIZED; 23 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_PROVISIONING_STARTED; 24 import static com.android.managedprovisioning.preprovisioning.PreProvisioningViewModel.STATE_SHOWING_USER_CONSENT; 25 26 import static com.google.common.truth.Truth.assertThat; 27 28 import static org.testng.Assert.assertThrows; 29 30 import android.app.Instrumentation; 31 import android.content.Context; 32 import android.content.Intent; 33 34 import androidx.test.InstrumentationRegistry; 35 import androidx.test.filters.SmallTest; 36 37 import com.android.managedprovisioning.TestUtils; 38 import com.android.managedprovisioning.analytics.TimeLogger; 39 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException; 40 import com.android.managedprovisioning.parser.MessageParser; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 45 @SmallTest 46 public final class PreProvisioningViewModelTest { 47 48 private final Instrumentation mInstrumentation = 49 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(); 50 private PreProvisioningViewModel mViewModel; 51 private TimeLogger mTimeLogger; 52 private EncryptionController mEncryptionController; 53 private final Context mContext = InstrumentationRegistry.getTargetContext(); 54 55 @Before setUp()56 public void setUp() { 57 mTimeLogger = new TimeLogger(mContext, /* category */ 0); 58 MessageParser messageParser = new MessageParser(mContext); 59 mEncryptionController = TestUtils.createEncryptionController(mContext); 60 mViewModel = new PreProvisioningViewModel( 61 mTimeLogger, 62 messageParser, 63 mEncryptionController); 64 } 65 66 @Test getState_defaultsToInitializing()67 public void getState_defaultsToInitializing() { 68 assertThat(mViewModel.getState().getValue()) 69 .isEqualTo(STATE_PREPROVISIONING_INITIALIZING); 70 } 71 72 @Test onReturnFromProvisioning_stateIsProvisioningFinalized()73 public void onReturnFromProvisioning_stateIsProvisioningFinalized() { 74 mInstrumentation.runOnMainSync( 75 () -> { 76 mViewModel.onReturnFromProvisioning(); 77 78 assertThat(mViewModel.getState().getValue()) 79 .isEqualTo(STATE_PROVISIONING_FINALIZED); 80 }); 81 } 82 83 @Test onAdminIntegratedFlowInitiated_stateIsGettingProvisioningMode()84 public void onAdminIntegratedFlowInitiated_stateIsGettingProvisioningMode() { 85 mInstrumentation.runOnMainSync( 86 () -> { 87 mViewModel.onAdminIntegratedFlowInitiated(); 88 assertThat(mViewModel.getState().getValue()) 89 .isEqualTo(STATE_GETTING_PROVISIONING_MODE); 90 }); 91 } 92 93 @Test onShowUserConsent_stateIsShowingUserConsent()94 public void onShowUserConsent_stateIsShowingUserConsent() { 95 mInstrumentation.runOnMainSync( 96 () -> { 97 mViewModel.onShowUserConsent(); 98 99 assertThat(mViewModel.getState().getValue()) 100 .isEqualTo(STATE_SHOWING_USER_CONSENT); 101 }); 102 } 103 104 @Test onProvisioningStartedAfterUserConsent_stateIsProvisioningStarted()105 public void onProvisioningStartedAfterUserConsent_stateIsProvisioningStarted() { 106 mInstrumentation.runOnMainSync( 107 () -> { 108 mViewModel.onProvisioningStartedAfterUserConsent(); 109 110 assertThat(mViewModel.getState().getValue()) 111 .isEqualTo(STATE_PROVISIONING_STARTED); 112 }); 113 } 114 115 @Test onProvisioningInitiated_stateIsProvisioningInitialized()116 public void onProvisioningInitiated_stateIsProvisioningInitialized() { 117 mInstrumentation.runOnMainSync( 118 () -> { 119 mViewModel.onProvisioningInitiated(); 120 121 assertThat(mViewModel.getState().getValue()) 122 .isEqualTo(STATE_PREPROVISIONING_INITIALIZED); 123 }); 124 } 125 126 @Test getEncryptionController_valuesAreEqual()127 public void getEncryptionController_valuesAreEqual() { 128 assertThat(mViewModel.getEncryptionController()) 129 .isEqualTo(mEncryptionController); 130 } 131 132 @Test getTimeLogger_valuesAreEqual()133 public void getTimeLogger_valuesAreEqual() { 134 assertThat(mViewModel.getTimeLogger()) 135 .isEqualTo(mTimeLogger); 136 } 137 138 @Test loadParamsIfNecessary_invalidParams_throwsException()139 public void loadParamsIfNecessary_invalidParams_throwsException() { 140 Intent invalidIntent = new Intent(); 141 142 assertThrows( 143 IllegalProvisioningArgumentException.class, 144 () -> mViewModel.loadParamsIfNecessary(invalidIntent)); 145 } 146 } 147