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 package com.android.loganalysis.item; 17 18 import com.android.loganalysis.item.WakelockItem.WakelockInfoItem; 19 20 import junit.framework.TestCase; 21 22 import org.json.JSONArray; 23 import org.json.JSONException; 24 import org.json.JSONObject; 25 26 /** 27 * Unit test for {@link WakelockItem}. 28 */ 29 public class WakelockItemTest extends TestCase { 30 31 /** 32 * Test that {@link WakelockItem#toJson()} returns correctly. 33 */ testToJson()34 public void testToJson() throws JSONException { 35 WakelockItem item = new WakelockItem(); 36 item.addWakeLock("screen","u100", 150000, 25, 37 WakelockItem.WakeLockCategory.PARTIAL_WAKELOCK); 38 item.addWakeLock("wlan_rx", 150000, 25, 39 WakelockItem.WakeLockCategory.KERNEL_WAKELOCK); 40 41 // Convert to JSON string and back again 42 JSONObject output = new JSONObject(item.toJson().toString()); 43 44 assertTrue(output.has(WakelockItem.WAKELOCKS)); 45 assertTrue(output.get(WakelockItem.WAKELOCKS) instanceof JSONArray); 46 47 JSONArray wakelockInfo = output.getJSONArray(WakelockItem.WAKELOCKS); 48 49 assertEquals(2, wakelockInfo.length()); 50 assertTrue(wakelockInfo.getJSONObject(0).has(WakelockInfoItem.NAME)); 51 assertTrue(wakelockInfo.getJSONObject(0).has(WakelockInfoItem.PROCESS_UID)); 52 assertTrue(wakelockInfo.getJSONObject(0).has(WakelockInfoItem.HELD_TIME)); 53 assertTrue(wakelockInfo.getJSONObject(0).has(WakelockInfoItem.LOCKED_COUNT)); 54 assertTrue(wakelockInfo.getJSONObject(0).has(WakelockInfoItem.CATEGORY)); 55 56 assertTrue(wakelockInfo.getJSONObject(1).has(WakelockInfoItem.NAME)); 57 assertFalse(wakelockInfo.getJSONObject(1).has(WakelockInfoItem.PROCESS_UID)); 58 assertTrue(wakelockInfo.getJSONObject(1).has(WakelockInfoItem.HELD_TIME)); 59 assertTrue(wakelockInfo.getJSONObject(1).has(WakelockInfoItem.LOCKED_COUNT)); 60 assertTrue(wakelockInfo.getJSONObject(1).has(WakelockInfoItem.CATEGORY)); 61 62 } 63 } 64