41 | * Rules are interceptors which are executed for each test method and will run before 42 | * any of your setup code in the {@link Before @Before} method. 43 | *
44 | * {@link ActivityTestRule} will create and launch of the activity for you and also expose
45 | * the activity under test. To get a reference to the activity you can use
46 | * the {@link ActivityTestRule#getActivity()} method.
47 | */
48 | // TODO(developer): Uncomment below member variable to add a test activity to this test class.
49 | // @Rule
50 | // public ActivityTestRule
43 | * Rules are interceptors which are executed for each test method and will run before
44 | * any of your setup code in the {@link Before @Before} method.
45 | *
46 | * {@link ActivityTestRule} will create and launch of the activity for you and also expose
47 | * the activity under test. To get a reference to the activity you can use
48 | * the {@link ActivityTestRule#getActivity()} method.
49 | */
50 | // TODO(developer): Uncomment below member variable to add a test activity to this test class.
51 | // @Rule
52 | // public ActivityTestRule
42 | * @Rule
43 | * public EnableBatteryStatsDump mEnableBatteryStatsDump = new EnableBatteryStatsDump();
44 | *
45 | */
46 | public class EnableBatteryStatsDump extends ExternalResource {
47 |
48 | private Logger logger = Logger.getLogger(EnableBatteryStatsDump.class.getName());
49 |
50 | private String mTestName;
51 |
52 | private String mTestClass;
53 |
54 | private File mLogFileAbsoluteLocation = null;
55 |
56 | public EnableBatteryStatsDump() { }
57 |
58 | /**
59 | * Allow the the log to be written to a specific location.
60 | */
61 | public EnableBatteryStatsDump(File logFileAbsoluteLocation) {
62 | mLogFileAbsoluteLocation = logFileAbsoluteLocation;
63 | }
64 |
65 | @Override
66 | public Statement apply(Statement base, Description description) {
67 | mTestName = description.getMethodName();
68 | mTestClass = description.getClassName();
69 | return super.apply(base, description);
70 | }
71 |
72 | @Override
73 | public void before() {
74 | if (android.os.Build.VERSION.SDK_INT >= 21) {
75 | try {
76 | ProcessBuilder builder = new ProcessBuilder();
77 | builder.command("dumpsys", "batterystats", "--reset");
78 | Process process = builder.start();
79 | process.waitFor();
80 | } catch (Exception exception) {
81 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception);
82 | }
83 | }
84 | }
85 |
86 | public void after() {
87 | if (android.os.Build.VERSION.SDK_INT >= 21) {
88 | FileWriter fileWriter = null;
89 | BufferedReader bufferedReader = null;
90 | try {
91 | Trace.beginSection("Taking battery dumpsys");
92 | ProcessBuilder processBuilder = new ProcessBuilder();
93 |
94 | processBuilder.command("dumpsys", "batterystats");
95 | processBuilder.redirectErrorStream();
96 | Process process = processBuilder.start();
97 | if (mLogFileAbsoluteLocation == null) {
98 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName,
99 | "battery.dumpsys.log");
100 | }
101 | fileWriter = new FileWriter(mLogFileAbsoluteLocation);
102 | bufferedReader = new BufferedReader(
103 | new InputStreamReader(process.getInputStream()));
104 | String line;
105 | while ((line = bufferedReader.readLine()) != null) {
106 | fileWriter.append(line);
107 | fileWriter.append(System.lineSeparator());
108 | }
109 | process.waitFor();
110 | if (process.exitValue() != 0) {
111 | throw new Exception("Error while taking dumpsys, exitCode=" +
112 | process.exitValue());
113 | }
114 | } catch (Exception exception) {
115 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception);
116 | } finally {
117 | if (fileWriter != null) {
118 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); }
119 | }
120 | if (bufferedReader != null) {
121 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); }
122 | }
123 | }
124 | }
125 | }
126 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnableDeviceGetPropsInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import org.junit.rules.ExternalResource;
20 | import org.junit.runner.Description;
21 | import org.junit.runners.model.Statement;
22 |
23 | import java.io.BufferedReader;
24 | import java.io.File;
25 | import java.io.FileWriter;
26 | import java.io.InputStreamReader;
27 | import java.util.logging.Level;
28 | import java.util.logging.Logger;
29 |
30 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile;
31 |
32 | /**
33 | * This rule records the result of calling the getprops command on a device. It has limited use for
34 | * short tests except when these properties are changing between tests. Alternatively, it can be
35 | * used manually in a {@code org.junit.runner.notification.RunListener}.
36 | *
37 | *
38 | * @Rule
39 | * public EnableDeviceGetPropsInfo mEnableDeviceGetPropsInfo = new EnableDeviceGetPropsInfo();
40 | *
41 | */
42 | public class EnableDeviceGetPropsInfo extends ExternalResource {
43 |
44 | private Logger logger = Logger.getLogger(EnableDeviceGetPropsInfo.class.getName());
45 |
46 | private String mTestName;
47 |
48 | private String mTestClass;
49 |
50 | private File mLogFileAbsoluteLocation = null;
51 |
52 | public EnableDeviceGetPropsInfo() { }
53 |
54 | /**
55 | * Allow the the log to be written to a specific location.
56 | */
57 | public EnableDeviceGetPropsInfo(File logFileAbsoluteLocation) {
58 | mLogFileAbsoluteLocation = logFileAbsoluteLocation;
59 | }
60 |
61 | @Override
62 | public Statement apply(Statement base, Description description) {
63 | mTestName = description.getMethodName();
64 | mTestClass = description.getClassName();
65 | return super.apply(base, description);
66 | }
67 |
68 | @Override
69 | public void before() {
70 | // Do nothing.
71 | }
72 |
73 | public void after() {
74 | FileWriter fileWriter = null;
75 | BufferedReader bufferedReader = null;
76 | try {
77 | ProcessBuilder processBuilder = new ProcessBuilder();
78 |
79 | processBuilder.command("getprop");
80 | processBuilder.redirectErrorStream();
81 | Process process = processBuilder.start();
82 | if (mLogFileAbsoluteLocation == null) {
83 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName,
84 | "getprops.log");
85 | }
86 | fileWriter = new FileWriter(mLogFileAbsoluteLocation);
87 | bufferedReader = new BufferedReader(
88 | new InputStreamReader(process.getInputStream()));
89 | String line;
90 | while ((line = bufferedReader.readLine()) != null) {
91 | fileWriter.append(line);
92 | fileWriter.append("\n");
93 | }
94 | process.waitFor();
95 | if (process.exitValue() != 0) {
96 | throw new Exception("Error while taking getprops, exitCode=" +
97 | process.exitValue());
98 | }
99 | } catch (Exception exception) {
100 | logger.log(Level.SEVERE, "Unable to take a getprops", exception);
101 | } finally {
102 | if (fileWriter != null) {
103 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); }
104 | }
105 | if (bufferedReader != null) {
106 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); }
107 | }
108 | }
109 | }
110 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnableLogcatDump.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import android.os.Build;
20 | import android.os.Trace;
21 | import android.util.Log;
22 |
23 | import com.google.android.perftesting.common.PerfTestingUtils;
24 |
25 | import org.junit.rules.ExternalResource;
26 | import org.junit.runner.Description;
27 | import org.junit.runners.model.Statement;
28 |
29 | /**
30 | * This rule executes clears logcat prior to a test then records the output at the end of a test.
31 | *
32 | *
33 | * @Rule
34 | * public EnableLogcatDump mEnableLogcatDump = new EnableLogcatDump();
35 | *
36 | */
37 | public class EnableLogcatDump extends ExternalResource {
38 |
39 | private static final String LOG_TAG = "EnableLogcatDump";
40 |
41 | private String mTestName;
42 |
43 | private String mTestClass;
44 |
45 | @Override
46 | public Statement apply(Statement base, Description description) {
47 | mTestName = description.getMethodName();
48 | mTestClass = description.getClassName();
49 | return super.apply(base, description);
50 | }
51 |
52 | /**
53 | * Clear logcat buffer prior to test run.
54 | */
55 | public void before() throws Throwable {
56 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
57 | ProcessBuilder processBuilder = new ProcessBuilder();
58 | processBuilder.command("logcat", "-c");
59 | processBuilder.redirectErrorStream();
60 | Process process = processBuilder.start();
61 | process.waitFor();
62 | if (process.exitValue() != 0) {
63 | Log.e(LOG_TAG, "Error while clearing logcat, exitValue=" + process.exitValue());
64 | }
65 | }
66 | }
67 |
68 | /**
69 | * Extract logcat buffer to a file ater test run.
70 | */
71 | public void after() {
72 | try {
73 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
74 | Trace.beginSection("Taking logcat");
75 | }
76 | ProcessBuilder processBuilder = new ProcessBuilder();
77 |
78 | processBuilder.command("logcat", "-d",
79 | "-f", PerfTestingUtils.getTestFile(mTestClass, mTestName, "logcat.log")
80 | .getAbsolutePath());
81 | processBuilder.redirectErrorStream();
82 | Process process = processBuilder.start();
83 | process.waitFor();
84 | if (process.exitValue() != 0) {
85 | Log.e(LOG_TAG, "Error exit value while extracting logcat, exitValue=" +
86 | process.exitValue());
87 | }
88 | } catch (Exception ignored) {
89 | Log.e(LOG_TAG, "Error while extracting logcat", ignored);
90 | } finally {
91 | if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
92 | Trace.endSection();
93 | }
94 | }
95 | }
96 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnableNetStatsDump.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import android.os.Trace;
20 |
21 | import org.junit.rules.ExternalResource;
22 | import org.junit.runner.Description;
23 | import org.junit.runners.model.Statement;
24 |
25 | import java.io.BufferedReader;
26 | import java.io.File;
27 | import java.io.FileWriter;
28 | import java.io.InputStreamReader;
29 | import java.util.logging.Level;
30 | import java.util.logging.Logger;
31 |
32 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile;
33 |
34 | /**
35 | * This rule resets network stats before a test and executes a dumpsys for netstats after
36 | * performing the test.
37 | *
38 | *
39 | * @Rule
40 | * public EnableNetStatsDump mEnableNetStatsDump = new EnableNetStatsDump();
41 | *
42 | */
43 | public class EnableNetStatsDump extends ExternalResource {
44 |
45 | private Logger logger = Logger.getLogger(EnableNetStatsDump.class.getName());
46 |
47 | private String mTestName;
48 |
49 | private String mTestClass;
50 |
51 | private File mLogFileAbsoluteLocation = null;
52 |
53 | public EnableNetStatsDump() {
54 |
55 | }
56 |
57 | /**
58 | * Allow the the log to be written to a specific location.
59 | */
60 | public EnableNetStatsDump(File logFileAbsoluteLocation) {
61 | mLogFileAbsoluteLocation = logFileAbsoluteLocation;
62 | }
63 |
64 | @Override
65 | public Statement apply(Statement base, Description description) {
66 | mTestName = description.getMethodName();
67 | mTestClass = description.getClassName();
68 | return super.apply(base, description);
69 | }
70 |
71 | @Override
72 | public void before() {
73 | if (android.os.Build.VERSION.SDK_INT >= 21) {
74 | try {
75 | ProcessBuilder builder = new ProcessBuilder();
76 | builder.command("dumpsys", "netstats", "--reset");
77 | Process process = builder.start();
78 | process.waitFor();
79 | } catch (Exception exception) {
80 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception);
81 | }
82 | }
83 | }
84 |
85 | public void after() {
86 | if (android.os.Build.VERSION.SDK_INT >= 21) {
87 | FileWriter fileWriter = null;
88 | BufferedReader bufferedReader = null;
89 | try {
90 | Trace.beginSection("Taking netstats dumpsys");
91 | ProcessBuilder processBuilder = new ProcessBuilder();
92 |
93 | processBuilder.command("dumpsys", "netstats", "full", "detail");
94 | processBuilder.redirectErrorStream();
95 | Process process = processBuilder.start();
96 | if (mLogFileAbsoluteLocation == null) {
97 | mLogFileAbsoluteLocation = getTestFile(mTestClass, mTestName,
98 | "netstats.dumpsys.log");
99 | }
100 | fileWriter = new FileWriter(mLogFileAbsoluteLocation);
101 | bufferedReader = new BufferedReader(
102 | new InputStreamReader(process.getInputStream()));
103 | String line;
104 | while ((line = bufferedReader.readLine()) != null) {
105 | fileWriter.append(line);
106 | fileWriter.append(System.lineSeparator());
107 | }
108 | process.waitFor();
109 | if (process.exitValue() != 0) {
110 | throw new Exception("Error while taking dumpsys, exitCode=" +
111 | process.exitValue());
112 | }
113 | } catch (Exception exception) {
114 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception);
115 | } finally {
116 | if (fileWriter != null) {
117 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); }
118 | }
119 | if (bufferedReader != null) {
120 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); }
121 | }
122 | }
123 | }
124 | }
125 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnablePerTestTraceFile.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import org.junit.rules.ExternalResource;
20 | import org.junit.runner.Description;
21 | import org.junit.runners.model.Statement;
22 |
23 | /**
24 | * This rule enables pulling an atrace file that can be run through systrace.py
25 | *
26 | * This JUnit rule requires the test be running on a device that has been rooted. If you don't
27 | * have a device with root you should prefer the systrace be pulled at the system level.
28 | *
29 | * To enable, add this rule to your test class.
30 | *
31 | *
32 | * @Rule
33 | * public EnablePerTestTraceFile mEnablePerTestTraceFile = new EnablePerTestTraceFile();
34 | *
35 | */
36 | public class EnablePerTestTraceFile extends ExternalResource {
37 |
38 | private String mTestName;
39 | private boolean aTraceInUse = false;
40 |
41 | @Override
42 | public Statement apply(Statement base, Description description) {
43 | mTestName = description.getMethodName();
44 | return super.apply(base, description);
45 | }
46 |
47 | @Override
48 | public void before() {
49 | try {
50 | ProcessBuilder builder = new ProcessBuilder();
51 | builder.command("atrace", "--async_start", "-a",
52 | // NOTE: Using the android app BuildConfig specifically.
53 | com.google.android.perftesting.BuildConfig.APPLICATION_ID);
54 | Process process = builder.start();
55 | process.waitFor();
56 | if (process.exitValue() == 0) {
57 | aTraceInUse = true;
58 | }
59 | } catch (Exception ignored) {
60 | // Not much we can do if atrace isn't enabled on the device.
61 | }
62 | }
63 |
64 | @Override
65 | public void after() {
66 | if (aTraceInUse) {
67 | try {
68 | ProcessBuilder builder = new ProcessBuilder();
69 | builder.command("atrace", "--async_stop", "-a",
70 | // NOTE: Using the android app BuildConfig specifically.
71 | com.google.android.perftesting.BuildConfig.APPLICATION_ID);
72 | Process process = builder.start();
73 | process.waitFor();
74 | } catch (Exception ignored) {
75 | // Not much we can do if atrace isn't enabled on the device.
76 | }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnablePostTestDumpsys.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import android.os.Trace;
20 |
21 | import org.junit.rules.ExternalResource;
22 | import org.junit.runner.Description;
23 | import org.junit.runners.model.Statement;
24 |
25 | import java.io.BufferedReader;
26 | import java.io.FileWriter;
27 | import java.io.InputStreamReader;
28 | import java.util.logging.Level;
29 | import java.util.logging.Logger;
30 |
31 | import static com.google.android.perftesting.common.PerfTestingUtils.getTestFile;
32 |
33 | /**
34 | * This rule executes a dumpsys graphics data dump after performing the test. If the API level is
35 | * less than 23 then this rule will do nothing since this dumpsys command isn't supported.
36 | *
37 | *
38 | * @Rule
39 | * public EnablePostTestDumpSys mEnablePostTestDumpSys = new EnablePostTestDumpSys();
40 | *
41 | */
42 | public class EnablePostTestDumpsys extends ExternalResource {
43 |
44 | private Logger logger = Logger.getLogger(EnablePostTestDumpsys.class.getName());
45 |
46 | private String mTestName;
47 | private String mTestClass;
48 |
49 | @Override
50 | public Statement apply(Statement base, Description description) {
51 | mTestName = description.getMethodName();
52 | mTestClass = description.getClassName();
53 | return super.apply(base, description);
54 | }
55 |
56 | @Override
57 | public void before() {
58 | try {
59 | ProcessBuilder builder = new ProcessBuilder();
60 | builder.command("dumpsys", "gfxinfo", "--reset",
61 | // NOTE: Using the android app BuildConfig specifically.
62 | com.google.android.perftesting.BuildConfig.APPLICATION_ID);
63 | Process process = builder.start();
64 | process.waitFor();
65 | } catch (Exception exception) {
66 | logger.log(Level.SEVERE, "Unable to reset dumpsys", exception);
67 | }
68 | }
69 |
70 | public void after() {
71 | if (android.os.Build.VERSION.SDK_INT >= 23) {
72 | FileWriter fileWriter = null;
73 | BufferedReader bufferedReader = null;
74 | try {
75 | Trace.beginSection("Taking Dumpsys");
76 | ProcessBuilder processBuilder = new ProcessBuilder();
77 |
78 | // TODO: If less than API level 23 we should remove framestats.
79 | processBuilder.command("dumpsys", "gfxinfo",
80 | // NOTE: Using the android app BuildConfig specifically.
81 | com.google.android.perftesting.BuildConfig.APPLICATION_ID,
82 | "framestats");
83 | processBuilder.redirectErrorStream();
84 | Process process = processBuilder.start();
85 | fileWriter = new FileWriter(getTestFile(mTestClass, mTestName, "gfxinfo.dumpsys"
86 | + ".log"));
87 | bufferedReader = new BufferedReader(
88 | new InputStreamReader(process.getInputStream()));
89 | String line;
90 | while ((line = bufferedReader.readLine()) != null) {
91 | fileWriter.append(line);
92 | fileWriter.append(System.lineSeparator());
93 | }
94 | process.waitFor();
95 | if (process.exitValue() != 0) {
96 | throw new Exception("Error while taking dumpsys, exitCode=" +
97 | process.exitValue());
98 | }
99 | } catch (Exception exception) {
100 | logger.log(Level.SEVERE, "Unable to take a dumpsys", exception);
101 | } finally {
102 | if (fileWriter != null) {
103 | try { fileWriter.close(); } catch (Exception e) { e.printStackTrace(); }
104 | }
105 | if (bufferedReader != null) {
106 | try { bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); }
107 | }
108 | Trace.endSection();
109 | }
110 | }
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/google/android/perftesting/testrules/EnableTestTracing.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015, Google Inc.
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.google.android.perftesting.testrules;
18 |
19 | import android.os.Trace;
20 |
21 | import org.junit.rules.ExternalResource;
22 | import org.junit.runner.Description;
23 | import org.junit.runners.model.Statement;
24 |
25 | /**
26 | * This rule enables {@link Trace Tracing} for each test. The section name
27 | * used for the Trace API is the name of the test being run.
28 | *
29 | * To enable AndroidTracing on a test simply add this rule like so and it will be enabled/disabled
30 | * when the platform support for Tracing exists (API Level 18 or higher).
31 | *
32 | *
33 | * @Rule
34 | * public EnableTestTracing mEnableTestTracing = new EnableTestTracing();
35 | *
36 | */
37 | public class EnableTestTracing extends ExternalResource {
38 |
39 | private String mTestName;
40 |
41 | @Override
42 | public Statement apply(Statement base, Description description) {
43 | mTestName = description.getMethodName();
44 | return super.apply(base, description);
45 | }
46 |
47 | @Override
48 | public void before() {
49 | if (android.os.Build.VERSION.SDK_INT >= 18) {
50 | Trace.beginSection(mTestName);
51 | }
52 | }
53 |
54 | @Override
55 | public void after() {
56 | if (android.os.Build.VERSION.SDK_INT >= 18) {
57 | Trace.endSection();
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/debug/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |