• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29 
30 import android.app.NotificationChannel;
31 import android.app.NotificationManager;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.os.UserManager;
35 
36 import androidx.preference.Preference;
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.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.shadows.ShadowApplication;
46 
47 @RunWith(RobolectricTestRunner.class)
48 public class AppLinkPreferenceControllerTest {
49 
50     private Context mContext;
51     @Mock
52     private NotificationManager mNm;
53     @Mock
54     private UserManager mUm;
55 
56     private AppLinkPreferenceController mController;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         ShadowApplication shadowApplication = ShadowApplication.getInstance();
62         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
63         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
64         mContext = RuntimeEnvironment.application;
65         mController = spy(new AppLinkPreferenceController(mContext));
66     }
67 
68     @Test
testNoCrashIfNoOnResume()69     public void testNoCrashIfNoOnResume() {
70         mController.isAvailable();
71         mController.updateState(mock(Preference.class));
72     }
73 
74     @Test
testIsAvailable_notIfNull()75     public void testIsAvailable_notIfNull() {
76         mController.onResume(null, null, null, null);
77         assertFalse(mController.isAvailable());
78     }
79 
80     @Test
testIsAvailable_notIfAppBlocked()81     public void testIsAvailable_notIfAppBlocked() {
82         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
83         appRow.banned = true;
84         mController.onResume(appRow, null, null, null);
85         assertFalse(mController.isAvailable());
86     }
87 
88     @Test
testIsAvailable_notIfChannelBlocked()89     public void testIsAvailable_notIfChannelBlocked() {
90         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
91         NotificationChannel channel = mock(NotificationChannel.class);
92         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
93         mController.onResume(appRow, channel, null, null);
94         assertFalse(mController.isAvailable());
95     }
96 
97     @Test
testIsAvailable_notNoIntent()98     public void testIsAvailable_notNoIntent() {
99         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
100         NotificationChannel channel = mock(NotificationChannel.class);
101         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
102         when(channel.getId()).thenReturn(DEFAULT_CHANNEL_ID);
103         mController.onResume(appRow, channel, null, null);
104         assertFalse(mController.isAvailable());
105     }
106 
107     @Test
testIsAvailable()108     public void testIsAvailable() {
109         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
110         appRow.settingsIntent = new Intent("test");
111         NotificationChannel channel = mock(NotificationChannel.class);
112         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
113         when(channel.getId()).thenReturn(DEFAULT_CHANNEL_ID);
114         mController.onResume(appRow, channel, null, null);
115         assertTrue(mController.isAvailable());
116     }
117 
118     @Test
testUpdateState()119     public void testUpdateState() {
120         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
121         Intent intent = new Intent("action");
122         appRow.settingsIntent = intent;
123         mController.onResume(appRow, null, null, null);
124 
125         Preference pref = new Preference(RuntimeEnvironment.application);
126         mController.updateState(pref);
127 
128         assertEquals(intent, pref.getIntent());
129     }
130 }
131