Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
      4 import static com.google.common.truth.Truth.assertThat;
      5 import static org.junit.Assert.fail;
      6 
      7 import android.os.Trace;
      8 import androidx.test.ext.junit.runners.AndroidJUnit4;
      9 import org.junit.Test;
     10 import org.junit.runner.RunWith;
     11 import org.robolectric.annotation.Config;
     12 
     13 /** Test for {@link ShadowTrace}. */
     14 @RunWith(AndroidJUnit4.class)
     15 @Config(minSdk = JELLY_BEAN_MR2)
     16 public class ShadowTraceTest {
     17   private static final String VERY_LONG_TAG_NAME = String.format(String.format("%%%ds", 128), "A");
     18 
     19   @Test
     20   public void beginSection_calledOnce_addsSection() throws Exception {
     21     Trace.beginSection("section1");
     22 
     23     assertThat(ShadowTrace.getCurrentSections()).containsExactly("section1");
     24     assertThat(ShadowTrace.getPreviousSections()).isEmpty();
     25   }
     26 
     27   @Test
     28   public void beginSection_calledTwice_addsBothSections() throws Exception {
     29     Trace.beginSection("section1");
     30     Trace.beginSection("section2");
     31 
     32     assertThat(ShadowTrace.getCurrentSections()).containsExactly("section1", "section2");
     33     assertThat(ShadowTrace.getPreviousSections()).isEmpty();
     34   }
     35 
     36   @Test
     37   public void beginSection_tagIsNull_throwsNullPointerException() throws Exception {
     38     try {
     39       Trace.beginSection(null);
     40       fail("Must throw");
     41     } catch (NullPointerException e) {
     42       // Must throw.
     43     }
     44   }
     45 
     46   @Test
     47   public void beginSection_tagIsNullAndCrashDisabled_doesNotThrow() throws Exception {
     48     ShadowTrace.doNotUseSetCrashOnIncorrectUsage(false);
     49     Trace.beginSection(null);
     50     // Should not crash.
     51   }
     52 
     53   @Test
     54   public void beginSection_tagIsTooLong_throwsIllegalArgumentException() throws Exception {
     55     try {
     56       Trace.beginSection(VERY_LONG_TAG_NAME);
     57       fail("Must throw");
     58     } catch (IllegalArgumentException e) {
     59       // Must throw.
     60     }
     61   }
     62 
     63   @Test
     64   public void beginSection_tagIsTooLongAndCrashDisabled_doesNotThrow() throws Exception {
     65     ShadowTrace.doNotUseSetCrashOnIncorrectUsage(false);
     66     Trace.beginSection(VERY_LONG_TAG_NAME);
     67     // Should not crash.
     68   }
     69 
     70   @Test
     71   public void endSection_oneSection_closesSection() throws Exception {
     72     Trace.beginSection("section1");
     73 
     74     Trace.endSection();
     75 
     76     assertThat(ShadowTrace.getCurrentSections()).isEmpty();
     77     assertThat(ShadowTrace.getPreviousSections()).containsExactly("section1");
     78   }
     79 
     80   @Test
     81   public void endSection_twoSections_closesLastSection() throws Exception {
     82     Trace.beginSection("section1");
     83     Trace.beginSection("section2");
     84 
     85     Trace.endSection();
     86 
     87     assertThat(ShadowTrace.getCurrentSections()).containsExactly("section1");
     88     assertThat(ShadowTrace.getPreviousSections()).containsExactly("section2");
     89   }
     90 
     91   @Test
     92   public void endSection_twoRecursiveSectionsAndCalledTwice_closesAllSections() throws Exception {
     93     Trace.beginSection("section1");
     94     Trace.beginSection("section2");
     95 
     96     Trace.endSection();
     97     Trace.endSection();
     98 
     99     assertThat(ShadowTrace.getCurrentSections()).isEmpty();
    100     assertThat(ShadowTrace.getPreviousSections()).containsExactly("section2", "section1");
    101   }
    102 
    103   @Test
    104   public void endSection_twoSequentialSections_closesAllSections() throws Exception {
    105     Trace.beginSection("section1");
    106     Trace.endSection();
    107     Trace.beginSection("section2");
    108     Trace.endSection();
    109 
    110     assertThat(ShadowTrace.getCurrentSections()).isEmpty();
    111     assertThat(ShadowTrace.getPreviousSections()).containsExactly("section1", "section2");
    112   }
    113 
    114   @Test
    115   public void endSection_calledBeforeBeginning_doesNotThrow() throws Exception {
    116     Trace.endSection();
    117     // Should not crash.
    118   }
    119 
    120   @Test
    121   public void endSection_oneSectionButCalledTwice_doesNotThrow() throws Exception {
    122     Trace.beginSection("section1");
    123 
    124     Trace.endSection();
    125     Trace.endSection();
    126     // Should not crash.
    127   }
    128 
    129   @Test
    130   public void reset_resetsInternalState() throws Exception {
    131     Trace.beginSection("section1");
    132     Trace.endSection();
    133     Trace.beginSection("section2");
    134 
    135     ShadowTrace.reset();
    136 
    137     assertThat(ShadowTrace.getCurrentSections()).isEmpty();
    138     assertThat(ShadowTrace.getPreviousSections()).isEmpty();
    139   }
    140 }
    141