summaryrefslogtreecommitdiff
path: root/tests/tests/hardware/src/android/hardware/input/cts/tests/VirtualDeviceTestCase.java
blob: fc03d5b1dd70f99220fa99159fab25036906bf74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 2022 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.hardware.input.cts.tests;

import static android.content.pm.PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import android.app.ActivityOptions;
import android.companion.AssociationInfo;
import android.companion.CompanionDeviceManager;
import android.companion.virtual.VirtualDeviceManager;
import android.companion.virtual.VirtualDeviceParams;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;

import com.android.compatibility.common.util.AdoptShellPermissionsRule;
import com.android.compatibility.common.util.SystemUtil;

import com.google.common.collect.ImmutableList;

import org.junit.Rule;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public abstract class VirtualDeviceTestCase extends InputTestCase {

    private static final String TAG = "VirtualDeviceTestCase";

    private static final int ARBITRARY_SURFACE_TEX_ID = 1;

    protected static final int PRODUCT_ID = 1;
    protected static final int VENDOR_ID = 1;
    protected static final int DISPLAY_WIDTH = 100;
    protected static final int DISPLAY_HEIGHT = 100;

    // Uses:
    // Manifest.permission.CREATE_VIRTUAL_DEVICE,
    // Manifest.permission.ADD_TRUSTED_DISPLAY
    // These cannot be specified as part of the call as ADD_TRUSTED_DISPLAY is hidden and therefore
    // not visible to CTS.
    @Rule
    public AdoptShellPermissionsRule mAdoptShellPermissionsRule = new AdoptShellPermissionsRule(
            InstrumentationRegistry.getInstrumentation().getUiAutomation());

    private final CountDownLatch mLatch = new CountDownLatch(1);
    private final InputManager.InputDeviceListener mInputDeviceListener =
            new InputManager.InputDeviceListener() {
                @Override
                public void onInputDeviceAdded(int deviceId) {
                    mLatch.countDown();
                }

                @Override
                public void onInputDeviceRemoved(int deviceId) {
                }

                @Override
                public void onInputDeviceChanged(int deviceId) {
                }
            };

    InputManager mInputManager;

    VirtualDeviceManager.VirtualDevice mVirtualDevice;
    VirtualDisplay mVirtualDisplay;

    /** Helper class to drop permissions temporarily and restore them at the end of a test. */
    static final class DropShellPermissionsTemporarily implements AutoCloseable {
        DropShellPermissionsTemporarily() {
            InstrumentationRegistry.getInstrumentation().getUiAutomation()
                    .dropShellPermissionIdentity();
        }

        @Override
        public void close() {
            InstrumentationRegistry.getInstrumentation().getUiAutomation()
                    .adoptShellPermissionIdentity();
        }
    }

    @Override
    void onBeforeLaunchActivity() {
        final Context context = InstrumentationRegistry.getTargetContext();
        final PackageManager packageManager = context.getPackageManager();
        // TVs do not support companion
        assumeTrue(packageManager.hasSystemFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP));
        // Virtual input devices only operate on virtual displays
        assumeTrue(packageManager.hasSystemFeature(
                PackageManager.FEATURE_ACTIVITIES_ON_SECONDARY_DISPLAYS));
        // TODO(b/261155110): Re-enable tests once freeform mode is supported in Virtual Display.
        assumeFalse("Skipping test: VirtualDisplay window policy doesn't support freeform.",
                packageManager.hasSystemFeature(FEATURE_FREEFORM_WINDOW_MANAGEMENT));

        final String packageName = context.getPackageName();
        associateCompanionDevice(packageName);
        AssociationInfo associationInfo = null;
        for (AssociationInfo ai : context.getSystemService(CompanionDeviceManager.class)
                .getMyAssociations()) {
            if (packageName.equals(ai.getPackageName())) {
                associationInfo = ai;
                break;
            }
        }
        if (associationInfo == null) {
            fail("Could not create association for test");
            return;
        }
        final VirtualDeviceManager virtualDeviceManager =
                context.getSystemService(VirtualDeviceManager.class);
        mVirtualDevice = virtualDeviceManager.createVirtualDevice(associationInfo.getId(),
                new VirtualDeviceParams.Builder().build());
        mVirtualDisplay = mVirtualDevice.createVirtualDisplay(
                /* width= */ DISPLAY_WIDTH,
                /* height= */ DISPLAY_HEIGHT,
                /* dpi= */ 50,
                /* surface= */ new Surface(new SurfaceTexture(ARBITRARY_SURFACE_TEX_ID)),
                /* flags= */ 0,
                /* executor= */ Runnable::run,
                /* callback= */ null);
        if (mVirtualDisplay == null) {
            fail("Could not create virtual display");
        }
    }

    @Override
    void onSetUp() {
        mInputManager = mInstrumentation.getTargetContext().getSystemService(InputManager.class);
        mInputManager.registerInputDeviceListener(mInputDeviceListener,
                new Handler(Looper.getMainLooper()));
        onSetUpVirtualInputDevice();
        waitForInputDevice();
        // Tap to gain window focus on the activity
        tapActivityToFocus();
    }

    abstract void onSetUpVirtualInputDevice();

    abstract void onTearDownVirtualInputDevice();

    @Override
    void onTearDown() {
        try {
            onTearDownVirtualInputDevice();
        } finally {
            if (mTestActivity != null) {
                mTestActivity.finish();
            }
            if (mVirtualDisplay != null) {
                mVirtualDisplay.release();
            }
            if (mVirtualDevice != null) {
                mVirtualDevice.close();
            }
            if (mInputManager != null) {
                mInputManager.unregisterInputDeviceListener(mInputDeviceListener);
            }
            final Context context = InstrumentationRegistry.getTargetContext();
            disassociateCompanionDevice(context.getPackageName());
        }
    }

    @Override
    @Nullable
    Bundle getActivityOptions() {
        return ActivityOptions.makeBasic()
                .setLaunchDisplayId(mVirtualDisplay.getDisplay().getDisplayId())
                .toBundle();
    }

    protected void waitForInputDevice() {
        try {
            mLatch.await(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            fail("Virtual input device setup was interrupted");
        }
    }

    private void associateCompanionDevice(String packageName) {
        // Associate this package for user 0 with a zeroed-out MAC address (not used in this test)
        SystemUtil.runShellCommand(
                String.format("cmd companiondevice associate %d %s 00:00:00:00:00:00",
                        Process.myUserHandle().getIdentifier(), packageName));
    }

    private void disassociateCompanionDevice(String packageName) {
        SystemUtil.runShellCommand(
                String.format("cmd companiondevice disassociate %d %s 00:00:00:00:00:00",
                        Process.myUserHandle().getIdentifier(), packageName));
    }

    protected void tapActivityToFocus() {
        final Point p = getViewCenterOnScreen(mTestActivity.getWindow().getDecorView());
        final int displayId = mTestActivity.getDisplayId();

        final long downTime = SystemClock.elapsedRealtime();
        final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_DOWN, p.x, p.y, 0 /* metaState */);
        downEvent.setDisplayId(displayId);
        final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.elapsedRealtime(),
                MotionEvent.ACTION_UP, p.x, p.y, 0 /* metaState */);
        upEvent.setDisplayId(displayId);

        try {
            mInstrumentation.sendPointerSync(downEvent);
            mInstrumentation.sendPointerSync(upEvent);
        } catch (IllegalArgumentException e) {
            fail("Failed to sending taps to the activity. Is the device unlocked?");
        }

        verifyEvents(ImmutableList.of(downEvent, upEvent));
    }

    private static Point getViewCenterOnScreen(@NonNull View view) {
        final int[] location = new int[2];
        view.getLocationOnScreen(location);
        return new Point(location[0] + view.getWidth() / 2,
                location[1] + view.getHeight() / 2);
    }

    public VirtualDisplay createUnownedVirtualDisplay() {
        return DisplayManager.createVirtualDisplay(
                "test",
                /* width= */ DISPLAY_WIDTH,
                /* height= */ DISPLAY_HEIGHT,
                /* displayIdToMirror= */ 50,
                /* surface= */ null
        );
    }
}