From 1252d0bc3c1e2c16ad8d4f9ec4e913601ef7472a Mon Sep 17 00:00:00 2001 From: Mark Renouf Date: Tue, 18 Aug 2020 11:35:19 -0400 Subject: [PATCH] Fix missing null check in collectRootScrollCaptureTargets If no window-level handlers have been added, the list is null (lazily init). Test: atest com.android.systemui.screenshot.ScrollCaptureTest Change-Id: I39d4295cebb091fa89f65c5917ba08bc2396ed37 --- core/java/android/view/ViewRootImpl.java | 3 + packages/SystemUI/tests/AndroidManifest.xml | 2 + .../SystemUI/tests/res/values/strings.xml | 21 ++++ .../screenshot/ScrollCaptureTest.java | 101 ++++++++++++++++++ .../screenshot/ScrollViewActivity.java | 42 ++++++++ 5 files changed, 169 insertions(+) create mode 100644 packages/SystemUI/tests/res/values/strings.xml create mode 100644 packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollCaptureTest.java create mode 100644 packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollViewActivity.java diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 64ddb2f4d9d9a..b6208f2845935 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -8905,6 +8905,9 @@ public final class ViewRootImpl implements ViewParent, * @param targets the search queue for targets */ private void collectRootScrollCaptureTargets(Queue targets) { + if (mRootScrollCaptureCallbacks == null) { + return; + } for (ScrollCaptureCallback cb : mRootScrollCaptureCallbacks) { // Add to the list for consideration Point offset = new Point(mView.getLeft(), mView.getTop()); diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml index c6331682f80ce..db878458721e7 100644 --- a/packages/SystemUI/tests/AndroidManifest.xml +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -80,6 +80,8 @@ android:excludeFromRecents="true" /> + + + + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultrices condimentum ultricies. Sed elementum at massa id sagittis. Nullam dictum massa lorem, nec ornare nunc pharetra vitae. Duis ultrices, felis eu condimentum congue, erat orci efficitur purus, ac rutrum odio lacus sed sapien. Suspendisse erat augue, eleifend eget auctor sagittis, porta eget nibh. Mauris pulvinar urna non justo condimentum, ut vehicula sapien finibus. Aliquam nibh magna, tincidunt ut viverra sed, placerat et turpis. Nam placerat, dui sed tincidunt consectetur, ante velit posuere mauris, tincidunt finibus velit lectus ac tortor. Cras eget lectus feugiat, porttitor velit nec, malesuada massa. + + diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollCaptureTest.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollCaptureTest.java new file mode 100644 index 0000000000000..e7ef64e6adadd --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollCaptureTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2020 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.systemui.screenshot; + +import static org.junit.Assert.fail; + +import android.content.Intent; +import android.graphics.Point; +import android.graphics.Rect; +import android.os.RemoteException; +import android.testing.AndroidTestingRunner; +import android.util.Log; +import android.view.Display; +import android.view.IScrollCaptureClient; +import android.view.IScrollCaptureController; +import android.view.IWindowManager; +import android.view.WindowManagerGlobal; + +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; + +import com.android.systemui.SysuiTestCase; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * Tests the of internal framework Scroll Capture API from SystemUI. + */ +@RunWith(AndroidTestingRunner.class) +@SmallTest +public class ScrollCaptureTest extends SysuiTestCase { + private static final String TAG = "ScrollCaptureTest"; + + /** + * Verifies that a request traverses from SystemUI, to WindowManager and to the app process and + * is returned without error. Device must be unlocked. + */ + @Test + public void testBasicOperation() throws InterruptedException { + IWindowManager wms = WindowManagerGlobal.getWindowManagerService(); + + // Start an activity to be on top that will be targeted + InstrumentationRegistry.getInstrumentation().startActivitySync( + new Intent(mContext, ScrollViewActivity.class).addFlags( + Intent.FLAG_ACTIVITY_NEW_TASK)); + + final CountDownLatch latch = new CountDownLatch(1); + try { + wms.requestScrollCapture(Display.DEFAULT_DISPLAY, null, -1, + new IScrollCaptureController.Stub() { + @Override + public void onClientConnected( + IScrollCaptureClient client, Rect scrollBounds, + Point positionInWindow) { + Log.d(TAG, + "client connected: " + client + "[scrollBounds= " + scrollBounds + + ", positionInWindow=" + positionInWindow + "]"); + latch.countDown(); + } + + @Override + public void onClientUnavailable() { + } + + @Override + public void onCaptureStarted() { + } + + @Override + public void onCaptureBufferSent(long frameNumber, Rect capturedArea) { + } + + @Override + public void onConnectionClosed() { + } + }); + } catch (RemoteException e) { + Log.e(TAG, "request failed", e); + fail("caught remote exception " + e); + } + latch.await(1000, TimeUnit.MILLISECONDS); + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollViewActivity.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollViewActivity.java new file mode 100644 index 0000000000000..bd3725942eca9 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScrollViewActivity.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2020 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.systemui.screenshot; + +import android.app.Activity; +import android.os.Bundle; +import android.util.TypedValue; +import android.widget.LinearLayout; +import android.widget.ScrollView; +import android.widget.TextView; + +import androidx.annotation.Nullable; + +public class ScrollViewActivity extends Activity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ScrollView scrollView = new ScrollView(this); + LinearLayout linearLayout = new LinearLayout(this); + linearLayout.setOrientation(LinearLayout.VERTICAL); + TextView text = new TextView(this); + text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 40); + text.setText(com.android.systemui.R.string.test_content); + linearLayout.addView(text); + scrollView.addView(linearLayout); + setContentView(scrollView); + } +}