浓缩咖啡如何等待一段时间(1小时)? [英] Espresso how to wait for some time(1 hour)?

查看:77
本文介绍了浓缩咖啡如何等待一段时间(1小时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试用例中,我必须在robotium solo.sleep(600000)中完成了1个小时的记录,但是在意式浓缩咖啡中,我对IdlingResource的概念感到困惑.我必须开始记录并等待一段时间(取决于测试类型)15分钟,60分钟等.

In my test case I have to record for 1 hour, in robotium solo.sleep(600000) had done my work, but In espresso I am confused with IdlingResource concept. I have to start recording and wait for some time(depending on the type of test) 15mins, 60mins etc.

robotium中的等效代码

Equivalent code in robotium

    solo.clickOnView(solo.getView("start_record"));
    solo.sleep(duration * 60 * 1000);
    solo.clickOnView(solo.getView("stop_record"));

我试图在浓缩咖啡中像这样使用它

I tried to use it like this in espresso

@RunWith(AndroidJUnit4.class)
@SmallTest
public class AaEspressoTest {

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.absd.rec.RecorderActivity";
private static Class<?> launcherActivityClass;
private Solo solo;
private static CoreRecordingTest skyroTestRunner;


private static Class<? extends Activity> activityClass;

static {
    try {
        activityClass = (Class<? extends Activity>) Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

@Rule
public final ActivityTestRule<?> activityRule
        = new ActivityTestRule<>(activityClass);

private IntentServiceIdlingResource idlingResource;

@Before
public void registerIntentServiceIdlingResource() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    idlingResource = new IntentServiceIdlingResource(instrumentation.getTargetContext());
    Espresso.registerIdlingResources(idlingResource);
}

@After
public void unregisterIntentServiceIdlingResource() {
    Espresso.unregisterIdlingResources(idlingResource);
}

@Test
public void testHello() throws Exception {

 onView(withId(AaEspressoTest.getId("recorderpage_record"))).perform(click());

    registerIntentServiceIdlingResource();

    onView(withId(AaEspressoTest.getId("recorderpage_stop"))).perform(click());

   }
}

闲置资源

public class IntentServiceIdlingResource implements IdlingResource {
private final Context context;
private ResourceCallback resourceCallback;
public static boolean status = false;

public IntentServiceIdlingResource(Context context) {
    this.context = context;
}

@Override
public String getName() {
    return IntentServiceIdlingResource.class.getName();
}

@Override
public boolean isIdleNow() {
    return getTimer();
}

@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
    this.resourceCallback = resourceCallback;

}

private static boolean getTimer() {

    new CountDownTimer(5000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            // Do Nothing
            status = false;
        }

        @Override
        public void onFinish() {             
            status = true;
        }
    };
    return status;
}
}

例外:

android.support.test.espresso.IdlingResourceTimeoutException: Wait for [com.adbs.recorder.IntentServiceIdlingResource] to become idle timed out

推荐答案

仅当经过特定时间后,您才需要带有isIdleNow()IdlingResource,该isIdleNow()返回true.为此,请保存开始时间并将其与当前时间进行比较:

You need an IdlingResource with an isIdleNow() that returns true only if the specific amount of time has passed. To achieve that, save the start time and compare it with current time:

public class ElapsedTimeIdlingResource implements IdlingResource {
  private final long startTime;
  private final long waitingTime;
  private ResourceCallback resourceCallback;

  public ElapsedTimeIdlingResource(long waitingTime) {
    this.startTime = System.currentTimeMillis();
    this.waitingTime = waitingTime;
  }

  @Override
  public String getName() {
    return ElapsedTimeIdlingResource.class.getName() + ":" + waitingTime;
  }

  @Override
  public boolean isIdleNow() {
    long elapsed = System.currentTimeMillis() - startTime;
    boolean idle = (elapsed >= waitingTime);
    if (idle) {
      resourceCallback.onTransitionToIdle();
    }
    return idle;
  }

  @Override
  public void registerIdleTransitionCallback(
      ResourceCallback resourceCallback) {
    this.resourceCallback = resourceCallback;
  }
}

在测试中创建并注册此空闲资源:

Create and register this idling resource in your test:

@Test
public static void waitForOneHour() {
  long waitingTime = DateUtils.HOUR_IN_MILLIS;

  // Start
  onView(withId(AaEspressoTest.getId("recorderpage_record")))
      .perform(click());

  // Make sure Espresso does not time out
  IdlingPolicies.setMasterPolicyTimeout(
      waitingTime * 2, TimeUnit.MILLISECONDS);
  IdlingPolicies.setIdlingResourceTimeout(
      waitingTime * 2, TimeUnit.MILLISECONDS);

  // Now we wait
  IdlingResource idlingResource = new ElapsedTimeIdlingResource(waitingTime);
  Espresso.registerIdlingResources(idlingResource);

  // Stop
  onView(withId(AaEspressoTest.getId("recorderpage_stop")))
      .perform(click());

  // Clean up
  Espresso.unregisterIdlingResources(idlingResource);
}

您需要setMasterPolicyTimeoutsetIdlingResourceTimeout调用,以确保Espresso不会由于超时而终止测试.

You need the setMasterPolicyTimeout and setIdlingResourceTimeout calls to make sure Espresso does not terminate the test due to time out.

完整示例: https://github.com/chiuki/espresso-样本/树/主/空闲资源经过时间

这篇关于浓缩咖啡如何等待一段时间(1小时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