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.RemoteException; 22 import android.os.connectivity.WifiBatteryStats; 23 import android.text.format.DateUtils; 24 25 import androidx.test.filters.SmallTest; 26 27 import com.android.internal.app.IBatteryStats; 28 import com.android.server.wifi.nano.WifiMetricsProto.WifiPowerStats; 29 import com.android.server.wifi.nano.WifiMetricsProto.WifiRadioUsage; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 import java.io.ByteArrayOutputStream; 37 import java.io.PrintWriter; 38 39 /** 40 * Unit tests for {@link com.android.server.wifi.WifiPowerMetrics}. 41 */ 42 @SmallTest 43 public class WifiPowerMetricsTest { 44 @Mock IBatteryStats 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 = new WifiBatteryStats(); 65 wifiBatteryStats.setLoggingDurationMs(loggingDuration); 66 wifiBatteryStats.setScanTimeMs(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 IBatteryStats 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 WifiRadioUsage has its fields set to the |DEFAULT_VALUE| when IBatteryStats 94 * throws a RemoteException 95 * @throws Exception 96 */ 97 @Test testBuildWifiRadioUsageProtoRemoteException()98 public void testBuildWifiRadioUsageProtoRemoteException() throws Exception { 99 doThrow(new RemoteException()).when(mBatteryStats).getWifiBatteryStats(); 100 WifiRadioUsage wifiRadioUsage = mWifiPowerMetrics.buildWifiRadioUsageProto(); 101 verify(mBatteryStats).getWifiBatteryStats(); 102 assertEquals("loggingDurationMs must be default value when getWifiBatteryStats throws " 103 + "RemoteException", DEFAULT_VALUE, wifiRadioUsage.loggingDurationMs); 104 assertEquals("scanTimeMs must be default value when getWifiBatteryStats throws " 105 + "RemoteException", DEFAULT_VALUE, wifiRadioUsage.scanTimeMs); 106 } 107 108 /** 109 * Tests that dump() pulls data from IBatteryStats 110 * @throws Exception 111 */ 112 @Test testDumpCallsAppropriateMethods()113 public void testDumpCallsAppropriateMethods() throws Exception { 114 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 115 PrintWriter writer = new PrintWriter(stream); 116 mWifiPowerMetrics.dump(writer); 117 verify(mBatteryStats, atLeastOnce()).getWifiBatteryStats(); 118 } 119 120 /** 121 * Tests that WifiPowerStats has its fields set according to the corresponding fields in 122 * WifiBatteryStats 123 * @throws Exception 124 */ 125 @Test testBuildProto()126 public void testBuildProto() throws Exception { 127 final long monitoredRailEnergyConsumedMaMs = 12000; 128 final double monitoredRailEnergyConsumedMah = monitoredRailEnergyConsumedMaMs 129 / ((double) DateUtils.HOUR_IN_MILLIS); 130 WifiBatteryStats wifiBatteryStats = new WifiBatteryStats(); 131 wifiBatteryStats.setEnergyConsumedMaMs(monitoredRailEnergyConsumedMaMs); 132 when(mBatteryStats.getWifiBatteryStats()).thenReturn(wifiBatteryStats); 133 WifiPowerStats wifiPowerStats = mWifiPowerMetrics.buildProto(); 134 verify(mBatteryStats).getWifiBatteryStats(); 135 assertEquals("monitoredRailEnergyConsumedMah must match with field from WifiPowerStats", 136 monitoredRailEnergyConsumedMah, wifiPowerStats.monitoredRailEnergyConsumedMah, 137 0.01); 138 } 139 140 /** 141 * Tests that WifiPowerStats has its fields set to the |DEFAULT_VALUE| when IBatteryStats 142 * returns null 143 * @throws Exception 144 */ 145 @Test testBuildProtoNull()146 public void testBuildProtoNull() throws Exception { 147 when(mBatteryStats.getWifiBatteryStats()).thenReturn(null); 148 WifiPowerStats wifiPowerStats = mWifiPowerMetrics.buildProto(); 149 verify(mBatteryStats).getWifiBatteryStats(); 150 assertEquals("monitoredRailEnergyConsumedMah must be default value when getWifiBatteryStats" 151 + " returns null", DEFAULT_VALUE, wifiPowerStats.monitoredRailEnergyConsumedMah, 152 0.01); 153 } 154 155 /** 156 * Tests that WifiPowerStats has its fields set to the |DEFAULT_VALUE| when IBatteryStats 157 * throws a RemoteException 158 * @throws Exception 159 */ 160 @Test testBuildProtoRemoteException()161 public void testBuildProtoRemoteException() throws Exception { 162 doThrow(new RemoteException()).when(mBatteryStats).getWifiBatteryStats(); 163 WifiPowerStats wifiPowerStats = mWifiPowerMetrics.buildProto(); 164 verify(mBatteryStats).getWifiBatteryStats(); 165 assertEquals("monitoredRailEnergyConsumedMah must be default value when getWifiBatteryStats" 166 + " throws RemoteException", DEFAULT_VALUE, 167 wifiPowerStats.monitoredRailEnergyConsumedMah, 0.01); 168 } 169 } 170