1 /* 2 * Copyright (C) 2025 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.performance; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import com.android.tradefed.config.ConfigurationException; 24 import com.android.tradefed.config.OptionSetter; 25 import com.android.tradefed.device.DeviceNotAvailableException; 26 import com.android.tradefed.device.ITestDevice; 27 import com.android.tradefed.invoker.TestInformation; 28 import com.android.tradefed.util.Pair; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 import org.mockito.stubbing.Answer; 35 36 import java.io.File; 37 import java.nio.charset.StandardCharsets; 38 import java.nio.file.Files; 39 import java.util.ArrayList; 40 import java.util.List; 41 42 /** Unit tests for {@link PerfettoJavaHeapConfigTargetPreparer}. */ 43 @RunWith(JUnit4.class) 44 public class PerfettoJavaHeapConfigTargetPreparerTest { 45 46 private final PerfettoJavaHeapConfigTargetPreparer mPreparer = 47 new PerfettoJavaHeapConfigTargetPreparer(); 48 private ITestDevice mITestDevice = mock(ITestDevice.class); 49 private List<Pair<String, String>> mPushedFiles = new ArrayList<>(); 50 51 @Before setUp()52 public void setUp() { 53 try { 54 when(mITestDevice.pushFile(any(), any())) 55 .thenAnswer( 56 (Answer<File>) 57 invocation -> { 58 final File localFile = (File) invocation.getArguments()[0]; 59 final String deviceFile = 60 (String) invocation.getArguments()[1]; 61 final String content = 62 Files.readString( 63 localFile.toPath(), StandardCharsets.UTF_8); 64 mPushedFiles.add(new Pair<>(deviceFile, content)); 65 return null; 66 }); 67 } catch (DeviceNotAvailableException e) { 68 throw new RuntimeException(e); 69 } 70 } 71 72 @Test testNoParameters_pushesDefaultConfig()73 public void testNoParameters_pushesDefaultConfig() { 74 runPreparer(); 75 76 assertOneFilePushed( 77 "/data/misc/perfetto-traces/trace_config_java_heap.textproto", 78 "buffers {\n" 79 + " size_kb: 256000\n" 80 + " fill_policy: DISCARD\n" 81 + "}\n" 82 + "\n" 83 + "data_sources {\n" 84 + " config {\n" 85 + " name: \"android.java_hprof\"\n" 86 + " java_hprof_config {\n" 87 + " process_cmdline: \"com.android.systemui\"\n" 88 + " dump_smaps: true\n" 89 + " }\n" 90 + " }\n" 91 + "}\n" 92 + "\n" 93 + "data_source_stop_timeout_ms: 100000\n" 94 + "data_sources {\n" 95 + " config {\n" 96 + " name: \"android.packages_list\"\n" 97 + " }\n" 98 + "}\n" 99 + "\n" 100 + "data_sources: {\n" 101 + " config {\n" 102 + " name: \"linux.process_stats\"\n" 103 + " process_stats_config {\n" 104 + " scan_all_processes_on_start: true\n" 105 + " }\n" 106 + " }\n" 107 + "}"); 108 } 109 110 @Test testChangeProcessName_pushesConfigWithPassedProcessName()111 public void testChangeProcessName_pushesConfigWithPassedProcessName() 112 throws ConfigurationException { 113 new OptionSetter(mPreparer).setOptionValue("process-names-to-profile", "com.other"); 114 115 runPreparer(); 116 117 assertOneFilePushed( 118 "/data/misc/perfetto-traces/trace_config_java_heap.textproto", 119 "buffers {\n" 120 + " size_kb: 256000\n" 121 + " fill_policy: DISCARD\n" 122 + "}\n" 123 + "\n" 124 + "data_sources {\n" 125 + " config {\n" 126 + " name: \"android.java_hprof\"\n" 127 + " java_hprof_config {\n" 128 + " process_cmdline: \"com.other\"\n" 129 + " dump_smaps: true\n" 130 + " }\n" 131 + " }\n" 132 + "}\n" 133 + "\n" 134 + "data_source_stop_timeout_ms: 100000\n" 135 + "data_sources {\n" 136 + " config {\n" 137 + " name: \"android.packages_list\"\n" 138 + " }\n" 139 + "}\n" 140 + "\n" 141 + "data_sources: {\n" 142 + " config {\n" 143 + " name: \"linux.process_stats\"\n" 144 + " process_stats_config {\n" 145 + " scan_all_processes_on_start: true\n" 146 + " }\n" 147 + " }\n" 148 + "}"); 149 } 150 runPreparer()151 private void runPreparer() { 152 final TestInformation testInformation = mock(TestInformation.class); 153 when(testInformation.getDevice()).thenReturn(mITestDevice); 154 try { 155 mPreparer.setUp(testInformation); 156 } catch (Throwable e) { 157 throw new RuntimeException(e); 158 } 159 } 160 assertOneFilePushed(String pushedPath, String fileContent)161 private void assertOneFilePushed(String pushedPath, String fileContent) { 162 assertEquals(1, mPushedFiles.size()); 163 assertEquals(pushedPath, mPushedFiles.get(0).first); 164 assertEquals(fileContent.strip(), mPushedFiles.get(0).second.strip()); 165 } 166 } 167