summaryrefslogtreecommitdiff
path: root/tests/src/com/android/launcher3/widget/picker/WidgetsDiffReporterTest.java
blob: b480a4c0025e45de8ac6ab99eba92496fad46389 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (C) 2021 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.launcher3.widget.picker;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;

import static com.android.launcher3.util.WidgetUtils.createAppWidgetProviderInfo;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;

import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.UserHandle;

import androidx.recyclerview.widget.RecyclerView;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.icons.BitmapInfo;
import com.android.launcher3.icons.ComponentWithLabel;
import com.android.launcher3.icons.IconCache;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.model.data.PackageItemInfo;
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
import com.android.launcher3.widget.model.WidgetsListContentEntry;
import com.android.launcher3.widget.model.WidgetsListHeaderEntry;
import com.android.launcher3.widget.picker.WidgetsListAdapter.WidgetListBaseRowEntryComparator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

@SmallTest
@RunWith(AndroidJUnit4.class)
public final class WidgetsDiffReporterTest {
    private static final String TEST_PACKAGE_PREFIX = "com.android.test";
    private static final WidgetListBaseRowEntryComparator COMPARATOR =
            new WidgetListBaseRowEntryComparator();

    @Mock private IconCache mIconCache;
    @Mock private RecyclerView.Adapter mAdapter;

    private InvariantDeviceProfile mTestProfile;
    private WidgetsDiffReporter mWidgetsDiffReporter;
    private Context mContext;
    private WidgetsListHeaderEntry mHeaderA;
    private WidgetsListHeaderEntry mHeaderB;
    private WidgetsListHeaderEntry mHeaderC;
    private WidgetsListHeaderEntry mHeaderD;
    private WidgetsListHeaderEntry mHeaderE;
    private WidgetsListContentEntry mContentC;
    private WidgetsListContentEntry mContentE;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mTestProfile = new InvariantDeviceProfile();
        mTestProfile.numRows = 5;
        mTestProfile.numColumns = 5;

        doAnswer(invocation -> ((ComponentWithLabel) invocation.getArgument(0))
                .getComponent().getPackageName())
                .when(mIconCache).getTitleNoCache(any());

