Hugo Benichi | 50a84c6 | 2016-09-02 09:00:59 +0900 | [diff] [blame] | 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.net.ConnectivityMetricsLogger; |
| 21 | import android.os.Bundle; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
| 25 | abstract public class MetricsTestUtil { |
| 26 | private MetricsTestUtil() { |
| 27 | } |
| 28 | |
| 29 | static ConnectivityMetricsEvent ipEv(Parcelable p) { |
| 30 | return ev(ConnectivityMetricsLogger.COMPONENT_TAG_CONNECTIVITY, p); |
| 31 | } |
| 32 | |
| 33 | static ConnectivityMetricsEvent telephonyEv() { |
| 34 | return ev(ConnectivityMetricsLogger.COMPONENT_TAG_TELEPHONY, new Bundle()); |
| 35 | } |
| 36 | |
| 37 | static ConnectivityMetricsEvent ev(int tag, Parcelable p) { |
| 38 | return new ConnectivityMetricsEvent(1L, tag, 0, p); |
| 39 | } |
| 40 | |
| 41 | // Utiliy interface for describing the content of a Parcel. This relies on |
| 42 | // the implementation defails of Parcelable and on the fact that the fully |
| 43 | // qualified Parcelable class names are written as string in the Parcels. |
| 44 | interface ParcelField { |
| 45 | void write(Parcel p); |
| 46 | } |
| 47 | |
| 48 | static ConnectivityMetricsEvent describeIpEvent(ParcelField... fs) { |
| 49 | Parcel p = Parcel.obtain(); |
| 50 | for (ParcelField f : fs) { |
| 51 | f.write(p); |
| 52 | } |
| 53 | p.setDataPosition(0); |
| 54 | return ipEv(p.readParcelable(ClassLoader.getSystemClassLoader())); |
| 55 | } |
| 56 | |
| 57 | static ParcelField aType(Class<?> c) { |
| 58 | return new ParcelField() { |
| 59 | public void write(Parcel p) { |
| 60 | p.writeString(c.getName()); |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | static ParcelField aBool(boolean b) { |
| 66 | return aByte((byte) (b ? 1 : 0)); |
| 67 | } |
| 68 | |
| 69 | static ParcelField aByte(byte b) { |
| 70 | return new ParcelField() { |
| 71 | public void write(Parcel p) { |
| 72 | p.writeByte(b); |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | static ParcelField anInt(int i) { |
| 78 | return new ParcelField() { |
| 79 | public void write(Parcel p) { |
| 80 | p.writeInt(i); |
| 81 | } |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | static ParcelField aLong(long l) { |
| 86 | return new ParcelField() { |
| 87 | public void write(Parcel p) { |
| 88 | p.writeLong(l); |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | static ParcelField aString(String s) { |
| 94 | return new ParcelField() { |
| 95 | public void write(Parcel p) { |
| 96 | p.writeString(s); |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | static ParcelField aByteArray(byte... ary) { |
| 102 | return new ParcelField() { |
| 103 | public void write(Parcel p) { |
| 104 | p.writeByteArray(ary); |
| 105 | } |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | static ParcelField anIntArray(int... ary) { |
| 110 | return new ParcelField() { |
| 111 | public void write(Parcel p) { |
| 112 | p.writeIntArray(ary); |
| 113 | } |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | static byte b(int i) { |
| 118 | return (byte) i; |
| 119 | } |
| 120 | } |