1 /* 2 * Copyright (C) 2017 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 package android.longevity.platform; 17 18 import static org.mockito.Mockito.any; 19 import static org.mockito.Mockito.never; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.Instrumentation; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.BatteryManager; 27 import android.os.Bundle; 28 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.internal.builders.AllDefaultPossibilitiesBuilder; 33 import org.junit.runner.RunWith; 34 import org.junit.runner.notification.RunNotifier; 35 import org.junit.runners.JUnit4; 36 import org.junit.runners.model.InitializationError; 37 import org.junit.runners.Suite.SuiteClasses; 38 import org.mockito.Mockito; 39 40 /** 41 * Unit tests for the {@link LongevitySuite} runner. 42 */ 43 @RunWith(JUnit4.class) 44 public class LongevitySuiteTest { 45 private final Instrumentation mInstrumentation = Mockito.mock(Instrumentation.class); 46 private final Context mContext = Mockito.mock(Context.class); 47 48 private final RunNotifier mRunNotifier = Mockito.spy(new RunNotifier()); 49 private final Intent mBatteryIntent = new Intent(); 50 51 private LongevitySuite mSuite; 52 53 @Before 54 public void setUpSuite() throws InitializationError { 55 // Android context mocking. 56 when(mContext.registerReceiver(any(), any())).thenReturn(mBatteryIntent); 57 // Create the core suite to test. 58 mSuite = new LongevitySuite(TestSuite.class, new AllDefaultPossibilitiesBuilder(true), 59 mInstrumentation, mContext, new Bundle()); 60 } 61 62 /** 63 * Tests that devices with batteries terminate on low battery. 64 */ 65 @Test 66 public void testDeviceWithBattery_registersReceiver() { 67 mBatteryIntent.putExtra(BatteryManager.EXTRA_PRESENT, true); 68 mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, 1); 69 mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100); 70 mSuite.run(mRunNotifier); 71 verify(mRunNotifier).pleaseStop(); 72 } 73 74 /** 75 * Tests that devices without batteries do not terminate on low battery. 76 */ 77 @Test 78 public void testDeviceWithoutBattery_doesNotRegisterReceiver() { 79 mBatteryIntent.putExtra(BatteryManager.EXTRA_PRESENT, false); 80 mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, 1); 81 mBatteryIntent.putExtra(BatteryManager.EXTRA_SCALE, 100); 82 mSuite.run(mRunNotifier); 83 verify(mRunNotifier, never()).pleaseStop(); 84 } 85 86 87 @RunWith(LongevitySuite.class) 88 @SuiteClasses({ 89 TestSuite.BasicTest.class, 90 }) 91 /** 92 * Sample device-side test cases. 93 */ 94 public static class TestSuite { 95 // no local test cases. 96 97 public static class BasicTest { 98 @Test 99 public void testNothing() { } 100 } 101 } 102 } 103