1 /*
2  * Copyright (C) 2017 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.compatibility.common.util;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 /**
23  * Generic helper used to set / get the the name of the test being run.
24  *
25  * <p>Typically used on {@code @Rule} classes.
26  */
27 public final class TestNameUtils {
28 
29     private static String sCurrentTestName;
30     private static String sCurrentTestClass;
31 
32     /**
33      * Gets the name of the test current running.
34      */
35     @NonNull
getCurrentTestName()36     public static String getCurrentTestName() {
37         if (sCurrentTestName != null) return sCurrentTestName;
38         if (sCurrentTestClass != null) return sCurrentTestClass;
39         return "(Unknown test)";
40     }
41 
42     /**
43      * Sets the name of the test current running
44      */
setCurrentTestName(@ullable String name)45     public static void setCurrentTestName(@Nullable String name) {
46         sCurrentTestName = name;
47     }
48 
49     /**
50      * Sets the name of the test class current running
51      */
setCurrentTestClass(@ullable String testClass)52     public static void setCurrentTestClass(@Nullable String testClass) {
53         sCurrentTestClass = testClass;
54     }
55 
56     /**
57      * Checks whether a test is running, based on whether {@link #setCurrentTestName(String)} was
58      * called.
59      */
isRunningTest()60     public static boolean isRunningTest() {
61         return sCurrentTestName != null;
62     }
63 
TestNameUtils()64     private TestNameUtils() {
65         throw new UnsupportedOperationException("contain static methods only");
66     }
67 }
68