title: Unit Testing Android app that relies on Robolectric / RxJava / RxAndroid
body: <p>I am using the Robolectric 3.0 snapshot.</p>
  
  <p>I have a test:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Test
  public void my_test() throws Exception {
          when(testReferenceManager.getUserServiceMock().checkUsernameAvailability(anyString())).thenReturn(Observable.just(Arrays.asList(new User("test@email.com", "password"))));
          when(testReferenceManager.getUserServiceMock().checkAuthStatus(anyString())).thenReturn(Observable.just(Arrays.asList(new User("test@email.com", "password"))));
  
          EditText emailText = (EditText)activity.findViewById(R.id.text_email);
          EditText passwordText = (EditText)activity.findViewById(R.id.text_password);
          Button signInButton = (Button)activity.findViewById(R.id.sign_in_button);
  
  
          emailText.setText("test@email.com");
          passwordText.setText("password");
  
          Robolectric.flushBackgroundScheduler();
          Robolectric.flushForegroundScheduler();    
  
          assertThat(signInButton.getText()).isEqualTo(App.R.getString(R.string.button_login));
  }
  </code></pre>
  
  <p>The key thing here is that if the API reports a user exists (which it does from the mocks above) the sign in button text should be the same as the value of the string resource named <code>R.string.button_login</code>.</p>
  
  <p>The setup for changing the button state is done in my Activity like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>        ReactiveEditText.textObservableForTextView(emailText)
              .startWith(emailText.getText().toString())
              .switchMap(new Func1&lt;String, Observable&lt;Boolean&gt;&gt;() {
                  @Override
                  public Observable&lt;Boolean&gt; call(String username) {
                      return webServices.usernameAvailable(username);
                  }
              })
              .subscribe(new EndlessObserver&lt;Boolean&gt;() {
                  @Override
                  public void onNext(Boolean available) {
                      if (available) {
                          signInButton.setText(getString(R.string.button_signup));
                      } else {
                          signInButton.setText(getString(R.string.button_login));
                      }
                  }
              });
  </code></pre>
  
  <p>ReactiveEditText.textObservableForTextView simply wraps the textChangedListener interface in a reactive fashion:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static Observable&lt;String&gt; textObservableForTextView(final TextView tv) {
          Observable&lt;String&gt; textObservable = Observable.create(new Observable.OnSubscribe&lt;String&gt;() {
              @Override
              public void call(final Subscriber&lt;? super String&gt; subscriber) {
                  tv.addTextChangedListener(new TextWatcher() {
                      @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                      @Override public void afterTextChanged(Editable s) { }
  
                      @Override
                      public void onTextChanged(CharSequence s, int start, int before, int count) {
                          subscriber.onNext(s.toString());
                      }
                  });
              }
          });
  
          return ViewObservable.bindView(tv, textObservable);
  }
  </code></pre>
  
  <p>usernameAvailable receives the mocked data above and looks like this:</p>
  
  <pre><code>    // If the incoming array is size 0 the username is available, otherwise it already exists. 
  
  public Observable&lt;Boolean&gt; usernameAvailable(final String username) {
      return userService.checkUsernameAvailability(username)
              .map(new Func1&lt;List&lt;User&gt;, Boolean&gt;() {
                  @Override
                  public Boolean call(List&lt;User&gt; usersMatchingUsername) {
                      return usersMatchingUsername.size() == 0;
                  }
              });
  }
  </code></pre>
  
  <p>Notice that I am using the 'immediate' scheduler to listen for changes to the EditText. My confusion is this: the unit test always fails, and it seems to me (when I jump into the debugger) that the <code>assertThat</code> statement in the test is firing before the observer ever sees a change in the EditText. I do see in the debugger that the Observer does eventually fire and the text is properly set on the signInButton. I thought using the immediate scheduler would make everything happen as I expect directly after <code>setText</code> is called.</p>