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