1 package com.xtremelabs.robolectric.shadows; 2 3 import android.app.Activity; 4 import android.support.v4.view.PagerAdapter; 5 import android.support.v4.view.ViewPager; 6 import android.view.View; 7 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import static junit.framework.Assert.*; 13 14 @RunWith(WithTestDefaultsRunner.class) 15 public class ViewPagerTest { 16 17 private ViewPager pager; 18 private TestPagerAdapter adapter; 19 20 @Before setUp()21 public void setUp() throws Exception { 22 pager = new ViewPager(new Activity()); 23 adapter = new TestPagerAdapter(); 24 } 25 26 @Test shouldSetAndGetAdapter()27 public void shouldSetAndGetAdapter() throws Exception { 28 assertNull(pager.getAdapter()); 29 30 pager.setAdapter(adapter); 31 assertSame(adapter, pager.getAdapter()); 32 } 33 34 @Test test_getAndSetCurrentItem()35 public void test_getAndSetCurrentItem() throws Exception { 36 pager.setCurrentItem(2); 37 assertEquals(2, pager.getCurrentItem()); 38 } 39 40 @Test setCurrentItem_shouldInvokeListener()41 public void setCurrentItem_shouldInvokeListener() throws Exception { 42 TestOnPageChangeListener listener = new TestOnPageChangeListener(); 43 pager.setOnPageChangeListener(listener); 44 assertFalse(listener.onPageSelectedCalled); 45 pager.setCurrentItem(2); 46 assertTrue(listener.onPageSelectedCalled); 47 } 48 49 private static class TestPagerAdapter extends PagerAdapter { 50 @Override getCount()51 public int getCount() { 52 return 0; 53 } 54 55 @Override isViewFromObject(View view, Object object)56 public boolean isViewFromObject(View view, Object object) { 57 return false; 58 } 59 } 60 61 private static class TestOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener { 62 public boolean onPageSelectedCalled; 63 64 @Override onPageSelected(int position)65 public void onPageSelected(int position) { 66 onPageSelectedCalled = true; 67 } 68 } 69 } 70