• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base;
6 
7 import static org.mockito.Mockito.verify;
8 
9 import androidx.test.filters.SmallTest;
10 
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 
19 import org.chromium.base.test.BaseRobolectricTestRunner;
20 import org.chromium.base.test.util.Feature;
21 import org.chromium.base.test.util.JniMocker;
22 
23 /** Tests for {@link TraceEvent}. */
24 @RunWith(BaseRobolectricTestRunner.class)
25 public class TraceEventTest {
26     @Rule public JniMocker mocker = new JniMocker();
27 
28     @Mock TraceEvent.Natives mNativeMock;
29 
30     @Before
setUp()31     public void setUp() {
32         MockitoAnnotations.initMocks(this);
33         mocker.mock(TraceEventJni.TEST_HOOKS, mNativeMock);
34     }
35 
36     @Test
37     @SmallTest
38     @Feature({"Android-AppBase"})
testDisableEventNameFiltering()39     public void testDisableEventNameFiltering() {
40         TraceEvent.setEventNameFilteringEnabled(false);
41         Assert.assertFalse(TraceEvent.eventNameFilteringEnabled());
42     }
43 
44     @Test
45     @SmallTest
46     @Feature({"Android-AppBase"})
testEnableEventNameFiltering()47     public void testEnableEventNameFiltering() {
48         TraceEvent.setEventNameFilteringEnabled(true);
49         Assert.assertTrue(TraceEvent.eventNameFilteringEnabled());
50     }
51 
52     @Test
53     @SmallTest
54     @Feature({"Android-AppBase"})
testEventNameUnfiltered()55     public void testEventNameUnfiltered() {
56         TraceEvent.setEventNameFilteringEnabled(false);
57         Assert.assertFalse(TraceEvent.eventNameFilteringEnabled());
58 
59         // Input string format:
60         // ">>>>> Finished to (TARGET) {HASH_CODE} TARGET_NAME: WHAT"
61         String realEventName =
62                 ">>>>> Finished to (org.chromium.myClass.myMethod) "
63                         + "{HASH_CODE} org.chromium.myOtherClass.instance: message";
64 
65         // Output string format:
66         // "{TraceEvent.BasicLooperMonitor.LOOPER_TASK_PREFIX} TARGET(TARGET_NAME)"
67         String realEventNameExpected =
68                 TraceEvent.BasicLooperMonitor.LOOPER_TASK_PREFIX
69                         + "org.chromium.myClass.myMethod(org.chromium.myOtherClass.instance)";
70         Assert.assertEquals(
71                 TraceEvent.BasicLooperMonitor.getTraceEventName(realEventName),
72                 realEventNameExpected);
73     }
74 
75     @Test
76     @SmallTest
77     @Feature({"Android-AppBase"})
testEventNameFiltered()78     public void testEventNameFiltered() {
79         TraceEvent.setEventNameFilteringEnabled(true);
80         Assert.assertTrue(TraceEvent.eventNameFilteringEnabled());
81 
82         String realEventName =
83                 TraceEvent.BasicLooperMonitor.LOOPER_TASK_PREFIX
84                         + "org.chromium.myClass.myMethod(org.chromium.myOtherClass.instance)";
85         Assert.assertEquals(
86                 TraceEvent.BasicLooperMonitor.getTraceEventName(realEventName),
87                 TraceEvent.BasicLooperMonitor.FILTERED_EVENT_NAME);
88     }
89 
90     @Test
91     @SmallTest
92     @Feature({"Android-AppBase"})
testScopedTraceEventWithIntArg()93     public void testScopedTraceEventWithIntArg() {
94         TraceEvent.setEnabled(true);
95         // Only string literals are allowed in Java event names.
96         try (TraceEvent event = TraceEvent.scoped("TestEvent", 15)) {}
97         verify(mNativeMock).beginWithIntArg("TestEvent", 15);
98         TraceEvent.setEnabled(false);
99     }
100 
101     @Test
102     @SmallTest
103     @Feature({"Android-AppBase"})
testWebViewStartupTotalFactoryInit()104     public void testWebViewStartupTotalFactoryInit() {
105         TraceEvent.setEnabled(true);
106         long startTime = 10;
107         long duration = 50;
108         TraceEvent.webViewStartupTotalFactoryInit(startTime, duration);
109         verify(mNativeMock).webViewStartupTotalFactoryInit(startTime, duration);
110         TraceEvent.setEnabled(false);
111     }
112 
113     @Test
114     @SmallTest
115     @Feature({"Android-AppBase"})
testWebViewStartupStage1()116     public void testWebViewStartupStage1() {
117         TraceEvent.setEnabled(true);
118         long startTime = 10;
119         long duration = 50;
120         TraceEvent.webViewStartupStage1(startTime, duration);
121         verify(mNativeMock).webViewStartupStage1(startTime, duration);
122         TraceEvent.setEnabled(false);
123     }
124 
125     @Test
126     @SmallTest
127     @Feature({"Android-AppBase"})
testWebViewStartupStage2()128     public void testWebViewStartupStage2() {
129         TraceEvent.setEnabled(true);
130         long startTime = 10;
131         long duration = 50;
132         boolean isCold = true;
133         TraceEvent.webViewStartupStage2(startTime, duration, isCold);
134         verify(mNativeMock).webViewStartupStage2(startTime, duration, isCold);
135         TraceEvent.setEnabled(false);
136     }
137 }
138