1/* 2 * Copyright (C) 2016 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 17package com.android.packageinstaller.permission.utils; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertTrue; 21 22import android.Manifest; 23 24import com.android.internal.logging.nano.MetricsProto; 25import com.android.packageinstaller.shadows.ShadowMetricsLogger; 26import com.android.packageinstaller.shadows.ShadowSystemProperties; 27 28import org.junit.Before; 29import org.junit.Test; 30import org.junit.runner.RunWith; 31import org.robolectric.RobolectricTestRunner; 32import org.robolectric.annotation.Config; 33 34/** 35 * Tests {@link com.android.packageinstaller.permission.utils.EventLogger} 36 */ 37@RunWith(RobolectricTestRunner.class) 38@Config(manifest = "packages/apps/PackageInstaller/AndroidManifest.xml", 39 shadows = {ShadowMetricsLogger.class, ShadowSystemProperties.class}, 40 sdk = 23) 41public class EventLoggerTest { 42 @Before 43 public void setUp() { 44 ShadowSystemProperties.setUserBuild(true); 45 ShadowMetricsLogger.clearLogs(); 46 } 47 48 @Test 49 public void testValidRequested() { 50 EventLogger.logPermissionRequested(null, Manifest.permission.READ_CALENDAR, 51 "testPackage"); 52 53 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 54 assertTrue(ShadowMetricsLogger.getLogs().contains( 55 new ShadowMetricsLogger.Log(null, 56 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_READ_CALENDAR, 57 "testPackage"))); 58 } 59 60 @Test 61 public void testValidAppOpRequested() { 62 EventLogger.logPermissionRequested(null, Manifest.permission.SYSTEM_ALERT_WINDOW, 63 "testPackage"); 64 65 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 66 assertTrue(ShadowMetricsLogger.getLogs().contains( 67 new ShadowMetricsLogger.Log(null, 68 MetricsProto.MetricsEvent.ACTION_APPOP_REQUEST_SYSTEM_ALERT_WINDOW, 69 "testPackage"))); 70 } 71 72 @Test 73 public void testValidDenied() { 74 EventLogger.logPermissionDenied(null, Manifest.permission.READ_CALENDAR, "testPackage"); 75 76 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 77 assertTrue(ShadowMetricsLogger.getLogs().contains( 78 new ShadowMetricsLogger.Log(null, 79 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_READ_CALENDAR + 2, 80 "testPackage"))); 81 } 82 83 @Test 84 public void testInvalidRequestedEngBuild() throws Throwable { 85 ShadowSystemProperties.setUserBuild(false); 86 EventLogger.logPermissionRequested(null, "invalid", "testPackage"); 87 88 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 89 assertTrue(ShadowMetricsLogger.getLogs().contains( 90 new ShadowMetricsLogger.Log(null, 91 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_UNKNOWN, 92 "testPackage"))); 93 } 94 95 @Test 96 public void testInvalidDeniedEngBuild() throws Throwable { 97 ShadowSystemProperties.setUserBuild(false); 98 EventLogger.logPermissionRequested(null, "invalid", "testPackage"); 99 100 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 101 assertTrue(ShadowMetricsLogger.getLogs().contains( 102 new ShadowMetricsLogger.Log(null, 103 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_UNKNOWN, 104 "testPackage"))); 105 } 106 107 @Test 108 public void testInvalidRequestedUserBuild() throws Throwable { 109 EventLogger.logPermissionRequested(null, "invalid", "testPackage"); 110 111 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 112 assertTrue(ShadowMetricsLogger.getLogs().contains( 113 new ShadowMetricsLogger.Log(null, 114 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_UNKNOWN, 115 "testPackage"))); 116 } 117 118 @Test 119 public void testInvalidDeniedUserBuild() throws Throwable { 120 EventLogger.logPermissionDenied(null, "invalid", "testPackage"); 121 122 assertEquals(1, ShadowMetricsLogger.getLogs().size()); 123 assertTrue(ShadowMetricsLogger.getLogs().contains( 124 new ShadowMetricsLogger.Log(null, 125 MetricsProto.MetricsEvent.ACTION_PERMISSION_REQUEST_UNKNOWN + 2, 126 "testPackage"))); 127 } 128} 129