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.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.annotation.NonNull;
27 import android.annotation.Nullable;
28 import android.app.NotificationManager;
29 import android.content.BroadcastReceiver;
30 import android.content.Context;
31 import android.content.ContextWrapper;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.content.pm.PackageManager;
35 import android.content.res.Resources;
36 import android.net.wifi.WifiConfiguration;
37 import android.nfc.FormatException;
38 import android.nfc.NdefMessage;
39 import android.nfc.NdefRecord;
40 import android.nfc.tech.Ndef;
41 import android.os.Handler;
42 import android.os.PowerManager;
43 import android.util.Log;
44 
45 import com.android.dx.mockito.inline.extended.ExtendedMockito;
46 import com.android.nfc.handover.HandoverDataParser;
47 
48 import org.junit.After;
49 import org.junit.Assert;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoSession;
55 import org.mockito.quality.Strictness;
56 
57 import androidx.test.ext.junit.runners.AndroidJUnit4;
58 import androidx.test.platform.app.InstrumentationRegistry;
59 
60 
61 @RunWith(AndroidJUnit4.class)
62 public class NfcDeveloperOptionNotificationTest {
63 
64     private static final String TAG = NfcDeveloperOptionNotificationTest.class.getSimpleName();
65     private boolean mNfcSupported;
66     private MockitoSession mStaticMockSession;
67     private Context mockContext;
68     private NfcDeveloperOptionNotification mNfcDevOptionNoti;
69     private  NotificationManager mockNotificationManager;
70 
71     @Before
setUp()72     public void setUp() throws Exception {
73         mStaticMockSession = ExtendedMockito.mockitoSession()
74                 .strictness(Strictness.LENIENT)
75                 .startMocking();
76 
77         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
78         PackageManager pm = context.getPackageManager();
79         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) {
80             mNfcSupported = false;
81             return;
82         }
83         mNfcSupported = true;
84 
85         mockNotificationManager = Mockito.mock(NotificationManager.class);
86         Resources mockResources = Mockito.mock(Resources.class);
87         when(mockResources.getBoolean(eq(R.bool.tag_intent_app_pref_supported)))
88                 .thenReturn(false);
89 
90         mockContext = new ContextWrapper(context) {
91             @Override
92             public Object getSystemService(String name) {
93                 if (Context.NOTIFICATION_SERVICE.equals(name)) {
94                     Log.i(TAG, "[Mock] mockNotificationManager");
95                     return mockNotificationManager;
96                 }
97                 return super.getSystemService(name);
98             }
99 
100             @Override
101             public Resources getResources() {
102                 Log.i(TAG, "[Mock] getResources");
103                 return mockResources;
104             }
105 
106             @Override
107             public Intent registerReceiverForAllUsers(@Nullable BroadcastReceiver receiver,
108                     @NonNull IntentFilter filter, @Nullable String broadcastPermission,
109                     @Nullable Handler scheduler) {
110                 Log.i(TAG, "[Mock] getIntent");
111                 return Mockito.mock(Intent.class);
112             }
113         };
114 
115         InstrumentationRegistry.getInstrumentation().runOnMainSync(
116                 () -> mNfcDevOptionNoti = new NfcDeveloperOptionNotification(mockContext));
117         Assert.assertNotNull(mNfcDevOptionNoti);
118     }
119 
120     @After
tearDown()121     public void tearDown() throws Exception {
122         mStaticMockSession.finishMocking();
123     }
124 
125     @Test
testStartNotification()126     public void testStartNotification() {
127         if (!mNfcSupported) return;
128 
129 
130         mNfcDevOptionNoti.startNotification();
131         verify(mockNotificationManager).createNotificationChannel(any());
132     }
133 }