1 /* 2 * Copyright (C) 2019 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.helpers; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.ArgumentMatchers.matches; 22 import static org.mockito.Mockito.doReturn; 23 24 import androidx.test.uiautomator.UiDevice; 25 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.mockito.Spy; 33 34 import java.io.IOException; 35 import java.util.Map; 36 37 @RunWith(JUnit4.class) 38 public final class PwrStatsUtilHelperTest { 39 private @Spy PwrStatsUtilHelper mHelper; 40 private @Mock UiDevice mUiDevice; 41 42 @Before setUp()43 public void setUp() { 44 MockitoAnnotations.initMocks(this); 45 } 46 47 @Test successfulRun()48 public void successfulRun() { 49 final String pid_output = "pid = 11386\n"; 50 final String metric_output = 51 "elapsed time: 14.8746s\n" 52 + "SLPI__Sleep=14505\n" 53 + "SLPI_ISLAND__uImage=14498\n" 54 + "SoC__CXSD=0\n" 55 + "SoC__AOSD=0\n"; 56 try { 57 doReturn(metric_output).when(mHelper).executeShellCommand(matches("kill -INT \\d+")); 58 doReturn(pid_output).when(mHelper).executeShellCommand(matches("pwrstats_util -d .*")); 59 } catch (IOException e) { 60 assertTrue(e.toString(), false); 61 } 62 63 // Validate startCollecting() 64 assertTrue(mHelper.startCollecting()); 65 assertNotNull(mHelper.mLogFile); 66 assertTrue( 67 "Expecting PID " + mHelper.mUtilPid + " to be greater than 0", 68 (mHelper.mUtilPid > 0)); 69 70 // Validate getMetrics() 71 Map<String, Long> metrics = mHelper.getMetrics(); 72 assertEquals(metrics.size(), 4); 73 74 // Validate that stopCollecting() works when run after getMetrics() 75 assertTrue(mHelper.stopCollecting()); 76 } 77 } 78