1 /* 2 * Copyright (C) 2025 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 package android.hardware.multiprocess.camera.cts; 17 18 import static junit.framework.Assert.*; 19 20 import android.app.ActivityManager; 21 import android.util.ArrayMap; 22 23 import java.util.List; 24 import java.util.Map; 25 26 /** 27 * Utility functions used throughout the multi-process unit tests. 28 */ 29 public class TestUtils { 30 /** 31 * Return the PID for the process with the given name in the given list of process info. 32 * 33 * @param processName the name of the process who's PID to return. 34 * @param list a list of {@link ActivityManager.RunningAppProcessInfo} to check. 35 * @return the PID of the given process, or -1 if it was not included in the list. 36 */ getPid( String processName, List<ActivityManager.RunningAppProcessInfo> list)37 public static int getPid( 38 String processName, List<ActivityManager.RunningAppProcessInfo> list) { 39 for (ActivityManager.RunningAppProcessInfo rai : list) { 40 if (processName.equals(rai.processName)) return rai.pid; 41 } 42 return -1; 43 } 44 45 /** 46 * Assert that there is only one event of the given type in the event list. 47 * 48 * @param event event type to check for. 49 * @param events {@link List} of events. 50 */ assertOnly(int event, List<ErrorLoggingService.LogEvent> events)51 public static void assertOnly(int event, List<ErrorLoggingService.LogEvent> events) { 52 assertTrue("Remote camera activity never received event: " + event, events != null); 53 for (ErrorLoggingService.LogEvent e : events) { 54 assertFalse( 55 "Remote camera activity received invalid event (" 56 + e 57 + ") while waiting for event: " 58 + event, 59 e.getEvent() < 0 || e.getEvent() != event); 60 } 61 assertTrue("Remote camera activity never received event: " + event, events.size() >= 1); 62 assertTrue( 63 "Remote camera activity received too many " 64 + event 65 + " events, received: " 66 + events.size(), 67 events.size() == 1); 68 } 69 70 /** 71 * Assert there were no logEvents in the given list. 72 * 73 * @param msg message to show on assertion failure. 74 * @param events {@link List} of events. 75 */ assertNone(String msg, List<ErrorLoggingService.LogEvent> events)76 public static void assertNone(String msg, List<ErrorLoggingService.LogEvent> events) { 77 if (events == null) return; 78 StringBuilder builder = new StringBuilder(msg + "\n"); 79 for (ErrorLoggingService.LogEvent e : events) { 80 builder.append(e).append("\n"); 81 } 82 assertTrue(builder.toString(), events.isEmpty()); 83 } 84 85 /** Return a Map of eventTag -> number of times encountered */ getEventTagCountMap( List<ErrorLoggingService.LogEvent> events)86 public static Map<Integer, Integer> getEventTagCountMap( 87 List<ErrorLoggingService.LogEvent> events) { 88 ArrayMap<Integer, Integer> eventTagCountMap = new ArrayMap<>(); 89 for (ErrorLoggingService.LogEvent e : events) { 90 int eventTag = e.getEvent(); 91 if (!eventTagCountMap.containsKey(eventTag)) { 92 eventTagCountMap.put(eventTag, 1); 93 } else { 94 eventTagCountMap.put(eventTag, eventTagCountMap.get(eventTag) + 1); 95 } 96 } 97 return eventTagCountMap; 98 } 99 } 100