diff options
author | 2020-10-22 13:59:42 -0700 | |
---|---|---|
committer | 2020-11-03 09:15:05 -0800 | |
commit | 1a702459c5dca9a70a965dbd24c17d7d336421f1 (patch) | |
tree | d7e07ad06229e77823b3d02aa1c05045d6bf2ece | |
parent | 3c99301acbd2be543cf32e1bbf29d8a00dd0fdc8 (diff) |
Add UWB Tests to verify Builders and Parcels
Bug: 170323306
Test: atest UwbManagerTests
Test: Runnng `atest` in frameworks/base/core/java/android/uwb runs
UwbManagerTests
Change-Id: I4d46baf7886376cd0255fb9ae78fc12bc94f10a6
18 files changed, 996 insertions, 0 deletions
diff --git a/core/java/android/uwb/AngleMeasurement.java b/core/java/android/uwb/AngleMeasurement.java index cf32b922078e..33bc121d8555 100644 --- a/core/java/android/uwb/AngleMeasurement.java +++ b/core/java/android/uwb/AngleMeasurement.java @@ -20,6 +20,8 @@ import android.annotation.FloatRange; import android.os.Parcel; import android.os.Parcelable; +import java.util.Objects; + /** * Angle measurement * @@ -75,6 +77,32 @@ public final class AngleMeasurement implements Parcelable { return mConfidenceLevel; } + /** + * @hide + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof AngleMeasurement) { + AngleMeasurement other = (AngleMeasurement) obj; + return mRadians == other.getRadians() + && mErrorRadians == other.getErrorRadians() + && mConfidenceLevel == other.getConfidenceLevel(); + } + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mRadians, mErrorRadians, mConfidenceLevel); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/AngleOfArrivalMeasurement.java b/core/java/android/uwb/AngleOfArrivalMeasurement.java index 646bd42cef2a..cd5af691ed51 100644 --- a/core/java/android/uwb/AngleOfArrivalMeasurement.java +++ b/core/java/android/uwb/AngleOfArrivalMeasurement.java @@ -21,6 +21,8 @@ import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; +import java.util.Objects; + /** * Represents an angle of arrival measurement between two devices using Ultra Wideband * @@ -72,6 +74,31 @@ public final class AngleOfArrivalMeasurement implements Parcelable { return mAltitudeAngleMeasurement; } + /** + * @hide + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof AngleOfArrivalMeasurement) { + AngleOfArrivalMeasurement other = (AngleOfArrivalMeasurement) obj; + return mAzimuthAngleMeasurement.equals(other.getAzimuth()) + && mAltitudeAngleMeasurement.equals(other.getAltitude()); + } + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mAzimuthAngleMeasurement, mAltitudeAngleMeasurement); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/DistanceMeasurement.java b/core/java/android/uwb/DistanceMeasurement.java index 9561be449a19..c959840c51ba 100644 --- a/core/java/android/uwb/DistanceMeasurement.java +++ b/core/java/android/uwb/DistanceMeasurement.java @@ -17,9 +17,12 @@ package android.uwb; import android.annotation.FloatRange; +import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; +import java.util.Objects; + /** * A data point for the distance measurement * @@ -71,6 +74,32 @@ public final class DistanceMeasurement implements Parcelable { return mConfidenceLevel; } + /** + * @hide + */ + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof DistanceMeasurement) { + DistanceMeasurement other = (DistanceMeasurement) obj; + return mMeters == other.getMeters() + && mErrorMeters == other.getErrorMeters() + && mConfidenceLevel == other.getConfidenceLevel(); + } + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mMeters, mErrorMeters, mConfidenceLevel); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/RangingMeasurement.java b/core/java/android/uwb/RangingMeasurement.java index d4d7fb23959a..f1c316289653 100644 --- a/core/java/android/uwb/RangingMeasurement.java +++ b/core/java/android/uwb/RangingMeasurement.java @@ -26,6 +26,7 @@ import android.os.SystemClock; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.Objects; /** * Representation of a ranging measurement between the local device and a remote device @@ -129,6 +130,35 @@ public final class RangingMeasurement implements Parcelable { return mAngleOfArrivalMeasurement; } + /** + * @hide + */ + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof RangingMeasurement) { + RangingMeasurement other = (RangingMeasurement) obj; + return mRemoteDeviceAddress.equals(other.getRemoteDeviceAddress()) + && mStatus == other.getStatus() + && mElapsedRealtimeNanos == other.getElapsedRealtimeNanos() + && mDistanceMeasurement.equals(other.getDistance()) + && mAngleOfArrivalMeasurement.equals(other.getAngleOfArrival()); + } + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mRemoteDeviceAddress, mStatus, mElapsedRealtimeNanos, + mDistanceMeasurement, mAngleOfArrivalMeasurement); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/RangingParams.java b/core/java/android/uwb/RangingParams.java index c5d4807859b5..f23d9ed0dd3a 100644 --- a/core/java/android/uwb/RangingParams.java +++ b/core/java/android/uwb/RangingParams.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; /** @@ -201,6 +202,43 @@ public final class RangingParams implements Parcelable { return new PersistableBundle(mSpecificationParameters); } + /** + * @hide + */ + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof RangingParams) { + RangingParams other = (RangingParams) obj; + + return mIsInitiator == other.mIsInitiator + && mIsController == other.mIsController + && mSamplePeriod.equals(other.mSamplePeriod) + && mLocalDeviceAddress.equals(other.mLocalDeviceAddress) + && mRemoteDeviceAddresses.equals(other.mRemoteDeviceAddresses) + && mChannelNumber == other.mChannelNumber + && mTransmitPreambleCodeIndex == other.mTransmitPreambleCodeIndex + && mReceivePreambleCodeIndex == other.mReceivePreambleCodeIndex + && mStsPhyPacketType == other.mStsPhyPacketType + && mSpecificationParameters.size() == other.mSpecificationParameters.size() + && mSpecificationParameters.kindofEquals(other.mSpecificationParameters); + } + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mIsInitiator, mIsController, mSamplePeriod, mLocalDeviceAddress, + mRemoteDeviceAddresses, mChannelNumber, mTransmitPreambleCodeIndex, + mReceivePreambleCodeIndex, mStsPhyPacketType, mSpecificationParameters); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/RangingReport.java b/core/java/android/uwb/RangingReport.java index 1d340b4b0358..45180bfa6981 100644 --- a/core/java/android/uwb/RangingReport.java +++ b/core/java/android/uwb/RangingReport.java @@ -17,11 +17,13 @@ package android.uwb; import android.annotation.NonNull; +import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * This class contains the UWB ranging data @@ -50,6 +52,31 @@ public final class RangingReport implements Parcelable { return mRangingMeasurements; } + /** + * @hide + */ + @Override + public boolean equals(@Nullable Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof RangingReport) { + RangingReport other = (RangingReport) obj; + return mRangingMeasurements.equals(other.getMeasurements()); + } + + return false; + } + + /** + * @hide + */ + @Override + public int hashCode() { + return Objects.hash(mRangingMeasurements); + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/uwb/TEST_MAPPING b/core/java/android/uwb/TEST_MAPPING new file mode 100644 index 000000000000..9e50bd64d089 --- /dev/null +++ b/core/java/android/uwb/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "presubmit": [ + { + "name": "UwbManagerTests" + } + ] +}
\ No newline at end of file diff --git a/core/tests/uwbtests/Android.bp b/core/tests/uwbtests/Android.bp new file mode 100644 index 000000000000..c41c346b131a --- /dev/null +++ b/core/tests/uwbtests/Android.bp @@ -0,0 +1,28 @@ +// Copyright 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +android_test { + name: "UwbManagerTests", + static_libs: [ + "androidx.test.ext.junit", + "androidx.test.rules", + ], + libs: [ + "android.test.runner", + ], + srcs: ["src/**/*.java"], + platform_apis: true, + certificate: "platform", + test_suites: ["device-tests"], +} diff --git a/core/tests/uwbtests/AndroidManifest.xml b/core/tests/uwbtests/AndroidManifest.xml new file mode 100644 index 000000000000..dc991ff636d2 --- /dev/null +++ b/core/tests/uwbtests/AndroidManifest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright 2020 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android.uwb"> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + + <!-- This is a self-instrumenting test package. --> + <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="android.uwb" + android:label="UWB Manager Tests"> + </instrumentation> + +</manifest> + diff --git a/core/tests/uwbtests/AndroidTest.xml b/core/tests/uwbtests/AndroidTest.xml new file mode 100644 index 000000000000..ff4b668cc625 --- /dev/null +++ b/core/tests/uwbtests/AndroidTest.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright 2020 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration description="Config for UWB Manager test cases"> + <option name="test-suite-tag" value="apct"/> + <option name="test-suite-tag" value="apct-instrumentation"/> + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="test-file-name" value="UwbManagerTests.apk" /> + </target_preparer> + + <option name="test-suite-tag" value="apct"/> + <option name="test-tag" value="UwbManagerTests"/> + + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="android.uwb" /> + <option name="hidden-api-checks" value="false"/> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner"/> + </test> +</configuration> diff --git a/core/tests/uwbtests/src/android/uwb/AngleMeasurementTest.java b/core/tests/uwbtests/src/android/uwb/AngleMeasurementTest.java new file mode 100644 index 000000000000..7769c28202f2 --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/AngleMeasurementTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test of {@link AngleMeasurement}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class AngleMeasurementTest { + private static final double EPSILON = 0.00000000001; + + @Test + public void testBuilder() { + double radians = 0.1234; + double errorRadians = 0.5678; + double confidence = 0.5; + + AngleMeasurement.Builder builder = new AngleMeasurement.Builder(); + tryBuild(builder, false); + + builder.setRadians(radians); + tryBuild(builder, false); + + builder.setErrorRadians(errorRadians); + tryBuild(builder, false); + + builder.setConfidenceLevel(confidence); + AngleMeasurement measurement = tryBuild(builder, true); + + assertEquals(measurement.getRadians(), radians, 0); + assertEquals(measurement.getErrorRadians(), errorRadians, 0); + assertEquals(measurement.getConfidenceLevel(), confidence, 0); + } + + private AngleMeasurement tryBuild(AngleMeasurement.Builder builder, boolean expectSuccess) { + AngleMeasurement measurement = null; + try { + measurement = builder.build(); + if (!expectSuccess) { + fail("Expected AngleMeasurement.Builder.build() to fail, but it succeeded"); + } + } catch (IllegalStateException e) { + if (expectSuccess) { + fail("Expected AngleMeasurement.Builder.build() to succeed, but it failed"); + } + } + return measurement; + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + AngleMeasurement measurement = UwbTestUtils.getAngleMeasurement(); + measurement.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + AngleMeasurement fromParcel = AngleMeasurement.CREATOR.createFromParcel(parcel); + assertEquals(measurement, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/AngleOfArrivalMeasurementTest.java b/core/tests/uwbtests/src/android/uwb/AngleOfArrivalMeasurementTest.java new file mode 100644 index 000000000000..077b08f41b59 --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/AngleOfArrivalMeasurementTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test of {@link AngleOfArrivalMeasurement}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class AngleOfArrivalMeasurementTest { + + @Test + public void testBuilder() { + AngleMeasurement azimuth = UwbTestUtils.getAngleMeasurement(); + AngleMeasurement altitude = UwbTestUtils.getAngleMeasurement(); + + AngleOfArrivalMeasurement.Builder builder = new AngleOfArrivalMeasurement.Builder(); + tryBuild(builder, false); + + builder.setAltitudeAngleMeasurement(altitude); + tryBuild(builder, false); + + builder.setAzimuthAngleMeasurement(azimuth); + AngleOfArrivalMeasurement measurement = tryBuild(builder, true); + + assertEquals(azimuth, measurement.getAzimuth()); + assertEquals(altitude, measurement.getAltitude()); + } + + private AngleMeasurement getAngleMeasurement(double radian, double error, double confidence) { + return new AngleMeasurement.Builder() + .setRadians(radian) + .setErrorRadians(error) + .setConfidenceLevel(confidence) + .build(); + } + + private AngleOfArrivalMeasurement tryBuild(AngleOfArrivalMeasurement.Builder builder, + boolean expectSuccess) { + AngleOfArrivalMeasurement measurement = null; + try { + measurement = builder.build(); + if (!expectSuccess) { + fail("Expected AngleOfArrivalMeasurement.Builder.build() to fail"); + } + } catch (IllegalStateException e) { + if (expectSuccess) { + fail("Expected AngleOfArrivalMeasurement.Builder.build() to succeed"); + } + } + return measurement; + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + AngleOfArrivalMeasurement measurement = UwbTestUtils.getAngleOfArrivalMeasurement(); + measurement.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + AngleOfArrivalMeasurement fromParcel = + AngleOfArrivalMeasurement.CREATOR.createFromParcel(parcel); + assertEquals(measurement, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/DistanceMeasurementTest.java b/core/tests/uwbtests/src/android/uwb/DistanceMeasurementTest.java new file mode 100644 index 000000000000..439c884723be --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/DistanceMeasurementTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test of {@link DistanceMeasurement}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class DistanceMeasurementTest { + private static final double EPSILON = 0.00000000001; + + @Test + public void testBuilder() { + double meters = 0.12; + double error = 0.54; + double confidence = 0.99; + + DistanceMeasurement.Builder builder = new DistanceMeasurement.Builder(); + tryBuild(builder, false); + + builder.setMeters(meters); + tryBuild(builder, false); + + builder.setErrorMeters(error); + tryBuild(builder, false); + + builder.setConfidenceLevel(confidence); + DistanceMeasurement measurement = tryBuild(builder, true); + + assertEquals(meters, measurement.getMeters(), 0); + assertEquals(error, measurement.getErrorMeters(), 0); + assertEquals(confidence, measurement.getConfidenceLevel(), 0); + } + + private DistanceMeasurement tryBuild(DistanceMeasurement.Builder builder, + boolean expectSuccess) { + DistanceMeasurement measurement = null; + try { + measurement = builder.build(); + if (!expectSuccess) { + fail("Expected DistanceMeasurement.Builder.build() to fail"); + } + } catch (IllegalStateException e) { + if (expectSuccess) { + fail("Expected DistanceMeasurement.Builder.build() to succeed"); + } + } + return measurement; + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + DistanceMeasurement measurement = UwbTestUtils.getDistanceMeasurement(); + measurement.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + DistanceMeasurement fromParcel = + DistanceMeasurement.CREATOR.createFromParcel(parcel); + assertEquals(measurement, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/RangingMeasurementTest.java b/core/tests/uwbtests/src/android/uwb/RangingMeasurementTest.java new file mode 100644 index 000000000000..a7559d86221e --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/RangingMeasurementTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.os.Parcel; +import android.os.SystemClock; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test of {@link RangingMeasurement}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class RangingMeasurementTest { + + @Test + public void testBuilder() { + int status = RangingMeasurement.RANGING_STATUS_SUCCESS; + UwbAddress address = UwbTestUtils.getUwbAddress(false); + long time = SystemClock.elapsedRealtimeNanos(); + AngleOfArrivalMeasurement angleMeasurement = UwbTestUtils.getAngleOfArrivalMeasurement(); + DistanceMeasurement distanceMeasurement = UwbTestUtils.getDistanceMeasurement(); + + RangingMeasurement.Builder builder = new RangingMeasurement.Builder(); + + builder.setStatus(status); + tryBuild(builder, false); + + builder.setElapsedRealtimeNanos(time); + tryBuild(builder, false); + + builder.setAngleOfArrivalMeasurement(angleMeasurement); + tryBuild(builder, false); + + builder.setDistanceMeasurement(distanceMeasurement); + tryBuild(builder, false); + + builder.setRemoteDeviceAddress(address); + RangingMeasurement measurement = tryBuild(builder, true); + + assertEquals(status, measurement.getStatus()); + assertEquals(address, measurement.getRemoteDeviceAddress()); + assertEquals(time, measurement.getElapsedRealtimeNanos()); + assertEquals(angleMeasurement, measurement.getAngleOfArrival()); + assertEquals(distanceMeasurement, measurement.getDistance()); + } + + private RangingMeasurement tryBuild(RangingMeasurement.Builder builder, + boolean expectSuccess) { + RangingMeasurement measurement = null; + try { + measurement = builder.build(); + if (!expectSuccess) { + fail("Expected RangingMeasurement.Builder.build() to fail"); + } + } catch (IllegalStateException e) { + if (expectSuccess) { + fail("Expected DistanceMeasurement.Builder.build() to succeed"); + } + } + return measurement; + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + RangingMeasurement measurement = UwbTestUtils.getRangingMeasurement(); + measurement.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + RangingMeasurement fromParcel = RangingMeasurement.CREATOR.createFromParcel(parcel); + assertEquals(measurement, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/RangingParamsTest.java b/core/tests/uwbtests/src/android/uwb/RangingParamsTest.java new file mode 100644 index 000000000000..c1fa5a5f6080 --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/RangingParamsTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.os.Parcel; +import android.os.PersistableBundle; +import android.util.Log; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.time.Duration; + +/** + * Test of {@link RangingParams}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class RangingParamsTest { + + @Test + public void testParams_Build() { + UwbAddress local = UwbAddress.fromBytes(new byte[] {(byte) 0xA0, (byte) 0x57}); + UwbAddress remote = UwbAddress.fromBytes(new byte[] {(byte) 0x4D, (byte) 0x8C}); + int channel = 9; + int rxPreamble = 16; + int txPreamble = 21; + boolean isController = true; + boolean isInitiator = false; + @RangingParams.StsPhyPacketType int stsPhyType = RangingParams.STS_PHY_PACKET_TYPE_SP2; + Duration samplePeriod = Duration.ofSeconds(1, 234); + PersistableBundle specParams = new PersistableBundle(); + specParams.putString("protocol", "some_protocol"); + + RangingParams params = new RangingParams.Builder() + .setChannelNumber(channel) + .setReceivePreambleCodeIndex(rxPreamble) + .setTransmitPreambleCodeIndex(txPreamble) + .setLocalDeviceAddress(local) + .addRemoteDeviceAddress(remote) + .setIsController(isController) + .setIsInitiator(isInitiator) + .setSamplePeriod(samplePeriod) + .setStsPhPacketType(stsPhyType) + .setSpecificationParameters(specParams) + .build(); + + assertEquals(params.getLocalDeviceAddress(), local); + assertEquals(params.getRemoteDeviceAddresses().size(), 1); + assertEquals(params.getRemoteDeviceAddresses().get(0), remote); + assertEquals(params.getChannelNumber(), channel); + assertEquals(params.isController(), isController); + assertEquals(params.isInitiator(), isInitiator); + assertEquals(params.getRxPreambleIndex(), rxPreamble); + assertEquals(params.getTxPreambleIndex(), txPreamble); + assertEquals(params.getStsPhyPacketType(), stsPhyType); + assertEquals(params.getSamplingPeriod(), samplePeriod); + assertTrue(params.getSpecificationParameters().kindofEquals(specParams)); + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + RangingParams params = new RangingParams.Builder() + .setChannelNumber(9) + .setReceivePreambleCodeIndex(16) + .setTransmitPreambleCodeIndex(21) + .setLocalDeviceAddress(UwbTestUtils.getUwbAddress(false)) + .addRemoteDeviceAddress(UwbTestUtils.getUwbAddress(true)) + .setIsController(false) + .setIsInitiator(true) + .setSamplePeriod(Duration.ofSeconds(2)) + .setStsPhPacketType(RangingParams.STS_PHY_PACKET_TYPE_SP1) + .build(); + params.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + RangingParams fromParcel = RangingParams.CREATOR.createFromParcel(parcel); + Log.w("bstack", "original: " + params.toString()); + Log.w("bstack", "parcel: " + fromParcel.toString()); + assertEquals(params, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/RangingReportTest.java b/core/tests/uwbtests/src/android/uwb/RangingReportTest.java new file mode 100644 index 000000000000..64c48ba4b6f4 --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/RangingReportTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.List; + +/** + * Test of {@link RangingReport}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class RangingReportTest { + + @Test + public void testBuilder() { + List<RangingMeasurement> measurements = UwbTestUtils.getRangingMeasurements(5); + + RangingReport.Builder builder = new RangingReport.Builder(); + builder.addMeasurements(measurements); + RangingReport report = tryBuild(builder, true); + verifyMeasurementsEqual(measurements, report.getMeasurements()); + + + builder = new RangingReport.Builder(); + for (RangingMeasurement measurement : measurements) { + builder.addMeasurement(measurement); + } + report = tryBuild(builder, true); + verifyMeasurementsEqual(measurements, report.getMeasurements()); + } + + private void verifyMeasurementsEqual(List<RangingMeasurement> expected, + List<RangingMeasurement> actual) { + assertEquals(expected.size(), actual.size()); + for (int i = 0; i < expected.size(); i++) { + assertEquals(expected.get(i), actual.get(i)); + } + } + + private RangingReport tryBuild(RangingReport.Builder builder, + boolean expectSuccess) { + RangingReport report = null; + try { + report = builder.build(); + if (!expectSuccess) { + fail("Expected RangingReport.Builder.build() to fail"); + } + } catch (IllegalStateException e) { + if (expectSuccess) { + fail("Expected RangingReport.Builder.build() to succeed"); + } + } + return report; + } + + @Test + public void testParcel() { + Parcel parcel = Parcel.obtain(); + RangingReport report = UwbTestUtils.getRangingReports(5); + report.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + RangingReport fromParcel = RangingReport.CREATOR.createFromParcel(parcel); + assertEquals(report, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/UwbAddressTest.java b/core/tests/uwbtests/src/android/uwb/UwbAddressTest.java new file mode 100644 index 000000000000..ccc88a9a5399 --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/UwbAddressTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import static org.junit.Assert.assertEquals; + +import android.os.Parcel; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test of {@link UwbAddress}. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class UwbAddressTest { + + @Test + public void testFromBytes_Short() { + runFromBytes(UwbAddress.SHORT_ADDRESS_BYTE_LENGTH); + } + + @Test + public void testFromBytes_Extended() { + runFromBytes(UwbAddress.EXTENDED_ADDRESS_BYTE_LENGTH); + } + + private void runFromBytes(int len) { + byte[] addressBytes = getByteArray(len); + UwbAddress address = UwbAddress.fromBytes(addressBytes); + assertEquals(address.size(), len); + assertEquals(addressBytes, address.toBytes()); + } + + private byte[] getByteArray(int len) { + byte[] res = new byte[len]; + for (int i = 0; i < len; i++) { + res[i] = (byte) i; + } + return res; + } + + @Test + public void testParcel_Short() { + runParcel(true); + } + + @Test + public void testParcel_Extended() { + runParcel(false); + } + + private void runParcel(boolean useShortAddress) { + Parcel parcel = Parcel.obtain(); + UwbAddress address = UwbTestUtils.getUwbAddress(useShortAddress); + address.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + UwbAddress fromParcel = UwbAddress.CREATOR.createFromParcel(parcel); + assertEquals(address, fromParcel); + } +} diff --git a/core/tests/uwbtests/src/android/uwb/UwbTestUtils.java b/core/tests/uwbtests/src/android/uwb/UwbTestUtils.java new file mode 100644 index 000000000000..62e0b629539b --- /dev/null +++ b/core/tests/uwbtests/src/android/uwb/UwbTestUtils.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.uwb; + +import android.os.SystemClock; + +import java.util.ArrayList; +import java.util.List; + +public class UwbTestUtils { + private UwbTestUtils() {} + + public static AngleMeasurement getAngleMeasurement() { + return new AngleMeasurement.Builder() + .setRadians(getDoubleInRange(-Math.PI, Math.PI)) + .setErrorRadians(getDoubleInRange(0, Math.PI)) + .setConfidenceLevel(getDoubleInRange(0, 1)) + .build(); + } + + public static AngleOfArrivalMeasurement getAngleOfArrivalMeasurement() { + return new AngleOfArrivalMeasurement.Builder() + .setAltitudeAngleMeasurement(getAngleMeasurement()) + .setAzimuthAngleMeasurement(getAngleMeasurement()) + .build(); + } + + public static DistanceMeasurement getDistanceMeasurement() { + return new DistanceMeasurement.Builder() + .setMeters(getDoubleInRange(0, 100)) + .setErrorMeters(getDoubleInRange(0, 10)) + .setConfidenceLevel(getDoubleInRange(0, 1)) + .build(); + } + + public static RangingMeasurement getRangingMeasurement() { + return getRangingMeasurement(getUwbAddress(false)); + } + + public static RangingMeasurement getRangingMeasurement(UwbAddress address) { + return new RangingMeasurement.Builder() + .setDistanceMeasurement(getDistanceMeasurement()) + .setAngleOfArrivalMeasurement(getAngleOfArrivalMeasurement()) + .setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()) + .setRemoteDeviceAddress(address != null ? address : getUwbAddress(false)) + .setStatus(RangingMeasurement.RANGING_STATUS_SUCCESS) + .build(); + } + + public static List<RangingMeasurement> getRangingMeasurements(int num) { + List<RangingMeasurement> result = new ArrayList<>(); + for (int i = 0; i < num; i++) { + result.add(getRangingMeasurement()); + } + return result; + } + + public static RangingReport getRangingReports(int numMeasurements) { + RangingReport.Builder builder = new RangingReport.Builder(); + for (int i = 0; i < numMeasurements; i++) { + builder.addMeasurement(getRangingMeasurement()); + } + return builder.build(); + } + + private static double getDoubleInRange(double min, double max) { + return min + (max - min) * Math.random(); + } + + public static UwbAddress getUwbAddress(boolean isShortAddress) { + byte[] addressBytes = new byte[isShortAddress ? UwbAddress.SHORT_ADDRESS_BYTE_LENGTH : + UwbAddress.EXTENDED_ADDRESS_BYTE_LENGTH]; + for (int i = 0; i < addressBytes.length; i++) { + addressBytes[i] = (byte) getDoubleInRange(1, 255); + } + return UwbAddress.fromBytes(addressBytes); + } +} |