• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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.settings.development.linuxterminal;
18 
19 import static android.system.virtualmachine.VirtualMachineManager.CAPABILITY_NON_PROTECTED_VM;
20 
21 import static com.android.settings.development.linuxterminal.LinuxTerminalPreferenceController.MEMORY_MIN_BYTES;
22 import static com.android.settings.development.linuxterminal.LinuxTerminalPreferenceController.STORAGE_MIN_BYTES;
23 import static com.android.settings.development.linuxterminal.LinuxTerminalPreferenceController.TERMINAL_PACKAGE_NAME_RESID;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.eq;
31 
32 import android.content.Context;
33 import android.content.pm.PackageInfo;
34 import android.content.pm.PackageManager;
35 import android.content.pm.PackageManager.NameNotFoundException;
36 import android.os.storage.StorageManager;
37 import android.system.virtualmachine.VirtualMachineManager;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RobolectricTestRunner;
45 
46 /** Tests {@link LinuxTerminalPreferenceController} */
47 @RunWith(RobolectricTestRunner.class)
48 public class LinuxTerminalPreferenceControllerTest {
49     private static final String TERMINAL_PACKAGE_NAME = "com.android.virtualization.terminal";
50 
51     @Mock private Context mContext;
52     @Mock private PackageManager mPackageManager;
53     @Mock private StorageManager mStorageManager;
54     @Mock private VirtualMachineManager mVirtualMachineManager;
55     @Mock private PackageInfo mPackageInfo;
56 
57     private LinuxTerminalPreferenceController mController;
58 
59     @Before
setup()60     public void setup() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         doReturn(TERMINAL_PACKAGE_NAME).when(mContext).getString(TERMINAL_PACKAGE_NAME_RESID);
63 
64         doReturn(mPackageManager).when(mContext).getPackageManager();
65         doReturn(mPackageInfo)
66                 .when(mPackageManager)
67                 .getPackageInfo(eq(TERMINAL_PACKAGE_NAME), anyInt());
68 
69         doReturn(mStorageManager).when(mContext).getSystemService(StorageManager.class);
70         doReturn(STORAGE_MIN_BYTES).when(mStorageManager).getPrimaryStorageSize();
71 
72         doReturn(mVirtualMachineManager)
73                 .when(mContext)
74                 .getSystemService(VirtualMachineManager.class);
75         doReturn(CAPABILITY_NON_PROTECTED_VM).when(mVirtualMachineManager).getCapabilities();
76     }
77 
78     @Test
isAvailable_whenMemoryInsufficient_returnFalse()79     public void isAvailable_whenMemoryInsufficient_returnFalse() {
80         mController = createController(mContext, MEMORY_MIN_BYTES / 2);
81 
82         assertThat(mController.isAvailable()).isFalse();
83     }
84 
85     @Test
isAvailable_whenDeviceStorageInsufficient_returnFalse()86     public void isAvailable_whenDeviceStorageInsufficient_returnFalse() {
87         doReturn(STORAGE_MIN_BYTES / 2).when(mStorageManager).getPrimaryStorageSize();
88 
89         mController = createController(mContext);
90 
91         assertThat(mController.isAvailable()).isFalse();
92     }
93 
94     @Test
isAvailable_whenVmNotSupported_returnFalse()95     public void isAvailable_whenVmNotSupported_returnFalse() {
96         doReturn(0).when(mVirtualMachineManager).getCapabilities();
97 
98         mController = createController(mContext);
99 
100         assertThat(mController.isAvailable()).isFalse();
101     }
102 
103     @Test
isAvailable_whenPackageExists_returnsTrue()104     public void isAvailable_whenPackageExists_returnsTrue() {
105         mController = createController(mContext);
106 
107         assertThat(mController.isAvailable()).isTrue();
108     }
109 
110     @Test
isAvailable_whenPackageNameIsNull_returnsFalse()111     public void isAvailable_whenPackageNameIsNull_returnsFalse() {
112         doReturn(null).when(mContext).getString(TERMINAL_PACKAGE_NAME_RESID);
113 
114         mController = createController(mContext);
115 
116         assertThat(mController.isAvailable()).isFalse();
117     }
118 
119     @Test
isAvailable_whenAppDoesNotExist_returnsFalse()120     public void isAvailable_whenAppDoesNotExist_returnsFalse() throws Exception {
121         doThrow(new NameNotFoundException())
122                 .when(mPackageManager)
123                 .getPackageInfo(eq(TERMINAL_PACKAGE_NAME), anyInt());
124 
125         mController = createController(mContext);
126 
127         assertThat(mController.isAvailable()).isFalse();
128     }
129 
createController(Context context)130     private LinuxTerminalPreferenceController createController(Context context) {
131         return createController(context, MEMORY_MIN_BYTES);
132     }
133 
createController(Context context, long totalMemory)134     private LinuxTerminalPreferenceController createController(Context context, long totalMemory) {
135         return new LinuxTerminalPreferenceController(context) {
136             @Override
137             public long getTotalMemory() {
138                 return totalMemory;
139             }
140         };
141     }
142 }
143