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.Mockito.mock;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.content.ContextWrapper;
25 import android.content.pm.PackageManager;
26 import android.content.res.Resources;
27 import android.os.Handler;
28 import android.util.Log;
29 
30 import org.junit.After;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoSession;
36 import org.mockito.quality.Strictness;
37 
38 import androidx.test.ext.junit.runners.AndroidJUnit4;
39 import androidx.test.platform.app.InstrumentationRegistry;
40 
41 import com.android.dx.mockito.inline.extended.ExtendedMockito;
42 
43 
44 @RunWith(AndroidJUnit4.class)
45 public class DeviceConfigFacadeTest {
46 
47     private static final String TAG = DeviceConfigFacadeTest.class.getSimpleName();
48     private boolean mNfcSupported;
49     private MockitoSession mStaticMockSession;
50     private DeviceConfigFacade mDeviceConfigFacade;
51     private DeviceConfigFacade mDeviceConfigFacadeFalse;
52 
53     @Before
setUp()54     public void setUp() throws Exception {
55         mStaticMockSession = ExtendedMockito.mockitoSession()
56                 .strictness(Strictness.LENIENT)
57                 .startMocking();
58 
59         Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
60         PackageManager pm = context.getPackageManager();
61         if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_ANY)) {
62             mNfcSupported = false;
63             return;
64         }
65         mNfcSupported = true;
66         Handler handler = mock(Handler.class);
67         InstrumentationRegistry.getInstrumentation().runOnMainSync(
68                 () -> mDeviceConfigFacade = new DeviceConfigFacade(getMockedContext(
69                         true, context), handler));
70         Assert.assertNotNull(mDeviceConfigFacade);
71 
72         InstrumentationRegistry.getInstrumentation().runOnMainSync(
73                 () -> mDeviceConfigFacadeFalse = new DeviceConfigFacade(getMockedContext(
74                         false, context), handler));
75         Assert.assertNotNull(mDeviceConfigFacadeFalse);
76     }
77 
getMockedContext(boolean isAntenaEnabled, Context context)78     private Context getMockedContext(boolean isAntenaEnabled, Context context) {
79         Resources mockResources = mock(Resources.class);
80         when(mockResources.getBoolean(eq(R.bool.enable_antenna_blocked_alert)))
81                 .thenReturn(isAntenaEnabled);
82 
83         return new ContextWrapper(context) {
84 
85             @Override
86             public Resources getResources() {
87                 Log.i(TAG, "[Mock] getResources");
88                 return mockResources;
89             }
90         };
91     }
92 
93     @After
94     public void tearDown() throws Exception {
95         mStaticMockSession.finishMocking();
96     }
97 
98     @Test
99     public void testIsAntennaBlockedAlertEnabled() {
100         if (!mNfcSupported) return;
101 
102         boolean isAlertEnabled = mDeviceConfigFacade.isAntennaBlockedAlertEnabled();
103         Log.d(TAG, "isAlertEnabled -" + isAlertEnabled);
104         Assert.assertTrue(isAlertEnabled);
105     }
106 
107     @Test
108     public void testIsAntennaBlockedAlertDisabled() {
109         if (!mNfcSupported) return;
110 
111         boolean isAlertEnabled = mDeviceConfigFacadeFalse.isAntennaBlockedAlertEnabled();
112         Log.d(TAG, "isAlertEnabled -" + isAlertEnabled);
113         Assert.assertFalse(isAlertEnabled);
114     }
115 }
116