1 /* 2 * Copyright (C) 2019 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.internal.logging.testing; 18 19 import com.android.internal.logging.InstanceId; 20 import com.android.internal.logging.UiEventLogger; 21 22 import java.util.LinkedList; 23 import java.util.List; 24 25 /** 26 * Fake logger that queues up logged events for inspection. 27 * 28 * @hide. 29 */ 30 @android.ravenwood.annotation.RavenwoodKeepWholeClass 31 public class UiEventLoggerFake implements UiEventLogger { 32 /** 33 * Immutable data class used to record fake log events. 34 */ 35 public static class FakeUiEvent { 36 public final int eventId; 37 public final int uid; 38 public final String packageName; 39 public final InstanceId instanceId; // Used only for WithInstanceId variants 40 public final int position; // Used only for Position variants 41 FakeUiEvent(int eventId, int uid, String packageName)42 FakeUiEvent(int eventId, int uid, String packageName) { 43 this.eventId = eventId; 44 this.uid = uid; 45 this.packageName = packageName; 46 this.instanceId = null; 47 this.position = 0; 48 } 49 FakeUiEvent(int eventId, int uid, String packageName, InstanceId instanceId)50 FakeUiEvent(int eventId, int uid, String packageName, InstanceId instanceId) { 51 this.eventId = eventId; 52 this.uid = uid; 53 this.packageName = packageName; 54 this.instanceId = instanceId; 55 this.position = 0; 56 } 57 FakeUiEvent(int eventId, int uid, String packageName, InstanceId instanceId, int position)58 FakeUiEvent(int eventId, int uid, String packageName, InstanceId instanceId, int position) { 59 this.eventId = eventId; 60 this.uid = uid; 61 this.packageName = packageName; 62 this.instanceId = instanceId; 63 this.position = position; 64 } 65 } 66 67 private List<FakeUiEvent> mLogs = new LinkedList<>(); 68 69 /** Returns list of all logging events recorded. */ getLogs()70 public List<FakeUiEvent> getLogs() { 71 return mLogs; 72 } 73 /** Returns number of logging events recorded. */ numLogs()74 public int numLogs() { 75 return mLogs.size(); 76 } 77 /** Returns a particular logging event. */ get(int index)78 public FakeUiEvent get(int index) { 79 return mLogs.get(index); 80 } 81 /** Returns event id (as integer) of a particular logging event. */ eventId(int index)82 public int eventId(int index) { 83 return mLogs.get(index).eventId; 84 } 85 86 @Override log(UiEventEnum event)87 public void log(UiEventEnum event) { 88 log(event, 0, null); 89 } 90 91 @Override log(UiEventEnum event, InstanceId instance)92 public void log(UiEventEnum event, InstanceId instance) { 93 logWithInstanceId(event, 0, null, instance); 94 } 95 96 @Override log(UiEventEnum event, int uid, String packageName)97 public void log(UiEventEnum event, int uid, String packageName) { 98 final int eventId = event.getId(); 99 if (eventId > 0) { 100 mLogs.add(new FakeUiEvent(eventId, uid, packageName)); 101 } 102 } 103 104 @Override logWithInstanceId(UiEventEnum event, int uid, String packageName, InstanceId instance)105 public void logWithInstanceId(UiEventEnum event, int uid, String packageName, 106 InstanceId instance) { 107 final int eventId = event.getId(); 108 if (eventId > 0) { 109 mLogs.add(new FakeUiEvent(eventId, uid, packageName, instance)); 110 } 111 } 112 113 @Override logWithPosition(UiEventEnum event, int uid, String packageName, int position)114 public void logWithPosition(UiEventEnum event, int uid, String packageName, int position) { 115 final int eventId = event.getId(); 116 if (eventId > 0) { 117 mLogs.add(new FakeUiEvent(eventId, uid, packageName, null, position)); 118 } 119 } 120 121 @Override logWithInstanceIdAndPosition(UiEventEnum event, int uid, String packageName, InstanceId instance, int position)122 public void logWithInstanceIdAndPosition(UiEventEnum event, int uid, String packageName, 123 InstanceId instance, int position) { 124 final int eventId = event.getId(); 125 if (eventId > 0) { 126 mLogs.add(new FakeUiEvent(eventId, uid, packageName, instance, position)); 127 } 128 } 129 } 130