Add BatteryTipDetector and LowBatteryTip stuffs.
This cl adds the infra of BatteryTipDetector and use LowBatteryTip as an example(tip model + detector). Also add SummaryTipDetector and related tests Bug: 70570352 Test: RunSettingsRoboTests Change-Id: Icf1349b6ede9eb7ee5ed69b39ee3a2661ac660fa
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.batterytip;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.batterytip.detectors.BatteryTipDetector;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
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.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class BatteryTipLoaderTest {
|
||||
@Mock
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@Mock
|
||||
private BatteryTipDetector mBatteryTipDetector;
|
||||
@Mock
|
||||
private BatteryTip mBatteryTip;
|
||||
private Context mContext;
|
||||
private BatteryTipLoader mBatteryTipLoader;
|
||||
private List<BatteryTip> mBatteryTips;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
doReturn(mBatteryTip).when(mBatteryTipDetector).detect();
|
||||
mBatteryTipLoader = new BatteryTipLoader(mContext, mBatteryStatsHelper);
|
||||
mBatteryTips = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddBatteryTipFromDetector_tipVisible_addAndUpdateCount() {
|
||||
doReturn(true).when(mBatteryTip).isVisible();
|
||||
mBatteryTipLoader.mVisibleTips = 0;
|
||||
|
||||
mBatteryTipLoader.addBatteryTipFromDetector(mBatteryTips, mBatteryTipDetector);
|
||||
|
||||
assertThat(mBatteryTips.contains(mBatteryTip)).isTrue();
|
||||
assertThat(mBatteryTipLoader.mVisibleTips).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddBatteryTipFromDetector_tipInvisible_doNotAddCount() {
|
||||
doReturn(false).when(mBatteryTip).isVisible();
|
||||
mBatteryTipLoader.mVisibleTips = 0;
|
||||
|
||||
mBatteryTipLoader.addBatteryTipFromDetector(mBatteryTips, mBatteryTipDetector);
|
||||
|
||||
assertThat(mBatteryTips.contains(mBatteryTip)).isTrue();
|
||||
assertThat(mBatteryTipLoader.mVisibleTips).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.batterytip.detectors;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.BatteryInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
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.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class LowBatteryDetectorTest {
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private BatteryInfo mBatteryInfo;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
private LowBatteryDetector mLowBatteryDetector;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPolicy = spy(new BatteryTipPolicy(mContext));
|
||||
mLowBatteryDetector = new LowBatteryDetector(mPolicy, mBatteryInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", false);
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_shortBatteryLife_tipVisible() {
|
||||
mBatteryInfo.discharging = true;
|
||||
mBatteryInfo.remainingTimeUs = 1 * DateUtils.MINUTE_IN_MILLIS;
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_longBatteryLife_tipInvisible() {
|
||||
mBatteryInfo.discharging = true;
|
||||
mBatteryInfo.remainingTimeUs = 1 * DateUtils.DAY_IN_MILLIS;
|
||||
|
||||
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
|
||||
}
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.batterytip.detectors;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
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;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class SummaryDetectorTest {
|
||||
private Context mContext;
|
||||
private BatteryTipPolicy mPolicy;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPolicy = spy(new BatteryTipPolicy(mContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_disabledByPolicy_tipInvisible() {
|
||||
ReflectionHelpers.setField(mPolicy, "summaryEnabled", false);
|
||||
SummaryDetector detector = new SummaryDetector(mPolicy, 0 /* visibleTips */);
|
||||
|
||||
assertThat(detector.detect().isVisible()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_noOtherTips_tipVisible() {
|
||||
SummaryDetector detector = new SummaryDetector(mPolicy, 0 /* visibleTips */);
|
||||
|
||||
assertThat(detector.detect().isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetect_hasOtherTips_tipInVisible() {
|
||||
SummaryDetector detector = new SummaryDetector(mPolicy, 1 /* visibleTips */);
|
||||
|
||||
assertThat(detector.detect().isVisible()).isFalse();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user