summaryrefslogtreecommitdiff
path: root/tests/src/com/android/dialer/calllog/GroupingListAdapterTests.java
blob: 4d8cb9cc09273db04195ace2095c78979147c3ed (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
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2015 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 com.android.dialer.calllog;

import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.support.v7.widget.RecyclerView;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

/**
 * Tests for {@link GroupingListAdapter}.
 *
 * Running all tests:
 *
 *   adb shell am instrument -e class com.android.dialer.calllog.GroupingListAdapterTests \
 *     -w com.android.dialer.tests/android.test.InstrumentationTestRunner
 */
@MediumTest
public class GroupingListAdapterTests extends AndroidTestCase {

    static private final String[] PROJECTION = new String[] {
        "_id",
        "group",
    };

    private static final int GROUPING_COLUMN_INDEX = 1;

    private MatrixCursor mCursor;
    private long mNextId;

    private GroupingListAdapter mAdapter = new GroupingListAdapter(null) {

        @Override
        protected void addGroups(Cursor cursor) {
            int count = cursor.getCount();
            int groupItemCount = 1;
            cursor.moveToFirst();
            String currentValue = cursor.getString(GROUPING_COLUMN_INDEX);
            for (int i = 1; i < count; i++) {
                cursor.moveToNext();
                String value = cursor.getString(GROUPING_COLUMN_INDEX);
                if (TextUtils.equals(value, currentValue)) {
                    groupItemCount++;
                } else {
                    addGroup(i - groupItemCount, groupItemCount);
                    groupItemCount = 1;
                    currentValue = value;
                }
            }
            addGroup(count - groupItemCount, groupItemCount);
        }

        @Override
        protected void addVoicemailGroups(Cursor c) {
            // Do nothing.
        }

        @Override
        public void onContentChanged() {
            // Do nothing.
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            // Do nothing.
        }
    };

    private void buildCursor(String... numbers) {
        mCursor = new MatrixCursor(PROJECTION);
        mNextId = 1;
        for (String number : numbers) {
            mCursor.addRow(new Object[]{mNextId, number});
            mNextId++;
        }
    }

    public void testGroupingWithoutGroups() {
        buildCursor("1", "2", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getItemCount());
        assertMetadata(0, 1, "1");
        assertMetadata(1, 1, "2");
        assertMetadata(2, 1, "3");
    }

    public void testGroupingWithGroupAtTheBeginning() {
        buildCursor("1", "1", "2");
        mAdapter.changeCursor(mCursor);

        assertEquals(2, mAdapter.getItemCount());
        assertMetadata(0, 2, "1");
        assertMetadata(1, 1, "2");
    }

    public void testGroupingWithGroupInTheMiddle() {
        buildCursor("1", "2", "2", "2", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getItemCount());
        assertMetadata(0, 1, "1");
        assertMetadata(1, 3, "2");
        assertMetadata(2, 1, "3");
    }

    public void testGroupingWithGroupAtTheEnd() {
        buildCursor("1", "2", "3", "3", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getItemCount());
        assertMetadata(0, 1, "1");
        assertMetadata(1, 1, "2");
        assertMetadata(2, 3, "3");
    }

    public void testGroupingWithMultipleGroups() {
        buildCursor("1", "2", "2", "3", "4", "4", "5", "5", "6");
        mAdapter.changeCursor(mCursor);

        assertEquals(6, mAdapter.getItemCount());
        assertMetadata(0, 1, "1");
        assertMetadata(1, 2, "2");
        assertMetadata(2, 1, "3");
        assertMetadata(3, 2, "4");
        assertMetadata(4, 2, "5");
        assertMetadata(5, 1, "6");
    }

    public void testGroupDescriptorArrayGrowth() {
        String[] numbers = new String[500];
        for (int i = 0; i < numbers.length; i++) {

            // Make groups of 2
            numbers[i] = String.valueOf((i / 2) * 2);
        }

        buildCursor(numbers);
        mAdapter.changeCursor(mCursor);

        assertEquals(250, mAdapter.getItemCount());
    }

    private void assertMetadata(int listPosition, int groupSize, String objectValue) {
        assertEquals(groupSize, mAdapter.getGroupSize(listPosition));
        MatrixCursor cursor = (MatrixCursor) mAdapter.getItem(listPosition);
        assertEquals(objectValue, cursor.getString(GROUPING_COLUMN_INDEX));
    }
}