Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62edc827 authored by Chavi Weingarten's avatar Chavi Weingarten
Browse files

Dismiss keyguard for SurfaceControlViewHostTests

Test: SurfaceControlViewHostTests
Fixes: 312312735
Change-Id: I9351ea9e710ef9a1c90f0f83cdf2fef30c56fe39
parent a29375a5
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -93,8 +93,6 @@
                  android:showWhenLocked="true"/>
        <activity android:name="android.view.cts.surfacevalidator.CapturedActivity"/>

        <activity android:name="com.android.server.wm.SurfaceControlViewHostTests$TestActivity" />

        <activity android:name="com.android.server.wm.SurfaceSyncGroupTests$TestActivity"
            android:screenOrientation="locked"
            android:turnScreenOn="true"
@@ -122,6 +120,13 @@
        <activity android:name="com.android.server.wm.ActivityRecordInputSinkTests$TestActivity"
                  android:exported="true">
        </activity>

        <activity android:name="com.android.server.wm.utils.TestActivity"
            android:screenOrientation="locked"
            android:turnScreenOn="true"
            android:showWhenLocked="true"
            android:theme="@style/WhiteBackgroundTheme"
            android:exported="true" />
    </application>

    <instrumentation
+36 −43
Original line number Diff line number Diff line
@@ -23,15 +23,14 @@ import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;

import static org.junit.Assert.assertTrue;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.platform.test.annotations.Presubmit;
import android.server.wm.BuildUtils;
import android.view.Gravity;
import android.view.IWindow;
import android.view.SurfaceControl;
@@ -46,12 +45,12 @@ import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

import com.android.server.wm.utils.CommonUtils;
import com.android.server.wm.utils.TestActivity;

import org.junit.After;
import org.junit.Before;
@@ -59,11 +58,14 @@ import org.junit.Test;
import org.junit.runner.RunWith;

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

@Presubmit
@SmallTest
@RunWith(WindowTestRunner.class)
public class SurfaceControlViewHostTests {
    private static final long WAIT_TIME_S = 5L * BuildUtils.HW_TIMEOUT_MULTIPLIER;

    private static final String TAG = "SurfaceControlViewHostTests";

    private final ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>(
@@ -76,6 +78,8 @@ public class SurfaceControlViewHostTests {
    private SurfaceControlViewHost mScvh1;
    private SurfaceControlViewHost mScvh2;

    private SurfaceView mSurfaceView;

    @Before
    public void setUp() throws Exception {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
@@ -96,15 +100,17 @@ public class SurfaceControlViewHostTests {
        mView1 = new Button(mActivity);
        mView2 = new Button(mActivity);

        mInstrumentation.runOnMainSync(() -> {
            try {
                mActivity.attachToSurfaceView(sc);
            } catch (InterruptedException e) {
            }
        CountDownLatch svReadyLatch = new CountDownLatch(1);
        mActivity.runOnUiThread(() -> addSurfaceView(svReadyLatch));
        assertTrue("Failed to wait for SV to get created",
                svReadyLatch.await(WAIT_TIME_S, TimeUnit.SECONDS));
        new SurfaceControl.Transaction().reparent(sc, mSurfaceView.getSurfaceControl())
                .show(sc).apply();

        mInstrumentation.runOnMainSync(() -> {
            TestWindowlessWindowManager wwm = new TestWindowlessWindowManager(
                    mActivity.getResources().getConfiguration(), sc,
                    mActivity.mSurfaceView.getHostToken());
                    mSurfaceView.getHostToken());

            mScvh1 = new SurfaceControlViewHost(mActivity, mActivity.getDisplay(),
                    wwm, "requestFocusWithMultipleWindows");
@@ -135,7 +141,7 @@ public class SurfaceControlViewHostTests {
        }
        assertTrue("Failed to wait for view2", wasVisible);

        IWindow window = IWindow.Stub.asInterface(mActivity.mSurfaceView.getWindowToken());
        IWindow window = IWindow.Stub.asInterface(mSurfaceView.getWindowToken());

        WindowManagerGlobal.getWindowSession().grantEmbeddedWindowFocus(window,
                mScvh1.getInputTransferToken(), true);
@@ -162,43 +168,30 @@ public class SurfaceControlViewHostTests {
        }
    }

    public static class TestActivity extends Activity implements SurfaceHolder.Callback {
        private SurfaceView mSurfaceView;
        private final CountDownLatch mSvReadyLatch = new CountDownLatch(1);

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final FrameLayout content = new FrameLayout(this);
            mSurfaceView = new SurfaceView(this);
            mSurfaceView.setBackgroundColor(Color.BLACK);
    private void addSurfaceView(CountDownLatch svReadyLatch) {
        final FrameLayout content = mActivity.getParentLayout();
        mSurfaceView = new SurfaceView(mActivity);
        mSurfaceView.setZOrderOnTop(true);
        final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(500, 500,
                Gravity.LEFT | Gravity.TOP);
            content.addView(mSurfaceView, lp);
            setContentView(content);
            mSurfaceView.getHolder().addCallback(this);
        }

        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
            mSvReadyLatch.countDown();
                svReadyLatch.countDown();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width,
                    int height) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        }

        public void attachToSurfaceView(SurfaceControl sc) throws InterruptedException {
            mSvReadyLatch.await();
            new SurfaceControl.Transaction().reparent(sc, mSurfaceView.getSurfaceControl())
                    .show(sc).apply();
            }
        });
        content.addView(mSurfaceView, lp);
    }
}
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 com.android.server.wm.utils;

import static android.view.WindowInsets.Type.displayCutout;
import static android.view.WindowInsets.Type.systemBars;
import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import android.app.Activity;
import android.app.KeyguardManager;
import android.os.Bundle;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

/**
 * TestActivity that will ensure it dismisses keyguard and shows as a fullscreen activity.
 */
public class TestActivity extends Activity {
    private static final int sTypeMask = systemBars() | displayCutout();
    private FrameLayout mParentLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mParentLayout = new FrameLayout(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        setContentView(mParentLayout, layoutParams);

        WindowInsetsController windowInsetsController = getWindow().getInsetsController();
        windowInsetsController.hide(sTypeMask);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
        getWindow().setAttributes(params);
        getWindow().setDecorFitsSystemWindows(false);

        final KeyguardManager keyguardManager = getInstrumentation().getContext().getSystemService(
                KeyguardManager.class);
        if (keyguardManager != null && keyguardManager.isKeyguardLocked()) {
            keyguardManager.requestDismissKeyguard(this, null);
        }
    }

    public FrameLayout getParentLayout() {
        return mParentLayout;
    }
}