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.NotificationChannel.DEFAULT_CHANNEL_ID; 20 import static android.app.NotificationManager.IMPORTANCE_LOW; 21 import static android.app.NotificationManager.IMPORTANCE_NONE; 22 import static junit.framework.Assert.assertFalse; 23 import static junit.framework.Assert.assertTrue; 24 import static org.junit.Assert.assertEquals; 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.NotificationManager; 31 import android.content.Context; 32 import android.content.Intent; 33 import android.os.UserManager; 34 import android.support.v7.preference.Preference; 35 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RuntimeEnvironment; 44 import org.robolectric.shadows.ShadowApplication; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 public class AppLinkPreferenceControllerTest { 48 49 private Context mContext; 50 @Mock 51 private NotificationManager mNm; 52 @Mock 53 private UserManager mUm; 54 55 private AppLinkPreferenceController 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 = RuntimeEnvironment.application; 64 mController = spy(new AppLinkPreferenceController(mContext)); 65 } 66 67 @Test testNoCrashIfNoOnResume()68 public void testNoCrashIfNoOnResume() { 69 mController.isAvailable(); 70 mController.updateState(mock(Preference.class)); 71 } 72 73 @Test testIsAvailable_notIfNull()74 public void testIsAvailable_notIfNull() { 75 mController.onResume(null, null, null, null); 76 assertFalse(mController.isAvailable()); 77 } 78 79 @Test testIsAvailable_notIfAppBlocked()80 public void testIsAvailable_notIfAppBlocked() { 81 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 82 appRow.banned = true; 83 mController.onResume(appRow, null, null, null); 84 assertFalse(mController.isAvailable()); 85 } 86 87 @Test testIsAvailable_notIfChannelBlocked()88 public void testIsAvailable_notIfChannelBlocked() { 89 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 90 NotificationChannel channel = mock(NotificationChannel.class); 91 when(channel.getImportance()).thenReturn(IMPORTANCE_NONE); 92 mController.onResume(appRow, channel, null, null); 93 assertFalse(mController.isAvailable()); 94 } 95 96 @Test testIsAvailable_notNoIntent()97 public void testIsAvailable_notNoIntent() { 98 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 99 NotificationChannel channel = mock(NotificationChannel.class); 100 when(channel.getImportance()).thenReturn(IMPORTANCE_LOW); 101 when(channel.getId()).thenReturn(DEFAULT_CHANNEL_ID); 102 mController.onResume(appRow, channel, null, null); 103 assertFalse(mController.isAvailable()); 104 } 105 106 @Test testIsAvailable()107 public void testIsAvailable() { 108 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 109 appRow.settingsIntent = new Intent("test"); 110 NotificationChannel channel = mock(NotificationChannel.class); 111 when(channel.getImportance()).thenReturn(IMPORTANCE_LOW); 112 when(channel.getId()).thenReturn(DEFAULT_CHANNEL_ID); 113 mController.onResume(appRow, channel, null, null); 114 assertTrue(mController.isAvailable()); 115 } 116 117 @Test testUpdateState()118 public void testUpdateState() { 119 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 120 Intent intent = new Intent("action"); 121 appRow.settingsIntent = intent; 122 mController.onResume(appRow, null, null, null); 123 124 Preference pref = new Preference(RuntimeEnvironment.application); 125 mController.updateState(pref); 126 127 assertEquals(intent, pref.getIntent()); 128 } 129 } 130