Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.testing; |
| 18 | |
| 19 | import android.util.EventLog; |
| 20 | |
| 21 | import org.robolectric.annotation.Implementation; |
| 22 | import org.robolectric.annotation.Implements; |
| 23 | |
| 24 | import java.util.Arrays; |
| 25 | import java.util.LinkedHashSet; |
| 26 | import java.util.List; |
| 27 | |
| 28 | @Implements(EventLog.class) |
| 29 | public class ShadowEventLog { |
| 30 | private final static LinkedHashSet<Entry> ENTRIES = new LinkedHashSet<>(); |
| 31 | |
| 32 | @Implementation |
| 33 | public static int writeEvent(int tag, Object... values) { |
| 34 | ENTRIES.add(new Entry(tag, Arrays.asList(values))); |
| 35 | // Currently we don't care about the return value, if we do, estimate it correctly |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | public static boolean hasEvent(int tag, Object... values) { |
| 40 | return ENTRIES.contains(new Entry(tag, Arrays.asList(values))); |
| 41 | } |
| 42 | |
| 43 | public static void clearEvents() { |
| 44 | ENTRIES.clear(); |
| 45 | } |
| 46 | |
| 47 | public static class Entry { |
| 48 | public final int tag; |
| 49 | public final List<Object> values; |
| 50 | |
| 51 | public Entry(int tag, List<Object> values) { |
| 52 | this.tag = tag; |
| 53 | this.values = values; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean equals(Object o) { |
| 58 | if (this == o) return true; |
| 59 | if (o == null || getClass() != o.getClass()) return false; |
| 60 | Entry entry = (Entry) o; |
| 61 | return tag == entry.tag && values.equals(entry.values); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int hashCode() { |
| 66 | int result = tag; |
| 67 | result = 31 * result + values.hashCode(); |
| 68 | return result; |
| 69 | } |
| 70 | } |
| 71 | } |