blob: 32f48ce5608796a165f0257edbd3664cd9d88855 [file] [log] [blame]
Richard Uhler35244722015-09-10 16:45:54 -07001/*
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
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.Instance;
20import java.io.IOException;
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertNull;
24import org.junit.Test;
25
26public class InstanceUtilsTest {
27 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070028 public void asStringBasic() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -070029 TestDump dump = TestDump.getTestDump();
30 Instance str = (Instance)dump.getDumpedThing("basicString");
31 assertEquals("hello, world", InstanceUtils.asString(str));
32 }
33
34 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070035 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 Uhler35244722015-09-10 16:45:54 -070064 TestDump dump = TestDump.getTestDump();
65 Instance obj = (Instance)dump.getDumpedThing("nullString");
66 assertNull(InstanceUtils.asString(obj));
67 }
68
69 @Test
Richard Uhler77ff54b2015-08-31 10:22:56 -070070 public void asStringNotString() throws IOException {
Richard Uhler35244722015-09-10 16:45:54 -070071 TestDump dump = TestDump.getTestDump();
72 Instance obj = (Instance)dump.getDumpedThing("anObject");
73 assertNotNull(obj);
74 assertNull(InstanceUtils.asString(obj));
75 }
Richard Uhlerb3577302015-10-29 13:02:42 -070076
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 Uhler35244722015-09-10 16:45:54 -070091}