1 /* 2 * Copyright (C) 2016 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 com.android.server.connectivity; 18 19 import android.net.ConnectivityMetricsEvent; 20 import android.os.Bundle; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.util.function.Consumer; 25 26 abstract public class MetricsTestUtil { MetricsTestUtil()27 private MetricsTestUtil() { 28 } 29 ev(Parcelable p)30 static ConnectivityMetricsEvent ev(Parcelable p) { 31 ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent(); 32 ev.timestamp = 1L; 33 ev.data = p; 34 return ev; 35 } 36 describeIpEvent(Consumer<Parcel>.... fs)37 static ConnectivityMetricsEvent describeIpEvent(Consumer<Parcel>... fs) { 38 Parcel p = Parcel.obtain(); 39 for (Consumer<Parcel> f : fs) { 40 f.accept(p); 41 } 42 p.setDataPosition(0); 43 return ev(p.readParcelable(ClassLoader.getSystemClassLoader())); 44 } 45 aType(Class<?> c)46 static Consumer<Parcel> aType(Class<?> c) { 47 return aString(c.getName()); 48 } 49 aBool(boolean b)50 static Consumer<Parcel> aBool(boolean b) { 51 return aByte((byte) (b ? 1 : 0)); 52 } 53 aByte(byte b)54 static Consumer<Parcel> aByte(byte b) { 55 return (p) -> p.writeByte(b); 56 } 57 anInt(int i)58 static Consumer<Parcel> anInt(int i) { 59 return (p) -> p.writeInt(i); 60 } 61 aLong(long l)62 static Consumer<Parcel> aLong(long l) { 63 return (p) -> p.writeLong(l); 64 } 65 aString(String s)66 static Consumer<Parcel> aString(String s) { 67 return (p) -> p.writeString(s); 68 } 69 aByteArray(byte... ary)70 static Consumer<Parcel> aByteArray(byte... ary) { 71 return (p) -> p.writeByteArray(ary); 72 } 73 anIntArray(int... ary)74 static Consumer<Parcel> anIntArray(int... ary) { 75 return (p) -> p.writeIntArray(ary); 76 } 77 b(int i)78 static byte b(int i) { 79 return (byte) i; 80 } 81 } 82