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

Commit 5946889e authored by Zhekai Hu's avatar Zhekai Hu Committed by Android (Google) Code Review
Browse files

Merge "Add test for multiple views" into main

parents eb6c0b62 9daf3cac
Loading
Loading
Loading
Loading
+2 −12
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/group_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="a standard text view"/>

    <android.view.contentcapture.MyCustomViewWithA11yProvider
        android:id="@+id/custom_view_with_a11y_provider"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

</LinearLayout>
+12 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package android.view.contentcapture;

import static android.view.contentcapture.CustomTestActivity.INTENT_EXTRA_CUSTOM_VIEWS;
import static android.view.contentcapture.CustomTestActivity.INTENT_EXTRA_LAYOUT_ID;
import static android.view.contentcapture.CustomTestActivity.INTENT_EXTRA_VIEW_TYPE;

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

@@ -279,6 +280,12 @@ public abstract class AbstractContentCapturePerfTestCase {
        return intent;
    }

    protected Intent getLaunchIntent(int layoutId, int numViews, int viewType) {
        final Intent intent = getLaunchIntent(layoutId, numViews);
        intent.putExtra(INTENT_EXTRA_VIEW_TYPE, viewType);
        return intent;
    }

    /**
     * Launch test activity with give layout and parameter
     */
@@ -286,4 +293,9 @@ public abstract class AbstractContentCapturePerfTestCase {
        final Intent intent = getLaunchIntent(layoutId, numViews);
        return (CustomTestActivity) sInstrumentation.startActivitySync(intent);
    }

    protected CustomTestActivity launchActivity(int layoutId, int numViews, int viewType) {
        final Intent intent = getLaunchIntent(layoutId, numViews, viewType);
        return (CustomTestActivity) sInstrumentation.startActivitySync(intent);
    }
}
+29 −0
Original line number Diff line number Diff line
@@ -35,10 +35,15 @@ import com.android.perftests.contentcapture.R;
public class CustomTestActivity extends Activity {
    public static final String INTENT_EXTRA_LAYOUT_ID = "layout_id";
    public static final String INTENT_EXTRA_CUSTOM_VIEWS = "custom_view_number";
    public static final String INTENT_EXTRA_VIEW_TYPE = "view_type";
    static final String INTENT_EXTRA_FINISH_ON_IDLE = "finish";
    static final String INTENT_EXTRA_DRAW_CALLBACK = "draw_callback";
    public static final int MAX_VIEWS = 500;
    public static final int VIEW_TYPE_CUSTOM_VIEW = 1;
    public static final int VIEW_TYPE_TEXT_VIEW = 2;
    private static final int CUSTOM_CONTAINER_LAYOUT_ID = R.layout.test_container_activity;
    private static final int LAYOUT_GROUP_VIRTUAL_NODES_ID =
            R.layout.test_export_virtual_assist_node_activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -47,10 +52,19 @@ public class CustomTestActivity extends Activity {
        if (getIntent().hasExtra(INTENT_EXTRA_LAYOUT_ID)) {
            final int layoutId = getIntent().getIntExtra(INTENT_EXTRA_LAYOUT_ID,
                    /* defaultValue= */0);
            final int view_type = getIntent().getIntExtra(INTENT_EXTRA_VIEW_TYPE, 0);
            setContentView(layoutId);
            if (layoutId == CUSTOM_CONTAINER_LAYOUT_ID) {
                createCustomViews(findViewById(R.id.root_view),
                        getIntent().getIntExtra(INTENT_EXTRA_CUSTOM_VIEWS, MAX_VIEWS));
            } else if (layoutId == LAYOUT_GROUP_VIRTUAL_NODES_ID) {
                if (view_type == VIEW_TYPE_CUSTOM_VIEW) {
                    createCustomViewsWithVirtualChildren(findViewById(R.id.group_root_view),
                            getIntent().getIntExtra(INTENT_EXTRA_CUSTOM_VIEWS, MAX_VIEWS));
                } else if (view_type == VIEW_TYPE_TEXT_VIEW) {
                    createTextViews(findViewById(R.id.group_root_view),
                            getIntent().getIntExtra(INTENT_EXTRA_CUSTOM_VIEWS, MAX_VIEWS));
                }
            }
        }

@@ -101,6 +115,21 @@ public class CustomTestActivity extends Activity {
        }
    }

    private void createCustomViewsWithVirtualChildren(LinearLayout root, int number) {
        for (int i = 0; i < number; i++) {
            MyCustomViewWithA11yProvider customView = new MyCustomViewWithA11yProvider(this);
            customView.setImportantForContentCapture(View.IMPORTANT_FOR_CONTENT_CAPTURE_YES);
            root.addView(customView);
        }
    }

