public final class

IntentSubject

extends Subject

 java.lang.Object

↳Subject

↳androidx.test.ext.truth.content.IntentSubject

Gradle dependencies

compile group: 'androidx.test.ext', name: 'truth', version: '1.6.0'

  • groupId: androidx.test.ext
  • artifactId: truth
  • version: 1.6.0

Artifact androidx.test.ext:truth:1.6.0 it located at Google repository (https://maven.google.com/)

Overview

Subject for making assertions about s.

Summary

Methods
public static IntentSubjectassertThat(Intent intent)

public final IterableSubjectcategories()

public final BundleSubjectextras()

public final voidfiltersEquallyTo(Intent intent)

public final voidhasAction(java.lang.String action)

public final voidhasComponent(ComponentName component)

public final voidhasComponent(java.lang.String packageName, java.lang.String className)

public final voidhasComponentClass(java.lang.Class<java.lang.Object> componentClass)

public final voidhasComponentClass(java.lang.String className)

public final voidhasComponentPackage(java.lang.String packageName)

public final voidhasData(Uri uri)

public final voidhasFlags(int flag)

Assert that the intent has the given flag set.

public final voidhasNoAction()

public final voidhasPackage(java.lang.String packageName)

public final voidhasType(java.lang.String type)

public static <any>intents()

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

Methods

public static IntentSubject assertThat(Intent intent)

public static <any> intents()

public final void hasComponentClass(java.lang.Class<java.lang.Object> componentClass)

See also: IntentSubject.hasComponentClass(String)

public final void hasComponent(java.lang.String packageName, java.lang.String className)

public final void hasComponent(ComponentName component)

public final void hasComponentClass(java.lang.String className)

See also: IntentSubject.hasComponentClass(Class)

public final void hasComponentPackage(java.lang.String packageName)

public final void hasPackage(java.lang.String packageName)

public final void hasAction(java.lang.String action)

public final void hasNoAction()

public final void hasData(Uri uri)

public final void hasType(java.lang.String type)

public final BundleSubject extras()

public final IterableSubject categories()

public final void hasFlags(int flag)

Assert that the intent has the given flag set.

public final void filtersEquallyTo(Intent intent)

Source

/*
 * Copyright (C) 2018 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.ext.truth.content;

import static com.google.common.truth.Fact.fact;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import androidx.test.ext.truth.internal.FlagUtil;
import androidx.test.ext.truth.os.BundleSubject;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.IterableSubject;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;
import java.util.List;

/** Subject for making assertions about {@link Intent}s. */
public final class IntentSubject extends Subject {

  public static IntentSubject assertThat(Intent intent) {
    return Truth.assertAbout(intents()).that(intent);
  }

  public static Subject.Factory<IntentSubject, Intent> intents() {
    return IntentSubject::new;
  }

  private final Intent actual;

  private IntentSubject(FailureMetadata failureMetadata, Intent subject) {
    super(failureMetadata, subject);
    this.actual = subject;
  }

  /** @see #hasComponentClass(String) */
  public final void hasComponentClass(Class<?> componentClass) {
    hasComponentClass(componentClass.getName());
  }

  public final void hasComponent(String packageName, String className) {
    hasComponentPackage(packageName);
    hasComponentClass(className);
  }

  public final void hasComponent(ComponentName component) {
    hasComponent(component.getPackageName(), component.getClassName());
  }

  /** @see #hasComponentClass(Class) */
  public final void hasComponentClass(String className) {
    failIfIntentIsNull();
    check("getComponent().getClassName()")
        .that(actual.getComponent().getClassName())
        .isEqualTo(className);
  }

  public final void hasComponentPackage(String packageName) {
    failIfIntentIsNull();
    check("getComponent().getPackageName()")
        .that(actual.getComponent().getPackageName())
        .isEqualTo(packageName);
  }

  public final void hasPackage(String packageName) {
    failIfIntentIsNull();
    check("getPackage()").that(actual.getPackage()).isEqualTo(packageName);
  }

  public final void hasAction(String action) {
    failIfIntentIsNull();
    check("getAction()").that(actual.getAction()).isEqualTo(action);
  }

  public final void hasNoAction() {
    hasAction(null);
  }

  public final void hasData(Uri uri) {
    failIfIntentIsNull();
    check("getData()").that(actual.getData()).isEqualTo(uri);
  }

  public final void hasType(String type) {
    failIfIntentIsNull();
    check("getType()").that(actual.getType()).isEqualTo(type);
  }

  public final BundleSubject extras() {
    failIfIntentIsNull();
    return check("getExtras()").about(BundleSubject.bundles()).that(actual.getExtras());
  }

  public final IterableSubject categories() {
    failIfIntentIsNull();
    return check("getCategories()").that(actual.getCategories());
  }

  /** Assert that the intent has the given flag set. */
  public final void hasFlags(int flag) {
    failIfIntentIsNull();
    List<String> actualFlags = FlagUtil.flagNames(actual.getFlags());
    List<String> expectedFlags = FlagUtil.flagNames(flag);
    check("getFlags()").that(actualFlags).containsAtLeastElementsIn(expectedFlags);
  }

  public final void filtersEquallyTo(Intent intent) {
    failIfIntentIsNull();
    if (!actual.filterEquals(intent)) {
      failWithActual("expected to be equal for intent filters to", intent);
    }
  }

  private final void failIfIntentIsNull() {
    if (actual == null) {
      failWithoutActual(fact("Intent not expected to be", null));
    }
  }
}