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.JSONException; 21 import org.json.JSONObject; 22 23 /** 24 * Unit test for {@link DvmLockSampleItem}. 25 */ 26 public class DvmLockSampleItemTest extends TestCase { 27 /** 28 * Test that {@link DvmLockSampleItem#toJson()} returns correctly. 29 */ testToJson()30 public void testToJson() throws JSONException { 31 DvmLockSampleItem item = new DvmLockSampleItem(); 32 33 item.setAttribute(DvmLockSampleItem.PROCESS_NAME, "android.support.test.aupt"); 34 item.setAttribute(DvmLockSampleItem.SENSITIVITY_FLAG, false); 35 item.setAttribute(DvmLockSampleItem.WAITING_THREAD_NAME, "Instr: android.support.test.aupt"); 36 item.setAttribute(DvmLockSampleItem.WAIT_TIME, 75); 37 item.setAttribute(DvmLockSampleItem.WAITING_SOURCE_FILE, "AccessibilityCache.java"); 38 item.setAttribute(DvmLockSampleItem.WAITING_SOURCE_LINE, 256); 39 item.setAttribute(DvmLockSampleItem.OWNER_FILE_NAME, "-"); 40 item.setAttribute(DvmLockSampleItem.OWNER_ACQUIRE_SOURCE_LINE, 96); 41 item.setAttribute(DvmLockSampleItem.SAMPLE_PERCENTAGE, 15); 42 43 // Convert to JSON string and back again 44 JSONObject output = new JSONObject(item.toJson().toString()); 45 46 // Assert that each field is the expected value 47 assertEquals("android.support.test.aupt", output.get(DvmLockSampleItem.PROCESS_NAME)); 48 assertEquals(false, output.get(DvmLockSampleItem.SENSITIVITY_FLAG)); 49 assertEquals("Instr: android.support.test.aupt", output.get(DvmLockSampleItem.WAITING_THREAD_NAME)); 50 assertEquals(75, output.get(DvmLockSampleItem.WAIT_TIME)); 51 assertEquals("AccessibilityCache.java", output.get(DvmLockSampleItem.WAITING_SOURCE_FILE)); 52 assertEquals(256, output.get(DvmLockSampleItem.WAITING_SOURCE_LINE)); 53 assertEquals("-", output.get(DvmLockSampleItem.OWNER_FILE_NAME)); 54 assertEquals(96, output.get(DvmLockSampleItem.OWNER_ACQUIRE_SOURCE_LINE)); 55 assertEquals(15, output.get(DvmLockSampleItem.SAMPLE_PERCENTAGE)); 56 } 57 } 58