summaryrefslogtreecommitdiff
path: root/robolectric_tests/src/com/android/launcher3/widget/picker/model/WidgetsListContentEntryTest.java
blob: 86df3f8ab2aa2b4345c685914c7cbc845fa36e64 (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) 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.model;

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

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.robolectric.Shadows.shadowOf;

import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
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.model.WidgetsListContentEntry;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowPackageManager;
import org.robolectric.util.ReflectionHelpers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(RobolectricTestRunner.class)
public final class WidgetsListContentEntryTest {
    private static final String PACKAGE_NAME = "com.google.test";
    private final PackageItemInfo mPackageItemInfo = new PackageItemInfo(PACKAGE_NAME);
    private final ComponentName mWidget1 = ComponentName.createRelative(PACKAGE_NAME, ".mWidget1");
    private final ComponentName mWidget2 = ComponentName.createRelative(PACKAGE_NAME, ".mWidget2");
    private final ComponentName mWidget3 = ComponentName.createRelative(PACKAGE_NAME, ".mWidget3");
    private final Map<ComponentName, String> mWidgetsToLabels = new HashMap();

    @Mock private IconCache mIconCache;

    private Context mContext;
    private InvariantDeviceProfile mTestProfile;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mWidgetsToLabels.put(mWidget1, "Cat");
        mWidgetsToLabels.put(mWidget2, "Dog");
        mWidgetsToLabels.put(mWidget3, "Bird");

        mContext = RuntimeEnvironment.application;
        mTestProfile = new InvariantDeviceProfile();
        mTestProfile.numRows = 5;
        mTestProfile.numColumns = 5;

        doAnswer(invocation -> {
            ComponentWithLabel componentWithLabel = (ComponentWithLabel) invocation.getArgument(0);
            return mWidgetsToLabels.get(componentWithLabel.getComponent());
        }).when(mIconCache).getTitleNoCache(any());
    }

    @Test
    public void unsortedWidgets_diffLabels_shouldSortWidgetItems() {
        // GIVEN a list of widgets in unsorted order.
        // Cat 2x3
        WidgetItem widgetItem1 = createWidgetItem(mWidget1, /* spanX= */ 2, /* spanY= */ 3);
        // Dog 2x3
        WidgetItem widgetItem2 = createWidgetItem(mWidget2, /* spanX= */ 2, /* spanY= */ 3);
        // Bird 2x3
        WidgetItem widgetItem3 = createWidgetItem(mWidget3, /* spanX= */ 2, /* spanY= */ 3);

        // WHEN creates a WidgetsListRowEntry with the unsorted widgets.
        WidgetsListContentEntry widgetsListRowEntry = new WidgetsListContentEntry(mPackageItemInfo,
                /* titleSectionName= */ "T",
                List.of(widgetItem1, widgetItem2, widgetItem3));

        // THEN the widgets list is sorted by their labels alphabetically: [Bird, Cat, Dog].
        assertThat(widgetsListRowEntry.mWidgets)
                .containsExactly(widgetItem3, widgetItem1, widgetItem2)
                .inOrder();
        assertThat(widgetsListRowEntry.mTitleSectionName).isEqualTo("T");
        assertThat(widgetsListRowEntry.mPkgItem).isEqualTo(mPackageItemInfo);
    }

    @Test
    public void unsortedWidgets_sameLabels_differentSize_shouldSortWidgetItems() {
        // GIVEN a list of widgets in unsorted order.
        // Cat 3x3
        WidgetItem widgetItem1 = createWidgetItem(mWidget1, /* spanX= */ 3, /* spanY= */ 3);
        // Cat 1x2
        WidgetItem widgetItem2 = createWidgetItem(mWidget1, /* spanX= */ 1, /* spanY= */ 2);
        // Cat 2x2
        WidgetItem widgetItem3 = createWidgetItem(mWidget1, /* spanX= */ 2, /* spanY= */ 2);

        // WHEN creates a WidgetsListRowEntry with the unsorted widgets.
        WidgetsListContentEntry widgetsListRowEntry = new WidgetsListContentEntry(mPackageItemInfo,
                /* titleSectionName= */ "T",
                List.of(widgetItem1, widgetItem2, widgetItem3));

        // THEN the widgets list is sorted by their gird sizes in an ascending order:
        // [1x2, 2x2, 3x3].
        assertThat(widgetsListRowEntry.mWidgets)
                .containsExactly(widgetItem2, widgetItem3, widgetItem1)
                .inOrder();
        assertThat(widgetsListRowEntry.mTitleSectionName).isEqualTo("T");
        assertThat(widgetsListRowEntry.mPkgItem).isEqualTo(mPackageItemInfo);
    }

    @Test
    public void unsortedWidgets_hodgepodge_shouldSortWidgetItems() {
        // GIVEN a list of widgets in unsorted order.
        // Cat 3x3
        WidgetItem widgetItem1 = createWidgetItem(mWidget1, /* spanX= */ 3, /* spanY= */ 3);
        // Cat 1x2
        WidgetItem widgetItem2 = createWidgetItem(mWidget1, /* spanX= */ 1, /* spanY= */ 2);
        // Dog 2x2
        WidgetItem widgetItem3 = createWidgetItem(mWidget2, /* spanX= */ 2, /* spanY= */ 2);
        // Bird 2x2
        WidgetItem widgetItem4 = createWidgetItem(mWidget3, /* spanX= */ 2, /* spanY= */ 2);

        // WHEN creates a WidgetsListRowEntry with the unsorted widgets.
        WidgetsListContentEntry widgetsListRowEntry = new WidgetsListContentEntry(mPackageItemInfo,
                /* titleSectionName= */ "T",
                List.of(widgetItem1, widgetItem2, widgetItem3, widgetItem4));

        // THEN the widgets list is first sorted by labels alphabetically. Then, for widgets with
        // same labels, they are sorted by their gird sizes in an ascending order:
        // [Bird 2x2, Cat 1x2, Cat 3x3, Dog 2x2]
        assertThat(widgetsListRowEntry.mWidgets)
                .containsExactly(widgetItem4, widgetItem2, widgetItem1, widgetItem3)
                .inOrder();
        assertThat(widgetsListRowEntry.mTitleSectionName).isEqualTo("T");
        assertThat(widgetsListRowEntry.mPkgItem).isEqualTo(mPackageItemInfo);
    }

    private WidgetItem createWidgetItem(ComponentName componentName, int spanX, int spanY) {
        String label = mWidgetsToLabels.get(componentName);
        ShadowPackageManager packageManager = shadowOf(mContext.getPackageManager());
        AppWidgetProviderInfo widgetInfo = new AppWidgetProviderInfo();
        widgetInfo.provider = componentName;
        ReflectionHelpers.setField(widgetInfo, "providerInfo",
                packageManager.addReceiverIfNotPresent(componentName));

        LauncherAppWidgetProviderInfo launcherAppWidgetProviderInfo =
                LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, widgetInfo);
        launcherAppWidgetProviderInfo.spanX = spanX;
        launcherAppWidgetProviderInfo.spanY = spanY;
        launcherAppWidgetProviderInfo.label = label;

        return new WidgetItem(launcherAppWidgetProviderInfo, mTestProfile, mIconCache);
    }
}