All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squareup.spoon.DeviceTestResult Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
package com.squareup.spoon;

import com.android.ddmlib.logcat.LogCatMessage;
import com.squareup.spoon.misc.StackTrace;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.unmodifiableList;
import static com.squareup.spoon.SpoonLogger.logError;

public final class DeviceTestResult {
  /** Separator between screenshot timestamp and tag. */
  public static final String SCREENSHOT_SEPARATOR = Spoon.NAME_SEPARATOR;

  public enum Status {
    PASS, FAIL
  }

  private final Status status;
  private final StackTrace exception;
  private final long duration;
  private final List screenshots;
  private final List files;
  private final File animatedGif;
  private final List log;

  private DeviceTestResult(Status status, StackTrace exception, long duration,
      List screenshots, File animatedGif, List log, List files) {
    this.status = status;
    this.exception = exception;
    this.duration = duration;
    this.screenshots = unmodifiableList(new ArrayList(screenshots));
    this.files = unmodifiableList(new ArrayList(files));
    this.animatedGif = animatedGif;
    this.log = unmodifiableList(new ArrayList(log));
  }

  /** Execution status. */
  public Status getStatus() {
    return status;
  }

  /** Exception thrown during execution. */
  public StackTrace getException() {
    return exception;
  }

  /** Length of test execution, in seconds. */
  public long getDuration() {
    return duration;
  }

  /** Screenshots taken during test. */
  public List getScreenshots() {
    return screenshots;
  }

  /** Animated GIF of screenshots. */
  public File getAnimatedGif() {
    return animatedGif;
  }

  /** Arbitrary files saved from the test */
  public List getFiles() {
    return files;
  }

  public List getLog() {
    return log;
  }

  public static class Builder {
    private final List screenshots = new ArrayList();
    private final List files = new ArrayList();
    private Status status = Status.PASS;
    private StackTrace exception;
    private long start;
    private long duration = -1;
    private File animatedGif;
    private List log;

    public Builder markTestAsFailed(String message) {
      checkNotNull(message);
      if (status != Status.PASS) {
        logError("Status was already marked as failed!");
      }
      status = Status.FAIL;
      exception = StackTrace.from(message);
      return this;
    }

    public Builder setLog(List log) {
      checkNotNull(log);
      checkArgument(this.log == null, "Log already added.");
      this.log = log;
      return this;
    }

    public Builder startTest() {
      checkArgument(start == 0, "Start already called.");
      start = System.nanoTime();
      return this;
    }

    public Builder endTest() {
      checkArgument(start != 0, "Start was not called.");
      if (duration != -1) {
        logError("Test was already marked as ended!");
      }
      duration = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start);
      return this;
    }

    public Builder addScreenshot(File screenshot) {
      checkNotNull(screenshot);
      screenshots.add(screenshot);
      return this;
    }

    public Builder addFile(File file) {
      checkNotNull(file);
      files.add(file);
      return this;
    }

    public Builder setAnimatedGif(File animatedGif) {
      checkNotNull(animatedGif);
      checkArgument(this.animatedGif == null, "Animated GIF already set.");
      this.animatedGif = animatedGif;
      return this;
    }

    public DeviceTestResult build() {
      if (log == null) {
        log = Collections.emptyList();
      }
      return new DeviceTestResult(status, exception, duration,
              screenshots, animatedGif, log, files);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy