如何在Espresso中允许繁忙的工作线程

时间:2019-05-26 22:40:26

标签: java android android-espresso android-testing

我正在尝试测试应用程序的进度指示器,该视图在视图模型获取数据时会显示不确定的进度指示器。

为了测试这一点,我正在模拟提供者,该提供者返回数据并将其阻塞,直到我的测试给出告诉它继续进行为止。基本设置如下:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Murva Plan</title>
  <link rel="stylesheet" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
  <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>




  <div class="backIMg">
    <div class="headTime" data-aos="fade-down" data-aos-duration="1000">Time:
      <span class="Time"> </span></div>
    <div class="Date" data-aos="fade-down" data-aos-duration="1000">
      <span class="subDate"> %11%/%04%/%2019%</span>
    </div>
    <div class="error-case" data-aos="fade-up" data-aos-duration="1800">
      <i class="fas fa-exclamation-circle error-sign">
        <span class="error-text">error</span>
      </i>
      <p class="error-description">you should pick a day</p>
    </div>


    <select class="optionList" id="optionList" data-aos="fade-up" data-aos-duration="1300">
      <option class="option-item" value="pick the day" selected>Pick the day</option>
      <option class="option-item" value="Monday">Monday</option>
      <option class="option-item" value="Tuesday">Tuesday</option>
      <option class="option-item" value="Wednesday">Wednesday</option>
      <option class="option-item" value="Thursday">Thursday</option>
      <option class="option-item" value="Friday">Friday</option>
      <option class="option-item" value="Saturday">Saturday</option>
      <option class="option-item" value="Sunday">Sunday</option>
    </select>
    <div class="from-1">
      FROM :
    </div>
    <input type="time" class="inputTime item" data-aos="fade-down" data-aos-duration="1600">

    <div class="to-1">
      TO :
    </div>
    <input type="time" class="inputTime2 item" data-aos="fade-down" data-aos-duration="1600" value="">

    <input type="text" class="inputText item" data-aos="fade-up" data-aos-duration="1900"
    placeholder="             I   '   m         g   o   i   n   g          t   o       .    .   .   .">

    <div class="checkbox item" data-aos="fade-up" data-aos-duration="1500">
      <input class="checkboxx" type="checkbox" id="prfix" value="true" checked>
      <label for="huey">Add the prefix I'm going to </label>
    </div>

    <button class="add__btn  submit-1"><i class="far fa-check-circle"></i></button>

    <div class="plansBackground">
      <div class="container">
        <!-- <h1 class="day">
          Monday
        </h1>

        <div class="plan-ID" style="text-align:center">
          <i class="fas fa-calendar-check check"></i>
          <span class="from">FROM : </span><span>10:30 AM</span>
          <span class="to"> TO : </span><span class=""> 11:30 AM</span> <span class="to-do">
            I'm going to study</span>
          <i class="fas fa-times-circle"></i>
          <span class="line">
          </span>
          </span>
        </div> -->



        <!-- </div>   <!--end of the container div-->
      </div>
    </div>







    <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
    <script>
      AOS.init();
    </script>
    <script src="script2.js" charset="utf-8"></script>
</body>

</html>

此代码很旧,因此提供程序正在使用@Test public void testProgressIndicator() { injectProvider(mockProvider); startTestActivity(); // The mock provider now runs on a worker thread and won't finish // until we tell it to. // We should now see a progress indicator. onView(withId(R.id.progress_indicator)).check( matches(withEffectiveVisibility(Visibility.VISIBLE))); // Tell the worker thread to finish up. mockProvider.setResult(); // The worker thread now returns a result, the progress indicator // should be gone. onView(withId(R.id.progress_indicator)).check( matches(withEffectiveVisibility(Visibility.GONE))); } 在工作线程上使用阻塞代码。

但是,Espresso通常会等待所有工作人员完成,以确保结果不取决于时间。特别是,它使用AsyncTask等待所有未决的AsyncTaskPoolMonitor对象。通常情况下这很好,但就我而言,我想让该线程在Espresso继续运行时保持繁忙。如何告诉Espresso不要等待这个特定线程?

便宜的解决方案只是到AsyncTask并通过Thread或类似的方式进行通信,但是找到解决方案同时保留使用Handler的设置将是很好的选择。

当我进入调试器时,我发现我的测试运行程序线程卡在第一个AsyncTask上:

check()

1 个答案:

答案 0 :(得分:0)

您应该使用CountingIdlingResource, 调用AsyncTask的onPreExecute()中的crement()和onPostExecute()中的decrement()

https://developer.android.com/reference/androidx/test/espresso/idling/CountingIdlingResource.html