1 /* 2 * Copyright (C) 2007 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 android.util; 18 19 import android.os.Process; 20 21 import com.google.android.collect.Lists; 22 23 import junit.framework.TestCase; 24 import junit.framework.Assert; 25 26 import java.util.ArrayList; 27 28 /** 29 * Functional tests of EventLog. 30 */ 31 32 public class EventLogFunctionalTest extends TestCase { 33 private static final String TAG = "EventLogFunctionalTest"; 34 35 private static final int TAG_SIZE = 4; 36 private static final int TYPE_FIELD_SIZE = 1; 37 private static final int STARTING_POS_OF_PAYLOAD = TAG_SIZE + TYPE_FIELD_SIZE; 38 39 private static final int TEST_TAG = 42; 40 private static final int TEST_TAG2 = 314; 41 42 //todo: For now all we do is test the returned length. More to come. testLogOfPosInt()43 public void testLogOfPosInt() throws Exception { 44 final int numBytes = EventLog.writeEvent(TEST_TAG, 0x01020304); 45 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 4, numBytes); 46 } 47 48 //todo: For now all we do is test the returned length. More to come. testLogOfPosLong()49 public void testLogOfPosLong() throws Exception { 50 final int numBytes = EventLog.writeEvent(TEST_TAG2, 0x0102030405060708L); 51 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 8, numBytes); 52 } 53 54 //todo: For now all we do is test the returned length. More to come. testLogOfString()55 public void testLogOfString() throws Exception { 56 final String valueStr = "foo bar baz"; 57 final int numBytes = EventLog.writeEvent(TEST_TAG, valueStr); 58 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 4 + valueStr.length() + 1, numBytes); 59 } 60 testLogOfListWithOneInt()61 public void testLogOfListWithOneInt() throws Exception { 62 final EventLog.List list = new EventLog.List(1234); 63 final int numBytes = EventLog.writeEvent(TEST_TAG, list); 64 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + 1 + 4 + 1, numBytes); 65 } 66 testLogOfListWithMultipleInts()67 public void testLogOfListWithMultipleInts() throws Exception { 68 final EventLog.List list = new EventLog.List(1234, 2345, 3456); 69 final int numBytes = EventLog.writeEvent(TEST_TAG, list); 70 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + 1 + 4 + 1 + 4 + 1 + 4 + 1, numBytes); 71 } 72 testLogOfListWithEmbeddedList()73 public void testLogOfListWithEmbeddedList() throws Exception { 74 final EventLog.List list = new EventLog.List( 75 new EventLog.List(1234, 2345, 3456)); 76 final int numBytes = EventLog.writeEvent(TEST_TAG, list); 77 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 2 + 1 + 1 + 4 + 1 + 4 + 1 + 4 + 1, numBytes); 78 } 79 testEventLargerThanInitialBufferCapacity()80 public void testEventLargerThanInitialBufferCapacity() throws Exception { 81 final Integer[] array = new Integer[127]; 82 for (int i = 0; i < array.length; i++) { 83 array[i] = i; 84 } 85 final EventLog.List list = new EventLog.List((Object[]) array); 86 final int numBytes = EventLog.writeEvent(TEST_TAG, list); 87 Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + (5 * array.length) + 1, numBytes); 88 } 89 90 // This test is obsolete. See http://b/issue?id=1262082 disableTestReadSimpleEvent()91 public void disableTestReadSimpleEvent() throws Exception { 92 long when = System.currentTimeMillis(); 93 EventLog.writeEvent(2718, 12345); 94 Log.i(TAG, "Wrote simple event at T=" + when); 95 96 ArrayList<EventLog.Event> list = new ArrayList<EventLog.Event>(); 97 EventLog.readEvents(new int[] { 2718 }, list); 98 99 boolean found = false; 100 for (EventLog.Event event : list) { 101 assertEquals(event.getTag(), 2718); 102 long eventTime = event.getTimeNanos() / 1000000; 103 Log.i(TAG, " Found event T=" + eventTime); 104 if (eventTime > when - 100 && eventTime < when + 1000) { 105 assertEquals(event.getProcessId(), Process.myPid()); 106 assertEquals(event.getThreadId(), Process.myTid()); 107 assertEquals(event.getData(), 12345); 108 109 assertFalse(found); 110 found = true; 111 } 112 } 113 114 assertTrue(found); 115 } 116 117 // This test is obsolete. See http://b/issue?id=1262082 disableTestReadCompoundEntry()118 public void disableTestReadCompoundEntry() throws Exception { 119 long when = System.currentTimeMillis(); 120 EventLog.writeEvent(2719, 121 new EventLog.List(1l, new EventLog.List("2", "three", "4"), 5)); 122 Log.i(TAG, "Wrote compound event at T=" + when); 123 124 ArrayList<EventLog.Event> list = new ArrayList<EventLog.Event>(); 125 EventLog.readEvents(new int[] { 2719 }, list); 126 127 boolean found = false; 128 for (EventLog.Event event : list) { 129 long eventTime = event.getTimeNanos() / 1000000; 130 Log.i(TAG, " Found event T=" + eventTime); 131 if (eventTime > when - 100 && eventTime < when + 1000) { 132 EventLog.List data = (EventLog.List) event.getData(); 133 assertEquals(data.getNumItems(), 3); 134 135 EventLog.List nested = (EventLog.List) data.getItem(1); 136 assertEquals(nested.getNumItems(), 3); 137 138 assertEquals(data.getItem(0), 1l); 139 assertEquals(nested.getItem(0), "2"); 140 assertEquals(nested.getItem(1), "three"); 141 assertEquals(nested.getItem(2), "4"); 142 assertEquals(data.getItem(2), 5); 143 144 assertFalse(found); 145 found = true; 146 } 147 } 148 149 assertTrue(found); 150 } 151 testEventLogTagsFile()152 public void testEventLogTagsFile() throws Exception { 153 EventLogTags tags = new EventLogTags(); 154 assertEquals(tags.get("answer").mTag, 42); 155 assertEquals(tags.get("pi").mTag, 314); 156 assertEquals(tags.get("e").mTag, 2718); 157 assertEquals(tags.get(42).mName, "answer"); 158 assertEquals(tags.get(314).mName, "pi"); 159 assertEquals(tags.get(2718).mName, "e"); 160 } 161 } 162