1 /* 2 * Copyright (C) 2017 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 junit.framework.Assert.assertFalse; 20 import static junit.framework.Assert.assertTrue; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.any; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.NotificationChannel; 29 import android.app.NotificationChannelGroup; 30 import android.app.NotificationManager; 31 import android.content.Context; 32 import android.os.UserManager; 33 import android.support.v7.preference.Preference; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.shadows.ShadowApplication; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class DeletedChannelsPreferenceControllerTest { 46 47 private Context mContext; 48 @Mock 49 private NotificationBackend mBackend; 50 @Mock 51 private NotificationManager mNm; 52 @Mock 53 private UserManager mUm; 54 55 private DeletedChannelsPreferenceController mController; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 61 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm); 62 shadowApplication.setSystemService(Context.USER_SERVICE, mUm); 63 mContext = shadowApplication.getApplicationContext(); 64 mController = new DeletedChannelsPreferenceController(mContext, mBackend); 65 } 66 67 @Test noCrashIfNoOnResume()68 public void noCrashIfNoOnResume() { 69 mController.isAvailable(); 70 mController.updateState(mock(Preference.class)); 71 } 72 73 @Test isAvailable_appScreen_notIfAppBlocked()74 public void isAvailable_appScreen_notIfAppBlocked() { 75 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 76 appRow.banned = true; 77 mController.onResume(appRow, null, null, null); 78 assertFalse(mController.isAvailable()); 79 } 80 81 @Test isAvailable_groupScreen_never()82 public void isAvailable_groupScreen_never() { 83 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 84 mController.onResume(appRow, null, mock(NotificationChannelGroup.class), null); 85 assertFalse(mController.isAvailable()); 86 } 87 88 @Test isAvailable_channelScreen_never()89 public void isAvailable_channelScreen_never() { 90 mController.onResume( 91 new NotificationBackend.AppRow(), mock(NotificationChannel.class), null, null); 92 assertFalse(mController.isAvailable()); 93 } 94 95 @Test isAvailable_appScreen_notIfNoDeletedChannels()96 public void isAvailable_appScreen_notIfNoDeletedChannels() { 97 when(mBackend.getDeletedChannelCount(any(), anyInt())).thenReturn(0); 98 mController.onResume(new NotificationBackend.AppRow(), null, null, null); 99 assertFalse(mController.isAvailable()); 100 } 101 102 @Test isAvailable_appScreen()103 public void isAvailable_appScreen() { 104 when(mBackend.getDeletedChannelCount(any(), anyInt())).thenReturn(1); 105 mController.onResume(new NotificationBackend.AppRow(), null, null, null); 106 assertTrue(mController.isAvailable()); 107 } 108 109 @Test updateState()110 public void updateState() { 111 when(mBackend.getDeletedChannelCount(any(), anyInt())).thenReturn(1); 112 mController.onResume(new NotificationBackend.AppRow(), null, null, null); 113 114 Preference pref = mock(Preference.class); 115 mController.updateState(pref); 116 117 verify(pref, times(1)).setSelectable(false); 118 verify(mBackend, times(1)).getDeletedChannelCount(any(), anyInt()); 119 ArgumentCaptor<CharSequence> argumentCaptor = ArgumentCaptor.forClass(CharSequence.class); 120 verify(pref, times(1)).setTitle(argumentCaptor.capture()); 121 assertTrue(argumentCaptor.getValue().toString().contains("1")); 122 } 123 } 124