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.nfc; 18 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import android.app.ActivityManager; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.util.Log; 26 import android.util.SparseArray; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.platform.app.InstrumentationRegistry; 30 31 import com.android.dx.mockito.inline.extended.ExtendedMockito; 32 33 import org.junit.After; 34 import org.junit.Assert; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.MockitoSession; 39 import org.mockito.quality.Strictness; 40 41 import java.util.List; 42 43 @RunWith(AndroidJUnit4.class) 44 public class ForegroundUtilsTest { 45 private static final String TAG = ForegroundUtilsTest.class.getSimpleName(); 46 private boolean mNfcSupported; 47 private MockitoSession mStaticMockSession; 48 private ForegroundUtils mForegroundUtils; 49 private ActivityManager mActivityManager; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 mStaticMockSession = ExtendedMockito.mockitoSession() 54 .strictness(Strictness.LENIENT) 55 .startMocking(); 56 57 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 58 PackageManager pm = context.getPackageManager(); 59 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { 60 mNfcSupported = false; 61 return; 62 } 63 mNfcSupported = true; 64 65 mActivityManager = mock(ActivityManager.class); 66 67 InstrumentationRegistry.getInstrumentation().runOnMainSync( 68 () -> mForegroundUtils = new ForegroundUtils(mActivityManager)); 69 Assert.assertNotNull(mForegroundUtils); 70 } 71 72 @After tearDown()73 public void tearDown() throws Exception { 74 mStaticMockSession.finishMocking(); 75 } 76 77 @Test testRegisterUidToBackgroundCallback()78 public void testRegisterUidToBackgroundCallback() { 79 if (!mNfcSupported) return; 80 81 ForegroundUtils.Callback callback = uid -> { 82 Log.d(TAG, "testRegisterUidToBackgroundCallback callback received"); 83 }; 84 when(mActivityManager.getUidImportance(0)).thenReturn(ActivityManager. 85 RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 86 boolean isRegistered = mForegroundUtils.registerUidToBackgroundCallback(callback, 0); 87 Assert.assertTrue(isRegistered); 88 } 89 90 @Test testIsInForeground()91 public void testIsInForeground() { 92 if (!mNfcSupported) return; 93 94 when(mActivityManager.getUidImportance(0)).thenReturn(100); 95 when(mActivityManager.getUidImportance(10)).thenReturn(1); 96 boolean isInForegroundTrue = mForegroundUtils.isInForeground(0); 97 Assert.assertTrue(isInForegroundTrue); 98 isInForegroundTrue = mForegroundUtils.isInForeground(10); 99 Assert.assertFalse(isInForegroundTrue); 100 } 101 102 @Test testOnUidImportance()103 public void testOnUidImportance() { 104 if (!mNfcSupported) return; 105 106 mForegroundUtils.clearForegroundlist(); 107 mForegroundUtils.onUidImportance(0, 108 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 109 List<Integer> uids = mForegroundUtils.getForegroundUids(); 110 Assert.assertNotNull(uids); 111 Assert.assertTrue(uids.size() > 0); 112 mForegroundUtils.onUidImportance(0, 113 ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE); 114 uids = mForegroundUtils.getForegroundUids(); 115 Assert.assertNotNull(uids); 116 Assert.assertTrue(uids.isEmpty()); 117 } 118 119 @Test testOnUidImportanceBackground()120 public void testOnUidImportanceBackground() { 121 if (!mNfcSupported) return; 122 123 mForegroundUtils.onUidImportance(0, 124 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 125 List<Integer> uids = mForegroundUtils.getForegroundUids(); 126 Assert.assertNotNull(uids); 127 Assert.assertTrue(uids.size() > 0); 128 ForegroundUtils.Callback callback = uid -> { 129 Log.d(TAG, "testOnUidImportanceBackground callback received"); 130 }; 131 mForegroundUtils.registerUidToBackgroundCallback(callback, 0); 132 133 SparseArray<List<ForegroundUtils.Callback>> 134 backGroundCallbacks = mForegroundUtils.getBackgroundCallbacks(); 135 List<ForegroundUtils.Callback> callbacks = backGroundCallbacks.get(0); 136 Assert.assertNotNull(callbacks); 137 Assert.assertFalse(callbacks.isEmpty()); 138 mForegroundUtils.onUidImportance(0, 139 ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND); 140 backGroundCallbacks = mForegroundUtils.getBackgroundCallbacks(); 141 callbacks = backGroundCallbacks.get(0); 142 Assert.assertNull(callbacks); 143 } 144 145 @Test testGetForegroundUids()146 public void testGetForegroundUids() { 147 if (!mNfcSupported) return; 148 149 mForegroundUtils.onUidImportance(0, 150 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 151 mForegroundUtils.onUidImportance(1, 152 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 153 mForegroundUtils.onUidImportance(2, 154 ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND); 155 List<Integer> uids = mForegroundUtils.getForegroundUids(); 156 Assert.assertNotNull(uids); 157 int uid = uids.get(0); 158 Assert.assertEquals(0, uid); 159 } 160 }