1 /* 2 * Copyright (C) 2018 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 17 package com.android.settings.fuelgauge.batterytip; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.os.Parcel; 22 import android.text.format.DateUtils; 23 24 import com.android.settings.fuelgauge.anomaly.Anomaly; 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.ArrayList; 32 import java.util.Collections; 33 import java.util.List; 34 35 @RunWith(SettingsRobolectricTestRunner.class) 36 public class AppInfoTest { 37 38 private static final String PACKAGE_NAME = "com.android.app"; 39 private static final int TYPE_WAKELOCK = Anomaly.AnomalyType.WAKE_LOCK; 40 private static final int TYPE_WAKEUP = Anomaly.AnomalyType.WAKEUP_ALARM; 41 private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS; 42 private static final int UID = 3452; 43 44 private AppInfo mAppInfo; 45 46 @Before 47 public void setUp() { 48 mAppInfo = new AppInfo.Builder() 49 .setPackageName(PACKAGE_NAME) 50 .addAnomalyType(TYPE_WAKELOCK) 51 .addAnomalyType(TYPE_WAKEUP) 52 .setScreenOnTimeMs(SCREEN_TIME_MS) 53 .setUid(UID) 54 .build(); 55 } 56 57 @Test 58 public void testParcel() { 59 Parcel parcel = Parcel.obtain(); 60 mAppInfo.writeToParcel(parcel, mAppInfo.describeContents()); 61 parcel.setDataPosition(0); 62 63 final AppInfo appInfo = new AppInfo(parcel); 64 65 assertThat(appInfo.packageName).isEqualTo(PACKAGE_NAME); 66 assertThat(appInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP); 67 assertThat(appInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS); 68 assertThat(appInfo.uid).isEqualTo(UID); 69 } 70 71 @Test 72 public void testCompareTo_hasCorrectOrder() { 73 final AppInfo appInfo = new AppInfo.Builder() 74 .setPackageName(PACKAGE_NAME) 75 .addAnomalyType(TYPE_WAKELOCK) 76 .setScreenOnTimeMs(SCREEN_TIME_MS + 100) 77 .build(); 78 79 List<AppInfo> appInfos = new ArrayList<>(); 80 appInfos.add(appInfo); 81 appInfos.add(mAppInfo); 82 83 Collections.sort(appInfos); 84 assertThat(appInfos.get(0).screenOnTimeMs).isLessThan(appInfos.get(1).screenOnTimeMs); 85 } 86 87 @Test 88 public void testBuilder() { 89 assertThat(mAppInfo.packageName).isEqualTo(PACKAGE_NAME); 90 assertThat(mAppInfo.anomalyTypes).containsExactly(TYPE_WAKELOCK, TYPE_WAKEUP); 91 assertThat(mAppInfo.screenOnTimeMs).isEqualTo(SCREEN_TIME_MS); 92 assertThat(mAppInfo.uid).isEqualTo(UID); 93 } 94 } 95