Home | History | Annotate | Download | only in connectivity
      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 {
     27     private MetricsTestUtil() {
     28     }
     29 
     30     static ConnectivityMetricsEvent ev(Parcelable p) {
     31         ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
     32         ev.timestamp = 1L;
     33         ev.data = p;
     34         return ev;
     35     }
     36 
     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 
     46     static Consumer<Parcel> aType(Class<?> c) {
     47         return aString(c.getName());
     48     }
     49 
     50     static Consumer<Parcel> aBool(boolean b) {
     51         return aByte((byte) (b ? 1 : 0));
     52     }
     53 
     54     static Consumer<Parcel> aByte(byte b) {
     55         return (p) -> p.writeByte(b);
     56     }
     57 
     58     static Consumer<Parcel> anInt(int i) {
     59         return (p) -> p.writeInt(i);
     60     }
     61 
     62     static Consumer<Parcel> aLong(long l) {
     63         return (p) -> p.writeLong(l);
     64     }
     65 
     66     static Consumer<Parcel> aString(String s) {
     67         return (p) -> p.writeString(s);
     68     }
     69 
     70     static Consumer<Parcel> aByteArray(byte... ary) {
     71         return (p) -> p.writeByteArray(ary);
     72     }
     73 
     74     static Consumer<Parcel> anIntArray(int... ary) {
     75         return (p) -> p.writeIntArray(ary);
     76     }
     77 
     78     static byte b(int i) {
     79         return (byte) i;
     80     }
     81 }
     82