Loading android/app/src/com/android/bluetooth/bas/BatteryService.java +5 −2 Original line number Diff line number Diff line Loading @@ -416,8 +416,11 @@ public class BatteryService extends ProfileService { return mAdapterService.getDatabase() .getProfileConnectionPolicy(device, BluetoothProfile.BATTERY); } void handleBatteryChanged(BluetoothDevice device, int batteryLevel) { /** * Called when the battery level of the device is notified. */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public void handleBatteryChanged(BluetoothDevice device, int batteryLevel) { mAdapterService.setBatteryLevel(device, batteryLevel); } Loading android/app/src/com/android/bluetooth/bas/BatteryStateMachine.java +6 −2 Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ public class BatteryStateMachine extends StateMachine { WeakReference<BatteryService> mServiceRef; BluetoothGatt mBluetoothGatt; BluetoothGattCallback mGattCallback; GattCallback mGattCallback; final BluetoothDevice mDevice; BatteryStateMachine(BluetoothDevice device, BatteryService service, Looper looper) { Loading Loading @@ -578,7 +578,11 @@ public class BatteryStateMachine extends StateMachine { } } private void updateBatteryLevel(byte[] value) { @VisibleForTesting void updateBatteryLevel(byte[] value) { if (value.length <= 0) { return; } int batteryLevel = value[0] & 0xFF; BatteryService service = mServiceRef.get(); Loading android/app/tests/unit/src/com/android/bluetooth/bas/BatteryStateMachineTest.java +34 −5 Original line number Diff line number Diff line Loading @@ -20,14 +20,15 @@ import static android.bluetooth.BluetoothGatt.GATT_SUCCESS; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.after; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.reset; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; Loading @@ -38,22 +39,20 @@ import android.os.Looper; import androidx.test.InstrumentationRegistry; import androidx.test.filters.LargeTest; import androidx.test.filters.MediumTest; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; Loading Loading @@ -215,6 +214,36 @@ public class BatteryStateMachineTest { IsInstanceOf.instanceOf(BatteryStateMachine.Disconnected.class)); } @Test public void testBatteryLevelChanged() { allowConnection(true); allowConnectGatt(true); // To create a callback mBatteryStateMachine.connectGatt(); mBatteryStateMachine.mGattCallback.updateBatteryLevel(new byte[]{(byte)0x30}); verify(mBatteryService, timeout(TIMEOUT_MS)) .handleBatteryChanged(any(BluetoothDevice.class), eq(0x30)); } @Test public void testEmptyBatteryLevelIgnored() { allowConnection(true); allowConnectGatt(true); // To create a callback mBatteryStateMachine.connectGatt(); try { mBatteryStateMachine.mGattCallback.updateBatteryLevel(new byte[0]); } catch (Exception ex) { fail(); } verify(mBatteryService, after(WAIT_MS).never()) .handleBatteryChanged(any(BluetoothDevice.class), anyInt()); } // It simulates GATT connection for testing. public class StubBatteryStateMachine extends BatteryStateMachine { boolean mShouldAllowGatt = true; Loading Loading
android/app/src/com/android/bluetooth/bas/BatteryService.java +5 −2 Original line number Diff line number Diff line Loading @@ -416,8 +416,11 @@ public class BatteryService extends ProfileService { return mAdapterService.getDatabase() .getProfileConnectionPolicy(device, BluetoothProfile.BATTERY); } void handleBatteryChanged(BluetoothDevice device, int batteryLevel) { /** * Called when the battery level of the device is notified. */ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) public void handleBatteryChanged(BluetoothDevice device, int batteryLevel) { mAdapterService.setBatteryLevel(device, batteryLevel); } Loading
android/app/src/com/android/bluetooth/bas/BatteryStateMachine.java +6 −2 Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ public class BatteryStateMachine extends StateMachine { WeakReference<BatteryService> mServiceRef; BluetoothGatt mBluetoothGatt; BluetoothGattCallback mGattCallback; GattCallback mGattCallback; final BluetoothDevice mDevice; BatteryStateMachine(BluetoothDevice device, BatteryService service, Looper looper) { Loading Loading @@ -578,7 +578,11 @@ public class BatteryStateMachine extends StateMachine { } } private void updateBatteryLevel(byte[] value) { @VisibleForTesting void updateBatteryLevel(byte[] value) { if (value.length <= 0) { return; } int batteryLevel = value[0] & 0xFF; BatteryService service = mServiceRef.get(); Loading
android/app/tests/unit/src/com/android/bluetooth/bas/BatteryStateMachineTest.java +34 −5 Original line number Diff line number Diff line Loading @@ -20,14 +20,15 @@ import static android.bluetooth.BluetoothGatt.GATT_SUCCESS; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.after; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.reset; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; Loading @@ -38,22 +39,20 @@ import android.os.Looper; import androidx.test.InstrumentationRegistry; import androidx.test.filters.LargeTest; import androidx.test.filters.MediumTest; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; Loading Loading @@ -215,6 +214,36 @@ public class BatteryStateMachineTest { IsInstanceOf.instanceOf(BatteryStateMachine.Disconnected.class)); } @Test public void testBatteryLevelChanged() { allowConnection(true); allowConnectGatt(true); // To create a callback mBatteryStateMachine.connectGatt(); mBatteryStateMachine.mGattCallback.updateBatteryLevel(new byte[]{(byte)0x30}); verify(mBatteryService, timeout(TIMEOUT_MS)) .handleBatteryChanged(any(BluetoothDevice.class), eq(0x30)); } @Test public void testEmptyBatteryLevelIgnored() { allowConnection(true); allowConnectGatt(true); // To create a callback mBatteryStateMachine.connectGatt(); try { mBatteryStateMachine.mGattCallback.updateBatteryLevel(new byte[0]); } catch (Exception ex) { fail(); } verify(mBatteryService, after(WAIT_MS).never()) .handleBatteryChanged(any(BluetoothDevice.class), anyInt()); } // It simulates GATT connection for testing. public class StubBatteryStateMachine extends BatteryStateMachine { boolean mShouldAllowGatt = true; Loading