홈>
두 개의
Activities A and B
가 있습니다
. 500ms 동안 백그라운드 스레드를 실행하고
TextView
를 업데이트합니다.
. 한 번
TextView
TextView
를 클릭하면 업데이트됩니다
B Activity
에 가다
.
Activity B
에서
500ms 동안 실행중인 다른 백그라운드 스레드가 있고
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, [email protected], 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)
- 답변 # 1
- 답변 # 2
이유는 서로 다른 두 개의
CountingIdlingResource
가 있기 때문입니다 두 활동 모두에서 인스턴스 A와 활동 A 중 하나만 등록되었습니다.IdlingResource
를 리턴하기 위해 동일한 메소드를 구현해야합니다. 활동 B에서이 답변을 사용하여 테스트를 실행하는 동안 올바른 활동을 얻으십시오.
관련 자료
- cmd - 일괄 입력이 두 번째로 작동하지 않습니다
- hard drive - HDD 연결 후 1 초간 동작 후 연결 해제
- unity3d - 맨 앞으로 가져 오기 전에로드 할 두 번째 Android 활동을 백그라운드에서 시작합니다
- html - 내 JavaScript 두 번째 카운터가 작동하지 않는 이유는 무엇입니까?
- xml - Android에서 작동하지 않는 사용자 정의 백그라운드 리소스
- javascript - 두 번째 더블 클릭 기능이 작동하지 않습니다
- android - 조각을 포함하여 두 번째 활동을 닫는 방법은 무엇입니까?
- javascript - vue 라우터 문제, 두 번째 계층에서 히스토리 모드가 작동하지 않음
- java - 두 번째 주요 활동에 버튼 클릭으로 텍스트 필드 추가
- python - pyqt5의 두 번째 창에서 버튼이 작동하지 않습니다
- div의 두 번째 Angularjs 앱 표현식이 작동하지 않습니다
싱글 톤 클래스를 사용하여 유휴 리소스를 제어 할 수 있습니다.