2012-06-18 2 views
7

"녹음"버튼을 클릭했을 때 인 텐트를 방송하고 있습니다. 부울 변수가 전달되어 녹음 시작 여부를 보여줍니다. 의도를 생성하는 코드는 다음과 같습니다.인 텐트가 방송 된 것을 테스트하는 방법

Intent recordIntent = new Intent(ACTION_RECORDING_STATUS_CHANGED); 
recordIntent.putExtra(RECORDING_STARTED, getIsRecordingStarted()); 
sendBroadcast(recordIntent); 

이 코드를 테스트하기 위해 테스트에서 수신기를 등록했습니다. 의도를 받았지만 전달 된 변수가 같지 않습니다. 코드를 디버깅하면 전송 된 값과 동일한 값을 볼 수 있지만 가져올 때 값은 같지 않습니다.

@Test 
public void pressingRecordButtonOnceGenerateStartRecordingIntent() 
     throws Exception { 
    // Assign 
    AppActivity activity = new AppActivity(); 
    activity.onCreate(null); 
    activity.onResume(); 

    activity.registerReceiver(new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context arg0, Intent intent) { 
      // Assert 
      ShadowIntent shadowIntent = Robolectric.shadowOf(intent); 
      assertThat(shadowIntent 
        .hasExtra(AppActivity.RECORDING_STARTED), 
        equalTo(true)); 
      Boolean expected = true; 
      Boolean actual = shadowIntent.getExtras().getBoolean(
        AppActivity.RECORDING_STARTED, false); 
      assertThat(actual, equalTo(expected)); 

     } 
    }, new IntentFilter(
      AppActivity.ACTION_RECORDING_STATUS_CHANGED)); 

    ImageButton recordButton = (ImageButton) activity 
      .findViewById(R.id.recordBtn); 

    // Act 
    recordButton.performClick(); 
    ShadowHandler.idleMainLooper(); 

} 

나는 또한 (대신 getBoolean의 대신 그림자의 실제 의도지만, 동일한 결과를 가져 오기를 (사용

답변

3

)에 대해 테스트 한)은 나를 위해 일했다.

public void pressingRecordButtonOnceGenerateStartRecordingIntent() 
     throws Exception { 
    // Assign 
    BreathAnalyzerAppActivity activity = new AppActivity(); 
    activity.onCreate(null); 
    activity.onResume(); 

    activity.registerReceiver(new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context arg0, Intent intent) { 
      // Assert 
      assertThat(intent 
        .hasExtra(AppActivity.RECORDING_STARTED), 
        equalTo(true)); 
      Boolean expected = true; 
      Boolean actual = (Boolean)intent.getExtras().get(
        AppActivity.RECORDING_STARTED); 
      assertThat(actual, equalTo(expected)); 


     } 
    }, new IntentFilter(
      AppActivity.ACTION_RECORDING_STATUS_CHANGED)); 

    ImageButton recordButton = (ImageButton) activity 
      .findViewById(R.id.recordBtn); 

    // Act 
    recordButton.performClick(); 
    ShadowHandler.idleMainLooper(); 

} 
+2

실제로'BroadcastReceiver'에서'assert' 중 하나라도 호출 되나요? 나는 assertThat (intent.hasExtra (AppActivity.RECORDING_STARTED), equalTo (true));와 assertThat (intent.hasExtra (AppActivity.RECORDING_STARTED), equalTo (false));를 시도하고 두 테스트 모두 실패하지 않았다. 사례. 그래서, 내 추측은 그러한 assert 문이 실제로 호출되지 않는다는 것입니다. – iRuth

+0

아니요, 호출되지 않습니다. – zavidovych

0

이 원본에 대한 도움말하지만,하지 않을 수도 있습니다, 미래의 사람들 : 당신은이 상황에서 자신을 발견하는 일 경우 - 먼저 의도하지 않은 방송이 수신기에 의해 수신되지 않도록 당신의 상수와 인 텐트 필터는 별개 확인 . 나는 그 문제를 인정하기 위해 신경 쓰는 것보다 오랜 시간을 보냈습니다!

관련 문제