1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18 package com.android.internal.os; 19 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.os.BatteryStats; 25 import android.support.test.filters.SmallTest; 26 import android.support.test.runner.AndroidJUnit4; 27 28 import junit.framework.TestCase; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 @RunWith(AndroidJUnit4.class) 37 @SmallTest 38 public class PowerCalculatorTest extends TestCase { 39 private static final long US_IN_HR = 1000L * 1000L * 60L * 60L; 40 41 @Mock 42 private PowerProfile mPowerProfile; 43 44 @Before 45 public void setUp() { 46 MockitoAnnotations.initMocks(this); 47 } 48 49 /** Test {@link MediaPowerCalculator#calculateApp} */ 50 @Test 51 public void testMediaPowerCalculator() { 52 when(mPowerProfile.getAveragePower(PowerProfile.POWER_AUDIO)).thenReturn(12.0); 53 when(mPowerProfile.getAveragePower(PowerProfile.POWER_VIDEO)).thenReturn(25.0); 54 55 BatteryStats.Uid u = mock(BatteryStats.Uid.class); 56 BatteryStats.Timer audioTimer = mock(BatteryStats.Timer.class); 57 when(u.getAudioTurnedOnTimer()).thenReturn(audioTimer); 58 when(audioTimer.getTotalTimeLocked(2L * US_IN_HR, 0)).thenReturn(2L * US_IN_HR); 59 BatteryStats.Timer videoTimer = mock(BatteryStats.Timer.class); 60 when(u.getVideoTurnedOnTimer()).thenReturn(videoTimer); 61 when(videoTimer.getTotalTimeLocked(2L * US_IN_HR, 0)).thenReturn(1L * US_IN_HR); 62 63 MediaPowerCalculator mediaPowerCalculator = new MediaPowerCalculator(mPowerProfile); 64 BatterySipper app = new BatterySipper(BatterySipper.DrainType.APP, null, 0); 65 66 mediaPowerCalculator.calculateApp(app, u, 2L * US_IN_HR, 2L * US_IN_HR, 0); 67 assertEquals(49.0, app.sumPower()); 68 } 69 70 71 } 72