1 /* 2 * Copyright (C) 2025 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.power.stats; 17 18 import static org.mockito.Mockito.mock; 19 import static org.mockito.Mockito.when; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.net.NetworkStats; 24 import android.platform.test.ravenwood.RavenwoodRule; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public class NetworkStatsTestUtils { 30 /** 31 * Equivalent to NetworkStats.subtract, reimplementing the method for Ravenwood tests. 32 */ 33 @NonNull networkStatsDelta(@onNull NetworkStats currentStats, @Nullable NetworkStats lastStats)34 public static NetworkStats networkStatsDelta(@NonNull NetworkStats currentStats, 35 @Nullable NetworkStats lastStats) { 36 if (!RavenwoodRule.isOnRavenwood()) { 37 if (lastStats == null) { 38 return currentStats; 39 } 40 return currentStats.subtract(lastStats); 41 } 42 43 List<NetworkStats.Entry> entries = new ArrayList<>(); 44 for (NetworkStats.Entry entry : currentStats) { 45 NetworkStats.Entry lastEntry = null; 46 int uid = entry.getUid(); 47 if (lastStats != null) { 48 for (NetworkStats.Entry e : lastStats) { 49 if (e.getUid() == uid && e.getSet() == entry.getSet() 50 && e.getTag() == entry.getTag() 51 && e.getMetered() == entry.getMetered() 52 && e.getRoaming() == entry.getRoaming() 53 && e.getDefaultNetwork() == entry.getDefaultNetwork() 54 /*&& Objects.equals(e.getIface(), entry.getIface())*/) { 55 lastEntry = e; 56 break; 57 } 58 } 59 } 60 long rxBytes, rxPackets, txBytes, txPackets; 61 if (lastEntry != null) { 62 rxBytes = Math.max(0, entry.getRxBytes() - lastEntry.getRxBytes()); 63 rxPackets = Math.max(0, entry.getRxPackets() - lastEntry.getRxPackets()); 64 txBytes = Math.max(0, entry.getTxBytes() - lastEntry.getTxBytes()); 65 txPackets = Math.max(0, entry.getTxPackets() - lastEntry.getTxPackets()); 66 } else { 67 rxBytes = entry.getRxBytes(); 68 rxPackets = entry.getRxPackets(); 69 txBytes = entry.getTxBytes(); 70 txPackets = entry.getTxPackets(); 71 } 72 73 NetworkStats.Entry uidEntry = mock(NetworkStats.Entry.class); 74 when(uidEntry.getUid()).thenReturn(uid); 75 when(uidEntry.getRxBytes()).thenReturn(rxBytes); 76 when(uidEntry.getRxPackets()).thenReturn(rxPackets); 77 when(uidEntry.getTxBytes()).thenReturn(txBytes); 78 when(uidEntry.getTxPackets()).thenReturn(txPackets); 79 80 entries.add(uidEntry); 81 } 82 NetworkStats delta = mock(NetworkStats.class); 83 when(delta.iterator()).thenAnswer(inv -> entries.iterator()); 84 return delta; 85 } 86 } 87