1 /* 2 * Copyright (C) 2018 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 com.android.server.wifi; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.mockito.Mockito.*; 20 21 import android.os.BatteryStatsManager; 22 import android.os.connectivity.WifiBatteryStats; 23 import android.text.format.DateUtils; 24 25 import androidx.test.filters.SmallTest; 26 27 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiPowerStats; 28 import com.android.server.wifi.proto.nano.WifiMetricsProto.WifiRadioUsage; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 35 import java.io.ByteArrayOutputStream; 36 import java.io.PrintWriter; 37 38 /** 39 * Unit tests for {@link com.android.server.wifi.WifiPowerMetrics}. 40 */ 41 @SmallTest 42 public class WifiPowerMetricsTest extends WifiBaseTest { 43 @Mock 44 BatteryStatsManager mBatteryStats; 45 WifiPowerMetrics mWifiPowerMetrics; 46 47 private static final long DEFAULT_VALUE = 0; 48 49 @Before setUp()50 public void setUp() throws Exception { 51 MockitoAnnotations.initMocks(this); 52 mWifiPowerMetrics = new WifiPowerMetrics(mBatteryStats); 53 } 54 55 /** 56 * Tests that WifiRadioUsage has its fields set according to the corresponding fields in 57 * WifiBatteryStats 58 * @throws Exception 59 */ 60 @Test testBuildWifiRadioUsageProto()61 public void testBuildWifiRadioUsageProto() throws Exception { 62 final long loggingDuration = 280; 63 final long scanTime = 23; 64 WifiBatteryStats wifiBatteryStats = mock(WifiBatteryStats.class); 65 when(wifiBatteryStats.getLoggingDurationMillis()).thenReturn(loggingDuration); 66 when(wifiBatteryStats.getScanTimeMillis()).thenReturn(scanTime); 67 when(mBatteryStats.getWifiBatteryStats()).thenReturn(wifiBatteryStats); 68 WifiRadioUsage wifiRadioUsage = mWifiPowerMetrics.buildWifiRadioUsageProto(); 69 verify(mBatteryStats).getWifiBatteryStats(); 70 assertEquals("loggingDurationMs must match with field from WifiBatteryStats", 71 loggingDuration, wifiRadioUsage.loggingDurationMs); 72 assertEquals("scanTimeMs must match with field from WifiBatteryStats", 73 scanTime, wifiRadioUsage.scanTimeMs); 74 } 75 76 /** 77 * Tests that WifiRadioUsage has its fields set to the |DEFAULT_VALUE| when BatteryStatsManager 78 * returns null 79 * @throws Exception 80 */ 81 @Test testBuildWifiRadioUsageProtoNull()82 public void testBuildWifiRadioUsageProtoNull() throws Exception { 83 when(mBatteryStats.getWifiBatteryStats()).thenReturn(null); 84 WifiRadioUsage wifiRadioUsage = mWifiPowerMetrics.buildWifiRadioUsageProto(); 85 verify(mBatteryStats).getWifiBatteryStats(); 86 assertEquals("loggingDurationMs must be default value when getWifiBatteryStats returns " 87 + "null", DEFAULT_VALUE, wifiRadioUsage.loggingDurationMs); 88 assertEquals("scanTimeMs must be default value when getWifiBatteryStats returns null", 89 DEFAULT_VALUE, wifiRadioUsage.scanTimeMs); 90 } 91 92 /** 93 * Tests that dump() pulls data from BatteryStatsManager 94 * @throws Exception 95 */ 96 @Test testDumpCallsAppropriateMethods()97 public void testDumpCallsAppropriateMethods() throws Exception { 98 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 99 PrintWriter writer = new PrintWriter(stream); 100 mWifiPowerMetrics.dump(writer); 101 verify(mBatteryStats, atLeastOnce()).getWifiBatteryStats(); 102 } 103 104 /** 105 * Tests that WifiPowerStats has its fields set according to the corresponding fields in 106 * WifiBatteryStats 107 * @throws Exception 108 */ 109 @Test testBuildProto()110 public void testBuildProto() throws Exception { 111 final long monitoredRailEnergyConsumedMaMs = 12000; 112 final long numBytesTx = 65000; 113 final long numBytesRx = 4560000; 114 final long numPacketsTx = 3456; 115 final long numPacketsRx = 5436456; 116 final long txTimeMs = 2300; 117 final long rxTimeMs = 343258; 118 final long idleTimeMs = 32322233; 119 final long scanTimeMs = 345566; 120 final long sleepTimeMs = 323270343; 121 final double monitoredRailEnergyConsumedMah = monitoredRailEnergyConsumedMaMs 122 / ((double) DateUtils.HOUR_IN_MILLIS); 123 WifiBatteryStats wifiBatteryStats = mock(WifiBatteryStats.class); 124 when(wifiBatteryStats.getEnergyConsumedMaMillis()) 125 .thenReturn(monitoredRailEnergyConsumedMaMs); 126 when(wifiBatteryStats.getNumBytesTx()).thenReturn(numBytesTx); 127 when(wifiBatteryStats.getNumBytesRx()).thenReturn(numBytesRx); 128 when(wifiBatteryStats.getNumPacketsTx()).thenReturn(numPacketsTx); 129 when(wifiBatteryStats.getNumPacketsRx()).thenReturn(numPacketsRx); 130 when(wifiBatteryStats.getTxTimeMillis()).thenReturn(txTimeMs); 131 when(wifiBatteryStats.getRxTimeMillis()).thenReturn(rxTimeMs); 132 when(wifiBatteryStats.getIdleTimeMillis()).thenReturn(idleTimeMs); 133 when(wifiBatteryStats.getScanTimeMillis()).thenReturn(scanTimeMs); 134 when(wifiBatteryStats.getSleepTimeMillis()).thenReturn(sleepTimeMs); 135 136 when(mBatteryStats.getWifiBatteryStats()).thenReturn(wifiBatteryStats); 137 WifiPowerStats wifiPowerStats = mWifiPowerMetrics.buildProto(); 138 verify(mBatteryStats).getWifiBatteryStats(); 139 assertEquals("monitoredRailEnergyConsumedMah must match with field from WifiPowerStats", 140 monitoredRailEnergyConsumedMah, wifiPowerStats.monitoredRailEnergyConsumedMah, 141 0.01); 142 assertEquals("numBytesTx must match with field from WifiBatteryStats", 143 numBytesTx, wifiPowerStats.numBytesTx); 144 assertEquals("numBytesRx must match with field from WifiBatteryStats", 145 numBytesRx, wifiPowerStats.numBytesRx); 146 assertEquals("numPacketsTx must match with field from WifiBatteryStats", 147 numPacketsTx, wifiPowerStats.numPacketsTx); 148 assertEquals("numPacketsRx must match with field from WifiBatteryStats", 149 numPacketsRx, wifiPowerStats.numPacketsRx); 150 assertEquals("txTimeMs must match with field from WifiBatteryStats", 151 txTimeMs, wifiPowerStats.txTimeMs); 152 assertEquals("rxTimeMs must match with field from WifiBatteryStats", 153 rxTimeMs, wifiPowerStats.rxTimeMs); 154 assertEquals("idleTimeMs must match with field from WifiBatteryStats", 155 idleTimeMs, wifiPowerStats.idleTimeMs); 156 assertEquals("scanTimeMs must match with field from WifiBatteryStats", 157 scanTimeMs, wifiPowerStats.scanTimeMs); 158 assertEquals("sleepTimeMs must match with field from WifiBatteryStats", 159 sleepTimeMs, wifiPowerStats.sleepTimeMs); 160 } 161 162 /** 163 * Tests that WifiPowerStats has its fields set to the |DEFAULT_VALUE| when BatteryStatsManager 164 * returns null 165 * @throws Exception 166 */ 167 @Test testBuildProtoNull()168 public void testBuildProtoNull() throws Exception { 169 when(mBatteryStats.getWifiBatteryStats()).thenReturn(null); 170 WifiPowerStats wifiPowerStats = mWifiPowerMetrics.buildProto(); 171 verify(mBatteryStats).getWifiBatteryStats(); 172 assertEquals("monitoredRailEnergyConsumedMah must be default value when getWifiBatteryStats" 173 + " returns null", DEFAULT_VALUE, wifiPowerStats.monitoredRailEnergyConsumedMah, 174 0.01); 175 } 176 } 177