1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.L; 4 import static android.os.Build.VERSION_CODES.M; 5 import static com.google.common.truth.Truth.assertThat; 6 import static org.junit.Assert.fail; 7 8 import android.content.Context; 9 import android.os.Debug; 10 import androidx.test.core.app.ApplicationProvider; 11 import androidx.test.ext.junit.runners.AndroidJUnit4; 12 import java.io.File; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.annotation.Config; 17 18 @RunWith(AndroidJUnit4.class) 19 public class ShadowDebugTest { 20 21 private static final String TRACE_FILENAME = "dmtrace.trace"; 22 private Context context; 23 24 @Before setUp()25 public void setUp() throws Exception { 26 context = ApplicationProvider.getApplicationContext(); 27 } 28 29 @Test initNoCrash()30 public void initNoCrash() { 31 assertThat(Debug.getNativeHeapAllocatedSize()).isAtLeast(0L); 32 } 33 34 @Test 35 @Config(minSdk = M) getRuntimeStats()36 public void getRuntimeStats() { 37 assertThat(Debug.getRuntimeStats()).isNotNull(); 38 } 39 40 @Test startStopTracingShouldWriteFile()41 public void startStopTracingShouldWriteFile() { 42 Debug.startMethodTracing(TRACE_FILENAME); 43 Debug.stopMethodTracing(); 44 45 assertThat(new File(context.getExternalFilesDir(null), TRACE_FILENAME).exists()).isTrue(); 46 } 47 48 @Test 49 @Config(minSdk = L) startStopTracingSamplingShouldWriteFile()50 public void startStopTracingSamplingShouldWriteFile() { 51 Debug.startMethodTracingSampling(TRACE_FILENAME, 100, 100); 52 Debug.stopMethodTracing(); 53 54 assertThat(new File(context.getExternalFilesDir(null), TRACE_FILENAME).exists()).isTrue(); 55 } 56 57 @Test startTracingShouldThrowIfAlreadyStarted()58 public void startTracingShouldThrowIfAlreadyStarted() { 59 Debug.startMethodTracing(TRACE_FILENAME); 60 61 try { 62 Debug.startMethodTracing(TRACE_FILENAME); 63 fail("RuntimeException not thrown."); 64 } catch (RuntimeException e) { 65 // expected 66 } 67 } 68 69 @Test stopTracingShouldThrowIfNotStarted()70 public void stopTracingShouldThrowIfNotStarted() { 71 try { 72 Debug.stopMethodTracing(); 73 fail("RuntimeException not thrown."); 74 } catch (RuntimeException e) { 75 // expected 76 } 77 } 78 } 79