1 /* 2 * Copyright (C) 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.internal.protolog; 18 19 import static org.junit.Assert.assertThrows; 20 21 import android.platform.test.annotations.Presubmit; 22 23 import com.android.internal.protolog.common.IProtoLogGroup; 24 25 import com.google.common.truth.Truth; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** Test class for {@link ProtoLog}. */ 32 @SuppressWarnings("ConstantConditions") 33 @Presubmit 34 @RunWith(JUnit4.class) 35 public class ProtoLogTest { 36 37 @Test canRunProtoLogInitMultipleTimes()38 public void canRunProtoLogInitMultipleTimes() { 39 ProtoLog.init(TEST_GROUP_1); 40 ProtoLog.init(TEST_GROUP_1); 41 ProtoLog.init(TEST_GROUP_2); 42 ProtoLog.init(TEST_GROUP_1, TEST_GROUP_2); 43 44 final var instance = ProtoLog.getSingleInstance(); 45 Truth.assertThat(instance.getRegisteredGroups()) 46 .containsExactly(TEST_GROUP_1, TEST_GROUP_2); 47 } 48 49 @Test deduplicatesRegisteringDuplicateGroup()50 public void deduplicatesRegisteringDuplicateGroup() { 51 ProtoLog.init(TEST_GROUP_1, TEST_GROUP_1, TEST_GROUP_2); 52 53 final var instance = ProtoLog.getSingleInstance(); 54 Truth.assertThat(instance.getRegisteredGroups()) 55 .containsExactly(TEST_GROUP_1, TEST_GROUP_2); 56 } 57 58 @Test throwOnRegisteringGroupsWithIdCollisions()59 public void throwOnRegisteringGroupsWithIdCollisions() { 60 final var assertion = assertThrows(RuntimeException.class, 61 () -> ProtoLog.init(TEST_GROUP_1, TEST_GROUP_WITH_COLLISION, TEST_GROUP_2)); 62 63 Truth.assertThat(assertion).hasMessageThat() 64 .contains("" + TEST_GROUP_WITH_COLLISION.getId()); 65 Truth.assertThat(assertion).hasMessageThat().contains("collision"); 66 } 67 68 private static final IProtoLogGroup TEST_GROUP_1 = new ProtoLogGroup("TEST_TAG_1", 1); 69 private static final IProtoLogGroup TEST_GROUP_2 = new ProtoLogGroup("TEST_TAG_2", 2); 70 private static final IProtoLogGroup TEST_GROUP_WITH_COLLISION = 71 new ProtoLogGroup("TEST_TAG_WITH_COLLISION", 1); 72 73 private static class ProtoLogGroup implements IProtoLogGroup { 74 private final boolean mEnabled; 75 private volatile boolean mLogToProto; 76 private volatile boolean mLogToLogcat; 77 private final String mTag; 78 private final int mId; 79 ProtoLogGroup(String tag, int id)80 ProtoLogGroup(String tag, int id) { 81 this(true, true, false, tag, id); 82 } 83 ProtoLogGroup( boolean enabled, boolean logToProto, boolean logToLogcat, String tag, int id)84 ProtoLogGroup( 85 boolean enabled, boolean logToProto, boolean logToLogcat, String tag, int id) { 86 this.mEnabled = enabled; 87 this.mLogToProto = logToProto; 88 this.mLogToLogcat = logToLogcat; 89 this.mTag = tag; 90 this.mId = id; 91 } 92 93 @Override name()94 public String name() { 95 return mTag; 96 } 97 98 @Override isEnabled()99 public boolean isEnabled() { 100 return mEnabled; 101 } 102 103 @Override isLogToProto()104 public boolean isLogToProto() { 105 return mLogToProto; 106 } 107 108 @Override isLogToLogcat()109 public boolean isLogToLogcat() { 110 return mLogToLogcat; 111 } 112 113 @Override isLogToAny()114 public boolean isLogToAny() { 115 return mLogToLogcat || mLogToProto; 116 } 117 118 @Override getTag()119 public String getTag() { 120 return mTag; 121 } 122 123 @Override setLogToProto(boolean logToProto)124 public void setLogToProto(boolean logToProto) { 125 this.mLogToProto = logToProto; 126 } 127 128 @Override setLogToLogcat(boolean logToLogcat)129 public void setLogToLogcat(boolean logToLogcat) { 130 this.mLogToLogcat = logToLogcat; 131 } 132 133 @Override getId()134 public int getId() { 135 return mId; 136 } 137 } 138 } 139