1 /*
2  * Copyright (C) 2022 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 android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
20 
21 import static com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.STATE_DOWNLOADED;
22 import static com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.STATE_DOWNLOADING;
23 import static com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.STATE_ERROR;
24 import static com.android.managedprovisioning.preprovisioning.DownloadRoleHolderViewModel.STATE_IDLE;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import android.app.Instrumentation;
29 import android.content.ComponentName;
30 
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import com.android.managedprovisioning.common.SettingsFacade;
34 import com.android.managedprovisioning.common.Utils;
35 import com.android.managedprovisioning.model.ProvisioningParams;
36 import com.android.managedprovisioning.util.LazyStringResource;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.JUnit4;
42 
43 @RunWith(JUnit4.class)
44 public class DownloadRoleHolderViewModelTest {
45 
46     private static final ComponentName ADMIN = new ComponentName("com.test.admin", ".Receiver");
47     private static final ProvisioningParams PROVISIONING_PARAMS = new ProvisioningParams.Builder()
48             .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
49             .setDeviceAdminComponentName(ADMIN)
50             .build();
51     private static final String ROLE_HOLDER_PACKAGE_NAME = "test.roleholder.package";
52     private static final int DIALOG_TITLE_RES_ID = 1;
53     private static final int DIALOG_MESSAGE_RES_ID = 2;
54     private static final String DIALOG_MESSAGE = "dialog message";
55     private DownloadRoleHolderViewModel mViewModel;
56     private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
57 
58     @Before
setUp()59     public void setUp() {
60         mViewModel = new DownloadRoleHolderViewModel(
61                 PROVISIONING_PARAMS, new Utils(), new SettingsFacade(), ROLE_HOLDER_PACKAGE_NAME);
62     }
63 
64     @Test
constructor_initialStateIdle()65     public void constructor_initialStateIdle() {
66         blockUntilNextUiThreadCycle();
67 
68         assertThat(mViewModel.observeState().getValue()).isEqualTo(STATE_IDLE);
69     }
70 
71     @Test
connectToNetworkAndDownloadRoleHolder_goesToDownloadingState()72     public void connectToNetworkAndDownloadRoleHolder_goesToDownloadingState() {
73         mViewModel.connectToNetworkAndDownloadRoleHolder(
74                 mInstrumentation.getContext());
75         blockUntilNextUiThreadCycle();
76 
77         assertThat(mViewModel.observeState().getValue()).isEqualTo(STATE_DOWNLOADING);
78     }
79 
80     @Test
provisioningTasksCompleted_goesToDownloadedState()81     public void provisioningTasksCompleted_goesToDownloadedState() {
82         mViewModel.provisioningTasksCompleted();
83         blockUntilNextUiThreadCycle();
84 
85         assertThat(mViewModel.observeState().getValue()).isEqualTo(STATE_DOWNLOADED);
86     }
87 
88     @Test
error_withMessageResId_goesToErrorState()89     public void error_withMessageResId_goesToErrorState() {
90         mViewModel.error(DIALOG_TITLE_RES_ID, DIALOG_MESSAGE_RES_ID, false);
91         blockUntilNextUiThreadCycle();
92 
93         assertThat(mViewModel.observeState().getValue()).isEqualTo(STATE_ERROR);
94     }
95 
96     @Test
error_withMessageText_goesToErrorState()97     public void error_withMessageText_goesToErrorState() {
98         mViewModel.error(DIALOG_TITLE_RES_ID, DIALOG_MESSAGE, false);
99         blockUntilNextUiThreadCycle();
100 
101         assertThat(mViewModel.observeState().getValue()).isEqualTo(STATE_ERROR);
102     }
103 
104     @Test
getError_withMessageResId_works()105     public void getError_withMessageResId_works() {
106         mInstrumentation.runOnMainSync(
107                 () -> {
108                     mViewModel.error(DIALOG_TITLE_RES_ID, DIALOG_MESSAGE_RES_ID,
109                             /* factoryResetRequired= */ true);
110 
111                     assertThat(mViewModel.getError().dialogTitleId).isEqualTo(DIALOG_TITLE_RES_ID);
112                     assertThat(mViewModel.getError().errorMessageRes).isInstanceOf(
113                             LazyStringResource.class);
114                     assertThat(mViewModel.getError().factoryResetRequired).isTrue();
115                 });
116     }
117 
118     @Test
getError_errorStateFollowedByNonErrorState_isNull()119     public void getError_errorStateFollowedByNonErrorState_isNull() {
120         mInstrumentation.runOnMainSync(
121                 () -> {
122                     mViewModel.error(DIALOG_TITLE_RES_ID, DIALOG_MESSAGE_RES_ID,
123                             /* factoryResetRequired= */ true);
124                     mViewModel.connectToNetworkAndDownloadRoleHolder(mInstrumentation.getContext());
125 
126                     assertThat(mViewModel.getError()).isNull();
127                 });
128     }
129 
130     @Test
getError_withMessageText_isNotNull()131     public void getError_withMessageText_isNotNull() {
132         mInstrumentation.runOnMainSync(
133                 () -> {
134                     mViewModel.error(DIALOG_TITLE_RES_ID, DIALOG_MESSAGE,
135                             /* factoryResetRequired= */ true);
136 
137                     assertThat(mViewModel.getError().dialogTitleId).isEqualTo(DIALOG_TITLE_RES_ID);
138                     assertThat(mViewModel.getError().errorMessageRes).isInstanceOf(
139                             LazyStringResource.class);
140                     assertThat(mViewModel.getError().factoryResetRequired).isTrue();
141                 });
142     }
143 
144     @Test
getError_nullByDefault()145     public void getError_nullByDefault() {
146         assertThat(mViewModel.getError()).isNull();
147     }
148 
blockUntilNextUiThreadCycle()149     private void blockUntilNextUiThreadCycle() {
150         InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
151         });
152     }
153 }
154