Add a NearbyScanRequest for requesting scan.
Currently only scan type and worksource are supported in the scan
request. More parameters will be added in the future.
Bug: 189355156
Test: atest NearbyUnitTests
Change-Id: I73d9e48865d33f5b13b29acac30eadc1a9eb9168
diff --git a/nearby/tests/Android.bp b/nearby/tests/Android.bp
new file mode 100644
index 0000000..09fe5ea
--- /dev/null
+++ b/nearby/tests/Android.bp
@@ -0,0 +1,43 @@
+// Copyright (C) 2021 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test {
+ name: "NearbyUnitTests",
+ defaults: ["mts-target-sdk-version-current"],
+ sdk_version: "test_current",
+ min_sdk_version: "31",
+
+ // Include all test java files.
+ srcs: ["src/**/*.java"],
+
+ libs: [
+ "android.test.runner",
+ "android.test.base",
+ ],
+ compile_multilib: "both",
+
+ static_libs: [
+ "androidx.test.rules",
+ "truth-prebuilt",
+ "framework-nearby-pre-jarjar",
+ ],
+ test_suites: [
+ "general-tests",
+ "mts",
+ ],
+}
diff --git a/nearby/tests/AndroidManifest.xml b/nearby/tests/AndroidManifest.xml
new file mode 100644
index 0000000..c756ff2
--- /dev/null
+++ b/nearby/tests/AndroidManifest.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2021 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.nearby.test">
+
+ <uses-permission android:name="android.permission.INTERNET"/>
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+
+ <application>
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.nearby.test"
+ android:label="Nearby Mainline Module Tests" />
+</manifest>
diff --git a/nearby/tests/AndroidTest.xml b/nearby/tests/AndroidTest.xml
new file mode 100644
index 0000000..8b08abb
--- /dev/null
+++ b/nearby/tests/AndroidTest.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2021 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="Runs Nearby Mainline API Tests.">
+ <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+ <option name="test-file-name" value="NearbyUnitTests.apk" />
+ </target_preparer>
+
+ <option name="test-suite-tag" value="apct" />
+ <option name="test-tag" value="NearbyUnitTests" />
+ <option name="config-descriptor:metadata" key="mainline-param"
+ value="com.google.android.nearby.apex" />
+ <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+ <option name="package" value="android.nearby.test" />
+ <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
+ <option name="hidden-api-checks" value="false"/>
+ </test>
+
+ <!-- Only run NearbyUnitTests in MTS if the Nearby Mainline module is installed. -->
+ <object type="module_controller"
+ class="com.android.tradefed.testtype.suite.module.MainlineTestModuleController">
+ <option name="mainline-module-package-name" value="com.google.android.nearby" />
+ </object>
+</configuration>
diff --git a/nearby/tests/src/android/nearby/ScanRequestTest.java b/nearby/tests/src/android/nearby/ScanRequestTest.java
new file mode 100644
index 0000000..12c8edc
--- /dev/null
+++ b/nearby/tests/src/android/nearby/ScanRequestTest.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2021 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.nearby;
+
+import static android.nearby.ScanRequest.SCAN_TYPE_EXPOSURE_NOTIFICATION;
+import static android.nearby.ScanRequest.SCAN_TYPE_FAST_PAIR;
+import static android.nearby.ScanRequest.SCAN_TYPE_NEARBY_PRESENCE;
+import static android.nearby.ScanRequest.SCAN_TYPE_NEARBY_SHARE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.os.Parcel;
+import android.os.WorkSource;
+
+import androidx.test.filters.SmallTest;
+
+import org.junit.Test;
+
+/** Units tests for {@link ScanRequest}. */
+@SmallTest
+public class ScanRequestTest {
+
+ /** Test creating a scan request. */
+ @Test
+ public void testScanRequestBuilder() {
+ final int scanType = SCAN_TYPE_FAST_PAIR;
+ ScanRequest request = new ScanRequest.Builder().setScanType(scanType).build();
+
+ assertThat(request.getScanType()).isEqualTo(scanType);
+ // Work source is null if not set.
+ assertThat(request.getWorkSource()).isNull();
+ }
+
+ /** Verify RuntimeException is thrown when creating scan request with invalid scan type. */
+ @Test(expected = RuntimeException.class)
+ public void testScanRequestBuilder_invalidScanType() {
+ final int invalidScanType = -1;
+ ScanRequest.Builder builder = new ScanRequest.Builder().setScanType(
+ invalidScanType);
+
+ builder.build();
+ }
+
+ /** Verify setting work source in the scan request. */
+ @Test
+ public void testSetWorkSource() {
+ WorkSource workSource = getWorkSource();
+ ScanRequest request = new ScanRequest.Builder()
+ .setScanType(SCAN_TYPE_NEARBY_SHARE)
+ .setWorkSource(workSource)
+ .build();
+
+ assertThat(request.getWorkSource()).isEqualTo(workSource);
+ }
+
+ /** Verify setting work source with null value in the scan request. */
+ @Test
+ public void testSetWorkSource_nullValue() {
+ ScanRequest request = new ScanRequest.Builder()
+ .setScanType(SCAN_TYPE_EXPOSURE_NOTIFICATION)
+ .setWorkSource(null)
+ .build();
+
+ // Null work source is allowed.
+ assertThat(request.getWorkSource()).isNull();
+ }
+
+ /** Verify toString returns expected string. */
+ @Test
+ public void testToString() {
+ WorkSource workSource = getWorkSource();
+ ScanRequest request = new ScanRequest.Builder()
+ .setScanType(SCAN_TYPE_NEARBY_SHARE)
+ .setWorkSource(workSource)
+ .build();
+
+ assertThat(request.toString()).isEqualTo(
+ "Request[scanType=2, workSource=WorkSource{1001 android.nearby.tests}]");
+ }
+
+ /** Verify toString works correctly with null WorkSource. */
+ @Test
+ public void testToString_nullWorkSource() {
+ ScanRequest request = new ScanRequest.Builder().setScanType(
+ SCAN_TYPE_FAST_PAIR).build();
+
+ assertThat(request.toString()).isEqualTo("Request[scanType=1]");
+ }
+
+ /** Verify writing and reading from parcel for scan request. */
+ @Test
+ public void testParceling() {
+ final int scanType = SCAN_TYPE_NEARBY_PRESENCE;
+ WorkSource workSource = getWorkSource();
+ ScanRequest originalRequest = new ScanRequest.Builder()
+ .setScanType(scanType)
+ .setWorkSource(workSource)
+ .build();
+
+ // Write the scan request to parcel, then read from it.
+ ScanRequest request = writeReadFromParcel(originalRequest);
+
+ // Verify the request read from parcel equals to the original request.
+ assertThat(request).isEqualTo(originalRequest);
+ }
+
+ /** Verify parceling with null WorkSource. */
+ @Test
+ public void testParceling_nullWorkSource() {
+ final int scanType = SCAN_TYPE_NEARBY_PRESENCE;
+ ScanRequest originalRequest = new ScanRequest.Builder()
+ .setScanType(scanType).build();
+
+ ScanRequest request = writeReadFromParcel(originalRequest);
+
+ assertThat(request).isEqualTo(originalRequest);
+ }
+
+ private ScanRequest writeReadFromParcel(ScanRequest originalRequest) {
+ Parcel parcel = Parcel.obtain();
+ originalRequest.writeToParcel(parcel, 0);
+ parcel.setDataPosition(0);
+ return ScanRequest.CREATOR.createFromParcel(parcel);
+ }
+
+ private static WorkSource getWorkSource() {
+ final int uid = 1001;
+ final String appName = "android.nearby.tests";
+ return new WorkSource(uid, appName);
+ }
+}