1 /*
2  * Copyright (C) 2018 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.ahat.heapdump.AhatInstance;
20 import com.android.ahat.heapdump.AhatSnapshot;
21 import com.android.ahat.heapdump.Site;
22 import java.io.IOException;
23 import java.util.List;
24 import org.junit.Test;
25 
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 
29 public class ObjectsHandlerTest {
30   @Test
getObjects()31   public void getObjects() throws IOException {
32     TestDump dump = TestDump.getTestDump();
33     AhatSnapshot snapshot = dump.getAhatSnapshot();
34 
35     Site root = snapshot.getRootSite();
36 
37     // We expect a single instance of DumpedStuff
38     List<AhatInstance> dumped = ObjectsHandler.getObjects(
39         root, "DumpedStuff", /* subclass */ false, /* heapName */ null);
40     assertEquals(1, dumped.size());
41     assertTrue(dumped.get(0).getClassName().equals("DumpedStuff"));
42 
43     // We expect no direct instances of SuperDumpedStuff
44     List<AhatInstance> direct = ObjectsHandler.getObjects(
45         root, "SuperDumpedStuff", /* subclass */ false, /* heapName */ null);
46     assertTrue(direct.isEmpty());
47 
48     // We expect one subclass instance of SuperDumpedStuff
49     List<AhatInstance> subclass = ObjectsHandler.getObjects(
50         root, "SuperDumpedStuff", /* subclass */ true, /* heapName */ null);
51     assertEquals(1, subclass.size());
52     assertTrue(subclass.get(0).getClassName().equals("DumpedStuff"));
53     assertEquals(dumped.get(0), subclass.get(0));
54   }
55 }
56