Soonil Nagarkar | b6375a4 | 2020-01-29 15:23:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.location; |
| 18 | |
| 19 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
| 20 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; |
| 21 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE; |
| 22 | |
| 23 | import static com.google.common.truth.Truth.assertThat; |
| 24 | |
| 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.Mockito.doAnswer; |
| 28 | import static org.mockito.Mockito.doReturn; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.timeout; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.MockitoAnnotations.initMocks; |
| 33 | |
| 34 | import android.app.ActivityManager; |
| 35 | import android.content.Context; |
| 36 | import android.platform.test.annotations.Presubmit; |
| 37 | |
| 38 | import androidx.test.filters.SmallTest; |
| 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.mockito.Mock; |
| 45 | |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
| 48 | |
| 49 | @Presubmit |
| 50 | @SmallTest |
| 51 | @RunWith(AndroidJUnit4.class) |
| 52 | public class AppForegroundHelperTest { |
| 53 | |
| 54 | private static final long TIMEOUT_MS = 5000; |
| 55 | |
| 56 | @Mock private Context mContext; |
| 57 | @Mock private ActivityManager mActivityManager; |
| 58 | |
| 59 | private List<ActivityManager.OnUidImportanceListener> mListeners = new ArrayList<>(); |
| 60 | |
| 61 | private AppForegroundHelper mHelper; |
| 62 | |
| 63 | @Before |
| 64 | public void setUp() { |
| 65 | initMocks(this); |
| 66 | |
| 67 | doReturn(mActivityManager).when(mContext).getSystemService(ActivityManager.class); |
| 68 | doAnswer(invocation -> { |
| 69 | mListeners.add(invocation.getArgument(0)); |
| 70 | return null; |
| 71 | }).when(mActivityManager).addOnUidImportanceListener(any( |
| 72 | ActivityManager.OnUidImportanceListener.class), eq(IMPORTANCE_FOREGROUND_SERVICE)); |
| 73 | |
| 74 | mHelper = new AppForegroundHelper(mContext); |
| 75 | mHelper.onSystemReady(); |
| 76 | } |
| 77 | |
| 78 | private void setImportance(int uid, int importance) { |
| 79 | doReturn(importance).when(mActivityManager).getUidImportance(uid); |
| 80 | for (ActivityManager.OnUidImportanceListener listener : mListeners) { |
| 81 | listener.onUidImportance(uid, importance); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testListeners() { |
| 87 | AppForegroundHelper.AppForegroundListener listener = mock( |
| 88 | AppForegroundHelper.AppForegroundListener.class); |
| 89 | mHelper.addListener(listener); |
| 90 | |
| 91 | setImportance(0, IMPORTANCE_FOREGROUND); |
| 92 | verify(listener, timeout(TIMEOUT_MS)).onAppForegroundChanged(0, true); |
| 93 | |
| 94 | setImportance(1, IMPORTANCE_FOREGROUND_SERVICE); |
| 95 | verify(listener, timeout(TIMEOUT_MS)).onAppForegroundChanged(1, true); |
| 96 | |
| 97 | setImportance(2, IMPORTANCE_VISIBLE); |
| 98 | verify(listener, timeout(TIMEOUT_MS)).onAppForegroundChanged(2, false); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testIsAppForeground() { |
| 103 | setImportance(0, IMPORTANCE_FOREGROUND); |
| 104 | assertThat(mHelper.isAppForeground(0)).isEqualTo(true); |
| 105 | |
| 106 | setImportance(0, IMPORTANCE_FOREGROUND_SERVICE); |
| 107 | assertThat(mHelper.isAppForeground(0)).isEqualTo(true); |
| 108 | |
| 109 | setImportance(0, IMPORTANCE_VISIBLE); |
| 110 | assertThat(mHelper.isAppForeground(0)).isEqualTo(false); |
| 111 | } |
| 112 | } |