1 /* 2 * Copyright (C) 2025 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.settings.notification; 18 19 import static android.service.notification.Adjustment.KEY_SUMMARIZATION; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.when; 25 26 import android.app.Flags; 27 import android.app.INotificationManager; 28 import android.content.Context; 29 import android.content.pm.ApplicationInfo; 30 import android.platform.test.annotations.EnableFlags; 31 import android.platform.test.flag.junit.SetFlagsRule; 32 33 import androidx.fragment.app.Fragment; 34 import androidx.preference.Preference; 35 import androidx.preference.PreferenceCategory; 36 import androidx.preference.PreferenceManager; 37 import androidx.preference.PreferenceScreen; 38 39 import com.android.settingslib.applications.ApplicationsState; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 50 import java.util.ArrayList; 51 import java.util.List; 52 53 @RunWith(RobolectricTestRunner.class) 54 @EnableFlags({Flags.FLAG_NM_SUMMARIZATION_UI, Flags.FLAG_NM_SUMMARIZATION, 55 Flags.FLAG_NOTIFICATION_CLASSIFICATION_UI}) 56 public class AdjustmentExcludedAppsPreferenceControllerTest { 57 58 @Rule 59 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 60 61 @Mock 62 private NotificationBackend mBackend; 63 @Mock 64 private ApplicationsState mApplicationState; 65 private AdjustmentExcludedAppsPreferenceController mController; 66 private Context mContext; 67 @Mock 68 INotificationManager mInm; 69 70 @Before setup()71 public void setup() { 72 MockitoAnnotations.initMocks(this); 73 mContext = RuntimeEnvironment.application; 74 75 mController = new AdjustmentExcludedAppsPreferenceController(mContext, "key"); 76 mController.onAttach(null, mock(Fragment.class), mBackend, KEY_SUMMARIZATION); 77 PreferenceScreen screen = new PreferenceManager(mContext).createPreferenceScreen(mContext); 78 mController.mPreferenceCategory = new PreferenceCategory(mContext); 79 screen.addPreference(mController.mPreferenceCategory); 80 81 mController.mApplicationsState = mApplicationState; 82 mController.mPrefContext = mContext; 83 } 84 85 @Test testIsAvailable()86 public void testIsAvailable() { 87 when(mBackend.isNotificationBundlingSupported()).thenReturn(true); 88 when(mBackend.isNotificationSummarizationSupported()).thenReturn(true); 89 assertThat(mController.isAvailable()).isTrue(); 90 } 91 92 @Test isAvailable_flagEnabledNasDoesNotSupport_shouldReturnFalse()93 public void isAvailable_flagEnabledNasDoesNotSupport_shouldReturnFalse() throws Exception { 94 when(mInm.getUnsupportedAdjustmentTypes()).thenReturn(List.of(KEY_SUMMARIZATION)); 95 assertThat(mController.isAvailable()).isFalse(); 96 } 97 98 @Test testUpdateAppList()99 public void testUpdateAppList() throws Exception { 100 when(mBackend.getAdjustmentDeniedPackages(KEY_SUMMARIZATION)).thenReturn( 101 new String[] {"cannot", "cannot2"}); 102 103 // GIVEN there are four apps, and two have KEY_SUMMARIZATION off 104 ApplicationsState.AppEntry canSummarize = 105 mock(ApplicationsState.AppEntry.class); 106 canSummarize.info = new ApplicationInfo(); 107 canSummarize.info.packageName = "canSummarize"; 108 canSummarize.info.uid = 0; 109 110 ApplicationsState.AppEntry canSummarize2 = mock(ApplicationsState.AppEntry.class); 111 canSummarize2.info = new ApplicationInfo(); 112 canSummarize2.info.packageName = "canSummarizeTwo"; 113 canSummarize2.info.uid = 0; 114 115 ApplicationsState.AppEntry cannot = 116 mock(ApplicationsState.AppEntry.class); 117 cannot.info = new ApplicationInfo(); 118 cannot.info.packageName = "cannot"; 119 cannot.info.uid = 0; 120 121 ApplicationsState.AppEntry cannot2 = 122 mock(ApplicationsState.AppEntry.class); 123 cannot2.info = new ApplicationInfo(); 124 cannot2.info.packageName = "cannot2"; 125 cannot2.info.uid = 0; 126 127 List<ApplicationsState.AppEntry> appEntries = new ArrayList<>(); 128 appEntries.add(canSummarize); 129 appEntries.add(canSummarize2); 130 appEntries.add(cannot); 131 appEntries.add(cannot2); 132 133 // WHEN the controller updates the app list with the app entries 134 mController.updateAppList(appEntries); 135 136 // THEN only the 'cannot' entries make it to the app list 137 assertThat(mController.mPreferenceCategory.getPreferenceCount()).isEqualTo(2); 138 assertThat((Preference) mController.mPreferenceCategory.findPreference( 139 AdjustmentExcludedAppsPreferenceController.getKey( 140 cannot.info.packageName,cannot.info.uid))).isNotNull(); 141 assertThat((Preference) mController.mPreferenceCategory.findPreference( 142 AdjustmentExcludedAppsPreferenceController.getKey( 143 cannot2.info.packageName,cannot2.info.uid))).isNotNull(); 144 } 145 146 @Test testUpdateAppList_nullApps()147 public void testUpdateAppList_nullApps() { 148 mController.updateAppList(null); 149 assertThat(mController.mPreferenceCategory.getPreferenceCount()).isEqualTo(0); 150 } 151 } 152