1/*
2 * Copyright (C) 2016 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
17package android.perftests.utils;
18
19import android.support.test.InstrumentationRegistry;
20
21import org.junit.rules.TestRule;
22import org.junit.runner.Description;
23import org.junit.runners.model.Statement;
24
25import static junit.framework.Assert.assertFalse;
26import static junit.framework.Assert.assertTrue;
27
28/**
29 * Use this rule to make sure we report the status after the test success.
30 *
31 * <code>
32 *
33 * @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
34 * @Test public void functionName() {
35 *     ...
36 *     BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
37 *     while (state.keepRunning()) {
38 *         // DO YOUR TEST HERE!
39 *     }
40 *     ...
41 * }
42 * </code>
43 *
44 * When test succeeded, the status report will use the key as
45 * "functionName[optional subTestName]_*"
46 *
47 * Notice that optional subTestName can't be just numbers, that means each sub test needs to have a
48 * name when using parameterization.
49 */
50
51public class PerfStatusReporter implements TestRule {
52    private final BenchmarkState mState = new BenchmarkState();
53
54    public BenchmarkState getBenchmarkState() {
55        return mState;
56    }
57
58    @Override
59    public Statement apply(Statement base, Description description) {
60        return new Statement() {
61            @Override
62            public void evaluate() throws Throwable {
63                String invokeMethodName = description.getMethodName();
64                // validate and simplify the function name.
65                // First, remove the "test" prefix which normally comes from CTS test.
66                // Then make sure the [subTestName] is valid, not just numbers like [0].
67                if (invokeMethodName.startsWith("test")) {
68                    assertTrue("The test name " + invokeMethodName + " is too short",
69                            invokeMethodName.length() > 5);
70                    invokeMethodName = invokeMethodName.substring(4, 5).toLowerCase()
71                            + invokeMethodName.substring(5);
72                }
73
74                int index = invokeMethodName.lastIndexOf('[');
75                if (index > 0) {
76                    boolean allDigits = true;
77                    for (int i = index + 1; i < invokeMethodName.length() - 1; i++) {
78                        if (!Character.isDigit(invokeMethodName.charAt(i))) {
79                            allDigits = false;
80                            break;
81                        }
82                    }
83                    assertFalse("The name in [] can't contain only digits for " + invokeMethodName,
84                            allDigits);
85                }
86
87                base.evaluate();
88
89                mState.sendFullStatusReport(InstrumentationRegistry.getInstrumentation(),
90                        invokeMethodName);
91            }
92        };
93    }
94}
95