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.server.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.AutomaticZenRule; 22 import android.provider.Settings; 23 import android.service.notification.ZenModeConfig; 24 import android.service.notification.ZenPolicy; 25 26 import androidx.test.filters.SmallTest; 27 import androidx.test.runner.AndroidJUnit4; 28 29 import com.android.os.dnd.ActiveRuleType; 30 import com.android.os.dnd.ChangeOrigin; 31 import com.android.os.dnd.ChannelPolicy; 32 import com.android.os.dnd.ConversationType; 33 import com.android.os.dnd.PeopleType; 34 import com.android.os.dnd.State; 35 import com.android.os.dnd.ZenMode; 36 37 import com.google.protobuf.Internal; 38 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.lang.reflect.Field; 43 import java.lang.reflect.Modifier; 44 import java.util.Arrays; 45 import java.util.Map; 46 import java.util.stream.Collectors; 47 48 /** Test to validate that logging enums used in Zen classes match their API definitions. */ 49 @SmallTest 50 @RunWith(AndroidJUnit4.class) 51 public class ZenEnumTest { 52 53 @Test testEnum_zenMode()54 public void testEnum_zenMode() { 55 testEnum(Settings.Global.class, "ZEN_MODE", ZenMode.class, "ZEN_MODE"); 56 } 57 58 @Test testEnum_activeRuleType()59 public void testEnum_activeRuleType() { 60 testEnum(AutomaticZenRule.class, "TYPE", ActiveRuleType.class, "TYPE"); 61 } 62 63 @Test testEnum_zenPolicyState()64 public void testEnum_zenPolicyState() { 65 testEnum(ZenPolicy.class, "STATE", State.class, "STATE"); 66 } 67 68 @Test testEnum_zenPolicyChannelPolicy()69 public void testEnum_zenPolicyChannelPolicy() { 70 testEnum(ZenPolicy.class, "CHANNEL_POLICY", ChannelPolicy.class, "CHANNEL_POLICY"); 71 } 72 73 @Test testEnum_zenPolicyConversationType()74 public void testEnum_zenPolicyConversationType() { 75 testEnum(ZenPolicy.class, "CONVERSATION_SENDERS", ConversationType.class, "CONV"); 76 } 77 78 @Test testEnum_zenPolicyPeopleType()79 public void testEnum_zenPolicyPeopleType() { 80 testEnum(ZenPolicy.class, "PEOPLE_TYPE", PeopleType.class, "PEOPLE"); 81 } 82 83 @Test testEnum_changeOrigin()84 public void testEnum_changeOrigin() { 85 testEnum(ZenModeConfig.class, "ORIGIN", ChangeOrigin.class, "ORIGIN"); 86 } 87 88 /** 89 * Verifies that any constants (i.e. {@code public static final int} fields) named {@code 90 * <apiPrefix>_SOMETHING} in {@code apiClass} are present and have the same numerical value 91 * in the enum values defined in {@code loggingProtoEnumClass}. 92 * 93 * <p>Note that <em>extra</em> values in the logging enum are accepted (since we have one of 94 * those, and the main goal of this test is that we don't forget to update the logging enum 95 * if new API enum values are added). 96 */ testEnum(Class<?> apiClass, String apiPrefix, Class<? extends Internal.EnumLite> loggingProtoEnumClass, String loggingPrefix)97 private static void testEnum(Class<?> apiClass, String apiPrefix, 98 Class<? extends Internal.EnumLite> loggingProtoEnumClass, 99 String loggingPrefix) { 100 Map<String, Integer> apiConstants = 101 Arrays.stream(apiClass.getDeclaredFields()) 102 .filter(f -> Modifier.isPublic(f.getModifiers())) 103 .filter(f -> Modifier.isStatic(f.getModifiers())) 104 .filter(f -> Modifier.isFinal(f.getModifiers())) 105 .filter(f -> f.getType().equals(int.class)) 106 .filter(f -> f.getName().startsWith(apiPrefix + "_")) 107 .collect(Collectors.toMap( 108 Field::getName, 109 ZenEnumTest::getStaticFieldIntValue)); 110 111 Map<String, Integer> loggingConstants = 112 Arrays.stream(loggingProtoEnumClass.getEnumConstants()) 113 .collect(Collectors.toMap( 114 v -> v.toString(), 115 v -> v.getNumber())); 116 117 Map<String, Integer> renamedApiConstants = apiConstants.entrySet().stream() 118 .collect(Collectors.toMap( 119 entry -> entry.getKey().replace(apiPrefix + "_", loggingPrefix + "_"), 120 Map.Entry::getValue)); 121 122 assertThat(loggingConstants).containsAtLeastEntriesIn(renamedApiConstants); 123 } 124 getStaticFieldIntValue(Field f)125 private static int getStaticFieldIntValue(Field f) { 126 try { 127 return f.getInt(null); 128 } catch (IllegalAccessException e) { 129 throw new RuntimeException(e); 130 } 131 } 132 } 133