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 | |
| 19 | import com.android.tools.perflib.heap.Instance; |
| 20 | import java.io.IOException; |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | import org.junit.Test; |
| 25 | |
| 26 | public class InstanceUtilsTest { |
| 27 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 28 | public void asStringBasic() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 29 | TestDump dump = TestDump.getTestDump(); |
| 30 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 31 | assertEquals("hello, world", InstanceUtils.asString(str)); |
| 32 | } |
| 33 | |
| 34 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 35 | public void asStringCharArray() throws IOException { |
| 36 | TestDump dump = TestDump.getTestDump(); |
| 37 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 38 | assertEquals("char thing", InstanceUtils.asString(str)); |
| 39 | } |
| 40 | |
| 41 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 42 | public void asStringTruncated() throws IOException { |
| 43 | TestDump dump = TestDump.getTestDump(); |
| 44 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 45 | assertEquals("hello", InstanceUtils.asString(str, 5)); |
| 46 | } |
| 47 | |
| 48 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 49 | public void asStringCharArrayTruncated() throws IOException { |
| 50 | TestDump dump = TestDump.getTestDump(); |
| 51 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 52 | assertEquals("char ", InstanceUtils.asString(str, 5)); |
| 53 | } |
| 54 | |
| 55 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 56 | public void asStringExactMax() throws IOException { |
| 57 | TestDump dump = TestDump.getTestDump(); |
| 58 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 59 | assertEquals("hello, world", InstanceUtils.asString(str, 12)); |
| 60 | } |
| 61 | |
| 62 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 63 | public void asStringCharArrayExactMax() throws IOException { |
| 64 | TestDump dump = TestDump.getTestDump(); |
| 65 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 66 | assertEquals("char thing", InstanceUtils.asString(str, 10)); |
| 67 | } |
| 68 | |
| 69 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 70 | public void asStringNotTruncated() throws IOException { |
| 71 | TestDump dump = TestDump.getTestDump(); |
| 72 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 73 | assertEquals("hello, world", InstanceUtils.asString(str, 50)); |
| 74 | } |
| 75 | |
| 76 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 77 | public void asStringCharArrayNotTruncated() throws IOException { |
| 78 | TestDump dump = TestDump.getTestDump(); |
| 79 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 80 | assertEquals("char thing", InstanceUtils.asString(str, 50)); |
| 81 | } |
| 82 | |
| 83 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 84 | public void asStringNegativeMax() throws IOException { |
| 85 | TestDump dump = TestDump.getTestDump(); |
| 86 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 87 | assertEquals("hello, world", InstanceUtils.asString(str, -3)); |
| 88 | } |
| 89 | |
| 90 | @Test |
Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 91 | public void asStringCharArrayNegativeMax() throws IOException { |
| 92 | TestDump dump = TestDump.getTestDump(); |
| 93 | Instance str = (Instance)dump.getDumpedThing("charArray"); |
| 94 | assertEquals("char thing", InstanceUtils.asString(str, -3)); |
| 95 | } |
| 96 | |
| 97 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 98 | public void asStringNull() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 99 | TestDump dump = TestDump.getTestDump(); |
| 100 | Instance obj = (Instance)dump.getDumpedThing("nullString"); |
| 101 | assertNull(InstanceUtils.asString(obj)); |
| 102 | } |
| 103 | |
| 104 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 105 | public void asStringNotString() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 106 | TestDump dump = TestDump.getTestDump(); |
| 107 | Instance obj = (Instance)dump.getDumpedThing("anObject"); |
| 108 | assertNotNull(obj); |
| 109 | assertNull(InstanceUtils.asString(obj)); |
| 110 | } |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 111 | |
| 112 | @Test |
| 113 | public void basicReference() throws IOException { |
| 114 | TestDump dump = TestDump.getTestDump(); |
| 115 | |
| 116 | Instance pref = (Instance)dump.getDumpedThing("aPhantomReference"); |
| 117 | Instance wref = (Instance)dump.getDumpedThing("aWeakReference"); |
| 118 | Instance referent = (Instance)dump.getDumpedThing("anObject"); |
| 119 | assertNotNull(pref); |
| 120 | assertNotNull(wref); |
| 121 | assertNotNull(referent); |
| 122 | assertEquals(referent, InstanceUtils.getReferent(pref)); |
| 123 | assertEquals(referent, InstanceUtils.getReferent(wref)); |
| 124 | assertNull(InstanceUtils.getReferent(referent)); |
| 125 | } |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 126 | } |