Debian Codegraph @

libartservice/service/javatests/com/android/server/art/UtilsTest.java

raw
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.server.art;
18
19 import static com.google.common.truth.Truth.assertThat;
20
21 import static org.mockito.Mockito.eq;
22 import static org.mockito.Mockito.lenient;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import android.os.SystemProperties;
27 import android.util.SparseArray;
28
29 import androidx.test.filters.SmallTest;
30 import androidx.test.runner.AndroidJUnit4;
31
32 import com.android.server.art.Utils;
33 import com.android.server.art.testing.StaticMockitoRule;
34 import com.android.server.pm.pkg.PackageState;
35
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.concurrent.Executor;
44 import java.util.concurrent.ForkJoinPool;
45
46 @SmallTest
47 @RunWith(AndroidJUnit4.class)
48 public class UtilsTest {
49 @Rule
50 public StaticMockitoRule mockitoRule =
51 new StaticMockitoRule(SystemProperties.class, Constants.class);
52
53 @Before
54 public void setUp() throws Exception {
55 lenient().when(SystemProperties.get(eq("ro.dalvik.vm.isa.x86_64"))).thenReturn("arm64");
56 lenient().when(SystemProperties.get(eq("ro.dalvik.vm.isa.x86"))).thenReturn("arm");
57
58 lenient().when(Constants.getPreferredAbi()).thenReturn("arm64-v8a");
59 lenient().when(Constants.getNative64BitAbi()).thenReturn("arm64-v8a");
60 lenient().when(Constants.getNative32BitAbi()).thenReturn("armeabi-v7a");
61 }
62
63 @Test
64 public void testCollectionIsEmptyTrue() {
65 assertThat(Utils.isEmpty(List.of())).isTrue();
66 }
67
68 @Test
69 public void testCollectionIsEmptyFalse() {
70 assertThat(Utils.isEmpty(List.of(1))).isFalse();
71 }
72
73 @Test
74 public void testSparseArrayIsEmptyTrue() {
75 assertThat(Utils.isEmpty(new SparseArray<Integer>())).isTrue();
76 }
77
78 @Test
79 public void testSparseArrayIsEmptyFalse() {
80 SparseArray<Integer> array = new SparseArray<>();
81 array.put(1, 1);
82 assertThat(Utils.isEmpty(array)).isFalse();
83 }
84
85 @Test
86 public void testArrayIsEmptyTrue() {
87 assertThat(Utils.isEmpty(new int[0])).isTrue();
88 }
89
90 @Test
91 public void testArrayIsEmptyFalse() {
92 assertThat(Utils.isEmpty(new int[] {1})).isFalse();
93 }
94
95 @Test
96 public void testGetAllAbis() {
97 var pkgState = mock(PackageState.class);
98 when(pkgState.getPrimaryCpuAbi()).thenReturn("armeabi-v7a");
99 when(pkgState.getSecondaryCpuAbi()).thenReturn("arm64-v8a");
100
101 assertThat(Utils.getAllAbis(pkgState))
102 .containsExactly(Utils.Abi.create("armeabi-v7a", "arm", true /* isPrimaryAbi */),
103 Utils.Abi.create("arm64-v8a", "arm64", false /* isPrimaryAbi */));
104 }
105
106 @Test
107 public void testGetAllAbisTranslated() {
108 var pkgState = mock(PackageState.class);
109 when(pkgState.getPrimaryCpuAbi()).thenReturn("x86_64");
110 when(pkgState.getSecondaryCpuAbi()).thenReturn("x86");
111
112 assertThat(Utils.getAllAbis(pkgState))
113 .containsExactly(Utils.Abi.create("arm64-v8a", "arm64", true /* isPrimaryAbi */),
114 Utils.Abi.create("armeabi-v7a", "arm", false /* isPrimaryAbi */));
115 }
116
117 @Test
118 public void testGetAllAbisPrimaryOnly() {
119 var pkgState = mock(PackageState.class);
120 when(pkgState.getPrimaryCpuAbi()).thenReturn("armeabi-v7a");
121 when(pkgState.getSecondaryCpuAbi()).thenReturn(null);
122
123 assertThat(Utils.getAllAbis(pkgState))
124 .containsExactly(Utils.Abi.create("armeabi-v7a", "arm", true /* isPrimaryAbi */));
125 }
126
127 @Test
128 public void testGetAllAbisNone() {
129 var pkgState = mock(PackageState.class);
130 when(pkgState.getPrimaryCpuAbi()).thenReturn(null);
131 when(pkgState.getSecondaryCpuAbi()).thenReturn(null);
132
133 assertThat(Utils.getAllAbis(pkgState))
134 .containsExactly(Utils.Abi.create("arm64-v8a", "arm64", true /* isPrimaryAbi */));
135
136 // Make sure the result does come from `Constants.getPreferredAbi()` rather than somewhere
137 // else.
138 when(Constants.getPreferredAbi()).thenReturn("armeabi-v7a");
139 assertThat(Utils.getAllAbis(pkgState))
140 .containsExactly(Utils.Abi.create("armeabi-v7a", "arm", true /* isPrimaryAbi */));
141 }
142
143 @Test(expected = IllegalStateException.class)
144 public void testGetAllAbisInvalidNativeIsa() {
145 lenient().when(SystemProperties.get(eq("ro.dalvik.vm.isa.x86_64"))).thenReturn("x86");
146
147 var pkgState = mock(PackageState.class);
148 when(pkgState.getPrimaryCpuAbi()).thenReturn("x86_64");
149 when(pkgState.getSecondaryCpuAbi()).thenReturn(null);
150
151 Utils.getAllAbis(pkgState);
152 }
153
154 @Test(expected = IllegalStateException.class)
155 public void testGetAllAbisUnsupportedTranslation() {
156 lenient().when(SystemProperties.get(eq("ro.dalvik.vm.isa.x86_64"))).thenReturn("");
157
158 var pkgState = mock(PackageState.class);
159 when(pkgState.getPrimaryCpuAbi()).thenReturn("x86_64");
160 when(pkgState.getSecondaryCpuAbi()).thenReturn(null);
161
162 Utils.getAllAbis(pkgState);
163 }
164
165 @Test
166 public void testImplies() {
167 assertThat(Utils.implies(false, false)).isTrue();
168 assertThat(Utils.implies(false, true)).isTrue();
169 assertThat(Utils.implies(true, false)).isFalse();
170 assertThat(Utils.implies(true, true)).isTrue();
171 }
172
173 @Test
174 public void testCheck() {
175 Utils.check(true);
176 }
177
178 @Test(expected = IllegalStateException.class)
179 public void testCheckFailed() throws Exception {
180 Utils.check(false);
181 }
182
183 @Test
184 public void testExecuteAndWait() {
185 Executor executor = ForkJoinPool.commonPool();
186 List<String> results = new ArrayList<>();
187 Utils.executeAndWait(executor, () -> {
188 try {
189 Thread.sleep(100);
190 } catch (InterruptedException e) {
191 throw new RuntimeException(e);
192 }
193 results.add("foo");
194 });
195 assertThat(results).containsExactly("foo");
196 }
197
198 @Test(expected = IllegalArgumentException.class)
199 public void testExecuteAndWaitPropagatesException() {
200 Executor executor = ForkJoinPool.commonPool();
201 Utils.executeAndWait(executor, () -> { throw new IllegalArgumentException(); });
202 }
203 }