3

I have the following problem. I'm making unit-tests for a custom DialogFragment. This dialog has a Scrollview. when reaching the end of which, a ViewSwitcher has to switch a view.

Here is the scroll listener:

ViewSwitcher vs = getDialog().findViewById(R.id.vs);
scrollView.getViewTreeObserver()
     .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
      @Override
      public void onScrollChanged() {
           if (scrollView.getChildAt(0).getBottom()
                <= (scrollView.getHeight() + scrollView.getScrollY())) {
                vs.setDisplayedChild(1);
            } else {
                vs.setDisplayedChild(0);
            }
       }
 });

A test setup method:

@Before
public void setUp() throws Exception {
    activity = Robolectric
        .buildActivity(FragmentTestActivity.class)
        .create()
        .resume()
        .get();

    subject = MemberConsentModal.newInstance("long text");
    subject.show(activity.getSupportFragmentManager(), MemberConsentModal.TAG);
}

And the test method code:

@Test
public void onScrolledToTopBottom_showOptionButtons() {
    final ScrollView scrollView = subject.getDialog().findViewById(R.id.member_consent_content);
    ViewSwitcher vs = subject.getDialog().findViewById(R.id.vs);

    scrollView.fullScroll(ScrollView.FOCUS_DOWN);

    assertThat(vs.getDisplayedChild()).isEqualTo(1);
}

When debugging, I see that addOnScrollChangedListener is never called, though, when launching the application as on an emulator, as on a physical device, it is working correctly. How can I make the scroll view to be scrolled in testing mode?

PS. I'm using Robolectric and Mockito for unit-testing.

Ajay Mehta
  • 843
  • 5
  • 14
caesar-84
  • 35
  • 6

0 Answers0