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