1 /* 2 * Copyright (C) 2018 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.devicepolicy; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.testng.Assert.assertThrows; 22 23 import android.app.admin.DevicePolicyEventLogger; 24 import android.content.ComponentName; 25 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 /** 32 * Unit tests for {@link DevicePolicyEventLogger}. 33 * <p/> 34 * Run with <code>atest DevicePolicyEventLoggerTest</code>. 35 */ 36 @RunWith(AndroidJUnit4.class) 37 public class DevicePolicyEventLoggerTest { 38 @Test testAllFields()39 public void testAllFields() { 40 final DevicePolicyEventLogger eventLogger = DevicePolicyEventLogger 41 .createEvent(5) 42 .setBoolean(true) 43 .setStrings("string1", "string2", "string3") 44 .setAdmin(new ComponentName("com.test.package", ".TestAdmin")) 45 .setInt(4321) 46 .setTimePeriod(1234L); 47 assertThat(eventLogger.getEventId()).isEqualTo(5); 48 assertThat(eventLogger.getBoolean()).isTrue(); 49 assertThat(eventLogger.getStringArray()).asList() 50 .containsExactly("string1", "string2", "string3"); 51 assertThat(eventLogger.getAdminPackageName()).isEqualTo("com.test.package"); 52 assertThat(eventLogger.getInt()).isEqualTo(4321); 53 assertThat(eventLogger.getTimePeriod()).isEqualTo(1234L); 54 } 55 56 @Test testStrings()57 public void testStrings() { 58 assertThat(DevicePolicyEventLogger 59 .createEvent(0) 60 .setStrings("string1", "string2", "string3").getStringArray()).asList() 61 .containsExactly("string1", "string2", "string3").inOrder(); 62 63 assertThat(DevicePolicyEventLogger 64 .createEvent(0) 65 .setStrings("string1", new String[] {"string2", "string3"}).getStringArray()) 66 .asList().containsExactly("string1", "string2", "string3").inOrder(); 67 68 assertThat(DevicePolicyEventLogger 69 .createEvent(0) 70 .setStrings("string1", "string2", new String[] {"string3"}).getStringArray()) 71 .asList().containsExactly("string1", "string2", "string3").inOrder(); 72 assertThat(DevicePolicyEventLogger 73 .createEvent(0) 74 .setStrings((String) null).getStringArray()).asList() 75 .containsExactly((String) null); 76 77 assertThat(DevicePolicyEventLogger 78 .createEvent(0) 79 .setStrings((String[]) null).getStringArray()) 80 .isEqualTo(null); 81 82 assertThrows(NullPointerException.class, () -> DevicePolicyEventLogger 83 .createEvent(0) 84 .setStrings("string1", "string2", null)); 85 } 86 87 @Test testAdmins()88 public void testAdmins() { 89 assertThat(DevicePolicyEventLogger 90 .createEvent(0) 91 .setAdmin("com.package.name") 92 .getAdminPackageName()) 93 .isEqualTo("com.package.name"); 94 95 assertThat(DevicePolicyEventLogger 96 .createEvent(0) 97 .setAdmin(new ComponentName("com.package.name", ".TestAdmin")) 98 .getAdminPackageName()) 99 .isEqualTo("com.package.name"); 100 } 101 102 @Test testDefaultValues()103 public void testDefaultValues() { 104 final DevicePolicyEventLogger eventLogger = DevicePolicyEventLogger 105 .createEvent(0); 106 assertThat(eventLogger.getEventId()).isEqualTo(0); 107 assertThat(eventLogger.getBoolean()).isFalse(); 108 assertThat(eventLogger.getStringArray()).isNull(); 109 assertThat(eventLogger.getAdminPackageName()).isNull(); 110 assertThat(eventLogger.getInt()).isEqualTo(0); 111 assertThat(eventLogger.getTimePeriod()).isEqualTo(0L); 112 } 113 } 114