1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.database.DataSetObserver;
4 import android.support.v4.view.PagerAdapter;
5 import android.view.View;
6 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 
10 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
11 import static junit.framework.Assert.assertTrue;
12 
13 @RunWith(WithTestDefaultsRunner.class)
14 public class PagerAdapterTest {
15     @Test
shouldNotifyDataSetChanged()16     public void shouldNotifyDataSetChanged() throws Exception {
17         PagerAdapter pagerAdapter = new NullPagerAdapter();
18         TestDataSetObserver datasetObserver= new TestDataSetObserver();
19         shadowOf(pagerAdapter).registerDataSetObserver(datasetObserver);
20         pagerAdapter.notifyDataSetChanged();
21 
22         assertTrue(datasetObserver.onChangedWasCalled);
23     }
24 
25     private static class NullPagerAdapter extends PagerAdapter {
26         @Override
getCount()27         public int getCount() {
28             return 0;
29         }
30 
31         @Override
isViewFromObject(View view, Object object)32         public boolean isViewFromObject(View view, Object object) {
33             return false;
34         }
35     }
36 
37     private class TestDataSetObserver extends DataSetObserver {
38         boolean onChangedWasCalled;
39         @Override
onChanged()40         public void onChanged() {
41             onChangedWasCalled = true;
42         }
43     }
44 }
45