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