Merge "Create BatterySaverReceiver" into oc-dr1-dev

This commit is contained in:
TreeHugger Robot
2017-07-07 19:31:47 +00:00
committed by Android (Google) Code Review
6 changed files with 261 additions and 85 deletions

View File

@@ -17,10 +17,12 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.os.PowerManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -77,6 +79,20 @@ public class BatterySaverControllerTest {
testUpdateStateInner(false);
}
@Test
public void testOnBatteryChanged_pluggedIn_setDisable() {
mBatterySaverController.onBatteryChanged(true /* pluggedIn */);
verify(mBatterySaverPref).setSwitchEnabled(false);
}
@Test
public void testOnBatteryChanged_notPluggedIn_setEnable() {
mBatterySaverController.onBatteryChanged(false /* pluggedIn */);
verify(mBatterySaverPref).setSwitchEnabled(true);
}
private void testOnPreferenceChangeInner(final boolean saverOn) {
when(mPowerManager.setPowerSaveMode(saverOn)).thenReturn(true);
when(mPowerManager.isPowerSaveMode()).thenReturn(!saverOn);

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2017 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;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.PowerManager;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BatterySaverReceiverTest {
@Mock
private BatterySaverReceiver.BatterySaverListener mBatterySaverListener;
@Mock
private Context mContext;
private BatterySaverReceiver mBatterySaverReceiver;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mBatterySaverReceiver = new BatterySaverReceiver(mContext);
mBatterySaverReceiver.setBatterySaverListener(mBatterySaverListener);
}
@Test
public void testOnReceive_devicePluggedIn_pluggedInTrue() {
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_PLUGGED, BatteryManager.BATTERY_PLUGGED_AC);
mBatterySaverReceiver.onReceive(mContext, intent);
verify(mBatterySaverListener).onBatteryChanged(true);
}
@Test
public void testOnReceive_deviceNotPluggedIn_pluggedInFalse() {
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
mBatterySaverReceiver.onReceive(mContext, intent);
verify(mBatterySaverListener).onBatteryChanged(false);
}
@Test
public void testOnReceive_powerSaveModeChanged_invokeCallback() {
Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
mBatterySaverReceiver.onReceive(mContext, intent);
verify(mBatterySaverListener).onPowerSaveModeChanged();
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 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;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.SwitchBar;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BatterySaverSettingsTest {
private Context mContext;
private BatterySaverSettings mBatterySaverSettings;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mBatterySaverSettings = new BatterySaverSettings();
mBatterySaverSettings.mSwitchBar = new SwitchBar(mContext);
}
@Test
public void testOnBatteryChanged_pluggedIn_setDisable() {
mBatterySaverSettings.onBatteryChanged(true /* pluggedIn */);
assertThat(mBatterySaverSettings.mSwitchBar.isEnabled()).isFalse();
}
@Test
public void testOnBatteryChanged_notPluggedIn_setEnable() {
mBatterySaverSettings.onBatteryChanged(false /* pluggedIn */);
assertThat(mBatterySaverSettings.mSwitchBar.isEnabled()).isTrue();
}
}