public class

TestScreenManager

extends ScreenManager

 java.lang.Object

androidx.car.app.ScreenManager

↳androidx.car.app.testing.TestScreenManager

Gradle dependencies

compile group: 'androidx.car.app', name: 'app-testing', version: '1.7.0-beta01'

  • groupId: androidx.car.app
  • artifactId: app-testing
  • version: 1.7.0-beta01

Artifact androidx.car.app:app-testing:1.7.0-beta01 it located at Google repository (https://maven.google.com/)

Overview

The ScreenManager that is used for testing.

This class will track the following usages of the ScreenManager throughout your test:

Summary

Methods
public java.util.List<Screen>getScreensPushed()

Returns all the Screens pushed via ScreenManager.push(Screen), and ScreenManager.pushForResult(Screen, OnScreenResultListener).

public java.util.List<Screen>getScreensRemoved()

Returns all the Screens removed via ScreenManager.pop(), ScreenManager.popTo(String), and ScreenManager.remove(Screen).

public booleanhasScreens()

Returns true if the Screen stack has any screens in it.

public voidpop()

Pops the top Screen from the stack.

public voidpopTo(java.lang.String marker)

Removes screens from the top of the stack until a Screen which has the given marker is found, or the root has been reached.

public voidpopToRoot()

Removes all screens from the stack until the root has been reached.

public voidpush(Screen screen)

Pushes the screen to the stack.

public voidpushForResult(Screen screen, OnScreenResultListener onScreenResultListener)

Pushes a Screen, for which you would like a result from, onto the stack.

public voidremove(Screen screen)

Removes the screen from the stack.

public voidreset()

Resets the values tracked by this TestScreenManager and the Screen stack.

from ScreenManagergetScreenStack, getScreenStackInternal, getStackSize, getTop
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public void reset()

Resets the values tracked by this TestScreenManager and the Screen stack.

public java.util.List<Screen> getScreensPushed()

Returns all the Screens pushed via ScreenManager.push(Screen), and ScreenManager.pushForResult(Screen, OnScreenResultListener).

The screens are stored in the order in which they were pushed, where the first screen in the list is the first screen that was pushed.

The screens will be stored until TestScreenManager.reset() is called.

public java.util.List<Screen> getScreensRemoved()

Returns all the Screens removed via ScreenManager.pop(), ScreenManager.popTo(String), and ScreenManager.remove(Screen).

The screens are stored in the order in which they were removed, where the first screen in the list, is the first screen that was removed.

The screens will be stored until TestScreenManager.reset() is called.

public boolean hasScreens()

Returns true if the Screen stack has any screens in it.

public void push(Screen screen)

Pushes the screen to the stack.

If the screen pushed is already in the stack it will be moved to the top of the stack.

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

public void pushForResult(Screen screen, OnScreenResultListener onScreenResultListener)

Pushes a Screen, for which you would like a result from, onto the stack.

When the given screen finishes, the onScreenResultCallback will receive a callback to OnScreenResultListener.onScreenResult(Object) with the result that the pushed screen set via Screen.setResult(Object).

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

Parameters:

screen: the Screen to push on top of the stack
onScreenResultListener: the listener that will be executed with the result pushed by the screen through Screen.setResult(Object). This callback will be executed on the main thread

public void pop()

Pops the top Screen from the stack.

If the top Screen is the only Screen in the stack, it will not be removed.

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

public void popTo(java.lang.String marker)

Removes screens from the top of the stack until a Screen which has the given marker is found, or the root has been reached.

The root Screen will not be popped.

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

See also: Screen.setMarker(String)

public void popToRoot()

Removes all screens from the stack until the root has been reached.

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

public void remove(Screen screen)

Removes the screen from the stack.

If the screen is the only Screen in the stack, it will not be removed.

If the app's lifecycle is already in the Lifecycle.State.DESTROYED state, this operation is a no-op.

Source

/*
 * Copyright 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 androidx.car.app.testing;

import static java.util.Objects.requireNonNull;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.car.app.OnScreenResultListener;
import androidx.car.app.Screen;
import androidx.car.app.ScreenManager;
import androidx.car.app.utils.CollectionUtils;
import androidx.lifecycle.Lifecycle.State;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * The {@link ScreenManager} that is used for testing.
 *
 * <p>This class will track the following usages of the {@link ScreenManager} throughout your test:
 *
 * <ul>
 *   <li>All the {@link Screen}s pushed via {@link ScreenManager#push}, or {@link
 *       ScreenManager#pushForResult}.
 *   <li>All the {@link Screen}s removed via, {@link ScreenManager#pop}, {@link
 *       ScreenManager#popTo}, or {@link ScreenManager#remove}.
 * </ul>
 */
public class TestScreenManager extends ScreenManager {
    private final List<Screen> mScreensPushed = new ArrayList<>();
    private final List<Screen> mScreensRemoved = new ArrayList<>();

    /**
     * Resets the values tracked by this {@link TestScreenManager} and the {@link Screen} stack.
     */
    public void reset() {
        getScreenStackInternal().clear();
        mScreensPushed.clear();
        mScreensRemoved.clear();
    }

    /**
     * Returns all the {@link Screen}s pushed via {@link ScreenManager#push}, and {@link
     * ScreenManager#pushForResult}.
     *
     * <p>The screens are stored in the order in which they were pushed, where the first screen
     * in the list is the first screen that was pushed.
     *
     * <p>The screens will be stored until {@link #reset} is called.
     */
    @NonNull
    public List<Screen> getScreensPushed() {
        return CollectionUtils.unmodifiableCopy(mScreensPushed);
    }

    /**
     * Returns all the {@link Screen}s removed via {@link ScreenManager#pop}, {@link
     * ScreenManager#popTo}, and {@link ScreenManager#remove}.
     *
     * <p>The screens are stored in the order in which they were removed, where the first screen
     * in the list, is the first screen that was removed.
     *
     * <p>The screens will be stored until {@link #reset} is called.
     */
    @NonNull
    public List<Screen> getScreensRemoved() {
        return CollectionUtils.unmodifiableCopy(mScreensRemoved);
    }

    /** Returns {@code true} if the {@link Screen} stack has any screens in it. */
    public boolean hasScreens() {
        return !getScreensInStack().isEmpty();
    }

    @Override
    public void push(@NonNull Screen screen) {
        mScreensPushed.add(requireNonNull(screen));
        super.push(screen);
    }

    @SuppressLint("ExecutorRegistration")
    @Override
    public void pushForResult(
            @NonNull Screen screen, @NonNull OnScreenResultListener onScreenResultListener) {
        mScreensPushed.add(screen);
        super.pushForResult(screen, onScreenResultListener);
    }

    @Override
    public void pop() {
        Screen top = getTop();
        super.pop();

        if (!top.equals(getTop())) {
            mScreensRemoved.add(top);
        }
    }

    @Override
    public void popTo(@NonNull String marker) {
        Set<Screen> screensBefore = getScreensInStack();
        super.popTo(marker);
        Set<Screen> screensAfter = getScreensInStack();

        for (Screen screen : screensBefore) {
            if (!screensAfter.contains(screen)) {
                mScreensRemoved.add(screen);
            }
        }
    }

    @Override
    public void popToRoot() {
        Set<Screen> screensBefore = getScreensInStack();
        super.popToRoot();
        Set<Screen> screensAfter = getScreensInStack();

        for (Screen screen : screensBefore) {
            if (!screensAfter.contains(screen)) {
                mScreensRemoved.add(screen);
            }
        }
    }

    @Override
    public void remove(@NonNull Screen screen) {
        super.remove(screen);
        if (screen.getLifecycle().getCurrentState() == State.DESTROYED) {
            mScreensRemoved.add(screen);
        }
    }

    private Set<Screen> getScreensInStack() {
        return new HashSet<>(getScreenStackInternal());
    }

    TestScreenManager(TestCarContext testCarContext) {
        super(testCarContext, testCarContext.getLifecycleOwner().mRegistry);
    }
}