summaryrefslogtreecommitdiff
path: root/android/support/v7/recyclerview/extensions/ListAdapterHelperTest.java
blob: aab7417265537e7bbf18a0619f1700e2408aaf86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.v7.recyclerview.extensions;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;

import android.arch.paging.TestExecutor;
import android.support.annotation.NonNull;
import android.support.test.filters.SmallTest;
import android.support.v7.util.ListUpdateCallback;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Arrays;

@SmallTest
@RunWith(JUnit4.class)
public class ListAdapterHelperTest {
    private TestExecutor mMainThread = new TestExecutor();
    private TestExecutor mBackgroundThread = new TestExecutor();


    private static final DiffCallback<String> STRING_DIFF_CALLBACK = new DiffCallback<String>() {
        @Override
        public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
            return oldItem.equals(newItem);
        }

        @Override
        public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
            return oldItem.equals(newItem);
        }
    };

    private static final ListUpdateCallback IGNORE_CALLBACK = new ListUpdateCallback() {
        @Override
        public void onInserted(int position, int count) {
        }

        @Override
        public void onRemoved(int position, int count) {
        }

        @Override
        public void onMoved(int fromPosition, int toPosition) {
        }

        @Override
        public void onChanged(int position, int count, Object payload) {
        }
    };


    private <T> ListAdapterHelper<T> createHelper(
            ListUpdateCallback listUpdateCallback, DiffCallback<T> diffCallback) {
        return new ListAdapterHelper<T>(listUpdateCallback,
                new ListAdapterConfig.Builder<T>()
                        .setDiffCallback(diffCallback)
                        .setMainThreadExecutor(mMainThread)
                        .setBackgroundThreadExecutor(mBackgroundThread)
                        .build());
    }

    @Test
    public void initialState() {
        ListUpdateCallback callback = mock(ListUpdateCallback.class);
        ListAdapterHelper<String> helper = createHelper(callback, STRING_DIFF_CALLBACK);
        assertEquals(0, helper.getItemCount());
        verifyZeroInteractions(callback);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void getEmpty() {
        ListAdapterHelper<String> helper = createHelper(IGNORE_CALLBACK, STRING_DIFF_CALLBACK);
        helper.getItem(0);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void getNegative() {
        ListAdapterHelper<String> helper = createHelper(IGNORE_CALLBACK, STRING_DIFF_CALLBACK);
        helper.setList(Arrays.asList("a", "b"));
        helper.getItem(-1);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void getPastEnd() {
        ListAdapterHelper<String> helper = createHelper(IGNORE_CALLBACK, STRING_DIFF_CALLBACK);
        helper.setList(Arrays.asList("a", "b"));
        helper.getItem(2);
    }

    @Test
    public void setListSimple() {
        ListUpdateCallback callback = mock(ListUpdateCallback.class);
        ListAdapterHelper<String> helper = createHelper(callback, STRING_DIFF_CALLBACK);

        helper.setList(Arrays.asList("a", "b"));

        assertEquals(2, helper.getItemCount());
        assertEquals("a", helper.getItem(0));
        assertEquals("b", helper.getItem(1));

        verify(callback).onInserted(0, 2);
        verifyNoMoreInteractions(callback);
        drain();
        verifyNoMoreInteractions(callback);
    }

    @Test
    public void setListUpdate() {
        ListUpdateCallback callback = mock(ListUpdateCallback.class);
        ListAdapterHelper<String> helper = createHelper(callback, STRING_DIFF_CALLBACK);

        // initial list (immediate)
        helper.setList(Arrays.asList("a", "b"));
        verify(callback).onInserted(0, 2);
        verifyNoMoreInteractions(callback);
        drain();
        verifyNoMoreInteractions(callback);

        // update (deferred)
        helper.setList(Arrays.asList("a", "b", "c"));
        verifyNoMoreInteractions(callback);
        drain();
        verify(callback).onInserted(2, 1);
        verifyNoMoreInteractions(callback);

        // clear (immediate)
        helper.setList(null);
        verify(callback).onRemoved(0, 3);
        verifyNoMoreInteractions(callback);
        drain();
        verifyNoMoreInteractions(callback);

    }

    private void drain() {
        boolean executed;
        do {
            executed = mBackgroundThread.executeAll();
            executed |= mMainThread.executeAll();
        } while (executed);
    }
}