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

Commit 816aea5c authored by Mark Renouf's avatar Mark Renouf
Browse files

Guard against NPE when root view is detached

Bug: 170137878
Test: atest FrameworksCoreTests:ViewRootImplTest
If null, simply skip searching the view hierarchy.

Change-Id: Icbef4022d73dcc3ddd8e6e41b010b00b09d98aa8
parent 316dbd3b
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -9154,14 +9154,14 @@ public final class ViewRootImpl implements ViewParent,
     * Handles an inbound request for scroll capture from the system. If a client is not already
     * active, a search will be dispatched through the view tree to locate scrolling content.
     * <p>
     * Either {@link IScrollCaptureCallbacks#onClientConnected(IScrollCaptureConnection, Rect,
     * Either {@link IScrollCaptureCallbacks#onConnected(IScrollCaptureConnection, Rect,
     * Point)} or {@link IScrollCaptureCallbacks#onUnavailable()} will be returned
     * depending on the results of the search.
     *
     * @param callbacks to receive responses
     * @see ScrollCaptureTargetResolver
     */
    private void handleScrollCaptureRequest(@NonNull IScrollCaptureCallbacks callbacks) {
    public void handleScrollCaptureRequest(@NonNull IScrollCaptureCallbacks callbacks) {
        LinkedList<ScrollCaptureTarget> targetList = new LinkedList<>();

        // Window (root) level callbacks
@@ -9169,10 +9169,12 @@ public final class ViewRootImpl implements ViewParent,

        // Search through View-tree
        View rootView = getView();
        if (rootView != null) {
            Point point = new Point();
            Rect rect = new Rect(0, 0, rootView.getWidth(), rootView.getHeight());
            getChildVisibleRect(rootView, rect, point);
            rootView.dispatchScrollCaptureSearch(rect, point, targetList);
        }

        // No-op path. Scroll capture not offered for this window.
        if (targetList.isEmpty()) {
+24 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import static androidx.test.InstrumentationRegistry.getInstrumentation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.content.Context;
import android.os.Binder;
@@ -50,6 +51,9 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

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

/**
 * Tests for {@link ViewRootImpl}
 *
@@ -195,6 +199,26 @@ public class ViewRootImplTest {
        assertEquals(behavior, controller.getSystemBarsBehavior());
    }

    /**
     * Ensure scroll capture request handles a ViewRootImpl with no view tree.
     */
    @Test
    public void requestScrollCapture_withoutContentRoot() {
        final CountDownLatch latch = new CountDownLatch(1);
        mViewRootImpl.handleScrollCaptureRequest(new IScrollCaptureCallbacks.Default() {
            @Override
            public void onUnavailable() {
                latch.countDown();
            }
        });
        try {
            if (latch.await(100, TimeUnit.MILLISECONDS)) {
                return; // pass
            }
        } catch (InterruptedException e) { /* ignore */ }
        fail("requestScrollCapture did not respond");
    }

    /**
     * When window doesn't have focus, keys should be dropped.
     */