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 android.app.NotificationManager.IMPORTANCE_NONE; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertFalse; 23 import static org.junit.Assert.assertTrue; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.when; 28 29 import android.app.NotificationChannel; 30 import android.app.NotificationChannelGroup; 31 import android.app.NotificationManager; 32 import android.content.Context; 33 import android.os.UserManager; 34 import android.view.View; 35 36 import androidx.fragment.app.FragmentActivity; 37 38 import com.android.settings.dashboard.DashboardFragment; 39 import com.android.settingslib.widget.LayoutPreference; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.shadows.ShadowApplication; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class HeaderPreferenceControllerTest { 52 53 private Context mContext; 54 @Mock 55 private NotificationManager mNm; 56 @Mock 57 private UserManager mUm; 58 59 private HeaderPreferenceController mController; 60 @Mock 61 private LayoutPreference mPreference; 62 @Mock 63 private View mView; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 69 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm); 70 shadowApplication.setSystemService(Context.USER_SERVICE, mUm); 71 mContext = RuntimeEnvironment.application; 72 DashboardFragment fragment = mock(DashboardFragment.class); 73 when(fragment.getContext()).thenReturn(mContext); 74 FragmentActivity activity = mock(FragmentActivity.class); 75 when(activity.getApplicationContext()).thenReturn(mContext); 76 when(fragment.getActivity()).thenReturn(activity); 77 mController = spy(new HeaderPreferenceController(mContext, fragment)); 78 when(mPreference.findViewById(anyInt())).thenReturn(mView); 79 } 80 81 @Test testNoCrashIfNoOnResume()82 public void testNoCrashIfNoOnResume() { 83 mController.isAvailable(); 84 mController.updateState(mock(LayoutPreference.class)); 85 } 86 87 @Test testIsAvailable_notIfNull()88 public void testIsAvailable_notIfNull() { 89 mController.onResume(null, null, null, null); 90 assertFalse(mController.isAvailable()); 91 } 92 93 @Test testIsAvailable()94 public void testIsAvailable() { 95 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 96 appRow.banned = true; 97 mController.onResume(appRow, null, null, null); 98 assertTrue(mController.isAvailable()); 99 } 100 101 @Test testGetLabel()102 public void testGetLabel() { 103 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 104 appRow.label = "bananas"; 105 mController.onResume(appRow, null, null, null); 106 assertEquals(appRow.label, mController.getLabel()); 107 108 NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); 109 mController.onResume(appRow, null, group, null); 110 assertEquals(group.getName(), mController.getLabel()); 111 112 NotificationChannel channel = new NotificationChannel("cid", "cname", IMPORTANCE_NONE); 113 mController.onResume(appRow, channel, group, null); 114 assertEquals(channel.getName(), mController.getLabel()); 115 116 NotificationChannel defaultChannel = new NotificationChannel( 117 NotificationChannel.DEFAULT_CHANNEL_ID, "", IMPORTANCE_NONE); 118 mController.onResume(appRow, defaultChannel, null, null); 119 assertEquals(appRow.label, mController.getLabel()); 120 } 121 122 @Test testGetSummary()123 public void testGetSummary() { 124 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 125 appRow.label = "bananas"; 126 mController.onResume(appRow, null, null, null); 127 assertEquals("", mController.getSummary()); 128 129 NotificationChannelGroup group = new NotificationChannelGroup("id", "name"); 130 mController.onResume(appRow, null, group, null); 131 assertEquals(appRow.label, mController.getSummary()); 132 133 NotificationChannel channel = new NotificationChannel("cid", "cname", IMPORTANCE_NONE); 134 mController.onResume(appRow, channel, group, null); 135 assertTrue(mController.getSummary().toString().contains(group.getName())); 136 assertTrue(mController.getSummary().toString().contains(appRow.label)); 137 138 mController.onResume(appRow, channel, null, null); 139 assertFalse(mController.getSummary().toString().contains(group.getName())); 140 assertTrue(mController.getSummary().toString().contains(appRow.label)); 141 142 NotificationChannel defaultChannel = new NotificationChannel( 143 NotificationChannel.DEFAULT_CHANNEL_ID, "", IMPORTANCE_NONE); 144 mController.onResume(appRow, defaultChannel, null, null); 145 assertEquals("", mController.getSummary()); 146 } 147 } 148