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.any; 25 import static org.mockito.Mockito.doNothing; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.times; 29 import static org.mockito.Mockito.verify; 30 import static org.mockito.Mockito.when; 31 32 import android.app.Activity; 33 import android.content.ComponentName; 34 import android.content.Context; 35 36 import androidx.preference.Preference; 37 38 import com.android.settings.R; 39 import com.android.settings.testutils.FakeFeatureFactory; 40 41 import org.junit.After; 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.Robolectric; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.annotation.Config; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class TopLevelBatteryPreferenceControllerTest { 52 private Context mContext; 53 private FakeFeatureFactory mFeatureFactory; 54 private TopLevelBatteryPreferenceController mController; 55 private BatterySettingsFeatureProvider mBatterySettingsFeatureProvider; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mFeatureFactory = FakeFeatureFactory.setupForTest(); 61 mContext = spy(Robolectric.setupActivity(Activity.class)); 62 mController = new TopLevelBatteryPreferenceController(mContext, "test_key"); 63 mBatterySettingsFeatureProvider = 64 mFeatureFactory.batterySettingsFeatureProvider; 65 } 66 67 @After cleanUp()68 public void cleanUp() { 69 TopLevelBatteryPreferenceController.sReplacingActivityMap.clear(); 70 } 71 72 @Test getAvailibilityStatus_availableByDefault()73 public void getAvailibilityStatus_availableByDefault() { 74 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 75 } 76 77 @Test 78 @Config(qualifiers = "mcc999") getAvailabilityStatus_unsupportedWhenSet()79 public void getAvailabilityStatus_unsupportedWhenSet() { 80 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 81 } 82 83 @Test handlePreferenceTreeClick_noFragment_noCustomActivityCalled()84 public void handlePreferenceTreeClick_noFragment_noCustomActivityCalled() { 85 Preference preference = new Preference(mContext); 86 87 assertThat(mController.handlePreferenceTreeClick(preference)).isFalse(); 88 } 89 90 @Test handlePreferenceTreeClick_sameActivityReturned_noCustomActivityCalled()91 public void handlePreferenceTreeClick_sameActivityReturned_noCustomActivityCalled() { 92 String fragmentPath = "my.fragment.ClassName"; 93 Preference preference = mock(Preference.class); 94 when(preference.getFragment()).thenReturn(fragmentPath); 95 ComponentName pathName = mController.convertClassPathToComponentName(fragmentPath); 96 when(mBatterySettingsFeatureProvider.getReplacingActivity(any())).thenReturn(pathName); 97 98 assertThat(mController.handlePreferenceTreeClick(preference)).isFalse(); 99 } 100 101 @Test handlePreferenceTreeClick_newActivityReturned_newActivityRedirected()102 public void handlePreferenceTreeClick_newActivityReturned_newActivityRedirected() { 103 String fragmentPath = "my.fragment.ClassName"; 104 Preference preference = mock(Preference.class); 105 when(preference.getFragment()).thenReturn(fragmentPath); 106 String newFragmentPath = "my.fragment.NewClassName"; 107 ComponentName newPathName = mController.convertClassPathToComponentName(newFragmentPath); 108 when(mBatterySettingsFeatureProvider.getReplacingActivity(any())).thenReturn( 109 newPathName); 110 doNothing().when(mContext).startActivity(any()); 111 112 assertThat(mController.handlePreferenceTreeClick(preference)).isTrue(); 113 } 114 115 @Test handlePreferenceTreeClick_calledMultipleTimes_fetchedFromCache()116 public void handlePreferenceTreeClick_calledMultipleTimes_fetchedFromCache() { 117 String fragmentPath = "my.fragment.ClassName"; 118 Preference preference = mock(Preference.class); 119 when(preference.getFragment()).thenReturn(fragmentPath); 120 String newFragmentPath = "my.fragment.NewClassName"; 121 ComponentName newPathName = mController.convertClassPathToComponentName(newFragmentPath); 122 when(mBatterySettingsFeatureProvider.getReplacingActivity(any())).thenReturn( 123 newPathName); 124 doNothing().when(mContext).startActivity(any()); 125 126 assertThat(mController.handlePreferenceTreeClick(preference)).isTrue(); 127 assertThat(mController.handlePreferenceTreeClick(preference)).isTrue(); 128 verify(mBatterySettingsFeatureProvider, times(1)).getReplacingActivity(any()); 129 } 130 131 @Test convertClassPathToComponentName_nullInput_returnsNull()132 public void convertClassPathToComponentName_nullInput_returnsNull() { 133 assertThat(mController.convertClassPathToComponentName(null)).isNull(); 134 } 135 136 @Test convertClassPathToComponentName_emptyStringInput_returnsNull()137 public void convertClassPathToComponentName_emptyStringInput_returnsNull() { 138 assertThat(mController.convertClassPathToComponentName("")).isNull(); 139 } 140 141 @Test convertClassPathToComponentName_singleClassName_returnsCorrectComponentName()142 public void convertClassPathToComponentName_singleClassName_returnsCorrectComponentName() { 143 ComponentName output = mController.convertClassPathToComponentName("ClassName"); 144 145 assertThat(output.getPackageName()).isEqualTo(""); 146 assertThat(output.getClassName()).isEqualTo("ClassName"); 147 } 148 149 @Test convertClassPathToComponentName_validAddress_returnsCorrectComponentName()150 public void convertClassPathToComponentName_validAddress_returnsCorrectComponentName() { 151 ComponentName output = mController.convertClassPathToComponentName("my.fragment.ClassName"); 152 153 assertThat(output.getPackageName()).isEqualTo("my.fragment"); 154 assertThat(output.getClassName()).isEqualTo("ClassName"); 155 } 156 157 @Test getDashboardLabel_returnsCorrectLabel()158 public void getDashboardLabel_returnsCorrectLabel() { 159 BatteryInfo info = new BatteryInfo(); 160 info.batteryPercentString = "3%"; 161 assertThat(mController.getDashboardLabel(mContext, info, true)) 162 .isEqualTo(info.batteryPercentString); 163 164 info.remainingLabel = "Phone will shut down soon"; 165 assertThat(mController.getDashboardLabel(mContext, info, true)) 166 .isEqualTo("3% - Phone will shut down soon"); 167 168 info.discharging = false; 169 info.chargeLabel = "5% - charging"; 170 assertThat(mController.getDashboardLabel(mContext, info, true)).isEqualTo("5% - charging"); 171 } 172 173 @Test getSummary_batteryNotPresent_shouldShowWarningMessage()174 public void getSummary_batteryNotPresent_shouldShowWarningMessage() { 175 mController.mIsBatteryPresent = false; 176 177 assertThat(mController.getSummary()) 178 .isEqualTo(mContext.getString(R.string.battery_missing_message)); 179 } 180 } 181