1 /* 2 * Copyright (C) 2018 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 17 package com.android.cts.atracetestapp; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.os.Bundle; 23 import android.os.Trace; 24 import android.system.Os; 25 26 import androidx.test.InstrumentationRegistry; 27 import androidx.test.filters.LargeTest; 28 import androidx.test.rule.ActivityTestRule; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.BeforeClass; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @LargeTest 37 @RunWith(AndroidJUnit4.class) 38 public class AtraceDeviceTests { 39 40 @BeforeClass reportPidTid()41 public static void reportPidTid() { 42 Bundle status = new Bundle(); 43 status.putLong("AtraceDeviceTests_pid", Os.getpid()); 44 status.putLong("AtraceDeviceTests_tid", Os.gettid()); 45 InstrumentationRegistry.getInstrumentation().addResults(status); 46 } 47 48 @Rule 49 public ActivityTestRule<AtraceTestAppActivity> mActivity = 50 new ActivityTestRule<>(AtraceTestAppActivity.class, true, false); 51 52 @Test assertTracingOn()53 public void assertTracingOn() { 54 assertTrue(Trace.isEnabled()); 55 assertTrue(AtraceNdkMethods.isEnabled()); 56 } 57 58 @Test assertTracingOff()59 public void assertTracingOff() { 60 assertFalse(Trace.isEnabled()); 61 assertFalse(AtraceNdkMethods.isEnabled()); 62 } 63 64 @Test beginEndSection()65 public void beginEndSection() { 66 assertTrue(Trace.isEnabled()); 67 assertTrue(AtraceNdkMethods.isEnabled()); 68 Trace.beginSection("AtraceDeviceTest::beginEndSection"); 69 Trace.endSection(); 70 AtraceNdkMethods.beginEndSection(); 71 } 72 73 @Test asyncBeginEndSection()74 public void asyncBeginEndSection() { 75 assertTrue(Trace.isEnabled()); 76 assertTrue(AtraceNdkMethods.isEnabled()); 77 Trace.beginAsyncSection("AtraceDeviceTest::asyncBeginEndSection", 42); 78 Trace.endAsyncSection("AtraceDeviceTest::asyncBeginEndSection", 42); 79 AtraceNdkMethods.asyncBeginEndSection(); 80 } 81 82 @Test counter()83 public void counter() { 84 assertTrue(Trace.isEnabled()); 85 assertTrue(AtraceNdkMethods.isEnabled()); 86 Trace.setCounter("AtraceDeviceTest::counter", 10); 87 Trace.setCounter("AtraceDeviceTest::counter", 20); 88 Trace.setCounter("AtraceDeviceTest::counter", 30); 89 Trace.setCounter("AtraceDeviceTest::counter", 9223372000000005807L); 90 AtraceNdkMethods.counter(); 91 } 92 93 @Test launchActivity()94 public void launchActivity() { 95 AtraceTestAppActivity activity = mActivity.launchActivity(null); 96 activity.waitForDraw(); 97 activity.finish(); 98 } 99 100 static { 101 System.loadLibrary("ctstrace_jni"); 102 } 103 } 104