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; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.Activity; 29 import android.content.ComponentName; 30 import android.content.Context; 31 32 import androidx.preference.Preference; 33 34 import com.android.settings.R; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.Robolectric; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.annotation.Config; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class TopLevelBatteryPreferenceControllerTest { 46 private Context mContext; 47 private TopLevelBatteryPreferenceController mController; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mContext = spy(Robolectric.setupActivity(Activity.class)); 53 mController = new TopLevelBatteryPreferenceController(mContext, "test_key"); 54 } 55 56 @Test getAvailibilityStatus_availableByDefault()57 public void getAvailibilityStatus_availableByDefault() { 58 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 59 } 60 61 @Test 62 @Config(qualifiers = "mcc999") getAvailabilityStatus_unsupportedWhenSet()63 public void getAvailabilityStatus_unsupportedWhenSet() { 64 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 65 } 66 67 @Test convertClassPathToComponentName_nullInput_returnsNull()68 public void convertClassPathToComponentName_nullInput_returnsNull() { 69 assertThat(mController.convertClassPathToComponentName(null)).isNull(); 70 } 71 72 @Test convertClassPathToComponentName_emptyStringInput_returnsNull()73 public void convertClassPathToComponentName_emptyStringInput_returnsNull() { 74 assertThat(mController.convertClassPathToComponentName("")).isNull(); 75 } 76 77 @Test convertClassPathToComponentName_singleClassName_returnsCorrectComponentName()78 public void convertClassPathToComponentName_singleClassName_returnsCorrectComponentName() { 79 ComponentName output = mController.convertClassPathToComponentName("ClassName"); 80 81 assertThat(output.getPackageName()).isEqualTo(""); 82 assertThat(output.getClassName()).isEqualTo("ClassName"); 83 } 84 85 @Test convertClassPathToComponentName_validAddress_returnsCorrectComponentName()86 public void convertClassPathToComponentName_validAddress_returnsCorrectComponentName() { 87 ComponentName output = mController.convertClassPathToComponentName("my.fragment.ClassName"); 88 89 assertThat(output.getPackageName()).isEqualTo("my.fragment"); 90 assertThat(output.getClassName()).isEqualTo("ClassName"); 91 } 92 93 @Test getDashboardLabel_returnsCorrectLabel()94 public void getDashboardLabel_returnsCorrectLabel() { 95 mController.mPreference = new Preference(mContext); 96 BatteryInfo info = new BatteryInfo(); 97 info.batteryPercentString = "3%"; 98 assertThat(mController.getDashboardLabel(mContext, info, true)) 99 .isEqualTo(info.batteryPercentString); 100 101 info.remainingLabel = "Phone will shut down soon"; 102 assertThat(mController.getDashboardLabel(mContext, info, true)) 103 .isEqualTo("3% - Phone will shut down soon"); 104 105 info.discharging = false; 106 info.chargeLabel = "5% - charging"; 107 assertThat(mController.getDashboardLabel(mContext, info, true)).isEqualTo("5% - charging"); 108 } 109 110 @Test getSummary_batteryNotPresent_shouldShowWarningMessage()111 public void getSummary_batteryNotPresent_shouldShowWarningMessage() { 112 mController.mIsBatteryPresent = false; 113 114 assertThat(mController.getSummary()) 115 .isEqualTo(mContext.getString(R.string.battery_missing_message)); 116 } 117 } 118