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
17package com.android.server.testing.shadows;
18
19import android.util.EventLog;
20
21import org.robolectric.annotation.Implementation;
22import org.robolectric.annotation.Implements;
23
24import java.util.Arrays;
25import java.util.LinkedHashSet;
26import java.util.List;
27
28@Implements(EventLog.class)
29public 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    /** Clears the entries */
44    public static void setUp() {
45        ENTRIES.clear();
46    }
47
48    public static class Entry {
49        public final int tag;
50        public final List<Object> values;
51
52        public Entry(int tag, List<Object> values) {
53            this.tag = tag;
54            this.values = values;
55        }
56
57        @Override
58        public boolean equals(Object o) {
59            if (this == o) return true;
60            if (o == null || getClass() != o.getClass()) return false;
61            Entry entry = (Entry) o;
62            return tag == entry.tag && values.equals(entry.values);
63        }
64
65        @Override
66        public int hashCode() {
67            int result = tag;
68            result = 31 * result + values.hashCode();
69            return result;
70        }
71    }
72}
73