1 /* 2 * Copyright (C) 2023 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.settings.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assume.assumeTrue; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.companion.CompanionDeviceManager; 26 import android.companion.Flags; 27 import android.companion.datatransfer.PermissionSyncRequest; 28 29 import androidx.preference.PreferenceCategory; 30 import androidx.preference.TwoStatePreference; 31 32 import com.android.settingslib.core.lifecycle.Lifecycle; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 import java.util.Collections; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class BluetoothDetailsDataSyncControllerTest extends BluetoothDetailsControllerTestBase { 46 47 private static final String MAC_ADDRESS = "AA:BB:CC:DD:EE:FF"; 48 private static final int DUMMY_ASSOCIATION_ID = -1; 49 private static final int ASSOCIATION_ID = 1; 50 private static final String KEY_PERM_SYNC = "perm_sync"; 51 52 private BluetoothDetailsDataSyncController mController; 53 @Mock 54 private Lifecycle mLifecycle; 55 @Mock 56 private PreferenceCategory mPreferenceCategory; 57 @Mock 58 private CompanionDeviceManager mCompanionDeviceManager; 59 60 private PermissionSyncRequest mPermissionSyncRequest; 61 private TwoStatePreference mPermSyncPreference; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 67 mContext = spy(RuntimeEnvironment.application); 68 when(mContext.getSystemService(CompanionDeviceManager.class)).thenReturn( 69 mCompanionDeviceManager); 70 when(mCachedDevice.getAddress()).thenReturn(MAC_ADDRESS); 71 when(mCompanionDeviceManager.getAllAssociations()).thenReturn(Collections.emptyList()); 72 mPermissionSyncRequest = new PermissionSyncRequest(ASSOCIATION_ID); 73 when(mCompanionDeviceManager.getPermissionSyncRequest(ASSOCIATION_ID)).thenReturn( 74 mPermissionSyncRequest); 75 76 mController = new BluetoothDetailsDataSyncController(mContext, mFragment, 77 mCachedDevice, mLifecycle); 78 mController.mAssociationId = ASSOCIATION_ID; 79 mController.mPreferenceCategory = mPreferenceCategory; 80 81 mPermSyncPreference = mController.createPermSyncPreference(mContext); 82 when(mPreferenceCategory.findPreference(KEY_PERM_SYNC)).thenReturn(mPermSyncPreference); 83 } 84 85 @Test isAvailable_noAssociations_returnsFalse()86 public void isAvailable_noAssociations_returnsFalse() { 87 mController.mAssociationId = DUMMY_ASSOCIATION_ID; 88 assertThat(mController.isAvailable()).isFalse(); 89 } 90 91 @Test isAvailable_hasAssociations_returnTrue()92 public void isAvailable_hasAssociations_returnTrue() { 93 assumeTrue(Flags.ongoingPermSync()); 94 assertThat(mController.isAvailable()).isTrue(); 95 } 96 97 @Test refresh_noAssociations_checkPreferenceInvisible()98 public void refresh_noAssociations_checkPreferenceInvisible() { 99 mController.mAssociationId = DUMMY_ASSOCIATION_ID; 100 mController.refresh(); 101 102 assertThat(mPermSyncPreference.isVisible()).isFalse(); 103 } 104 105 @Test refresh_permSyncNull_checkPreferenceInvisible()106 public void refresh_permSyncNull_checkPreferenceInvisible() { 107 mPermissionSyncRequest = null; 108 when(mCompanionDeviceManager.getPermissionSyncRequest(ASSOCIATION_ID)).thenReturn( 109 mPermissionSyncRequest); 110 mController.refresh(); 111 112 assertThat(mPermSyncPreference.isVisible()).isFalse(); 113 } 114 115 @Test refresh_permSyncEnabled_checkPreferenceOn()116 public void refresh_permSyncEnabled_checkPreferenceOn() { 117 mPermissionSyncRequest.setUserConsented(true); 118 mController.refresh(); 119 120 assertThat(mPermSyncPreference.isVisible()).isTrue(); 121 assertThat(mPermSyncPreference.isChecked()).isTrue(); 122 } 123 124 @Test refresh_permSyncDisabled_checkPreferenceOff()125 public void refresh_permSyncDisabled_checkPreferenceOff() { 126 mPermissionSyncRequest.setUserConsented(false); 127 mController.refresh(); 128 129 assertThat(mPermSyncPreference.isVisible()).isTrue(); 130 assertThat(mPermSyncPreference.isChecked()).isFalse(); 131 } 132 } 133