summaryrefslogtreecommitdiffstats
path: root/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTipTest.java
blob: cab8913c17a9e82234c3c0e87da328d3d1c9967c (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
/*
 * Copyright (C) 2018 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.settings.fuelgauge.batterytip.tips;

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

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Parcel;
import android.util.Pair;

import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.fuelgauge.batterytip.AppInfo;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;

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

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

@RunWith(SettingsRobolectricTestRunner.class)
public class RestrictAppTipTest {
    private static final String PACKAGE_NAME = "com.android.app";
    private static final String UNINSTALL_PACKAGE_NAME = "com.android.app.unintall";
    private static final String DISPLAY_NAME = "app";
    private static final int ANOMALY_WAKEUP = 0;
    private static final int ANOMALY_WAKELOCK = 1;

    private Context mContext;
    private RestrictAppTip mNewBatteryTip;
    private RestrictAppTip mHandledBatteryTip;
    private RestrictAppTip mInvisibleBatteryTip;
    private List<AppInfo> mUsageAppList;
    private AppInfo mAppInfo;
    private AppInfo mUninstallAppInfo;
    @Mock
    private ApplicationInfo mApplicationInfo;
    @Mock
    private PackageManager mPackageManager;
    @Mock
    private MetricsFeatureProvider mMetricsFeatureProvider;

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

        mContext = spy(RuntimeEnvironment.application);
        doReturn(mPackageManager).when(mContext).getPackageManager();
        doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfo(PACKAGE_NAME,
                PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.MATCH_ANY_USER);
        doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager)
                .getApplicationInfo(UNINSTALL_PACKAGE_NAME,
                        PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.MATCH_ANY_USER);
        doReturn(DISPLAY_NAME).when(mApplicationInfo).loadLabel(mPackageManager);

        mAppInfo = new AppInfo.Builder()
                .setPackageName(PACKAGE_NAME)
                .addAnomalyType(ANOMALY_WAKEUP)
                .addAnomalyType(ANOMALY_WAKELOCK)
                .build();
        mUninstallAppInfo = new AppInfo.Builder()
                .setPackageName(UNINSTALL_PACKAGE_NAME)
                .addAnomalyType(ANOMALY_WAKEUP)
                .build();
        mUsageAppList = new ArrayList<>();
        mUsageAppList.add(mAppInfo);
        mNewBatteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, mUsageAppList);
        mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
        mInvisibleBatteryTip = new RestrictAppTip(BatteryTip.StateType.INVISIBLE, new ArrayList<>());
    }

    @Test
    public void parcelable() {
        Parcel parcel = Parcel.obtain();
        mNewBatteryTip.writeToParcel(parcel, mNewBatteryTip.describeContents());
        parcel.setDataPosition(0);

        final RestrictAppTip parcelTip = new RestrictAppTip(parcel);

        assertThat(parcelTip.getType()).isEqualTo(BatteryTip.TipType.APP_RESTRICTION);
        assertThat(parcelTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
        final AppInfo app = parcelTip.getRestrictAppList().get(0);
        assertThat(app.packageName).isEqualTo(PACKAGE_NAME);
    }

    @Test
    public void getTitle_stateNew_showRestrictTitle() {
        assertThat(mNewBatteryTip.getTitle(mContext)).isEqualTo("Restrict 1 app");
    }

    @Test
    public void getTitle_oneAppHandled_showHandledTitle() {
        assertThat(mHandledBatteryTip.getTitle(mContext)).isEqualTo("app recently restricted");
    }

    @Test
    public void getTitle_moreAppsHandled_showHandledTitle() {
        mUsageAppList.add(new AppInfo.Builder().build());
        mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
        assertThat(mHandledBatteryTip.getTitle(mContext)).isEqualTo("2 apps recently restricted");
    }

    @Test
    public void getSummary_stateNew_showRestrictSummary() {
        assertThat(mNewBatteryTip.getSummary(mContext))
            .isEqualTo("app has high background battery usage");
    }

    @Test
    public void getSummary_oneAppHandled_showHandledSummary() {
        assertThat(mHandledBatteryTip.getSummary(mContext).toString())
                .isEqualTo(mContext.getResources().getQuantityString(
                        R.plurals.battery_tip_restrict_handled_summary, 1));
    }

    @Test
    public void getSummary_moreAppsHandled_showHandledSummary() {
        mUsageAppList.add(new AppInfo.Builder().build());
        mHandledBatteryTip = new RestrictAppTip(BatteryTip.StateType.HANDLED, mUsageAppList);
        assertThat(mHandledBatteryTip.getSummary(mContext))
                .isEqualTo(mContext.getResources().getQuantityString(
                        R.plurals.battery_tip_restrict_handled_summary, 2));
    }

    @Test
    public void update_anomalyBecomeInvisible_stateHandled() {
        mNewBatteryTip.updateState(mInvisibleBatteryTip);

        assertThat(mNewBatteryTip.getState()).isEqualTo(BatteryTip.StateType.HANDLED);
    }

    @Test
    public void update_handledAnomlayBecomeInvisible_stateInvisible() {
        mHandledBatteryTip.updateState(mInvisibleBatteryTip);

        assertThat(mHandledBatteryTip.getState()).isEqualTo(BatteryTip.StateType.INVISIBLE);
    }

    @Test
    public void update_newAnomalyComes_stateNew() {
        mInvisibleBatteryTip.updateState(mNewBatteryTip);
        assertThat(mInvisibleBatteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW);

        mHandledBatteryTip.updateState(mNewBatteryTip);
        assertThat(mHandledBatteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
    }

    @Test
    public void update_newHandledAnomalyComes_containHandledAnomaly() {
        mInvisibleBatteryTip.updateState(mHandledBatteryTip);
        assertThat(mInvisibleBatteryTip.getState()).isEqualTo(BatteryTip.StateType.HANDLED);
        assertThat(mInvisibleBatteryTip.getRestrictAppList()).containsExactly(mAppInfo);
    }

    @Test
    public void sanityCheck_appUninstalled_stateInvisible() {
        final List<AppInfo> appInfos = new ArrayList<>();
        appInfos.add(mUninstallAppInfo);
        final BatteryTip batteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, appInfos);

        batteryTip.sanityCheck(mContext);

        assertThat(batteryTip.getState()).isEqualTo(BatteryTip.StateType.INVISIBLE);
    }

    @Test
    public void sanityCheck_twoRestrictedAppsWhileUninstallOne_stateVisible() {
        final List<AppInfo> appInfos = new ArrayList<>();
        appInfos.add(mAppInfo);
        appInfos.add(mUninstallAppInfo);
        final BatteryTip batteryTip = new RestrictAppTip(BatteryTip.StateType.NEW, appInfos);

        batteryTip.sanityCheck(mContext);

        assertThat(batteryTip.getState()).isEqualTo(BatteryTip.StateType.NEW);
    }

    @Test
    public void toString_containsAppData() {
        assertThat(mNewBatteryTip.toString()).isEqualTo(
                "type=1 state=0 { packageName=com.android.app,anomalyTypes={0, 1},screenTime=0 }");
    }

    @Test
    public void testLog_stateNew_logAppInfo() {
        mNewBatteryTip.log(mContext, mMetricsFeatureProvider);

        verify(mMetricsFeatureProvider).action(mContext,
                MetricsProto.MetricsEvent.ACTION_APP_RESTRICTION_TIP, BatteryTip.StateType.NEW);
        verify(mMetricsFeatureProvider).action(mContext,
                MetricsProto.MetricsEvent.ACTION_APP_RESTRICTION_TIP_LIST,
                PACKAGE_NAME,
                Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_WAKEUP));
        verify(mMetricsFeatureProvider).action(mContext,
                MetricsProto.MetricsEvent.ACTION_APP_RESTRICTION_TIP_LIST,
                PACKAGE_NAME,
                Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, ANOMALY_WAKELOCK));
    }

    @Test
    public void testLog_stateHandled_doNotLogAppInfo() {
        mHandledBatteryTip.log(mContext, mMetricsFeatureProvider);

        verify(mMetricsFeatureProvider).action(mContext,
                MetricsProto.MetricsEvent.ACTION_APP_RESTRICTION_TIP, BatteryTip.StateType.HANDLED);
        verify(mMetricsFeatureProvider, never()).action(any(), anyInt(), anyString(), any());

    }
}