1 /* 2 * Copyright 2022 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 androidx.benchmark.macro 18 19 import androidx.annotation.RestrictTo 20 import androidx.benchmark.DeviceInfo 21 22 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) 23 object BatteryCharge { 24 25 private const val MIN_BATTERY_PERCENT = 50 26 hasMinimumChargenull27 fun hasMinimumCharge(throwOnMissingMetrics: Boolean = false): Boolean { 28 if (DeviceInfo.initialBatteryPercent >= MIN_BATTERY_PERCENT) { 29 return true 30 } 31 32 /** 33 * Checks if battery is at least halfway full on test device. 34 * 35 * @throws AssertionError if `throwOnMissingMetrics == true` and charge is too low. 36 */ 37 if (throwOnMissingMetrics) { 38 throw AssertionError( 39 """ 40 ERROR: Device has low battery (${DeviceInfo.initialBatteryPercent}) 41 When battery is low during a test that involves disconnecting the charge, devices 42 risk running out of power during the test. Wait for your battery to charge to at 43 least $MIN_BATTERY_PERCENT%. Currently at ${DeviceInfo.initialBatteryPercent}%. 44 """ 45 .trimIndent() 46 ) 47 } 48 49 return false 50 } 51 } 52