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 /** Test cases for {@link BluetoothActivityEnergyInfo}. */ 35 @SmallTest 36 @RunWith(AndroidJUnit4.class) 37 public class BluetoothActivityEnergyInfoTest { 38 39 @Test constructor()40 public void constructor() { 41 long timestamp = 10000; 42 int stackState = BT_STACK_STATE_INVALID; 43 long txTime = 100; 44 long rxTime = 200; 45 long idleTime = 300; 46 long energyUsed = 10; 47 BluetoothActivityEnergyInfo info = 48 new BluetoothActivityEnergyInfo( 49 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 50 51 assertThat(info.getTimestampMillis()).isEqualTo(timestamp); 52 assertThat(info.getBluetoothStackState()).isEqualTo(stackState); 53 assertThat(info.getControllerTxTimeMillis()).isEqualTo(txTime); 54 assertThat(info.getControllerRxTimeMillis()).isEqualTo(rxTime); 55 assertThat(info.getControllerIdleTimeMillis()).isEqualTo(idleTime); 56 assertThat(info.getControllerEnergyUsed()).isEqualTo(energyUsed); 57 assertThat(info.getUidTraffic()).isEmpty(); 58 } 59 60 @Test setUidTraffic()61 public void setUidTraffic() { 62 long timestamp = 10000; 63 int stackState = BT_STACK_STATE_INVALID; 64 long txTime = 100; 65 long rxTime = 200; 66 long idleTime = 300; 67 long energyUsed = 10; 68 BluetoothActivityEnergyInfo info = 69 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 = 90 new BluetoothActivityEnergyInfo( 91 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 92 93 assertThat(info.isValid()).isEqualTo(true); 94 95 info = 96 new BluetoothActivityEnergyInfo( 97 timestamp, stackState, -1, rxTime, idleTime, energyUsed); 98 assertThat(info.isValid()).isEqualTo(false); 99 100 info = 101 new BluetoothActivityEnergyInfo( 102 timestamp, stackState, txTime, -1, idleTime, energyUsed); 103 assertThat(info.isValid()).isEqualTo(false); 104 105 info = 106 new BluetoothActivityEnergyInfo( 107 timestamp, stackState, txTime, rxTime, -1, energyUsed); 108 assertThat(info.isValid()).isEqualTo(false); 109 } 110 111 @Test writeToParcel()112 public void writeToParcel() { 113 long timestamp = 10000; 114 int stackState = BT_STACK_STATE_INVALID; 115 long txTime = 100; 116 long rxTime = 200; 117 long idleTime = 300; 118 long energyUsed = 10; 119 BluetoothActivityEnergyInfo info = 120 new BluetoothActivityEnergyInfo( 121 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 122 123 Parcel parcel = Parcel.obtain(); 124 info.writeToParcel(parcel, 0); 125 parcel.setDataPosition(0); 126 127 BluetoothActivityEnergyInfo infoFromParcel = 128 BluetoothActivityEnergyInfo.CREATOR.createFromParcel(parcel); 129 parcel.recycle(); 130 131 assertThat(infoFromParcel.getTimestampMillis()).isEqualTo(timestamp); 132 assertThat(infoFromParcel.getBluetoothStackState()).isEqualTo(stackState); 133 assertThat(infoFromParcel.getControllerTxTimeMillis()).isEqualTo(txTime); 134 assertThat(infoFromParcel.getControllerRxTimeMillis()).isEqualTo(rxTime); 135 assertThat(infoFromParcel.getControllerIdleTimeMillis()).isEqualTo(idleTime); 136 assertThat(infoFromParcel.getControllerEnergyUsed()).isEqualTo(energyUsed); 137 assertThat(infoFromParcel.getUidTraffic()).isEmpty(); 138 } 139 140 @Test toString_ThrowsNoExceptions()141 public void toString_ThrowsNoExceptions() { 142 long timestamp = 10000; 143 int stackState = BT_STACK_STATE_INVALID; 144 long txTime = 100; 145 long rxTime = 200; 146 long idleTime = 300; 147 long energyUsed = 10; 148 BluetoothActivityEnergyInfo info = 149 new BluetoothActivityEnergyInfo( 150 timestamp, stackState, txTime, rxTime, idleTime, energyUsed); 151 152 try { 153 String infoString = info.toString(); 154 } catch (Exception e) { 155 Assert.fail("Should throw a RuntimeException"); 156 } 157 } 158 } 159