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 com.android.server.uwb;
18 
19 import static android.uwb.RangingMeasurement.RANGING_STATUS_SUCCESS;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.uwb.AngleMeasurement;
27 import android.uwb.AngleOfArrivalMeasurement;
28 import android.uwb.DistanceMeasurement;
29 import android.uwb.RangingMeasurement;
30 import android.uwb.UwbAddress;
31 
32 import com.android.server.uwb.correction.TestHelpers;
33 import com.android.server.uwb.correction.UwbFilterEngine;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 
40 public class UwbControleeTest {
41     public static final UwbAddress UWB_ADDRESS = UwbAddress.fromBytes(new byte[] {1, 2});
42     UwbControlee mControlee;
43     @Mock
44     UwbInjector mUwbInjector;
45 
46     @Before
setUp()47     public void setUp() {
48         UwbFilterEngine.Builder builder = new UwbFilterEngine.Builder();
49         UwbFilterEngine engine = builder.build();
50         mUwbInjector = mock(UwbInjector.class);
51         when(mUwbInjector.getElapsedSinceBootMillis()).thenReturn(10L);
52         mControlee = new UwbControlee(
53                 UWB_ADDRESS,
54                 engine,
55                 mUwbInjector);
56     }
57 
58     @After
shutdown()59     public void shutdown() throws Exception {
60         mControlee.close();
61     }
62 
63     @Test
testGetUwbAddress()64     public void testGetUwbAddress() {
65         assertThat(mControlee.getUwbAddress()).isEqualTo(UWB_ADDRESS);
66     }
67 
68     @Test
testFilterMeasurement()69     public void testFilterMeasurement() {
70         final double testRads = 0.1;
71         final double testDist = 2;
72         AngleMeasurement am = new AngleMeasurement(testRads, 0.0, 1.0);
73         AngleOfArrivalMeasurement aoam = new AngleOfArrivalMeasurement.Builder(am).build();
74         DistanceMeasurement dm = new DistanceMeasurement.Builder()
75                 .setMeters(testDist)
76                 .setErrorMeters(0.0)
77                 .setConfidenceLevel(1.0)
78                 .build();
79 
80         RangingMeasurement.Builder rm = new RangingMeasurement.Builder()
81                 .setDistanceMeasurement(dm)
82                 .setAngleOfArrivalMeasurement(aoam)
83                 .setStatus(RANGING_STATUS_SUCCESS)
84                 .setRemoteDeviceAddress(UWB_ADDRESS)
85                 .setElapsedRealtimeNanos(100);
86 
87         // Filtering a single measurement value should just yield that same value.
88         mControlee.filterMeasurement(rm);
89 
90         RangingMeasurement newMeasure = rm.build();
91         TestHelpers.assertClose(newMeasure.getAngleOfArrivalMeasurement().getAzimuth()
92                 .getRadians(), testRads);
93     }
94 
95     @Test
testNonAoaMeasurement()96     public void testNonAoaMeasurement() {
97         final double testDist = 2;
98         DistanceMeasurement dm = new DistanceMeasurement.Builder()
99                 .setMeters(testDist)
100                 .setErrorMeters(0.0)
101                 .setConfidenceLevel(1.0)
102                 .build();
103 
104         RangingMeasurement.Builder rm = new RangingMeasurement.Builder()
105                 .setDistanceMeasurement(dm)
106                 .setStatus(RANGING_STATUS_SUCCESS)
107                 .setRemoteDeviceAddress(UWB_ADDRESS)
108                 .setElapsedRealtimeNanos(100);
109 
110         // Filtering a single measurement value should just yield that same value.
111         mControlee.filterMeasurement(rm);
112 
113         RangingMeasurement newMeasure = rm.build();
114         assertThat(newMeasure.getAngleOfArrivalMeasurement()).isNull();
115     }
116 }
117