• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package android.support.v7.util;
17 
18 import android.test.suitebuilder.annotation.SmallTest;
19 
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 import static org.mockito.Mockito.*;
25 
26 @RunWith(JUnit4.class)
27 @SmallTest
28 public class BatchingListUpdateCallbackTest {
29     BatchingListUpdateCallback mBatching;
30     ListUpdateCallback mCallback;
31 
32     @Before
setup()33     public void setup() {
34         mCallback = mock(ListUpdateCallback.class);
35         mBatching = new BatchingListUpdateCallback(mCallback);
36     }
37 
38     @Test
addSimple()39     public void addSimple() {
40         mBatching.onInserted(3, 2);
41         mBatching.dispatchLastEvent();
42         verify(mCallback).onInserted(3, 2);
43         verifyNoMoreInteractions(mCallback);
44     }
45 
46     @Test
addToSamePos()47     public void addToSamePos() {
48         mBatching.onInserted(3, 2);
49         mBatching.onInserted(3, 1);
50         mBatching.dispatchLastEvent();
51         verify(mCallback).onInserted(3, 3);
52         verifyNoMoreInteractions(mCallback);
53     }
54 
55     @Test
addInsidePrevious()56     public void addInsidePrevious() {
57         mBatching.onInserted(3, 5);
58         mBatching.onInserted(5, 1);
59         mBatching.dispatchLastEvent();
60         verify(mCallback).onInserted(3, 6);
61         verifyNoMoreInteractions(mCallback);
62     }
63 
64     @Test
addBefore()65     public void addBefore() {
66         mBatching.onInserted(3, 5);
67         mBatching.onInserted(2, 1);
68         mBatching.dispatchLastEvent();
69         verify(mCallback).onInserted(3, 5);
70         verify(mCallback).onInserted(2, 1);
71         verifyNoMoreInteractions(mCallback);
72     }
73 
74     @Test
removeSimple()75     public void removeSimple() {
76         mBatching.onRemoved(3, 2);
77         mBatching.dispatchLastEvent();
78         verify(mCallback).onRemoved(3, 2);
79         verifyNoMoreInteractions(mCallback);
80     }
81 
82     @Test
removeSamePosition()83     public void removeSamePosition() {
84         mBatching.onRemoved(3, 2);
85         mBatching.onRemoved(3, 1);
86         mBatching.dispatchLastEvent();
87         verify(mCallback).onRemoved(3, 3);
88         verifyNoMoreInteractions(mCallback);
89     }
90 
91     @Test
removeInside()92     public void removeInside() {
93         mBatching.onRemoved(3, 5);
94         mBatching.onRemoved(4, 2);
95         mBatching.dispatchLastEvent();
96         verify(mCallback).onRemoved(3, 5);
97         verify(mCallback).onRemoved(4, 2);
98         verifyNoMoreInteractions(mCallback);
99     }
100 
101     @Test
removeBefore()102     public void removeBefore() {
103         mBatching.onRemoved(3, 2);
104         mBatching.onRemoved(2, 1);
105         mBatching.dispatchLastEvent();
106         verify(mCallback).onRemoved(2, 3);
107         verifyNoMoreInteractions(mCallback);
108     }
109 
110     @Test
removeBefore2()111     public void removeBefore2() {
112         mBatching.onRemoved(3, 2);
113         mBatching.onRemoved(2, 4);
114         mBatching.dispatchLastEvent();
115         verify(mCallback).onRemoved(2, 6);
116         verifyNoMoreInteractions(mCallback);
117     }
118 
119     @Test
removeBefore3()120     public void removeBefore3() {
121         mBatching.onRemoved(3, 2);
122         mBatching.onRemoved(1, 1);
123         mBatching.dispatchLastEvent();
124         verify(mCallback).onRemoved(3, 2);
125         verify(mCallback).onRemoved(1, 1);
126         verifyNoMoreInteractions(mCallback);
127     }
128 
129     @Test
moveSimple()130     public void moveSimple() {
131         mBatching.onMoved(3, 2);
132         mBatching.dispatchLastEvent();
133         verify(mCallback).onMoved(3, 2);
134         verifyNoMoreInteractions(mCallback);
135     }
136 
137     @Test
moveTwice()138     public void moveTwice() {
139         mBatching.onMoved(3, 2);
140         mBatching.onMoved(5, 6);
141         mBatching.dispatchLastEvent();
142         verify(mCallback).onMoved(3, 2);
143         verify(mCallback).onMoved(5, 6);
144         verifyNoMoreInteractions(mCallback);
145     }
146 
147     @Test
changeSimple()148     public void changeSimple() {
149         mBatching.onChanged(3, 2, null);
150         mBatching.dispatchLastEvent();
151         verify(mCallback).onChanged(3, 2, null);
152         verifyNoMoreInteractions(mCallback);
153     }
154 
155     @Test
changeConsecutive()156     public void changeConsecutive() {
157         mBatching.onChanged(3, 2, null);
158         mBatching.onChanged(5, 2, null);
159         mBatching.dispatchLastEvent();
160         verify(mCallback).onChanged(3, 4, null);
161         verifyNoMoreInteractions(mCallback);
162     }
163 
164     @Test
changeTheSame()165     public void changeTheSame() {
166         mBatching.onChanged(3, 2, null);
167         mBatching.onChanged(4, 2, null);
168         mBatching.dispatchLastEvent();
169         verify(mCallback).onChanged(3, 3, null);
170         verifyNoMoreInteractions(mCallback);
171     }
172 
173     @Test
changeTheSame2()174     public void changeTheSame2() {
175         mBatching.onChanged(3, 2, null);
176         mBatching.onChanged(3, 2, null);
177         mBatching.dispatchLastEvent();
178         verify(mCallback).onChanged(3, 2, null);
179         verifyNoMoreInteractions(mCallback);
180     }
181 
182     @Test
changeBefore()183     public void changeBefore() {
184         mBatching.onChanged(3, 2, null);
185         mBatching.onChanged(2, 1, null);
186         mBatching.dispatchLastEvent();
187         verify(mCallback).onChanged(2, 3, null);
188         verifyNoMoreInteractions(mCallback);
189     }
190 
191     @Test
changeBeforeOverlap()192     public void changeBeforeOverlap() {
193         mBatching.onChanged(3, 2, null);
194         mBatching.onChanged(2, 2, null);
195         mBatching.dispatchLastEvent();
196         verify(mCallback).onChanged(2, 3, null);
197         verifyNoMoreInteractions(mCallback);
198     }
199 
200     @Test
changeSimpleWithPayload()201     public void changeSimpleWithPayload() {
202         Object payload = new Object();
203         mBatching.onChanged(3, 2, payload);
204         mBatching.dispatchLastEvent();
205         verify(mCallback).onChanged(3, 2, payload);
206     }
207 
208     @Test
changeConsecutiveWithPayload()209     public void changeConsecutiveWithPayload() {
210         Object payload = new Object();
211         mBatching.onChanged(3, 2, payload);
212         mBatching.onChanged(5, 2, payload);
213         mBatching.dispatchLastEvent();
214         verify(mCallback).onChanged(3, 4, payload);
215         verifyNoMoreInteractions(mCallback);
216     }
217 
218     @Test
changeTheSameWithPayload()219     public void changeTheSameWithPayload() {
220         Object payload = new Object();
221         mBatching.onChanged(3, 2, payload);
222         mBatching.onChanged(4, 2, payload);
223         mBatching.dispatchLastEvent();
224         verify(mCallback).onChanged(3, 3, payload);
225         verifyNoMoreInteractions(mCallback);
226     }
227 
228     @Test
changeTheSame2WithPayload()229     public void changeTheSame2WithPayload() {
230         Object payload = new Object();
231         mBatching.onChanged(3, 2, payload);
232         mBatching.onChanged(3, 2, payload);
233         mBatching.dispatchLastEvent();
234         verify(mCallback).onChanged(3, 2, payload);
235         verifyNoMoreInteractions(mCallback);
236     }
237 
238     @Test
changeBeforeWithPayload()239     public void changeBeforeWithPayload() {
240         Object payload = new Object();
241         mBatching.onChanged(3, 2, payload);
242         mBatching.onChanged(2, 1, payload);
243         mBatching.dispatchLastEvent();
244         verify(mCallback).onChanged(2, 3, payload);
245         verifyNoMoreInteractions(mCallback);
246     }
247 
248     @Test
changeBeforeOverlapWithPayload()249     public void changeBeforeOverlapWithPayload() {
250         Object payload = new Object();
251         mBatching.onChanged(3, 2, payload);
252         mBatching.onChanged(2, 2, payload);
253         mBatching.dispatchLastEvent();
254         verify(mCallback).onChanged(2, 3, payload);
255         verifyNoMoreInteractions(mCallback);
256     }
257 
258     @Test
changeWithNewPayload()259     public void changeWithNewPayload() {
260         Object payload1 = new Object();
261         Object payload2 = new Object();
262         mBatching.onChanged(3, 2, payload1);
263         mBatching.onChanged(2, 2, payload2);
264         mBatching.dispatchLastEvent();
265         verify(mCallback).onChanged(3, 2, payload1);
266         verify(mCallback).onChanged(2, 2, payload2);
267         verifyNoMoreInteractions(mCallback);
268     }
269 
270     @Test
changeWithEmptyPayload()271     public void changeWithEmptyPayload() {
272         Object payload = new Object();
273         mBatching.onChanged(3, 2, payload);
274         mBatching.onChanged(2, 2, null);
275         mBatching.dispatchLastEvent();
276         verify(mCallback).onChanged(3, 2, payload);
277         verify(mCallback).onChanged(2, 2, null);
278         verifyNoMoreInteractions(mCallback);
279     }
280 
281     @Test
changeWithEmptyPayload2()282     public void changeWithEmptyPayload2() {
283         Object payload = new Object();
284         mBatching.onChanged(3, 2, null);
285         mBatching.onChanged(2, 2, payload);
286         mBatching.dispatchLastEvent();
287         verify(mCallback).onChanged(3, 2, null);
288         verify(mCallback).onChanged(2, 2, payload);
289         verifyNoMoreInteractions(mCallback);
290     }
291 }
292