1 /*
2  * Copyright (C) 2024 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 package com.android.adservices.shared.testing;
17 
18 import com.android.adservices.shared.testing.Logger.RealLogger;
19 
20 import com.google.common.truth.Expect;
21 
22 import org.junit.Rule;
23 
24 import java.util.concurrent.atomic.AtomicInteger;
25 
26 /**
27  * "Uber" superclass for all tests.
28  *
29  * <p>It provide the bare minimum features that will be used by all sort of tests (unit / CTS,
30  * device/host side, project-specific).
31  */
32 public abstract class SidelessTestCase implements TestNamer {
33 
34     private static final AtomicInteger sNextInvocationId = new AtomicInteger();
35 
36     // TODO(b/342639109): set order
37     @Rule public final Expect expect = Expect.create();
38 
39     private final int mInvocationId = sNextInvocationId.incrementAndGet();
40 
41     // TODO(b/285014040): log test number / to String on constructor (will require logV()).
42     // Something like (which used to be on AdServicesTestCase):
43     // Log.d(TAG, "setTestNumber(): " + getTestName() + " is test #" + mTestNumber);
44 
45     protected final Logger mLog;
46     protected final RealLogger mRealLogger;
47 
SidelessTestCase()48     public SidelessTestCase() {
49         this(DynamicLogger.getInstance());
50     }
51 
SidelessTestCase(RealLogger realLogger)52     public SidelessTestCase(RealLogger realLogger) {
53         mRealLogger = realLogger;
54         mLog = new Logger(realLogger, getClass());
55     }
56 
57     @Override
getTestName()58     public String getTestName() {
59         return DEFAULT_TEST_NAME;
60     }
61 
62     /** Gets a unique id for the test invocation. */
getTestInvocationId()63     public final int getTestInvocationId() {
64         return mInvocationId;
65     }
66 
67     // TODO(b/285014040): add more features like:
68     // - sleep()
69     // - logV()
70     // - toString()
71 }
72