diff options
author | Michael Rosenfeld <mrosenfeld@google.com> | 2018-05-01 16:02:14 -0700 |
---|---|---|
committer | Michael Rosenfeld <mrosenfeld@google.com> | 2018-05-04 17:57:28 -0700 |
commit | e581227468117791c0fc24f16c572540a78e5b45 (patch) | |
tree | c7ef09722e5a648d1f8241cb8fdf29b4d940192c | |
parent | 625f576bd4cc3a9732c33af903aaa83b324e9558 (diff) | |
download | platform_testing-e581227468117791c0fc24f16c572540a78e5b45.tar.gz |
Add support for battery-less devices to longevity.
Bug: 69068190
Test: included
Change-Id: Ife59b0726171bfa907fe240d1e82001516eb36d0
3 files changed, 141 insertions, 5 deletions
diff --git a/libraries/longevity/src/android/longevity/platform/LongevitySuite.java b/libraries/longevity/src/android/longevity/platform/LongevitySuite.java index 56b599551..cb83f7066 100644 --- a/libraries/longevity/src/android/longevity/platform/LongevitySuite.java +++ b/libraries/longevity/src/android/longevity/platform/LongevitySuite.java @@ -15,14 +15,22 @@ */ package android.longevity.platform; +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.longevity.platform.listener.BatteryTerminator; import android.longevity.platform.listener.ErrorTerminator; import android.longevity.platform.listener.TimeoutTerminator; +import android.os.BatteryManager; import android.os.Bundle; +import android.support.annotation.VisibleForTesting; import android.support.test.InstrumentationRegistry; import android.util.Log; + import java.util.HashMap; import java.util.Map; + import org.junit.runner.notification.RunNotifier; import org.junit.runners.model.InitializationError; import org.junit.runners.model.RunnerBuilder; @@ -35,6 +43,9 @@ import org.junit.runners.model.RunnerBuilder; public final class LongevitySuite extends android.longevity.core.LongevitySuite { private static final String LOG_TAG = LongevitySuite.class.getSimpleName(); + private Instrumentation mInstrumentation; + private Context mContext; + /** * Takes a {@link Bundle} and maps all String K/V pairs into a {@link Map<String, String>}. * @@ -59,14 +70,28 @@ public final class LongevitySuite extends android.longevity.core.LongevitySuite */ public LongevitySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError { - super(klass, builder, toMap(InstrumentationRegistry.getArguments())); + this(klass, builder, InstrumentationRegistry.getInstrumentation(), + InstrumentationRegistry.getContext(), InstrumentationRegistry.getArguments()); + } + + /** + * Used to pass in mock-able Android features for testing. + */ + @VisibleForTesting + public LongevitySuite(Class<?> klass, RunnerBuilder builder, + Instrumentation instrumentation, Context context, Bundle arguments) + throws InitializationError { + super(klass, builder, toMap(arguments)); + mInstrumentation = instrumentation; + mContext = context; } @Override public void run(final RunNotifier notifier) { - // Register the battery terminator available only on the platform library. - notifier.addListener( - new BatteryTerminator(notifier, mArguments, InstrumentationRegistry.getContext())); + // Register the battery terminator available only on the platform library, if present. + if (hasBattery()) { + notifier.addListener(new BatteryTerminator(notifier, mArguments, mContext)); + } // Register other listeners and continue with standard longevity run. super.run(notifier); } @@ -88,4 +113,13 @@ public final class LongevitySuite extends android.longevity.core.LongevitySuite final RunNotifier notifier) { return new TimeoutTerminator(notifier, mArguments); } + + /** + * Determines if the device has a battery attached. + */ + private boolean hasBattery () { + final Intent batteryInfo = + mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); + } } diff --git a/libraries/longevity/tests/src/android/longevity/core/LongevitySuiteTest.java b/libraries/longevity/tests/src/android/longevity/core/LongevitySuiteTest.java index 9ee64ef23..1b369056c 100644 --- a/libraries/longevity/tests/src/android/longevity/core/LongevitySuiteTest.java +++ b/libraries/longevity/tests/src/android/longevity/core/LongevitySuiteTest.java @@ -54,7 +54,7 @@ public class LongevitySuiteTest { private static class NoSuiteClassesSuite { } /** - * + * Tests that the {@link LongevitySuite} properly accounts for the number of tests in children. */ @Test public void testChildAccounting() throws InitializationError { diff --git a/libraries/longevity/tests/src/android/longevity/platform/LongevitySuiteTest.java b/libraries/longevity/tests/src/android/longevity/platform/LongevitySuiteTest.java new file mode 100644 index 000000000..8a7263f5e --- /dev/null +++ b/libraries/longevity/tests/src/android/longevity/platform/LongevitySuiteTest.java @@ -0,0 +1,102 @@ +/* + * 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 android.longevity.platform; + +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.os.BatteryManager; +import android.os.Bundle; + + +import org.junit.Before; +import org.junit.Test; +import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; +import org.junit.runner.RunWith; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.JUnit4; +import org.junit.runners.model.InitializationError; +import org.junit.runners.Suite.SuiteClasses; +import org.mockito.Mockito; + +/** + * Unit tests for the {@link LongevitySuite} runner. + */ +@RunWith(JUnit4.class) +public class LongevitySuiteTest { + private final Instrumentation mInstrumentation = Mockito.mock(Instrumentation.class); + private final Context mContext = Mockito.mock(Context.class); + + private final RunNotifier mRunNotifier = Mockito.spy(new RunNotifier()); + private final Intent mBatteryIntent = new Intent(); + + private LongevitySuite mSuite; + + @Before + public void setUpSuite() throws InitializationError { + // Android context mocking. + when(mContext.registerReceiver(any(), any())).thenReturn(mBatteryIntent); + // Create the core suite to test. + mSuite = new LongevitySuite(TestSuite.class, new AllDefaultPossibilitiesBuilder(true), + mInstrumentation, mContext, new Bundle()); + } + + /** + * Tests that devices with batteries terminate on low battery. + */ + @Test + public void testDeviceWithBattery_registersReceiver() { + mBatteryIntent.putExtra(BatteryManager.EXTRA_PRESENT, true); + mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, 1); + mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100); + mSuite.run(mRunNotifier); + verify(mRunNotifier).pleaseStop(); + } + + /** + * Tests that devices without batteries do not terminate on low battery. + */ + @Test + public void testDeviceWithoutBattery_doesNotRegisterReceiver() { + mBatteryIntent.putExtra(BatteryManager.EXTRA_PRESENT, false); + mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, 1); + mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100); + mSuite.run(mRunNotifier); + verify(mRunNotifier, never()).pleaseStop(); + } + + + @RunWith(LongevitySuite.class) + @SuiteClasses({ + TestSuite.BasicTest.class, + }) + /** + * Sample device-side test cases. + */ + public static class TestSuite { + // no local test cases. + + public static class BasicTest { + @Test + public void testNothing() { } + } + } +} |