Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.ahat; |
| 18 | |
Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 19 | import com.android.tools.perflib.heap.ArrayInstance; |
| 20 | import com.android.tools.perflib.heap.ClassObj; |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 21 | import com.android.tools.perflib.heap.Instance; |
| 22 | import java.io.IOException; |
Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 23 | import java.util.List; |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 25 | import static org.junit.Assert.assertFalse; |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertNotNull; |
| 27 | import static org.junit.Assert.assertNull; |
Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | |
| 31 | public class InstanceUtilsTest { |
| 32 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 33 | public void asStringBasic() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 34 | TestDump dump = TestDump.getTestDump(); |
| 35 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 36 | assertEquals("hello, world", InstanceUtils.asString(str)); |
| 37 | } |
| 38 | |
| 39 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 40 | public void asStringCharArray() throws IOException { |
| 41 | TestDump dump = TestDump.getTestDump(); |
| 42 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 43 | assertEquals("char thing", InstanceUtils.asString(str)); |
| 44 | } |
| 45 | |
| 46 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 47 | public void asStringTruncated() throws IOException { |
| 48 | TestDump dump = TestDump.getTestDump(); |
| 49 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 50 | assertEquals("hello", InstanceUtils.asString(str, 5)); |
| 51 | } |
| 52 | |
| 53 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 54 | public void asStringCharArrayTruncated() throws IOException { |
| 55 | TestDump dump = TestDump.getTestDump(); |
| 56 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 57 | assertEquals("char ", InstanceUtils.asString(str, 5)); |
| 58 | } |
| 59 | |
| 60 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 61 | public void asStringExactMax() throws IOException { |
| 62 | TestDump dump = TestDump.getTestDump(); |
| 63 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 64 | assertEquals("hello, world", InstanceUtils.asString(str, 12)); |
| 65 | } |
| 66 | |
| 67 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 68 | public void asStringCharArrayExactMax() throws IOException { |
| 69 | TestDump dump = TestDump.getTestDump(); |
| 70 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 71 | assertEquals("char thing", InstanceUtils.asString(str, 10)); |
| 72 | } |
| 73 | |
| 74 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 75 | public void asStringNotTruncated() throws IOException { |
| 76 | TestDump dump = TestDump.getTestDump(); |
| 77 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 78 | assertEquals("hello, world", InstanceUtils.asString(str, 50)); |
| 79 | } |
| 80 | |
| 81 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 82 | public void asStringCharArrayNotTruncated() throws IOException { |
| 83 | TestDump dump = TestDump.getTestDump(); |
| 84 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 85 | assertEquals("char thing", InstanceUtils.asString(str, 50)); |
| 86 | } |
| 87 | |
| 88 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 89 | public void asStringNegativeMax() throws IOException { |
| 90 | TestDump dump = TestDump.getTestDump(); |
| 91 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 92 | assertEquals("hello, world", InstanceUtils.asString(str, -3)); |
| 93 | } |
| 94 | |
| 95 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 96 | public void asStringCharArrayNegativeMax() throws IOException { |
| 97 | TestDump dump = TestDump.getTestDump(); |
| 98 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 99 | assertEquals("char thing", InstanceUtils.asString(str, -3)); |
| 100 | } |
| 101 | |
| 102 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 103 | public void asStringNull() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 104 | TestDump dump = TestDump.getTestDump(); |
| 105 | Instance obj = (Instance)dump.getDumpedThing("nullString"); |
| 106 | assertNull(InstanceUtils.asString(obj)); |
| 107 | } |
| 108 | |
| 109 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 110 | public void asStringNotString() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 111 | TestDump dump = TestDump.getTestDump(); |
| 112 | Instance obj = (Instance)dump.getDumpedThing("anObject"); |
| 113 | assertNotNull(obj); |
| 114 | assertNull(InstanceUtils.asString(obj)); |
| 115 | } |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 116 | |
| 117 | @Test |
| 118 | public void basicReference() throws IOException { |
| 119 | TestDump dump = TestDump.getTestDump(); |
| 120 | |
| 121 | Instance pref = (Instance)dump.getDumpedThing("aPhantomReference"); |
| 122 | Instance wref = (Instance)dump.getDumpedThing("aWeakReference"); |
| 123 | Instance referent = (Instance)dump.getDumpedThing("anObject"); |
| 124 | assertNotNull(pref); |
| 125 | assertNotNull(wref); |
| 126 | assertNotNull(referent); |
| 127 | assertEquals(referent, InstanceUtils.getReferent(pref)); |
| 128 | assertEquals(referent, InstanceUtils.getReferent(wref)); |
| 129 | assertNull(InstanceUtils.getReferent(referent)); |
| 130 | } |
Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 131 | |
| 132 | @Test |
| 133 | public void gcRootPath() throws IOException { |
| 134 | TestDump dump = TestDump.getTestDump(); |
| 135 | |
| 136 | ClassObj main = dump.getAhatSnapshot().findClass("Main"); |
| 137 | ArrayInstance gcPathArray = (ArrayInstance)dump.getDumpedThing("gcPathArray"); |
| 138 | Object[] values = gcPathArray.getValues(); |
| 139 | Instance base = (Instance)values[2]; |
| 140 | Instance left = InstanceUtils.getRefField(base, "left"); |
| 141 | Instance right = InstanceUtils.getRefField(base, "right"); |
| 142 | Instance target = InstanceUtils.getRefField(left, "right"); |
| 143 | |
| 144 | List<InstanceUtils.PathElement> path = InstanceUtils.getPathFromGcRoot(target); |
| 145 | assertEquals(6, path.size()); |
| 146 | |
| 147 | assertEquals(main, path.get(0).instance); |
| 148 | assertEquals(".stuff", path.get(0).field); |
| 149 | assertTrue(path.get(0).isDominator); |
| 150 | |
| 151 | assertEquals(".gcPathArray", path.get(1).field); |
| 152 | assertTrue(path.get(1).isDominator); |
| 153 | |
| 154 | assertEquals(gcPathArray, path.get(2).instance); |
| 155 | assertEquals("[2]", path.get(2).field); |
| 156 | assertTrue(path.get(2).isDominator); |
| 157 | |
| 158 | assertEquals(base, path.get(3).instance); |
| 159 | assertTrue(path.get(3).isDominator); |
| 160 | |
| 161 | // There are two possible paths. Either it can go through the 'left' node, |
| 162 | // or the 'right' node. |
| 163 | if (path.get(3).field.equals(".left")) { |
| 164 | assertEquals(".left", path.get(3).field); |
| 165 | |
| 166 | assertEquals(left, path.get(4).instance); |
| 167 | assertEquals(".right", path.get(4).field); |
| 168 | assertFalse(path.get(4).isDominator); |
| 169 | |
| 170 | } else { |
| 171 | assertEquals(".right", path.get(3).field); |
| 172 | |
| 173 | assertEquals(right, path.get(4).instance); |
| 174 | assertEquals(".left", path.get(4).field); |
| 175 | assertFalse(path.get(4).isDominator); |
| 176 | } |
| 177 | |
| 178 | assertEquals(target, path.get(5).instance); |
| 179 | assertEquals("", path.get(5).field); |
| 180 | assertTrue(path.get(5).isDominator); |
| 181 | } |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 182 | } |