1 /* 2 * Copyright 2018 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 package com.android.settings.connecteddevice; 17 18 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 19 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.provider.Settings; 23 import com.android.settings.R; 24 import com.android.settings.nfc.NfcPreferenceController; 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.MockitoAnnotations; 30 import org.robolectric.RuntimeEnvironment; 31 import org.robolectric.annotation.Config; 32 import org.robolectric.shadows.ShadowNfcAdapter; 33 import org.robolectric.util.ReflectionHelpers; 34 35 import static com.google.common.truth.Truth.assertThat; 36 import static org.mockito.Mockito.spy; 37 import static org.robolectric.Shadows.shadowOf; 38 39 @RunWith(SettingsRobolectricTestRunner.class) 40 @Config(shadows = ShadowNfcAdapter.class) 41 public class AdvancedConnectedDeviceControllerTest { 42 43 private static final String KEY = "test_key"; 44 private static final String DRIVING_MODE_SETTINGS_ENABLED = 45 "gearhead:driving_mode_settings_enabled"; 46 47 private Context mContext; 48 private NfcPreferenceController mNfcController; 49 private ShadowNfcAdapter mShadowNfcAdapter; 50 private ContentResolver mContentResolver; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 56 mContext = spy(RuntimeEnvironment.application); 57 mContentResolver = mContext.getContentResolver(); 58 mNfcController = new NfcPreferenceController(mContext, 59 NfcPreferenceController.KEY_TOGGLE_NFC); 60 mShadowNfcAdapter = shadowOf(ShadowNfcAdapter.getNfcAdapter(mContext)); 61 } 62 63 @Test getAvailabilityStatus_returnStatusIsAvailable()64 public void getAvailabilityStatus_returnStatusIsAvailable() { 65 AdvancedConnectedDeviceController controller = 66 new AdvancedConnectedDeviceController(mContext, KEY); 67 68 assertThat(controller.getAvailabilityStatus()).isEqualTo( 69 AVAILABLE); 70 } 71 72 @Test isDrivingModeAvailable_returnTrue()73 public void isDrivingModeAvailable_returnTrue() { 74 Settings.System.putInt(mContentResolver, DRIVING_MODE_SETTINGS_ENABLED, 1); 75 76 assertThat(AdvancedConnectedDeviceController.isDrivingModeAvailable(mContext)).isTrue(); 77 } 78 79 @Test isDrivingModeAvailable_returnFalse()80 public void isDrivingModeAvailable_returnFalse() { 81 Settings.System.putInt(mContentResolver, DRIVING_MODE_SETTINGS_ENABLED, 0); 82 83 assertThat(AdvancedConnectedDeviceController.isDrivingModeAvailable(mContext)).isFalse(); 84 } 85 86 @Test getConnectedDevicesSummaryResourceId_NFCAndDrivingModeAvailable()87 public void getConnectedDevicesSummaryResourceId_NFCAndDrivingModeAvailable() { 88 // NFC available, driving mode available 89 mShadowNfcAdapter.setEnabled(true); 90 assertThat(AdvancedConnectedDeviceController 91 .getConnectedDevicesSummaryResourceId(mNfcController, true)) 92 .isEqualTo(R.string.connected_devices_dashboard_summary); 93 } 94 95 @Test getConnectedDevicesSummaryResourceId_NFCAvailableAndDrivingModeNotAvailable()96 public void getConnectedDevicesSummaryResourceId_NFCAvailableAndDrivingModeNotAvailable() { 97 // NFC is available, driving mode not available 98 mShadowNfcAdapter.setEnabled(true); 99 assertThat(AdvancedConnectedDeviceController 100 .getConnectedDevicesSummaryResourceId(mNfcController, false)) 101 .isEqualTo(R.string.connected_devices_dashboard_no_driving_mode_summary); 102 } 103 104 @Test getConnectedDevicesSummaryResourceId_NFCNotAvailableDrivingModeAvailable()105 public void getConnectedDevicesSummaryResourceId_NFCNotAvailableDrivingModeAvailable() { 106 // NFC not available, driving mode available 107 ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null); 108 assertThat(AdvancedConnectedDeviceController 109 .getConnectedDevicesSummaryResourceId(mNfcController, true)) 110 .isEqualTo(R.string.connected_devices_dashboard_no_nfc_summary); 111 } 112 113 @Test getConnectedDevicesSummaryResourceId_NFCAndDrivingModeNotAvailable()114 public void getConnectedDevicesSummaryResourceId_NFCAndDrivingModeNotAvailable() { 115 // NFC not available, driving mode not available 116 ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null); 117 assertThat(AdvancedConnectedDeviceController 118 .getConnectedDevicesSummaryResourceId(mNfcController, false)) 119 .isEqualTo(R.string.connected_devices_dashboard_no_driving_mode_no_nfc_summary); 120 } 121 } 122