• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.nfc;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.app.NotificationManager;
27 import android.content.BroadcastReceiver;
28 import android.content.Context;
29 import android.content.ContextWrapper;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.pm.PackageManager;
33 import android.content.res.Resources;
34 import android.os.Handler;
35 import android.util.Log;
36 
37 import androidx.test.ext.junit.runners.AndroidJUnit4;
38 import androidx.test.platform.app.InstrumentationRegistry;
39 
40 import com.android.dx.mockito.inline.extended.ExtendedMockito;
41 
42 import org.junit.After;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.mockito.MockitoSession;
49 import org.mockito.quality.Strictness;
50 
51 
52 @RunWith(AndroidJUnit4.class)
53 public class NfcBlockedNotificationTest {
54 
55     private static final String TAG = NfcBlockedNotificationTest.class.getSimpleName();
56     private MockitoSession mStaticMockSession;
57     private Context mockContext;
58     private NfcBlockedNotification mBlockedNotification;
59     private  NotificationManager mockNotificationManager;
60 
61     @Before
setUp()62     public void setUp() throws Exception {
63         mStaticMockSession = ExtendedMockito.mockitoSession()
64                 .strictness(Strictness.LENIENT)
65                 .startMocking();
66         mockNotificationManager = Mockito.mock(NotificationManager.class);
67         Resources mockResources = Mockito.mock(Resources.class);
68         when(mockResources.getBoolean(eq(R.bool.tag_intent_app_pref_supported)))
69                 .thenReturn(false);
70 
71 	Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
72         mockContext = new ContextWrapper(context) {
73             @Override
74             public Object getSystemService(String name) {
75                 if (Context.NOTIFICATION_SERVICE.equals(name)) {
76                     Log.i(TAG, "[Mock] mockNotificationManager");
77                     return mockNotificationManager;
78                 }
79                 return super.getSystemService(name);
80             }
81 
82             @Override
83             public Resources getResources() {
84                 Log.i(TAG, "[Mock] getResources");
85                 return mockResources;
86             }
87 
88             @Override
89             public Intent registerReceiverForAllUsers(@Nullable BroadcastReceiver receiver,
90                     @NonNull IntentFilter filter, @Nullable String broadcastPermission,
91                     @Nullable Handler scheduler) {
92                 Log.i(TAG, "[Mock] getIntent");
93                 return Mockito.mock(Intent.class);
94             }
95         };
96 
97         InstrumentationRegistry.getInstrumentation().runOnMainSync(
98                 () -> mBlockedNotification = new NfcBlockedNotification(mockContext));
99         Assert.assertNotNull(mBlockedNotification);
100     }
101 
102     @After
tearDown()103     public void tearDown() throws Exception {
104         mStaticMockSession.finishMocking();
105     }
106 
107     @Test
testStartNotification()108     public void testStartNotification() {
109         mBlockedNotification.startNotification();
110         verify(mockNotificationManager).createNotificationChannel(any());
111     }
112 }
113