1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.viewpager.widget;
18 
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.support.test.InstrumentationRegistry;
25 import android.support.test.filters.MediumTest;
26 import android.support.test.rule.ActivityTestRule;
27 import android.view.View;
28 import android.view.ViewGroup;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 
33 import org.junit.Rule;
34 import org.junit.Test;
35 
36 @MediumTest
37 public final class ViewPagerTest {
38     @Rule
39     public final ActivityTestRule<ViewPagerActivity> activityRule = new ActivityTestRule<>(
40             ViewPagerActivity.class);
41 
42     public static final class ViewPagerActivity extends Activity {
43         public ViewPager pager;
44 
45         @Override
onCreate(@ullable Bundle savedInstanceState)46         protected void onCreate(@Nullable Bundle savedInstanceState) {
47             super.onCreate(savedInstanceState);
48             pager = new ViewPager(this);
49             setContentView(pager);
50         }
51     }
52 
53     @Test
setPrimaryItemNotCalledWhenAdapterIsEmpty()54     public void setPrimaryItemNotCalledWhenAdapterIsEmpty() {
55         ViewPager pager = activityRule.getActivity().pager;
56         final PrimaryItemPagerAdapter adapter = new PrimaryItemPagerAdapter();
57         pager.setAdapter(adapter);
58 
59         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
60         assertFalse(adapter.primaryCalled);
61 
62         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
63             @Override
64             public void run() {
65                 adapter.count = 1;
66                 adapter.notifyDataSetChanged();
67             }
68         });
69 
70         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
71         assertTrue(adapter.primaryCalled);
72     }
73 
74     static final class PrimaryItemPagerAdapter extends PagerAdapter {
75         public volatile int count;
76         public volatile boolean primaryCalled;
77 
78         @Override
getCount()79         public int getCount() {
80             return count;
81         }
82 
83         @Override
setPrimaryItem(@onNull ViewGroup container, int position, @NonNull Object object)84         public void setPrimaryItem(@NonNull ViewGroup container, int position,
85                 @NonNull Object object) {
86             primaryCalled = true;
87         }
88 
89         @Override
isViewFromObject(@onNull View view, @NonNull Object object)90         public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
91             return view == object;
92         }
93 
94         @NonNull
95         @Override
instantiateItem(@onNull ViewGroup container, int position)96         public Object instantiateItem(@NonNull ViewGroup container, int position) {
97             View view = new View(container.getContext());
98             container.addView(view);
99             return view;
100         }
101 
102         @Override
destroyItem(@onNull ViewGroup container, int position, @NonNull Object object)103         public void destroyItem(@NonNull ViewGroup container, int position,
104                 @NonNull Object object) {
105             container.removeView((View) object);
106         }
107     }
108 }
109