1 /* 2 * Copyright 2022 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 android.bluetooth; 18 19 import static android.bluetooth.BluetoothActivityEnergyInfo.BT_STACK_STATE_INVALID; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.os.Parcel; 24 25 import androidx.test.filters.SmallTest; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Assert; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 import java.util.ArrayList; 33 34 /** 35 * Test cases for {@link BluetoothActivityEnergyInfo}. 36 */ 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 public class BluetoothActivityEnergyInfoTest { 40 41 @Test constructor()42 public void constructor() { 43 long timestamp = 10000; 44 int stackState = BT_STACK_STATE_INVALID; 45 long txTime = 100; 46 long rxTime = 200; 47 long idleTime = 300; 48 long energyUsed = 10; 49 BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo( 50 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 51 52 assertThat(info.getTimestampMillis()).isEqualTo(timestamp); 53 assertThat(info.getBluetoothStackState()).isEqualTo(stackState); 54 assertThat(info.getControllerTxTimeMillis()).isEqualTo(txTime); 55 assertThat(info.getControllerRxTimeMillis()).isEqualTo(rxTime); 56 assertThat(info.getControllerIdleTimeMillis()).isEqualTo(idleTime); 57 assertThat(info.getControllerEnergyUsed()).isEqualTo(energyUsed); 58 assertThat(info.getUidTraffic()).isEmpty(); 59 } 60 61 @Test setUidTraffic()62 public void setUidTraffic() { 63 long timestamp = 10000; 64 int stackState = BT_STACK_STATE_INVALID; 65 long txTime = 100; 66 long rxTime = 200; 67 long idleTime = 300; 68 long energyUsed = 10; 69 BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo( 70 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 71 72 ArrayList<UidTraffic> traffics = new ArrayList<>(); 73 UidTraffic traffic = new UidTraffic(123, 300, 400); 74 traffics.add(traffic); 75 info.setUidTraffic(traffics); 76 77 assertThat(info.getUidTraffic().size()).isEqualTo(1); 78 assertThat(info.getUidTraffic().get(0)).isEqualTo(traffic); 79 } 80 81 @Test isValid()82 public void isValid() { 83 long timestamp = 10000; 84 int stackState = BT_STACK_STATE_INVALID; 85 long txTime = 100; 86 long rxTime = 200; 87 long idleTime = 300; 88 long energyUsed = 10; 89 BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo( 90 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 91 92 assertThat(info.isValid()).isEqualTo(true); 93 94 info = new BluetoothActivityEnergyInfo( 95 timestamp, stackState, -1, rxTime, idleTime, energyUsed); 96 assertThat(info.isValid()).isEqualTo(false); 97 98 info = new BluetoothActivityEnergyInfo( 99 timestamp, stackState, txTime, -1, idleTime, energyUsed); 100 assertThat(info.isValid()).isEqualTo(false); 101 102 info = new BluetoothActivityEnergyInfo( 103 timestamp, stackState, txTime, rxTime, -1, energyUsed); 104 assertThat(info.isValid()).isEqualTo(false); 105 } 106 107 @Test writeToParcel()108 public void writeToParcel() { 109 long timestamp = 10000; 110 int stackState = BT_STACK_STATE_INVALID; 111 long txTime = 100; 112 long rxTime = 200; 113 long idleTime = 300; 114 long energyUsed = 10; 115 BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo( 116 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 117 118 Parcel parcel = Parcel.obtain(); 119 info.writeToParcel(parcel, 0); 120 parcel.setDataPosition(0); 121 122 BluetoothActivityEnergyInfo infoFromParcel = 123 BluetoothActivityEnergyInfo.CREATOR.createFromParcel(parcel); 124 parcel.recycle(); 125 126 assertThat(infoFromParcel.getTimestampMillis()).isEqualTo(timestamp); 127 assertThat(infoFromParcel.getBluetoothStackState()).isEqualTo(stackState); 128 assertThat(infoFromParcel.getControllerTxTimeMillis()).isEqualTo(txTime); 129 assertThat(infoFromParcel.getControllerRxTimeMillis()).isEqualTo(rxTime); 130 assertThat(infoFromParcel.getControllerIdleTimeMillis()).isEqualTo(idleTime); 131 assertThat(infoFromParcel.getControllerEnergyUsed()).isEqualTo(energyUsed); 132 assertThat(infoFromParcel.getUidTraffic()).isEmpty(); 133 } 134 135 @Test toString_ThrowsNoExceptions()136 public void toString_ThrowsNoExceptions() { 137 long timestamp = 10000; 138 int stackState = BT_STACK_STATE_INVALID; 139 long txTime = 100; 140 long rxTime = 200; 141 long idleTime = 300; 142 long energyUsed = 10; 143 BluetoothActivityEnergyInfo info = new BluetoothActivityEnergyInfo( 144 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 145 146 try { 147 String infoString = info.toString(); 148 } catch (Exception e) { 149 Assert.fail("Should throw a RuntimeException"); 150 } 151 } 152 } 153