public class

UiThreadTestRule

extends java.lang.Object

 java.lang.Object

↳androidx.test.rule.UiThreadTestRule

Gradle dependencies

compile group: 'androidx.test', name: 'rules', version: '1.6.1'

  • groupId: androidx.test
  • artifactId: rules
  • version: 1.6.1

Artifact androidx.test:rules:1.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:rules com.android.support.test:rules

Androidx class mapping:

androidx.test.rule.UiThreadTestRule android.support.test.rule.UiThreadTestRule

Overview

This rule allows the test method annotated with UiThreadTest to execute on the application's main thread (or UI thread).

Note, methods annotated with Before and After will also be executed on the UI thread.

Summary

Constructors
publicUiThreadTestRule()

Methods
public Statementapply(Statement base, Description description)

public voidrunOnUiThread(java.lang.Runnable runnable)

Helper method for running part of a method on the UI thread.

protected booleanshouldRunOnUiThread(Description description)

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

Constructors

public UiThreadTestRule()

Methods

public Statement apply(Statement base, Description description)

protected boolean shouldRunOnUiThread(Description description)

public void runOnUiThread(java.lang.Runnable runnable)

Helper method for running part of a method on the UI thread.

Note: In most cases it is simpler to annotate the test method with UiThreadTest.

Use this method if you need to switch in and out of the UI thread within your method.

Parameters:

runnable: runnable containing test code in the run method

See also: UiThreadTest

Source

/*
 * Copyright (C) 2014 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.rule;

import android.util.Log;
import androidx.test.annotation.UiThreadTest;
import androidx.test.internal.runner.junit4.statement.UiThreadStatement;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * This rule allows the test method annotated with {@link UiThreadTest} to execute on the
 * application's main thread (or UI thread).
 *
 * <p>Note, methods annotated with <a
 * href="http://junit.sourceforge.net/javadoc/org/junit/Before.html"><code>Before</code></a> and <a
 * href="http://junit.sourceforge.net/javadoc/org/junit/After.html"><code>After</code></a> will also
 * be executed on the UI thread.
 *
 * @see androidx.test.annotation.UiThreadTest if you need to switch in and out of the UI
 *     thread within your method.
 * @deprecated use {@link UiThreadTest} directly without this rule. {@link UiThreadTest} is now
 *     supported as part of the core Android test runner to provide the ability to run methods
 *     annotated with <code>@Before</code> and <code>@After</code> on the UI thread regardless of
 *     what <code>@Test</code> is annotated with.
 */
@Deprecated
public class UiThreadTestRule implements TestRule {
  private static final String TAG = "UiThreadTestRule";

  @Override
  public Statement apply(final Statement base, Description description) {
    if (base instanceof FailOnTimeout
        || (base instanceof UiThreadStatement && !((UiThreadStatement) base).isRunOnUiThread())) {
      // In upstream junit code Rules Statements are handled last. Since we now handle
      // @UiThreadTest as part of the core Android runner, there is a chance that
      // UiThreadStatement was already applied on the current statement.
      // This is mainly for compatibility reasons to deprecated this rule.
      return base;
    }
    return new UiThreadStatement(base, shouldRunOnUiThread(description));
  }

  protected boolean shouldRunOnUiThread(Description description) {
    if (description.getAnnotation(android.test.UiThreadTest.class) != null) {
      Log.w(
          TAG,
          "Deprecated android.test.UiThreadTest annotation is used! please switch "
              + "to using androidx.test.annotation.UiThreadTest instead.");
      return true;
    }
    return description.getAnnotation(UiThreadTest.class) != null;
  }

  /**
   * Helper method for running part of a method on the UI thread.
   *
   * <p>Note: In most cases it is simpler to annotate the test method with {@link UiThreadTest}.
   *
   * <p>Use this method if you need to switch in and out of the UI thread within your method.
   *
   * @param runnable runnable containing test code in the {@link Runnable#run()} method
   * @see androidx.test.annotation.UiThreadTest
   */
  public void runOnUiThread(final Runnable runnable) throws Throwable {
    UiThreadStatement.runOnUiThread(runnable);
  }
}