• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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.searchuiservice.cts;
18 
19 import static android.app.search.SearchTargetEvent.ACTION_TAP;
20 import static android.app.search.SearchTargetEvent.Builder;
21 import static android.app.search.SearchTargetEvent.FLAG_IME_SHOWN;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import androidx.annotation.NonNull;
26 
27 import android.app.search.SearchAction;
28 import android.app.search.SearchTargetEvent;
29 import android.os.Parcel;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 import java.util.ArrayList;
36 
37 @RunWith(JUnit4.class)
38 public class SearchTargetEventTest {
39     private final String ID = "id";
40     private final String LOCATION = "location";
41     private final Builder mBuilder = new Builder(ID, ACTION_TAP).setLaunchLocation(LOCATION);
42     private final Builder mBuilderList = new Builder(new ArrayList(), ACTION_TAP);
43 
44     @Test
testBuilder()45     public void testBuilder() {
46         SearchTargetEvent event = mBuilder
47                 .setLaunchLocation(LOCATION).setFlags(FLAG_IME_SHOWN).build();
48         assertEverything(event);
49         mBuilderList.build();
50     }
51 
52     @Test
testParcel()53     public void testParcel() {
54         SearchTargetEvent event = mBuilder
55                 .setLaunchLocation(LOCATION).setFlags(FLAG_IME_SHOWN).build();
56         assertEverything(event);
57         final SearchTargetEvent clone = cloneThroughParcel(event);
58         assertEverything(clone);
59 
60     }
61 
assertEverything(@onNull SearchTargetEvent event)62     private void assertEverything(@NonNull SearchTargetEvent event) {
63         assertThat(event.getTargetId()).isEqualTo(ID);
64         assertThat(event.getAction()).isEqualTo(ACTION_TAP);
65         assertThat(event.getFlags()).isEqualTo(FLAG_IME_SHOWN);
66         assertThat(event.getLaunchLocation()).isEqualTo(LOCATION);
67     }
68 
cloneThroughParcel(@onNull SearchTargetEvent event)69     private SearchTargetEvent cloneThroughParcel(@NonNull SearchTargetEvent event) {
70         final Parcel parcel = Parcel.obtain();
71 
72         try {
73             // Write to parcel
74             parcel.setDataPosition(0);
75             event.writeToParcel(parcel, 0);
76 
77             // Read from parcel
78             parcel.setDataPosition(0);
79             final SearchTargetEvent clone = SearchTargetEvent.CREATOR
80                     .createFromParcel(parcel);
81             assertThat(clone).isNotNull();
82             return clone;
83         } finally {
84             parcel.recycle();
85         }
86     }
87 }
88