1 /*
2  * Copyright 2021 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 org.junit.Assert.assertEquals;
20 
21 import android.os.Parcel;
22 import android.uwb.UwbAddress;
23 
24 import androidx.test.ext.junit.runners.AndroidJUnit4;
25 import androidx.test.filters.SmallTest;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 /**
31  * Test of {@link UwbAddress}.
32  */
33 @SmallTest
34 @RunWith(AndroidJUnit4.class)
35 public class UwbAddressTest {
36 
37     @Test
testFromBytes_Short()38     public void testFromBytes_Short() {
39         runFromBytes(UwbAddress.SHORT_ADDRESS_BYTE_LENGTH);
40     }
41 
42     @Test
testFromBytes_Extended()43     public void testFromBytes_Extended() {
44         runFromBytes(UwbAddress.EXTENDED_ADDRESS_BYTE_LENGTH);
45     }
46 
runFromBytes(int len)47     private void runFromBytes(int len) {
48         byte[] addressBytes = getByteArray(len);
49         UwbAddress address = UwbAddress.fromBytes(addressBytes);
50         assertEquals(address.size(), len);
51         assertEquals(addressBytes, address.toBytes());
52     }
53 
getByteArray(int len)54     private byte[] getByteArray(int len) {
55         byte[] res = new byte[len];
56         for (int i = 0; i < len; i++) {
57             res[i] = (byte) i;
58         }
59         return res;
60     }
61 
62     @Test
testParcel_Short()63     public void testParcel_Short() {
64         runParcel(true);
65     }
66 
67     @Test
testParcel_Extended()68     public void testParcel_Extended() {
69         runParcel(false);
70     }
71 
runParcel(boolean useShortAddress)72     private void runParcel(boolean useShortAddress) {
73         Parcel parcel = Parcel.obtain();
74         UwbAddress address = UwbTestUtils.getUwbAddress(useShortAddress);
75         address.writeToParcel(parcel, 0);
76         parcel.setDataPosition(0);
77         UwbAddress fromParcel = UwbAddress.CREATOR.createFromParcel(parcel);
78         assertEquals(address, fromParcel);
79     }
80 }
81