1/*
2 * Copyright (C) 2015 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
17package com.android.setupwizardlib.test;
18
19import static org.mockito.Matchers.eq;
20import static org.mockito.Mockito.verify;
21
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25import android.support.v7.widget.RecyclerView;
26import android.view.View;
27import android.view.ViewGroup;
28
29import com.android.setupwizardlib.view.HeaderRecyclerView.HeaderAdapter;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36
37/**
38 * Test for {@link com.android.setupwizardlib.view.HeaderRecyclerView}
39 */
40@RunWith(AndroidJUnit4.class)
41@SmallTest
42public class HeaderRecyclerViewTest {
43
44    private TestAdapter mWrappedAdapter;
45    private HeaderAdapter mHeaderAdapter;
46
47    @Mock
48    private RecyclerView.AdapterDataObserver mObserver;
49
50    @Before
51    public void setUp() {
52        MockitoAnnotations.initMocks(this);
53
54        mWrappedAdapter = new TestAdapter();
55
56        mHeaderAdapter = new HeaderAdapter(mWrappedAdapter);
57        mHeaderAdapter.registerAdapterDataObserver(mObserver);
58    }
59
60    /**
61     * Test that notifyDataSetChanged gets propagated by HeaderRecyclerView's adapter.
62     */
63    @Test
64    public void testNotifyChanged() {
65        mWrappedAdapter.notifyDataSetChanged();
66
67        verify(mObserver).onChanged();
68    }
69
70    /**
71     * Test that notifyItemChanged gets propagated by HeaderRecyclerView's adapter.
72     */
73    @Test
74    public void testNotifyItemChangedNoHeader() {
75        mWrappedAdapter.notifyItemChanged(12);
76
77        verify(mObserver).onItemRangeChanged(eq(12), eq(1), eq(null));
78    }
79
80    /**
81     * Test that notifyItemChanged gets propagated by HeaderRecyclerView's adapter and adds 1 to the
82     * position for the extra header items.
83     */
84    @Test
85    public void testNotifyItemChangedWithHeader() {
86        mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
87        mWrappedAdapter.notifyItemChanged(12);
88
89        verify(mObserver).onItemRangeChanged(eq(13), eq(1), eq(null));
90    }
91
92    /**
93     * Test that notifyItemInserted gets propagated by HeaderRecyclerView's adapter.
94     */
95    @Test
96    public void testNotifyItemInsertedNoHeader() {
97        mWrappedAdapter.notifyItemInserted(12);
98
99        verify(mObserver).onItemRangeInserted(eq(12), eq(1));
100    }
101
102    /**
103     * Test that notifyItemInserted gets propagated by HeaderRecyclerView's adapter and adds 1 to
104     * the position for the extra header item.
105     */
106    @Test
107    public void testNotifyItemInsertedWithHeader() {
108        mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
109        mWrappedAdapter.notifyItemInserted(12);
110
111        verify(mObserver).onItemRangeInserted(eq(13), eq(1));
112    }
113
114    /**
115     * Test that notifyItemRemoved gets propagated by HeaderRecyclerView's adapter.
116     */
117    @Test
118    public void testNotifyItemRemovedNoHeader() {
119        mWrappedAdapter.notifyItemRemoved(12);
120
121        verify(mObserver).onItemRangeRemoved(eq(12), eq(1));
122    }
123
124    /**
125     * Test that notifyItemRemoved gets propagated by HeaderRecyclerView's adapter and adds 1 to
126     * the position for the extra header item.
127     */
128    @Test
129    public void testNotifyItemRemovedWithHeader() {
130        mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
131        mWrappedAdapter.notifyItemRemoved(12);
132
133        verify(mObserver).onItemRangeRemoved(eq(13), eq(1));
134    }
135
136    /**
137     * Test that notifyItemMoved gets propagated by HeaderRecyclerView's adapter.
138     */
139    @Test
140    public void testNotifyItemMovedNoHeader() {
141        mWrappedAdapter.notifyItemMoved(12, 18);
142
143        verify(mObserver).onItemRangeMoved(eq(12), eq(18), eq(1));
144    }
145
146    /**
147     * Test that notifyItemMoved gets propagated by HeaderRecyclerView's adapter and adds 1 to
148     * the position for the extra header item.
149     */
150    @Test
151    public void testNotifyItemMovedWithHeader() {
152        mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
153        mWrappedAdapter.notifyItemMoved(12, 18);
154
155        verify(mObserver).onItemRangeMoved(eq(13), eq(19), eq(1));
156    }
157
158    /**
159     * Test adapter to be wrapped inside {@link HeaderAdapter} to to send item change notifications.
160     */
161    public static class TestAdapter extends RecyclerView.Adapter {
162
163        @Override
164        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
165            return null;
166        }
167
168        @Override
169        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
170        }
171
172        @Override
173        public int getItemCount() {
174            return 0;
175        }
176    }
177}
178