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.Manifest;
23 import android.app.Instrumentation;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.hardware.input.InputManager;
27 import android.platform.test.annotations.AppModeSdkSandbox;
28 import android.view.InputDevice;
29 import android.view.KeyEvent;
30 import android.view.SearchEvent;
31 
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.filters.MediumTest;
34 import androidx.test.rule.ActivityTestRule;
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import com.android.compatibility.common.util.AdoptShellPermissionsRule;
38 import com.android.compatibility.common.util.PollingCheck;
39 
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 @MediumTest
46 @RunWith(AndroidJUnit4.class)
47 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
48 public class SearchEventTest {
49 
50     private Instrumentation mInstrumentation;
51     private SearchEventActivity mActivity;
52 
53     @Rule(order = 0)
54     public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
55             androidx.test.platform.app.InstrumentationRegistry
56                     .getInstrumentation().getUiAutomation(),
57             Manifest.permission.START_ACTIVITIES_FROM_SDK_SANDBOX);
58 
59     @Rule(order = 1)
60     public ActivityTestRule<SearchEventActivity> mActivityRule =
61             new ActivityTestRule<>(SearchEventActivity.class);
62 
63     @Before
setup()64     public void setup() {
65         mInstrumentation = InstrumentationRegistry.getInstrumentation();
66         mActivity = mActivityRule.getActivity();
67         PollingCheck.waitFor(5000, mActivity::hasWindowFocus);
68     }
69 
70     @Test
testConstructor()71     public void testConstructor() {
72         final InputManager inputManager = (InputManager) mInstrumentation.getTargetContext().
73                 getSystemService(Context.INPUT_SERVICE);
74         if (inputManager == null) {
75             return;
76         }
77         final int[] inputDeviceIds = inputManager.getInputDeviceIds();
78         if (inputDeviceIds != null) {
79             for (int inputDeviceId : inputDeviceIds) {
80                 final InputDevice inputDevice = inputManager.getInputDevice(inputDeviceId);
81                 new SearchEvent(inputDevice);
82             }
83         }
84     }
85 
86     @Test
testBasics()87     public void testBasics() {
88         // Only run for non-watch devices
89         if (mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
90             return;
91         }
92         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_SEARCH);
93         SearchEvent se = mActivity.getTestSearchEvent();
94         assertNotNull(se);
95         InputDevice id = se.getInputDevice();
96         assertNotNull(id);
97         assertEquals(-1, id.getId());
98     }
99 }
100