1 /*
2  * Copyright (C) 2015 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.view.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 
22 import android.app.Instrumentation;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.hardware.input.InputManager;
26 import android.view.InputDevice;
27 import android.view.KeyEvent;
28 import android.view.SearchEvent;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.filters.MediumTest;
32 import androidx.test.rule.ActivityTestRule;
33 import androidx.test.runner.AndroidJUnit4;
34 
35 import com.android.compatibility.common.util.PollingCheck;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 @MediumTest
43 @RunWith(AndroidJUnit4.class)
44 public class SearchEventTest {
45 
46     private Instrumentation mInstrumentation;
47     private SearchEventActivity mActivity;
48 
49     @Rule
50     public ActivityTestRule<SearchEventActivity> mActivityRule =
51             new ActivityTestRule<>(SearchEventActivity.class);
52 
53     @Before
setup()54     public void setup() {
55         mInstrumentation = InstrumentationRegistry.getInstrumentation();
56         mActivity = mActivityRule.getActivity();
57         PollingCheck.waitFor(5000, mActivity::hasWindowFocus);
58     }
59 
60     @Test
testConstructor()61     public void testConstructor() {
62         final InputManager inputManager = (InputManager) mInstrumentation.getTargetContext().
63                 getSystemService(Context.INPUT_SERVICE);
64         if (inputManager == null) {
65             return;
66         }
67         final int[] inputDeviceIds = inputManager.getInputDeviceIds();
68         if (inputDeviceIds != null) {
69             for (int inputDeviceId : inputDeviceIds) {
70                 final InputDevice inputDevice = inputManager.getInputDevice(inputDeviceId);
71                 new SearchEvent(inputDevice);
72             }
73         }
74     }
75 
76     @Test
testBasics()77     public void testBasics() {
78         // Only run for non-watch devices
79         if (mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
80             return;
81         }
82         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_SEARCH);
83         SearchEvent se = mActivity.getTestSearchEvent();
84         assertNotNull(se);
85         InputDevice id = se.getInputDevice();
86         assertNotNull(id);
87         assertEquals(-1, id.getId());
88     }
89 }
90