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 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.ArgumentMatchers.anyString; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.pm.PackageManager; 32 import android.content.pm.ResolveInfo; 33 import android.nfc.NfcAdapter; 34 import android.os.BugreportManager; 35 36 import androidx.test.ext.junit.runners.AndroidJUnit4; 37 38 import com.android.dx.mockito.inline.extended.ExtendedMockito; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.ArgumentCaptor; 45 import org.mockito.Captor; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.mockito.MockitoSession; 49 import org.mockito.quality.Strictness; 50 51 import java.util.ArrayList; 52 import java.util.List; 53 54 @RunWith(AndroidJUnit4.class) 55 public class NfcDiagnosticsTest { 56 57 private MockitoSession mStaticMockSession; 58 private NfcDiagnostics mNfcDiagnostics; 59 @Mock 60 Context mContext; 61 @Mock 62 PackageManager mPackageManager; 63 @Mock 64 BugreportManager mBugreportManager; 65 @Captor 66 ArgumentCaptor<Intent> mIntentArgumentCaptor; 67 68 @Before setUp()69 public void setUp() throws PackageManager.NameNotFoundException { 70 mStaticMockSession = ExtendedMockito.mockitoSession() 71 .strictness(Strictness.LENIENT) 72 .startMocking(); 73 74 MockitoAnnotations.initMocks(this); 75 76 when(mContext.getSystemService(BugreportManager.class)).thenReturn(mBugreportManager); 77 List<ResolveInfo> list = new ArrayList<>(); 78 list.add(mock(ResolveInfo.class)); 79 when(mPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(list); 80 when(mContext.getPackageManager()).thenReturn(mPackageManager); 81 82 mNfcDiagnostics = new NfcDiagnostics(mContext); 83 } 84 85 @After tearDown()86 public void tearDown() { 87 mStaticMockSession.finishMocking(); 88 } 89 90 @Test testTakeBugReport()91 public void testTakeBugReport() { 92 mNfcDiagnostics.takeBugReport("test1", "test description"); 93 verify(mPackageManager).queryIntentActivities(mIntentArgumentCaptor.capture(), anyInt()); 94 Intent intent = mIntentArgumentCaptor.getValue(); 95 assertThat(intent).isNotNull(); 96 assertThat(intent.getStringExtra("EXTRA_ISSUE_TITLE")).isEqualTo("test1"); 97 assertThat(intent.getAction()) 98 .isEqualTo("com.google.android.apps.betterbug.intent.FILE_BUG_DEEPLINK"); 99 verify(mContext).startActivity(any()); 100 } 101 102 @Test testTakeBugreportThroughBugreportManager()103 public void testTakeBugreportThroughBugreportManager() { 104 when(mPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(new ArrayList<>()); 105 mNfcDiagnostics.takeBugReport("test1", "test description"); 106 verify(mPackageManager).queryIntentActivities(mIntentArgumentCaptor.capture(), anyInt()); 107 Intent intent = mIntentArgumentCaptor.getValue(); 108 assertThat(intent).isNotNull(); 109 assertThat(intent.getStringExtra("EXTRA_ISSUE_TITLE")).isEqualTo("test1"); 110 assertThat(intent.getAction()) 111 .isEqualTo("com.google.android.apps.betterbug.intent.FILE_BUG_DEEPLINK"); 112 verify(mBugreportManager).requestBugreport(any(), anyString(), anyString()); 113 } 114 } 115