Skip to content

Commit aa63925

Browse files
committed
Added test cases for OnPageChangeListener
1 parent 3fe88e7 commit aa63925

11 files changed

Lines changed: 721 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test;
17+
18+
import org.androidannotations.annotations.EActivity;
19+
import org.androidannotations.annotations.PageScrollStateChanged;
20+
import org.androidannotations.annotations.PageScrolled;
21+
import org.androidannotations.annotations.PageSelected;
22+
23+
import android.app.Activity;
24+
import android.support.v4.view.ViewPager;
25+
26+
@EActivity(R.layout.viewpagers)
27+
public class PageChangeListenerActivity extends Activity {
28+
29+
public ViewPager viewPager = null;
30+
public int position = 0;
31+
public float positionOffset = 0f;
32+
public int positionOffsetPixels = 0;
33+
34+
public int state = 0;
35+
36+
@PageScrolled(R.id.viewPager1)
37+
public void pageScrolled(ViewPager v, int p, float pOffset, int pOffsetPixels) {
38+
viewPager = v;
39+
position = p;
40+
positionOffset = pOffset;
41+
positionOffsetPixels = pOffsetPixels;
42+
}
43+
44+
@PageScrollStateChanged(R.id.viewPager1)
45+
public void pageScrollStateChanged(ViewPager v, int s) {
46+
viewPager = v;
47+
state = s;
48+
}
49+
50+
@PageSelected(R.id.viewPager1)
51+
public void pageSelected(ViewPager v, int p) {
52+
viewPager = v;
53+
position = p;
54+
}
55+
56+
@PageSelected(R.id.viewPager2)
57+
public void pageSelected2() {
58+
}
59+
60+
@PageScrolled(R.id.viewPager2)
61+
public void pageScrolled2() {
62+
}
63+
64+
@PageScrollStateChanged(R.id.viewPager2)
65+
public void pageScrollStateChanged2() {
66+
}
67+
68+
@PageScrollStateChanged(R.id.viewPager3)
69+
public void pageScrollStateChanged3(int s, ViewPager v) {
70+
viewPager = v;
71+
state = s;
72+
}
73+
74+
@PageSelected(R.id.viewPager3)
75+
public void pageSelected3(int p, ViewPager v) {
76+
viewPager = v;
77+
position = p;
78+
}
79+
80+
@PageScrolled(R.id.viewPager3)
81+
public void pageScrolled3(int p) {
82+
position = p;
83+
}
84+
85+
@PageScrolled(R.id.viewPager4)
86+
public void pageScrolled4(float f, int p) {
87+
positionOffset = f;
88+
positionOffsetPixels = p;
89+
}
90+
91+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2015 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="fill_parent"
21+
android:layout_height="fill_parent"
22+
android:orientation="vertical">
23+
24+
<android.support.v4.view.ViewPager
25+
android:id="@+id/viewPager1"
26+
android:layout_width="fill_parent"
27+
android:layout_height="wrap_content"/>
28+
29+
<android.support.v4.view.ViewPager
30+
android:id="@+id/viewPager2"
31+
android:layout_width="fill_parent"
32+
android:layout_height="wrap_content"/>
33+
34+
<android.support.v4.view.ViewPager
35+
android:id="@+id/viewPager3"
36+
android:layout_width="fill_parent"
37+
android:layout_height="wrap_content"/>
38+
39+
<android.support.v4.view.ViewPager
40+
android:id="@+id/viewPager4"
41+
android:layout_width="fill_parent"
42+
android:layout_height="wrap_content"/>
43+
44+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.test;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
import static org.fest.reflect.core.Reflection.field;
20+
import static org.robolectric.Robolectric.setupActivity;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.robolectric.RobolectricTestRunner;
26+
27+
import android.support.v4.view.ViewPager;
28+
29+
@RunWith(RobolectricTestRunner.class)
30+
public class PageChangeListenerActivityTest {
31+
private PageChangeListenerActivity_ activity;
32+
private ViewPager viewPager;
33+
private ViewPager.OnPageChangeListener listener;
34+
35+
@Before
36+
public void setUp() {
37+
activity = setupActivity(PageChangeListenerActivity_.class);
38+
}
39+
40+
private void initViewPager(int viewPagerId) {
41+
viewPager = (ViewPager) activity.findViewById(viewPagerId);
42+
listener = field("mOnPageChangeListener").ofType(ViewPager.OnPageChangeListener.class).in(viewPager).get();
43+
}
44+
45+
private void assertReadyForTest() {
46+
assertViewPagerIsNull();
47+
assertPositionIsNotSet();
48+
assertPositionOffsetIsNotSet();
49+
assertPositionOffsetPixelsIsNotSet();
50+
assertStateIsNotSet();
51+
}
52+
53+
private void assertViewPagerIsNull() {
54+
assertThat(activity.viewPager).isNull();
55+
}
56+
57+
private void assertPositionIsNotSet() {
58+
assertThat(activity.position).isEqualTo(0);
59+
}
60+
61+
private void assertPositionOffsetPixelsIsNotSet() {
62+
assertThat(activity.positionOffsetPixels).isEqualTo(0);
63+
}
64+
65+
private void assertPositionOffsetIsNotSet() {
66+
assertThat(activity.positionOffset).isEqualTo(0f);
67+
}
68+
69+
private void assertStateIsNotSet() {
70+
assertThat(activity.state).isEqualTo(0);
71+
}
72+
73+
@Test
74+
public void handlePageSelected() {
75+
initViewPager(R.id.viewPager1);
76+
77+
assertViewPagerIsNull();
78+
assertPositionIsNotSet();
79+
80+
int position = 100;
81+
82+
listener.onPageSelected(position);
83+
84+
assertThat(activity.viewPager).isEqualTo(viewPager);
85+
assertThat(activity.position).isEqualTo(position);
86+
}
87+
88+
@Test
89+
public void handlePageScrolled() {
90+
initViewPager(R.id.viewPager1);
91+
assertReadyForTest();
92+
93+
int position = 101;
94+
float positionOffset = 102f;
95+
int positionOffsetPixels = 103;
96+
97+
listener.onPageScrolled(position, positionOffset, positionOffsetPixels);
98+
99+
assertThat(activity.viewPager).isEqualTo(viewPager);
100+
assertThat(activity.position).isEqualTo(position);
101+
assertThat(activity.positionOffset).isEqualTo(positionOffset);
102+
assertThat(activity.positionOffsetPixels).isEqualTo(positionOffsetPixels);
103+
}
104+
105+
@Test
106+
public void handlePageScrollStateChanged() {
107+
initViewPager(R.id.viewPager1);
108+
assertReadyForTest();
109+
110+
int state = 104;
111+
112+
listener.onPageScrollStateChanged(state);
113+
114+
assertThat(activity.viewPager).isEqualTo(viewPager);
115+
assertThat(activity.state).isEqualTo(state);
116+
}
117+
118+
@Test
119+
public void handleNoPageSelectedParameter() {
120+
initViewPager(R.id.viewPager2);
121+
assertReadyForTest();
122+
123+
listener.onPageSelected(1);
124+
125+
assertViewPagerIsNull();
126+
assertPositionIsNotSet();
127+
}
128+
129+
@Test
130+
public void handleNoPageScrolledParameter() {
131+
initViewPager(R.id.viewPager2);
132+
assertReadyForTest();
133+
134+
listener.onPageScrolled(1, 1f, 2);
135+
136+
assertViewPagerIsNull();
137+
assertPositionIsNotSet();
138+
assertPositionOffsetIsNotSet();
139+
assertPositionOffsetPixelsIsNotSet();
140+
}
141+
142+
@Test
143+
public void handleNoPageScrollStateChangedParameter() {
144+
initViewPager(R.id.viewPager2);
145+
assertReadyForTest();
146+
147+
listener.onPageScrollStateChanged(1);
148+
149+
assertViewPagerIsNull();
150+
assertStateIsNotSet();
151+
}
152+
153+
@Test
154+
public void handleAnyOrderPageSelectedParameter() {
155+
initViewPager(R.id.viewPager3);
156+
assertReadyForTest();
157+
158+
int position = 105;
159+
160+
listener.onPageSelected(position);
161+
162+
assertThat(activity.viewPager).isEqualTo(viewPager);
163+
assertThat(activity.position).isEqualTo(position);
164+
}
165+
166+
@Test
167+
public void handleAnyOrderPageScrollStateChangedParameter() {
168+
initViewPager(R.id.viewPager3);
169+
assertReadyForTest();
170+
171+
int state = 106;
172+
173+
listener.onPageScrollStateChanged(state);
174+
175+
assertThat(activity.viewPager).isEqualTo(viewPager);
176+
assertThat(activity.state).isEqualTo(state);
177+
}
178+
179+
@Test
180+
public void handlePageScrollFirstIntBecomesPositionParameter() {
181+
initViewPager(R.id.viewPager3);
182+
assertReadyForTest();
183+
184+
int position = 107;
185+
186+
listener.onPageScrolled(position, 108f, 109);
187+
188+
assertViewPagerIsNull();
189+
assertThat(activity.position).isEqualTo(position);
190+
assertPositionOffsetIsNotSet();
191+
assertPositionOffsetPixelsIsNotSet();
192+
}
193+
194+
@Test
195+
public void handleIntAfterPositionOffsetBecomesPositionOffsetPixels() {
196+
initViewPager(R.id.viewPager4);
197+
assertReadyForTest();
198+
199+
float positionOffset = 111f;
200+
int positionOffsetPixels = 112;
201+
202+
listener.onPageScrolled(110, positionOffset, positionOffsetPixels);
203+
204+
assertViewPagerIsNull();
205+
assertPositionIsNotSet();
206+
assertThat(activity.positionOffset).isEqualTo(positionOffset);
207+
assertThat(activity.positionOffsetPixels).isEqualTo(positionOffsetPixels);
208+
}
209+
210+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* 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, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package android.support.v4.view;
17+
18+
import android.content.Context;
19+
import android.util.AttributeSet;
20+
import android.view.View;
21+
22+
public class ViewPager extends View {
23+
public ViewPager(Context context) {
24+
super(context);
25+
}
26+
27+
public ViewPager(Context context, AttributeSet attrs) {
28+
super(context, attrs);
29+
}
30+
31+
public ViewPager(Context context, AttributeSet attrs, int defStyle) {
32+
super(context, attrs, defStyle);
33+
}
34+
35+
public void addOnPageChangeListener(OnPageChangeListener listener) {
36+
37+
}
38+
39+
public interface OnPageChangeListener {
40+
void onPageScrollStateChanged(int state);
41+
42+
void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
43+
44+
void onPageSelected(int position);
45+
}
46+
}

0 commit comments

Comments
 (0)