blob: 5064b9bd91b9b326126c1d06a3a3f8746fbcadcf [file] [log] [blame]
Hugo Benichi50a84c62016-09-02 09:00:59 +09001/*
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
17package com.android.server.connectivity;
18
19import android.net.ConnectivityMetricsEvent;
Hugo Benichi50a84c62016-09-02 09:00:59 +090020import android.os.Bundle;
21import android.os.Parcel;
22import android.os.Parcelable;
23
Hugo Benichi95cb2262017-01-11 10:11:26 +090024import java.util.function.Consumer;
25
Hugo Benichi50a84c62016-09-02 09:00:59 +090026abstract public class MetricsTestUtil {
27 private MetricsTestUtil() {
28 }
29
Hugo Benichi95cb2262017-01-11 10:11:26 +090030 static ConnectivityMetricsEvent ev(Parcelable p) {
Hugo Benichiec27c4d2017-03-15 15:07:42 +090031 ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
32 ev.timestamp = 1L;
33 ev.data = p;
34 return ev;
Hugo Benichi50a84c62016-09-02 09:00:59 +090035 }
36
Hugo Benichi95cb2262017-01-11 10:11:26 +090037 static ConnectivityMetricsEvent describeIpEvent(Consumer<Parcel>... fs) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090038 Parcel p = Parcel.obtain();
Hugo Benichi95cb2262017-01-11 10:11:26 +090039 for (Consumer<Parcel> f : fs) {
40 f.accept(p);
Hugo Benichi50a84c62016-09-02 09:00:59 +090041 }
42 p.setDataPosition(0);
Hugo Benichi95cb2262017-01-11 10:11:26 +090043 return ev(p.readParcelable(ClassLoader.getSystemClassLoader()));
Hugo Benichi50a84c62016-09-02 09:00:59 +090044 }
45
Hugo Benichi95cb2262017-01-11 10:11:26 +090046 static Consumer<Parcel> aType(Class<?> c) {
47 return aString(c.getName());
Hugo Benichi50a84c62016-09-02 09:00:59 +090048 }
49
Hugo Benichi95cb2262017-01-11 10:11:26 +090050 static Consumer<Parcel> aBool(boolean b) {
Hugo Benichi50a84c62016-09-02 09:00:59 +090051 return aByte((byte) (b ? 1 : 0));
52 }
53
Hugo Benichi95cb2262017-01-11 10:11:26 +090054 static Consumer<Parcel> aByte(byte b) {
55 return (p) -> p.writeByte(b);
Hugo Benichi50a84c62016-09-02 09:00:59 +090056 }
57
Hugo Benichi95cb2262017-01-11 10:11:26 +090058 static Consumer<Parcel> anInt(int i) {
59 return (p) -> p.writeInt(i);
Hugo Benichi50a84c62016-09-02 09:00:59 +090060 }
61
Hugo Benichi95cb2262017-01-11 10:11:26 +090062 static Consumer<Parcel> aLong(long l) {
63 return (p) -> p.writeLong(l);
Hugo Benichi50a84c62016-09-02 09:00:59 +090064 }
65
Hugo Benichi95cb2262017-01-11 10:11:26 +090066 static Consumer<Parcel> aString(String s) {
67 return (p) -> p.writeString(s);
Hugo Benichi50a84c62016-09-02 09:00:59 +090068 }
69
Hugo Benichi95cb2262017-01-11 10:11:26 +090070 static Consumer<Parcel> aByteArray(byte... ary) {
71 return (p) -> p.writeByteArray(ary);
Hugo Benichi50a84c62016-09-02 09:00:59 +090072 }
73
Hugo Benichi95cb2262017-01-11 10:11:26 +090074 static Consumer<Parcel> anIntArray(int... ary) {
75 return (p) -> p.writeIntArray(ary);
Hugo Benichi50a84c62016-09-02 09:00:59 +090076 }
77
78 static byte b(int i) {
79 return (byte) i;
80 }
81}