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 MockitoSession mStaticMockSession; 49 private DeviceConfigFacade mDeviceConfigFacade; 50 private DeviceConfigFacade mDeviceConfigFacadeFalse; 51 52 @Before setUp()53 public void setUp() throws Exception { 54 mStaticMockSession = ExtendedMockito.mockitoSession() 55 .strictness(Strictness.LENIENT) 56 .startMocking(); 57 Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 58 Handler handler = mock(Handler.class); 59 InstrumentationRegistry.getInstrumentation().runOnMainSync( 60 () -> mDeviceConfigFacade = new DeviceConfigFacade(getMockedContext( 61 true, context), handler)); 62 Assert.assertNotNull(mDeviceConfigFacade); 63 64 InstrumentationRegistry.getInstrumentation().runOnMainSync( 65 () -> mDeviceConfigFacadeFalse = new DeviceConfigFacade(getMockedContext( 66 false, context), handler)); 67 Assert.assertNotNull(mDeviceConfigFacadeFalse); 68 } 69 getMockedContext(boolean isAntenaEnabled, Context context)70 private Context getMockedContext(boolean isAntenaEnabled, Context context) { 71 Resources mockResources = mock(Resources.class); 72 when(mockResources.getBoolean(eq(R.bool.enable_antenna_blocked_alert))) 73 .thenReturn(isAntenaEnabled); 74 75 return new ContextWrapper(context) { 76 77 @Override 78 public Resources getResources() { 79 Log.i(TAG, "[Mock] getResources"); 80 return mockResources; 81 } 82 }; 83 } 84 85 @After 86 public void tearDown() throws Exception { 87 mStaticMockSession.finishMocking(); 88 } 89 90 @Test 91 public void testIsAntennaBlockedAlertEnabled() { 92 boolean isAlertEnabled = mDeviceConfigFacade.isAntennaBlockedAlertEnabled(); 93 Log.d(TAG, "isAlertEnabled -" + isAlertEnabled); 94 Assert.assertTrue(isAlertEnabled); 95 } 96 97 @Test 98 public void testIsAntennaBlockedAlertDisabled() { 99 boolean isAlertEnabled = mDeviceConfigFacadeFalse.isAntennaBlockedAlertEnabled(); 100 Log.d(TAG, "isAlertEnabled -" + isAlertEnabled); 101 Assert.assertFalse(isAlertEnabled); 102 } 103 } 104