• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 package com.android.loganalysis.item;
17 
18 import junit.framework.TestCase;
19 
20 import org.json.JSONArray;
21 import org.json.JSONException;
22 import org.json.JSONObject;
23 
24 /**
25  * Unit test for {@link ProcrankItem}.
26  */
27 public class ProcrankItemTest extends TestCase {
28 
29     /**
30      * Test that {@link ProcrankItem#toJson()} returns correctly.
31      */
testToJson()32     public void testToJson() throws JSONException {
33         ProcrankItem item = new ProcrankItem();
34         item.addProcrankLine(0, "process0", 1, 2, 3, 4);
35         item.addProcrankLine(5, "process1", 6, 7, 8, 9);
36         item.setText("foo\nbar");
37 
38         // Convert to JSON string and back again
39         JSONObject output = new JSONObject(item.toJson().toString());
40 
41         assertTrue(output.has(ProcrankItem.LINES));
42         assertTrue(output.get(ProcrankItem.LINES) instanceof JSONArray);
43         assertTrue(output.has(ProcrankItem.TEXT));
44         assertEquals("foo\nbar", output.get(ProcrankItem.TEXT));
45 
46         JSONArray lines = output.getJSONArray(ProcrankItem.LINES);
47 
48         assertEquals(2, lines.length());
49         assertTrue(lines.get(0) instanceof JSONObject);
50 
51         JSONObject line = lines.getJSONObject(0);
52 
53         assertEquals(0, line.get(ProcrankItem.PID));
54         assertEquals("process0", line.get(ProcrankItem.PROCESS_NAME));
55         assertEquals(1, line.get(ProcrankItem.VSS));
56         assertEquals(2, line.get(ProcrankItem.RSS));
57         assertEquals(3, line.get(ProcrankItem.PSS));
58         assertEquals(4, line.get(ProcrankItem.USS));
59     }
60 }
61