2013-03-17 3 views
3

버튼이 포함 된 간단한 활동이 있습니다. 버튼을 누르면 두 번째 활동이 실행됩니다. 이제 안드로이드 계측 테스트가 처음입니다. 지금까지 내가Android 계측 실행 활동

public class TestSplashActivity extends 
    ActivityInstrumentationTestCase2<ActivitySplashScreen> { 

private Button mLeftButton; 
private ActivitySplashScreen activitySplashScreen; 
private ActivityMonitor childMonitor = null; 
public TestSplashActivity() { 
    super(ActivitySplashScreen.class); 
} 

@Override 
protected void setUp() throws Exception { 
    super.setUp(); 
    final ActivitySplashScreen a = getActivity(); 
    assertNotNull(a); 
    activitySplashScreen=a; 
    mLeftButton=(Button) a.findViewById(R.id.btn1); 

} 

@SmallTest 
public void testNameOfButton(){ 
    assertEquals("Press Me", mLeftButton.getText().toString()); 
    this.childMonitor = new ActivityMonitor(SecondActivity.class.getName(), null, true); 
    this.getInstrumentation().addMonitor(childMonitor); 
    activitySplashScreen.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      mLeftButton.performClick(); 
    }}); 

    Activity childActivity=this.getInstrumentation().waitForMonitorWithTimeout(childMonitor, 5000); 
    assertEquals(childActivity, SecondActivity.class); 

} 

} 이제

I 버튼 작동의 텍스트를 얻을 첫 번째 어설 쓴 것입니다. 내가 수행 클릭을 호출 할 때, 나는 지금은 지금은 계측 테스트의 관점에서, 안드로이드 응용 프로그램의 맥락에서이 예외를 이해 예외를

Only the original thread that created a view hierarchy can touch its views. 

를 얻을. 어떻게하면 버튼에 대한 클릭 이벤트를 수행 할 수 있으며 두 번째 활동이로드되었는지 확인할 수 있습니다.

답변

2

당신이 InstrumentationTestCase를 확장하는 테스트 클래스를 가정하면, 당신은 시험 방법에 있는지,이 논리를 따라야합니다

  1. 당신이 확인하고 싶은 활동에 관심을 등록합니다.
  2. 시작은
  3. 당신이 그것으로 원하는 일을. 구성 요소가 올바른지 확인하고, 사용자 작업을 수행하고, 그런 종류의 일을 수행하십시오.
  4. "시퀀스"의 다음 활동에 대한 관심을 등록하십시오.
  5. 시퀀스의 다음 활동을 수행하는 활동을 수행하십시오.

    Instrumentation mInstrumentation = getInstrumentation(); 
    // We register our interest in the activity 
    Instrumentation.ActivityMonitor monitor = mInstrumentation.addMonitor(YourClass.class.getName(), null, false); 
    // We launch it 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setClassName(mInstrumentation.getTargetContext(), YourClass.class.getName()); 
    mInstrumentation.startActivitySync(intent); 
    
    Activity currentActivity = getInstrumentation().waitForMonitor(monitor); 
    assertNotNull(currentActivity); 
    // We register our interest in the next activity from the sequence in this use case 
    mInstrumentation.removeMonitor(monitor); 
    monitor = mInstrumentation.addMonitor(YourNextClass.class.getName(), null, false); 
    

    는 다음과 같이 수행 뭔가를 클릭을 보내려면 :

  6. 반복,이 논리 다음 ... 코드의 관점에서

, 이것은 다음과 같은 일이 발생할 것 내 응용 프로그램에서

View v = currentActivity.findViewById(....R.id...); 
assertNotNull(v); 
TouchUtils.clickView(this, v); 
mInstrumentation.sendStringSync("Some text to send into that view, if it would be a text view for example. If it would be a button it would already have been clicked by now."); 
+0

은 버튼을 클릭하면 새로운 활동을 시작합니다. 두 번째 활동이 시작되거나 시작될 때 버튼을 클릭 한 후이 시나리오를 테스트하고 싶습니다. 이 시나리오를 어떻게 테스트 할 수 있습니까? – user1730789

+0

내가 편집 한 부분 코드입니다. – user1730789

+0

나는 본다. 그런 클릭을 보내지 마십시오. 계측 클래스를 사용하여 클릭을 보내야합니다. 내 게시물을 편집했습니다. –

관련 문제