        mContext = getApplicationContext();
        mWidgetsDiffReporter = new WidgetsDiffReporter(mIconCache, mAdapter);
        mHeaderA = createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "A",
                /* appName= */ "A", /* numOfWidgets= */ 3);
        mHeaderB = createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "B",
                /* appName= */ "B", /* numOfWidgets= */ 3);
        mHeaderC = createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "C",
                /* appName= */ "C", /* numOfWidgets= */ 3);
        mContentC = createWidgetsContentEntry(TEST_PACKAGE_PREFIX + "C",
                /* appName= */ "C", /* numOfWidgets= */ 3);
        mHeaderD = createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "D",
                /* appName= */ "D", /* numOfWidgets= */ 3);
        mHeaderE = createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "E",
                /* appName= */ "E", /* numOfWidgets= */ 3);
        mContentE = createWidgetsContentEntry(TEST_PACKAGE_PREFIX + "E",
                /* appName= */ "E", /* numOfWidgets= */ 3);
    }

    @Test
    public void listNotChanged_shouldNotInvokeAnyCallbacks() {
        // GIVEN the current list has app headers [A, B, C].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mHeaderC));

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, currentList, COMPARATOR);

        // THEN there is no adaptor callback.
        verifyZeroInteractions(mAdapter);
        // THEN the current list contains the same entries.
        assertThat(currentList).containsExactly(mHeaderA, mHeaderB, mHeaderC);
    }

    @Test
    public void headersOnly_emptyListToNonEmpty_shouldInvokeNotifyDataSetChanged() {
        // GIVEN the current list has app headers [A, B, C].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>();

        List<WidgetsListBaseEntry> newList = List.of(
                createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "A", "A", 3),
                createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "B", "B", 3),
                createWidgetsHeaderEntry(TEST_PACKAGE_PREFIX + "C", "C", 3));

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN notifyDataSetChanged is called
        verify(mAdapter).notifyDataSetChanged();
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }

    @Test
    public void headersOnly_nonEmptyToEmptyList_shouldInvokeNotifyDataSetChanged() {
        // GIVEN the current list has app headers [A, B, C].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mHeaderC));
        // GIVEN the new list is empty.
        List<WidgetsListBaseEntry> newList = List.of();

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN notifyDataSetChanged is called.
        verify(mAdapter).notifyDataSetChanged();
        // THEN the current list isEmpty.
        assertThat(currentList).isEmpty();
    }

    @Test
    public void headersOnly_itemAddedAndRemovedInTheNewList_shouldInvokeCorrectCallbacks() {
        // GIVEN the current list has app headers [A, B, D].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mHeaderD));
        // GIVEN the new list has app headers [A, C, E].
        List<WidgetsListBaseEntry> newList = List.of(mHeaderA, mHeaderC, mHeaderE);

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN "B" is removed from position 1.
        verify(mAdapter).notifyItemRemoved(/* position= */ 1);
        // THEN "D" is removed from position 2.
        verify(mAdapter).notifyItemRemoved(/* position= */ 2);
        // THEN "C" is inserted at position 1.
        verify(mAdapter).notifyItemInserted(/* position= */ 1);
        // THEN "E" is inserted at position 2.
        verify(mAdapter).notifyItemInserted(/* position= */ 2);
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }

    @Test
    public void headersContentsMix_itemAddedAndRemovedInTheNewList_shouldInvokeCorrectCallbacks() {
        // GIVEN the current list has app headers [A, B, E content].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mContentE));
        // GIVEN the new list has app headers [A, C content, D].
        List<WidgetsListBaseEntry> newList = List.of(mHeaderA, mContentC, mHeaderD);

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN "B" is removed from position 1.
        verify(mAdapter).notifyItemRemoved(/* position= */ 1);
        // THEN "C content" is inserted at position 1.
        verify(mAdapter).notifyItemInserted(/* position= */ 1);
        // THEN "D" is inserted at position 2.
        verify(mAdapter).notifyItemInserted(/* position= */ 2);
        // THEN "E content" is removed from position 3.
        verify(mAdapter).notifyItemRemoved(/* position= */ 3);
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }

    @Test
    public void headersContentsMix_userInteractWithHeader_shouldInvokeCorrectCallbacks() {
        // GIVEN the current list has app headers [A, B, E content].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mContentE));
        // GIVEN the new list has app headers [A, B, E content] and the user has interacted with B.
        List<WidgetsListBaseEntry> newList =
                List.of(mHeaderA, mHeaderB.withWidgetListShown(), mContentE);

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN notify "B" has been changed.
        verify(mAdapter).notifyItemChanged(/* position= */ 1);
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }

    @Test
    public void headersContentsMix_headerWidgetsModified_shouldInvokeCorrectCallbacks() {
        // GIVEN the current list has app headers [A, B, E content].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mContentE));
        // GIVEN the new list has one of the headers widgets list modified.
        List<WidgetsListBaseEntry> newList = List.of(
                new WidgetsListHeaderEntry(
                        mHeaderA.mPkgItem, mHeaderA.mTitleSectionName,
                        mHeaderA.mWidgets.subList(0, 1)),
                mHeaderB, mContentE);

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN notify "A" has been changed.
        verify(mAdapter).notifyItemChanged(/* position= */ 0);
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }

    @Test
    public void headersContentsMix_contentMaxSpanSizeModified_shouldInvokeCorrectCallbacks() {
        // GIVEN the current list has app headers [A, B, E content].
        ArrayList<WidgetsListBaseEntry> currentList = new ArrayList<>(
                List.of(mHeaderA, mHeaderB, mContentE));
        // GIVEN the new list has max span size in "E content" modified.
        List<WidgetsListBaseEntry> newList = List.of(
                mHeaderA,
                mHeaderB,
                new WidgetsListContentEntry(
                        mContentE.mPkgItem,
                        mContentE.mTitleSectionName,
                        mContentE.mWidgets,
                        mContentE.getMaxSpanSizeInCells() + 1));

        // WHEN computing the list difference.
        mWidgetsDiffReporter.process(currentList, newList, COMPARATOR);

        // THEN notify "E content" has been changed.
        verify(mAdapter).notifyItemChanged(/* position= */ 2);
        // THEN the current list contains all elements from the new list.
        assertThat(currentList).containsExactlyElementsIn(newList);
    }


    private WidgetsListHeaderEntry createWidgetsHeaderEntry(String packageName, String appName,
            int numOfWidgets) {
        List<WidgetItem> widgetItems = generateWidgetItems(packageName, numOfWidgets);
        PackageItemInfo pInfo = createPackageItemInfo(packageName, appName,
                widgetItems.get(0).user);

        return new WidgetsListHeaderEntry(pInfo, /* titleSectionName= */ "", widgetItems);
    }

    private WidgetsListContentEntry createWidgetsContentEntry(String packageName, String appName,
            int numOfWidgets) {
        List<WidgetItem> widgetItems = generateWidgetItems(packageName, numOfWidgets);
        PackageItemInfo pInfo = createPackageItemInfo(packageName, appName,
                widgetItems.get(0).user);

        return new WidgetsListContentEntry(pInfo, /* titleSectionName= */ "", widgetItems);
    }

    private PackageItemInfo createPackageItemInfo(String packageName, String appName,
            UserHandle userHandle) {
        PackageItemInfo pInfo = new PackageItemInfo(packageName, userHandle);
        pInfo.title = appName;
        pInfo.bitmap = BitmapInfo.of(Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8), 0);
        return pInfo;
    }

    private List<WidgetItem> generateWidgetItems(String packageName, int numOfWidgets) {
        ArrayList<WidgetItem> widgetItems = new ArrayList<>();
        for (int i = 0; i < numOfWidgets; i++) {
            ComponentName cn = ComponentName.createRelative(packageName, ".SampleWidget" + i);
            AppWidgetProviderInfo widgetInfo = createAppWidgetProviderInfo(cn);

            WidgetItem widgetItem = new WidgetItem(
                    LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo),
                    mTestProfile, mIconCache);
            widgetItems.add(widgetItem);
        }
        return widgetItems;
    }
}