public class

SessionController

extends java.lang.Object

 java.lang.Object

↳androidx.car.app.testing.SessionController

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

SessionController provides API that allows testing of a Session.

This controller allows:

  • Injecting a TestCarContext into the Session instance, which provides access to the test managers and other testing functionalities.

Summary

Constructors
publicSessionController(Session session, TestCarContext context, Intent intent)

Creates a SessionController to control the provided Session.

Methods
public SessiongetSession()

Returns the Session that is being controlled.

public SessionControllermoveToState(Lifecycle.State state)

Moves the Session being controlled to the input state.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SessionController(Session session, TestCarContext context, Intent intent)

Creates a SessionController to control the provided Session.

Parameters:

session: the Session to control
context: the TestCarContext that the session should use.
intent: the that the session should start with during the Lifecycle.State.CREATED state.

Methods

public Session getSession()

Returns the Session that is being controlled.

public SessionController moveToState(Lifecycle.State state)

Moves the Session being controlled to the input state.

Note that is a terminal state, and you cannot move to any other state after the Session reaches that state.

See also: Session.getLifecycle()

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.content.Intent;

import androidx.annotation.NonNull;
import androidx.car.app.Session;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Lifecycle.Event;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

/**
 * {@link SessionController} provides API that allows testing of a {@link Session}.
 *
 * <p>This controller allows:
 *
 * <ul>
 *   <li>Injecting a {@link TestCarContext} into the {@link Session} instance, which provides access
 *   to the test managers and other testing functionalities.
 * </ul>
 */
public class SessionController {
    final Session mSession;
    final TestCarContext mTestCarContext;
    final Intent mIntent;
    private final TestLifecycleOwner mLifecycleOwner;

    /**
     * Creates a {@link SessionController} to control the provided {@link Session}.
     *
     * @param session the {@link Session} to control
     * @param context the {@link TestCarContext} that the {@code session} should use.
     * @param intent  the {@link Intent} that the {@code session} should start with during the
     *                {@link androidx.lifecycle.Lifecycle.State#CREATED} state.
     * @throws NullPointerException if {@code session} or {@code context} is {@code null}
     */
    public SessionController(@NonNull Session session, @NonNull TestCarContext context,
            @NonNull Intent intent) {
        mSession = requireNonNull(session);
        mTestCarContext = requireNonNull(context);
        mIntent = requireNonNull(intent);

        mLifecycleOwner = new TestLifecycleOwner();
        mLifecycleOwner.getRegistry().addObserver(new SessionLifecycleObserver());

        mSession.setCarContextInternal(mTestCarContext);
        mSession.setLifecycleRegistryInternal(mTestCarContext.getLifecycleOwner().getRegistry());
    }

    /** Returns the {@link Session} that is being controlled. */
    @NonNull
    public Session getSession() {
        return mSession;
    }

    /**
     * Moves the {@link Session} being controlled to the input {@code state}.
     *
     * <p>Note that {@link Lifecycle.State#DESTROYED} is a terminal state, and you cannot move to
     * any other state after the {@link Session} reaches that state.</p>
     *
     * @see Session#getLifecycle
     */
    @NonNull
    public SessionController moveToState(@NonNull Lifecycle.State state) {
        mLifecycleOwner.getRegistry().setCurrentState(state);
        return this;
    }

    /**
     * A helper class to forward the lifecycle events from this controller to the session.
     */
    class SessionLifecycleObserver implements DefaultLifecycleObserver {
        @Override
        public void onCreate(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            TestScreenManager screenManager = mTestCarContext.getCarService(
                    TestScreenManager.class);
            registry.handleLifecycleEvent(Event.ON_CREATE);
            screenManager.push(mSession.onCreateScreen(mIntent));
        }

        @Override
        public void onStart(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            registry.handleLifecycleEvent(Event.ON_START);
        }

        @Override
        public void onResume(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            registry.handleLifecycleEvent(Event.ON_RESUME);
        }

        @Override
        public void onPause(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            registry.handleLifecycleEvent(Event.ON_PAUSE);
        }

        @Override
        public void onStop(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            registry.handleLifecycleEvent(Event.ON_STOP);
        }

        @Override
        public void onDestroy(@NonNull LifecycleOwner owner) {
            LifecycleRegistry registry = (LifecycleRegistry) mSession.getLifecycle();
            registry.handleLifecycleEvent(Event.ON_DESTROY);
        }
    }
}