• 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.eq;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.verifyNoMoreInteractions;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.nfc.Constants;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 
35 public class NfcBootCompletedReceiverTest {
36     @Mock
37     private Context mMockContext;
38     @Mock
39     private Intent mMockIntent;
40     @Mock
41     private PackageManager mMockPkgManager;
42     private NfcBootCompletedReceiver mNfcBootCompletedReceiver;
43     private static final String PACKAGE_NAME = "com.android.nfc";
44 
45     @Before
setUp()46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         when(mMockContext.getPackageManager()).thenReturn(mMockPkgManager);
49         when(mMockContext.getPackageName()).thenReturn(PACKAGE_NAME);
50 
51         mNfcBootCompletedReceiver = new NfcBootCompletedReceiver();
52     }
53 
54     @Test
testFeatureNoNfc()55     public void testFeatureNoNfc() {
56         when(mMockIntent.getAction()).thenReturn(Intent.ACTION_BOOT_COMPLETED);
57         when(mMockPkgManager.hasSystemFeature(Constants.FEATURE_NFC_ANY)).thenReturn(false);
58 
59         mNfcBootCompletedReceiver.onReceive(mMockContext, mMockIntent);
60         verify(mMockPkgManager).setApplicationEnabledSetting(eq(PACKAGE_NAME),
61                 eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED),
62                 eq(0));
63     }
64 
65     @Test
testFeatureSupportNfc()66     public void testFeatureSupportNfc() {
67         when(mMockIntent.getAction()).thenReturn(Intent.ACTION_BOOT_COMPLETED);
68         when(mMockPkgManager.hasSystemFeature(Constants.FEATURE_NFC_ANY)).thenReturn(true);
69 
70         mNfcBootCompletedReceiver.onReceive(mMockContext, mMockIntent);
71         verify(mMockPkgManager).setComponentEnabledSetting(
72                 eq(new ComponentName(mMockContext, NfcBootCompletedReceiver.class)),
73                 eq(PackageManager.COMPONENT_ENABLED_STATE_DISABLED),
74                 eq(PackageManager.DONT_KILL_APP));
75     }
76 
77     @Test
testFeatureNonBootCompleteAction()78     public void testFeatureNonBootCompleteAction() {
79         when(mMockIntent.getAction()).thenReturn(Intent.ACTION_ALARM_CHANGED);
80 
81         mNfcBootCompletedReceiver.onReceive(mMockContext, mMockIntent);
82         verifyNoMoreInteractions(mMockPkgManager);
83     }
84 }
85