Fix the logic error on PBAP permission assignment

-add test case

Bug: 114808220
Test: make -j42 RunSettingsRoboTests
Change-Id: Id77ade12c28e31a21c1c7a5dcb28906112d3465b
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingController.java b/src/com/android/settings/bluetooth/BluetoothPairingController.java
index c39f1d9..94bdfe8 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingController.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingController.java
@@ -54,8 +54,7 @@
 
     // Bluetooth dependencies for the connection we are trying to establish
     private LocalBluetoothManager mBluetoothManager;
-    @VisibleForTesting
-    BluetoothDevice mDevice;
+    private BluetoothDevice mDevice;
     @VisibleForTesting
     int mType;
     private String mUserInput;
@@ -189,16 +188,16 @@
      *
      */
      public void  setContactSharingState() {
-        if ((mDevice.getPhonebookAccessPermission() != BluetoothDevice.ACCESS_ALLOWED)
-                && (mDevice.getPhonebookAccessPermission() != BluetoothDevice.ACCESS_REJECTED)) {
-                 if (mDevice.getBluetoothClass().getDeviceClass()
-                        == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) {
-                    onCheckedChanged(null, true);
-            } else {
-                onCheckedChanged(null, false);
-            }
-        }
-    }
+         final int permission = mDevice.getPhonebookAccessPermission();
+         if (permission == BluetoothDevice.ACCESS_ALLOWED
+                 || (permission == BluetoothDevice.ACCESS_UNKNOWN && mDevice.getBluetoothClass().
+                        getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE)) {
+             onCheckedChanged(null, true);
+         } else {
+             onCheckedChanged(null, false);
+         }
+
+     }
 
     /**
      * A method for querying if the provided editable is a valid passkey/pin format for this device.
diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingControllerTest.java
index b28a8b2..5233d84 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothPairingControllerTest.java
@@ -17,9 +17,10 @@
 
 import static android.bluetooth.BluetoothDevice.PAIRING_VARIANT_CONSENT;
 
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.bluetooth.BluetoothClass;
 import android.bluetooth.BluetoothDevice;
 import android.content.Context;
 import android.content.Intent;
@@ -39,6 +40,8 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(shadows = {ShadowBluetoothPan.class, ShadowBluetoothAdapter.class})
 public class BluetoothPairingControllerTest {
+    private final BluetoothClass mBluetoothClass =
+            new BluetoothClass(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE);
     @Mock
     private BluetoothDevice mBluetoothDevice;
     private Context mContext;
@@ -51,7 +54,7 @@
         mContext = RuntimeEnvironment.application;
         final Intent intent = new Intent();
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mBluetoothDevice);
-        mBluetoothPairingController = spy(new BluetoothPairingController(intent, mContext));
+        mBluetoothPairingController = new BluetoothPairingController(intent, mContext);
     }
 
     @Test
@@ -63,4 +66,36 @@
 
         verify(mBluetoothDevice).setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
     }
+
+    @Test
+    public void onSetContactSharingState_permissionAllowed_setPBAPAllowed() {
+        when(mBluetoothDevice.getPhonebookAccessPermission()).thenReturn(
+                BluetoothDevice.ACCESS_ALLOWED);
+        mBluetoothPairingController.setContactSharingState();
+        mBluetoothPairingController.onDialogPositiveClick(null);
+
+        verify(mBluetoothDevice).setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
+    }
+
+    @Test
+    public void onSetContactSharingState_permissionUnknown_audioVideoHandsfree_setPBAPAllowed() {
+        when(mBluetoothDevice.getPhonebookAccessPermission()).thenReturn(
+                BluetoothDevice.ACCESS_UNKNOWN);
+        when(mBluetoothDevice.getBluetoothClass()).thenReturn(mBluetoothClass);
+        mBluetoothPairingController.setContactSharingState();
+        mBluetoothPairingController.onDialogPositiveClick(null);
+
+        verify(mBluetoothDevice).setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED);
+    }
+
+    @Test
+    public void onSetContactSharingState_permissionRejected_setPBAPRejected() {
+        when(mBluetoothDevice.getPhonebookAccessPermission()).thenReturn(
+                BluetoothDevice.ACCESS_REJECTED);
+        when(mBluetoothDevice.getBluetoothClass()).thenReturn(mBluetoothClass);
+        mBluetoothPairingController.setContactSharingState();
+        mBluetoothPairingController.onDialogPositiveClick(null);
+
+        verify(mBluetoothDevice).setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED);
+    }
 }