闲置资源在android

时间:2018-02-21 07:06:10

标签: android android-espresso android-testing

我有两个Activities A and B。我正在运行500毫秒的后台线程并更新TextView。更新TextView后,点击TextView转到B Activity。在Activity B我有另一个后台线程,该线程运行500毫秒并更新TextView中的B Activity。我正在使用Espresso测试此流程。我等待background thread使用Idling Resource完成执行。我在Idling Resource中使用B Activity时遇到了问题。我在下面写下了我的代码:

MainActivityTest.java

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    private IdlingResource mIdlingResource;

    @Before
    public void registerIdlingResource(){
        mIdlingResource = mainActivityActivityTestRule.getActivity().getIdlingResource();
        // To prove that the test fails, omit this call:
//        Espresso.registerIdlingResources(mIdlingResource);
        IdlingRegistry.getInstance().register(mIdlingResource);
    }

    @Test
    public void mainScreenLoads(){
        onView(withId(R.id.my_text)).check(matches(ViewMatchers.withText("Boom")));
        onView(withId(R.id.my_text)).perform(click());
        onView(withId(R.id.second_text)).check(matches(ViewMatchers.withText("Boom")));
    }

    @After
    public void unregisterIdlingResource() {
        if (mIdlingResource != null) {
//            Espresso.unregisterIdlingResources(mIdlingResource);
            IdlingRegistry.getInstance().unregister(mIdlingResource);
        }
    }
}

活动

public class MainActivity extends AppCompatActivity {

    CountingIdlingResource countingIdlingResource = new CountingIdlingResource("DATA_LOADER");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        countingIdlingResource.increment();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((TextView)findViewById(R.id.my_text)).setText("Boom");
                        ((TextView)findViewById(R.id.my_text)).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                                startActivity(intent);
                            }
                        });
                        countingIdlingResource.decrement();
                    }
                });
            }
        }).start();
    }

    /**
     * Only called from test, creates and returns a new {@link SimpleIdlingResource}.
     */
    @VisibleForTesting
    @NonNull
    public IdlingResource getIdlingResource() {
        return countingIdlingResource;
    }
}

B活动

public class SecondActivity extends AppCompatActivity {

    CountingIdlingResource countingIdlingResource = new CountingIdlingResource("DATA_LOADER");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

    @Override
    protected void onResume() {
        super.onResume();
        countingIdlingResource.increment();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((TextView)findViewById(R.id.second_text)).setText("Boom");
//                        if (mIdlingResource != null) {
//                            mIdlingResource.setIdleState(true);
//                        }
                        countingIdlingResource.decrement();
                    }
                });
            }
        }).start();
    }
}

我收到以下错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Boom"' doesn't match the selected view.
Expected: with text: is "Boom"
Got: "AppCompatTextView{id=2131165282, res-name=second_text, visibility=VISIBLE, width=246, height=51, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@94bfcec, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Second Screen, input-type=0, ime-target=false, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1566)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
at com.example.sagarsuri.rxjavademo.MainActivityTest.mainScreenLoads(MainActivityTest.java:47)

2 个答案:

答案 0 :(得分:2)

您可以使用单例类并从那里控制空闲资源。

class EspressoKIdlingResource {
companion object {
    val countingIdlingResource = CountingIdlingResource("data_loaded")

    fun getInstance() : CountingIdlingResource{
        return countingIdlingResource
    }

    fun increment(){
        countingIdlingResource.increment()
    }

    fun decrement(){
        countingIdlingResource.decrement()
    }
}

答案 1 :(得分:1)

原因是,您在两个活动中都有两个不同的CountingIdlingResource实例,并且仅注册了活动A中的一个。您应该实现与在活动B中返回IdlingResource相同的方法,并在运行测试时使用this answer获得正确的活动。