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 boolean mNfcSupported; 57 private MockitoSession mStaticMockSession; 58 private Context mockContext; 59 private NfcBlockedNotification mBlockedNotification; 60 private NotificationManager mockNotificationManager; 61 62 @Before setUp()63 public void setUp() throws Exception { 64 mStaticMockSession = ExtendedMockito.mockitoSession() 65 .strictness(Strictness.LENIENT) 66 .startMocking(); 67 68 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 69 PackageManager pm = context.getPackageManager(); 70 if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) { 71 mNfcSupported = false; 72 return; 73 } 74 mNfcSupported = true; 75 76 mockNotificationManager = Mockito.mock(NotificationManager.class); 77 Resources mockResources = Mockito.mock(Resources.class); 78 when(mockResources.getBoolean(eq(R.bool.tag_intent_app_pref_supported))) 79 .thenReturn(false); 80 81 mockContext = new ContextWrapper(context) { 82 @Override 83 public Object getSystemService(String name) { 84 if (Context.NOTIFICATION_SERVICE.equals(name)) { 85 Log.i(TAG, "[Mock] mockNotificationManager"); 86 return mockNotificationManager; 87 } 88 return super.getSystemService(name); 89 } 90 91 @Override 92 public Resources getResources() { 93 Log.i(TAG, "[Mock] getResources"); 94 return mockResources; 95 } 96 97 @Override 98 public Intent registerReceiverForAllUsers(@Nullable BroadcastReceiver receiver, 99 @NonNull IntentFilter filter, @Nullable String broadcastPermission, 100 @Nullable Handler scheduler) { 101 Log.i(TAG, "[Mock] getIntent"); 102 return Mockito.mock(Intent.class); 103 } 104 }; 105 106 InstrumentationRegistry.getInstrumentation().runOnMainSync( 107 () -> mBlockedNotification = new NfcBlockedNotification(mockContext)); 108 Assert.assertNotNull(mBlockedNotification); 109 } 110 111 @After tearDown()112 public void tearDown() throws Exception { 113 mStaticMockSession.finishMocking(); 114 } 115 116 @Test testStartNotification()117 public void testStartNotification() { 118 if (!mNfcSupported) return; 119 120 121 mBlockedNotification.startNotification(); 122 verify(mockNotificationManager).createNotificationChannel(any()); 123 } 124 }