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.shadows;
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 import java.util.Set;
28 
29 /** Don't forget to call {@link ShadowEventLog#setUp()} before every test. */
30 @Implements(EventLog.class)
31 public class ShadowEventLog {
32     private static final LinkedHashSet<Entry> ENTRIES = new LinkedHashSet<>();
33 
34     @Implementation
writeEvent(int tag, Object... values)35     protected static int writeEvent(int tag, Object... values) {
36         ENTRIES.add(new Entry(tag, Arrays.asList(values)));
37         // Currently we don't care about the return value, if we do, estimate it correctly
38         return 0;
39     }
40 
41     @Implementation
writeEvent(int tag, String string)42     protected static int writeEvent(int tag, String string) {
43         return writeEvent(tag, (Object) string);
44     }
45 
getEntries()46     public static Set<Entry> getEntries() {
47         return new LinkedHashSet<>(ENTRIES);
48     }
49 
50     /** Clears the entries. */
setUp()51     public static void setUp() {
52         ENTRIES.clear();
53     }
54 
55     public static class Entry {
56         public final int tag;
57         public final List<Object> values;
58 
Entry(int tag, List<Object> values)59         public Entry(int tag, List<Object> values) {
60             this.tag = tag;
61             this.values = values;
62         }
63 
64         @Override
equals(Object o)65         public boolean equals(Object o) {
66             if (this == o) return true;
67             if (o == null || getClass() != o.getClass()) return false;
68             Entry entry = (Entry) o;
69             return tag == entry.tag && values.equals(entry.values);
70         }
71 
72         @Override
hashCode()73         public int hashCode() {
74             int result = tag;
75             result = 31 * result + values.hashCode();
76             return result;
77         }
78 
79         @Override
toString()80         public String toString() {
81             return "Entry{" + tag + ", " + values + '}';
82         }
83     }
84 }
85