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 android.uwb.cts; 18 19 import static android.uwb.UwbManager.AdapterStateCallback.STATE_ENABLED_INACTIVE; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertThrows; 23 import static org.junit.Assert.fail; 24 25 import android.os.Parcel; 26 import android.uwb.UwbActivityEnergyInfo; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.filters.SmallTest; 30 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Test of {@link UwbActivityEnergyInfo}. 37 */ 38 @SmallTest 39 @RunWith(AndroidJUnit4.class) 40 public class UwbActivityEnergyInfoTest { 41 private static final long TIMESTAMP_MS = 10000; 42 private static final int STACK_STATE = STATE_ENABLED_INACTIVE; 43 private static final long TX_DURATION_MS = 10; 44 private static final long RX_DURATION_MS = 20; 45 private static final long IDLE_DURATION_MS = 300; 46 private static final long WAKE_COUNT = 50; 47 48 @Test testBuilder()49 public void testBuilder() { 50 UwbActivityEnergyInfo.Builder builder = new UwbActivityEnergyInfo.Builder(); 51 tryBuild(builder, false); 52 53 builder.setTimeSinceBootMillis(TIMESTAMP_MS); 54 tryBuild(builder, false); 55 56 builder.setStackState(STACK_STATE); 57 tryBuild(builder, false); 58 59 builder.setControllerTxDurationMillis(TX_DURATION_MS); 60 tryBuild(builder, false); 61 62 builder.setControllerRxDurationMillis(RX_DURATION_MS); 63 tryBuild(builder, false); 64 65 builder.setControllerIdleDurationMillis(IDLE_DURATION_MS); 66 tryBuild(builder, false); 67 68 builder.setControllerWakeCount(WAKE_COUNT); 69 UwbActivityEnergyInfo info = tryBuild(builder, true); 70 71 assertEquals(TIMESTAMP_MS, info.getTimeSinceBootMillis()); 72 assertEquals(STACK_STATE, info.getStackState()); 73 assertEquals(TX_DURATION_MS, info.getControllerTxDurationMillis()); 74 assertEquals(RX_DURATION_MS, info.getControllerRxDurationMillis()); 75 assertEquals(IDLE_DURATION_MS, info.getControllerIdleDurationMillis()); 76 assertEquals(WAKE_COUNT, info.getControllerWakeCount()); 77 } 78 tryBuild(UwbActivityEnergyInfo.Builder builder, boolean expectSuccess)79 private UwbActivityEnergyInfo tryBuild(UwbActivityEnergyInfo.Builder builder, 80 boolean expectSuccess) { 81 UwbActivityEnergyInfo info = null; 82 try { 83 info = builder.build(); 84 if (!expectSuccess) { 85 fail("Expected UwbActivityEnergyInfo.Builder.build() to fail"); 86 } 87 } catch (IllegalStateException e) { 88 if (expectSuccess) { 89 fail("Expected UwbActivityEnergyInfo.Builder.build() to succeed"); 90 } 91 } 92 return info; 93 } 94 95 @Test testInvalidParams()96 public void testInvalidParams() throws Exception { 97 UwbActivityEnergyInfo.Builder builder = new UwbActivityEnergyInfo.Builder(); 98 99 assertThrows(IllegalArgumentException.class, 100 () -> builder.setTimeSinceBootMillis(-1)); 101 assertThrows(IllegalArgumentException.class, 102 () -> builder.setStackState(-1)); 103 assertThrows(IllegalArgumentException.class, 104 () -> builder.setControllerTxDurationMillis(-1)); 105 assertThrows(IllegalArgumentException.class, 106 () -> builder.setControllerRxDurationMillis(-1)); 107 assertThrows(IllegalArgumentException.class, 108 () -> builder.setControllerIdleDurationMillis(-1)); 109 assertThrows(IllegalArgumentException.class, 110 () -> builder.setControllerWakeCount(-1)); 111 } 112 113 @Test testParcel()114 public void testParcel() throws Exception { 115 Parcel parcel = Parcel.obtain(); 116 UwbActivityEnergyInfo info = UwbTestUtils.getUwbActivityEnergyInfo(); 117 info.writeToParcel(parcel, 0); 118 parcel.setDataPosition(0); 119 120 UwbActivityEnergyInfo infoFromParcel = 121 UwbActivityEnergyInfo.CREATOR.createFromParcel(parcel); 122 assertEquals(info, infoFromParcel); 123 } 124 125 @Test testToStringThrowsNoExceptions()126 public void testToStringThrowsNoExceptions() throws Exception { 127 UwbActivityEnergyInfo info = UwbTestUtils.getUwbActivityEnergyInfo(); 128 try { 129 String infoString = info.toString(); 130 } catch (Exception e) { 131 throw new AssertionError("Should throw a RuntimeException", e); 132 } 133 } 134 } 135