1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base.test.util; 6 7 import androidx.test.espresso.IdlingPolicies; 8 9 import org.junit.rules.TestRule; 10 import org.junit.runner.Description; 11 import org.junit.runners.model.Statement; 12 13 import java.util.concurrent.TimeUnit; 14 15 /** 16 * Sets Espresso's master timeout policy. This helps reduce the time Espresso waits before failing 17 * test cases that hang. This results in more useful stacks and errors messages than when the 18 * process is killed from the outside. 19 */ 20 public final class EspressoIdleTimeoutRule implements TestRule { 21 private final long mTimeout; 22 private final TimeUnit mUnit; 23 EspressoIdleTimeoutRule(long timeout, TimeUnit unit)24 public EspressoIdleTimeoutRule(long timeout, TimeUnit unit) { 25 mTimeout = timeout; 26 mUnit = unit; 27 } 28 29 @Override apply(Statement base, Description description)30 public Statement apply(Statement base, Description description) { 31 return new Statement() { 32 @Override 33 public void evaluate() throws Throwable { 34 IdlingPolicies.setMasterPolicyTimeout(mTimeout, mUnit); 35 base.evaluate(); 36 } 37 }; 38 } 39 } 40