public class

SimpleAtom

extends java.lang.Object

implements Atom<Evaluation>

 java.lang.Object

↳androidx.test.espresso.web.model.SimpleAtom

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-web', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-web
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-web:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-web com.android.support.test.espresso:espresso-web

Androidx class mapping:

androidx.test.espresso.web.model.SimpleAtom android.support.test.espresso.web.model.SimpleAtom

Overview

A simple implementation of Atom suitable for subclassing.

Summary

Constructors
publicSimpleAtom(java.lang.String script)

Creates a SimpleAtom which runs the given script and places any ElementReference as the first argument to the script.

publicSimpleAtom(java.lang.String script, SimpleAtom.ElementReferencePlacement elementPlacement)

Creates a SimpleAtom which runs the given script and places any supplied ElementReference either as the first or last argument to the script.

Methods
public final java.util.List<java.lang.Object>getArguments(ElementReference elementRef)

The SimpleAtom presents an argument list to the script which follows some basic conventions.

protected java.util.List<java.lang.Object>getNonContextualArguments()

Extend this method to pass additional arguments to the script.

public final java.lang.StringgetScript()

Returns the script this SimpleAtom was created with.

protected EvaluationhandleBadEvaluation(Evaluation e)

Extend this method to handle a failure code in the Evaluation object.

protected voidhandleNoElementReference()

Extend this method to handle the case where getArguments() has been called without an ElementReference.

public final Evaluationtransform(Evaluation e)

The SimpleAtom's transform method checks the Evaluation object for success.

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

Constructors

public SimpleAtom(java.lang.String script)

Creates a SimpleAtom which runs the given script and places any ElementReference as the first argument to the script.

public SimpleAtom(java.lang.String script, SimpleAtom.ElementReferencePlacement elementPlacement)

Creates a SimpleAtom which runs the given script and places any supplied ElementReference either as the first or last argument to the script.

Methods

public final java.lang.String getScript()

Returns the script this SimpleAtom was created with.

public final Evaluation transform(Evaluation e)

The SimpleAtom's transform method checks the Evaluation object for success.

If the Evaluation object has an error, the default behaviour is to throw an exception, subclasses may change this behaviour via the handleBadEvaluation method.

public final java.util.List<java.lang.Object> getArguments(ElementReference elementRef)

The SimpleAtom presents an argument list to the script which follows some basic conventions.

If an ElementReference is provided, it is placed either in the first or last position based on the ElementReferencePlacement provided to the constructor.

The nonContextualArguments (if provided via getNonContextualArguments) will appear after the ElementReference.

protected void handleNoElementReference()

Extend this method to handle the case where getArguments() has been called without an ElementReference. Implementors may want to throw an exception here if they require an ElementReference to evaluate properly.

protected java.util.List<java.lang.Object> getNonContextualArguments()

Extend this method to pass additional arguments to the script.

Returns:

a list of arguments (non-null)

protected Evaluation handleBadEvaluation(Evaluation e)

Extend this method to handle a failure code in the Evaluation object.

The default implementation will throw an exception, subclasses may want to ignore certain failure cases.

Returns:

Evaluation the evaluation object (must be non-null)

Source

/*
 * Copyright (C) 2015 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.test.espresso.web.model;

import static androidx.test.internal.util.Checks.checkNotNull;

import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** A simple implementation of Atom suitable for subclassing. */
public class SimpleAtom implements Atom<Evaluation> {

  private final String script;
  private final ElementReferencePlacement elementPlacement;

  /** Controls whether the ElementReference appears as the first arg or last arg to the script. */
  public enum ElementReferencePlacement {
    FIRST,
    LAST
  };

  /**
   * Creates a SimpleAtom which runs the given script and places any ElementReference as the first
   * argument to the script.
   */
  public SimpleAtom(String script) {
    this(script, ElementReferencePlacement.FIRST);
  }

  /**
   * Creates a SimpleAtom which runs the given script and places any supplied ElementReference
   * either as the first or last argument to the script.
   */
  public SimpleAtom(String script, ElementReferencePlacement elementPlacement) {
    this.script = checkNotNull(script);
    this.elementPlacement = checkNotNull(elementPlacement);
  }

  /** Returns the script this SimpleAtom was created with. */
  @Override
  public final String getScript() {
    return script;
  }

  /**
   * The SimpleAtom's transform method checks the Evaluation object for success.
   *
   * <p>If the Evaluation object has an error, the default behaviour is to throw an exception,
   * subclasses may change this behaviour via the handleBadEvaluation method.
   */
  @Override
  public final Evaluation transform(Evaluation e) {
    if (e.getStatus() != 0) {
      return checkNotNull(handleBadEvaluation(e), "Evaluation bad and handler returned null! " + e);
    }
    return e;
  }

  /**
   * The SimpleAtom presents an argument list to the script which follows some basic conventions.
   *
   * <p>If an ElementReference is provided, it is placed either in the first or last position based
   * on the ElementReferencePlacement provided to the constructor.
   *
   * <p>The nonContextualArguments (if provided via getNonContextualArguments) will appear after the
   * ElementReference.
   */
  @Override
  public final List<Object> getArguments(@Nullable ElementReference elementRef) {
    List<Object> nonContextualArguments = checkNotNull(getNonContextualArguments());
    if (null == elementRef) {
      handleNoElementReference();
    }

    if (nonContextualArguments.size() == 0 && null == elementRef) {
      return Collections.EMPTY_LIST;
    } else {
      if (null == elementRef) {
        return nonContextualArguments;
      } else {
        List<Object> args = new ArrayList<Object>(nonContextualArguments.size() + 1);
        if (elementPlacement == ElementReferencePlacement.FIRST) {
          args.add(elementRef);
          args.addAll(nonContextualArguments);
        } else {
          args.addAll(nonContextualArguments);
          args.add(elementRef);
        }
        return args;
      }
    }
  }

  /**
   * Extend this method to handle the case where getArguments() has been called without an
   * ElementReference. Implementors may want to throw an exception here if they require an
   * ElementReference to evaluate properly.
   */
  protected void handleNoElementReference() {}

  /**
   * Extend this method to pass additional arguments to the script.
   *
   * @return a list of arguments (non-null)
   */
  protected List<Object> getNonContextualArguments() {
    return Collections.EMPTY_LIST;
  }

  /**
   * Extend this method to handle a failure code in the Evaluation object.
   *
   * <p>The default implementation will throw an exception, subclasses may want to ignore certain
   * failure cases.
   *
   * @return Evaluation the evaluation object (must be non-null)
   * @throws RuntimeException if the badness level is too high.
   */
  protected Evaluation handleBadEvaluation(Evaluation e) {
    throw new RuntimeException("Error in evaluation" + e);
  }
}