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 | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 35 | public void asStringTruncated() throws IOException { |
| 36 | TestDump dump = TestDump.getTestDump(); |
| 37 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 38 | assertEquals("hello", InstanceUtils.asString(str, 5)); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void asStringExactMax() throws IOException { |
| 43 | TestDump dump = TestDump.getTestDump(); |
| 44 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 45 | assertEquals("hello, world", InstanceUtils.asString(str, 12)); |
| 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void asStringNotTruncated() throws IOException { |
| 50 | TestDump dump = TestDump.getTestDump(); |
| 51 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 52 | assertEquals("hello, world", InstanceUtils.asString(str, 50)); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void asStringNegativeMax() throws IOException { |
| 57 | TestDump dump = TestDump.getTestDump(); |
| 58 | Instance str = (Instance)dump.getDumpedThing("basicString"); |
| 59 | assertEquals("hello, world", InstanceUtils.asString(str, -3)); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void asStringNull() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 64 | TestDump dump = TestDump.getTestDump(); |
| 65 | Instance obj = (Instance)dump.getDumpedThing("nullString"); |
| 66 | assertNull(InstanceUtils.asString(obj)); |
| 67 | } |
| 68 | |
| 69 | @Test |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 70 | public void asStringNotString() throws IOException { |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 71 | TestDump dump = TestDump.getTestDump(); |
| 72 | Instance obj = (Instance)dump.getDumpedThing("anObject"); |
| 73 | assertNotNull(obj); |
| 74 | assertNull(InstanceUtils.asString(obj)); |
| 75 | } |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 76 | |
| 77 | @Test |
| 78 | public void basicReference() throws IOException { |
| 79 | TestDump dump = TestDump.getTestDump(); |
| 80 | |
| 81 | Instance pref = (Instance)dump.getDumpedThing("aPhantomReference"); |
| 82 | Instance wref = (Instance)dump.getDumpedThing("aWeakReference"); |
| 83 | Instance referent = (Instance)dump.getDumpedThing("anObject"); |
| 84 | assertNotNull(pref); |
| 85 | assertNotNull(wref); |
| 86 | assertNotNull(referent); |
| 87 | assertEquals(referent, InstanceUtils.getReferent(pref)); |
| 88 | assertEquals(referent, InstanceUtils.getReferent(wref)); |
| 89 | assertNull(InstanceUtils.getReferent(referent)); |
| 90 | } |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 91 | } |