    private void createTextViews(LinearLayout root, int number) {
        for (int i = 0; i < number; i++) {
            TextView textView = new TextView(this);
            root.addView(textView);
        }
    }

    private LinearLayout createHorizontalLayout() {
        final LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.HORIZONTAL);
+47 −14
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package android.view.contentcapture;

import static android.view.contentcapture.CustomTestActivity.VIEW_TYPE_CUSTOM_VIEW;
import static android.view.contentcapture.CustomTestActivity.VIEW_TYPE_TEXT_VIEW;
import static com.android.compatibility.common.util.ActivitiesWatcher.ActivityLifecycle.DESTROYED;

import android.content.Intent;
@@ -233,17 +235,48 @@ public class LoginTest extends AbstractContentCapturePerfTestCase {
    }

    @Test
    public void testNotifyVirtualChildrenAppearedWithCustomView() throws Throwable {
    public void testNotifyVirtualChildrenAppearedWithCustomView_1() throws Throwable {
        testNumberOfTypeViewAppearedEvents(
                R.layout.test_export_virtual_assist_node_activity,
                /* numberOfViews= */ 1,
                /* expectedViewAppearedCounts= */ 6,
                VIEW_TYPE_CUSTOM_VIEW);
    }

    @Test
    public void testNotifyVirtualChildrenAppearedWithCustomView_10() throws Throwable {
        testNumberOfTypeViewAppearedEvents(
                R.layout.test_export_virtual_assist_node_activity,
                /* numberOfViews= */ 10,
                /* expectedViewAppearedCounts= */ 51,
                VIEW_TYPE_CUSTOM_VIEW);
    }

    @Test
    public void testNotifyVirtualChildrenAppearedWithCustomView_100() throws Throwable {
        testNumberOfTypeViewAppearedEvents(
                R.layout.test_export_virtual_assist_node_activity,
                /* numberOfViews= */ 100,
                /* expectedViewAppearedCounts= */ 501,
                VIEW_TYPE_CUSTOM_VIEW);
    }

    @Test
    public void testWithTextView_500() throws Throwable {
        testNumberOfTypeViewAppearedEvents(
                R.layout.test_export_virtual_assist_node_activity,
                /* numberOfViews= */ 500,
                /* expectedViewAppearedCounts= */ 501,
                VIEW_TYPE_TEXT_VIEW);
    }

    private void testNumberOfTypeViewAppearedEvents(
            int layoutId, int numberOfViews,
            int expectedViewAppearedCounts, int viewType) throws Throwable {
        // Arrange
        MyContentCaptureService service = enableService();
        CustomTestActivity activity = launchActivity(
                R.layout.test_export_virtual_assist_node_activity, 0);
        // TODO: Add multiple views with virtual children and check the performance.
        View hostView = activity.findViewById(R.id.custom_view_with_a11y_provider);
        hostView.setImportantForContentCapture(View.IMPORTANT_FOR_CONTENT_CAPTURE_YES);
        // Expected: 1 for a11y host node (-1), 3 for virtual children (1, 2, 3)
        // 1 for host view
        int expectedViewAppeared = 5;
        CustomTestActivity activity = launchActivity(layoutId, numberOfViews, viewType);
        View rootView = activity.findViewById(R.id.group_root_view);
        long eventTimeoutMs = 20000;
        BenchmarkState state = mPerfStatusReporter.getBenchmarkState();

@@ -252,17 +285,17 @@ public class LoginTest extends AbstractContentCapturePerfTestCase {
            state.pauseTiming();
            service.clearEvents();
            // Trigger content capture structure provision.
            sInstrumentation.runOnMainSync(() -> hostView.setVisibility(View.GONE));
            sInstrumentation.runOnMainSync(() -> rootView.setVisibility(View.GONE));
            sInstrumentation.waitForIdleSync();
            state.resumeTiming();
            sInstrumentation.runOnMainSync(() -> hostView.setVisibility(View.VISIBLE));
            sInstrumentation.runOnMainSync(() -> rootView.setVisibility(View.VISIBLE));
            sInstrumentation.waitForIdleSync();
            state.pauseTiming();
            service.waitForAppearedEvents(expectedViewAppeared, eventTimeoutMs);
            service.waitForAppearedEvents(expectedViewAppearedCounts, eventTimeoutMs);

            // Assert
            Assert.assertEquals("Expected " + expectedViewAppeared
                            + " TYPE_VIEW_APPEARED events", expectedViewAppeared,
            Assert.assertEquals("Expected " + expectedViewAppearedCounts
                            + " TYPE_VIEW_APPEARED events", expectedViewAppearedCounts,
                    service.getAppearedCount());
            state.resumeTiming();
        }