1 /* 2 * Copyright (C) 2016 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.tradefed.util; 17 18 import com.android.tradefed.device.ITestDevice; 19 20 import com.android.tradefed.util.SimplePerfUtil.SimplePerfType; 21 22 import junit.framework.TestCase; 23 24 import org.easymock.EasyMock; 25 import org.junit.Assert; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Unit tests for SimplePerfUtil 32 */ 33 public class SimplePerfUtilTest extends TestCase { 34 35 private SimplePerfUtil spu = null; 36 private ITestDevice mockDevice = null; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 mockDevice = EasyMock.createMock(ITestDevice.class); 42 } 43 44 public void testCommandStringPreparerWithoutParams() { 45 spu = SimplePerfUtil.newInstance(mockDevice, SimplePerfType.STAT); 46 String command = "ls -l"; 47 String fullCommand = spu.commandStringPreparer(command); 48 Assert.assertEquals("simpleperf stat " + command, fullCommand); 49 } 50 51 public void testCommandStringPreparerWithParams() { 52 spu = SimplePerfUtil.newInstance(mockDevice, SimplePerfType.RECORD); 53 String command = "sleep 10"; 54 // Use LinkedHashMap to maintain order 55 List<String> params = new ArrayList<>(); 56 params.add("-e cpu-cycles:k"); 57 params.add("--no-inherit"); 58 params.add("perf.data"); 59 spu.setArgumentList(params); 60 String fullCommand = spu.commandStringPreparer(command); 61 Assert.assertEquals("simpleperf record -e cpu-cycles:k --no-inherit perf.data " + command, 62 fullCommand); 63 } 64 65 public void testCommandStringPreparerWithNullCommand() { 66 spu = SimplePerfUtil.newInstance(mockDevice, SimplePerfType.LIST); 67 String fullCommand = spu.commandStringPreparer(null); 68 Assert.assertEquals("simpleperf list ", fullCommand); 69 } 70 71 public void testConstructorWithNullParameter() { 72 try { 73 spu = SimplePerfUtil.newInstance(null, SimplePerfType.STAT); 74 fail("Missing Exception"); 75 } catch (NullPointerException npe) { 76 Assert.assertTrue(true); 77 } 78 } 79 } 80