1 /*
2  * Copyright (C) 2024 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.voiceinteraction.cts.unittests;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.testng.Assert.assertThrows;
22 
23 import android.os.Parcel;
24 import android.service.voice.VisualQueryDetectedResult;
25 
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 @RunWith(AndroidJUnit4.class)
32 public class VisualQueryDetectedResultTest {
33 
34     private static final byte[] TEST_BYTES = new byte[] { 0, 1, 2, 3 };
35     private static final String TEST_QUERY = "What time is it?";
36 
37     @Test
testVisualQueryDetectedResult_setInvalidSpeakerId()38     public void testVisualQueryDetectedResult_setInvalidSpeakerId() throws Exception {
39         assertThrows(IllegalArgumentException.class,
40                 () -> new VisualQueryDetectedResult.Builder().setSpeakerId(-1).build());
41         assertThrows(IllegalArgumentException.class,
42                 () -> new VisualQueryDetectedResult.Builder().setSpeakerId(
43                         VisualQueryDetectedResult.getMaxSpeakerId() + 1).build());
44     }
45 
46     @Test
testVisualQueryDetectedResultBuilder()47     public void testVisualQueryDetectedResultBuilder() throws Exception {
48         final VisualQueryDetectedResult visualQueryDetectedResult =
49                 buildVisualQueryDetectedResult(
50                         /* partialQuery= */ TEST_QUERY,
51                         /* speakerId= */ 1,
52                         /* AccessibilityDetectionData= */ TEST_BYTES);
53         assertVisualQueryDetectedResult(visualQueryDetectedResult);
54 
55     }
56 
57     @Test
testHotwordDetectedResultParcelizeDeparcelize()58     public void testHotwordDetectedResultParcelizeDeparcelize() throws Exception {
59         final VisualQueryDetectedResult visualQueryDetectedResult =
60                 buildVisualQueryDetectedResult(
61                         /* partialQuery= */ TEST_QUERY,
62                         /* speakerId= */ 1,
63                         /* AccessibilityDetectionData= */ TEST_BYTES);
64         final Parcel p = Parcel.obtain();
65         visualQueryDetectedResult.writeToParcel(p, 0);
66         p.setDataPosition(0);
67 
68         final VisualQueryDetectedResult targetVisualQueryDetectedResult =
69                 VisualQueryDetectedResult.CREATOR.createFromParcel(p);
70         p.recycle();
71         assertVisualQueryDetectedResult(targetVisualQueryDetectedResult);
72     }
73 
buildVisualQueryDetectedResult( String partialQuery, int speakerId, byte[] accessibilityDetectionData)74     private VisualQueryDetectedResult buildVisualQueryDetectedResult(
75             String partialQuery,
76             int speakerId,
77             byte[] accessibilityDetectionData) {
78         return new VisualQueryDetectedResult.Builder()
79                 .setPartialQuery(TEST_QUERY)
80                 .setSpeakerId(speakerId)
81                 .setAccessibilityDetectionData(accessibilityDetectionData)
82                 .build();
83     }
84 
assertVisualQueryDetectedResult( VisualQueryDetectedResult visualQueryDetectedResult)85     private void assertVisualQueryDetectedResult(
86             VisualQueryDetectedResult visualQueryDetectedResult) {
87         assertThat(visualQueryDetectedResult.getPartialQuery()).isEqualTo(TEST_QUERY);
88         assertThat(visualQueryDetectedResult.getSpeakerId()).isEqualTo(1);
89         assertThat(visualQueryDetectedResult.getAccessibilityDetectionData()).isEqualTo(TEST_BYTES);
90     }
91 }
92