• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 android.platform.test.longevity.samples.testing;
18 
19 import android.platform.test.longevity.ProfileSuite;
20 import android.platform.test.scenario.annotation.Scenario;
21 import androidx.test.InstrumentationRegistry;
22 
23 import org.junit.Assert;
24 import org.junit.ClassRule;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TestRule;
28 import org.junit.runner.Description;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 import org.junit.runners.Suite.SuiteClasses;
32 import org.junit.runners.model.Statement;
33 
34 @RunWith(ProfileSuite.class)
35 @SuiteClasses({
36     SampleExtraArgsSuite.ClassLevelExtraArgsScenario.class,
37     SampleExtraArgsSuite.TestLevelExtraArgsScenario.class,
38     SampleExtraArgsSuite.InTestExtraArgsScenario.class,
39     SampleExtraArgsSuite.NotInInstrumentationArgsTest.class
40 })
41 /** A sample test suite to verify that extra args are injected before the test class starts. */
42 public class SampleExtraArgsSuite {
43 
44     // A string to filter out expected assertion failures in the test.
45     public static final String ASSERTION_FAILURE_MESSAGE = "Test assertion failed";
46 
47     /** This test simulates a test option that is used as a {@link ClassRule}. */
48     @Scenario
49     @RunWith(JUnit4.class)
50     public static class ClassLevelExtraArgsScenario {
51         public static final String CLASS_LEVEL_OPTION = "class-level-option";
52         public static final String CLASS_LEVEL_DEFAULT = "class-level-default";
53         public static final String CLASS_LEVEL_OVERRIDE = "class-level-override";
54 
55         @ClassRule
56         public static TestRule classLevelOption =
57                 new TestRule() {
58                     @Override
59                     public Statement apply(Statement base, Description description) {
60                         return new Statement() {
61                             @Override
62                             public void evaluate() throws Throwable {
63                                 Assert.assertEquals(
64                                         ASSERTION_FAILURE_MESSAGE,
65                                         CLASS_LEVEL_OVERRIDE,
66                                         InstrumentationRegistry.getArguments()
67                                                 .getString(
68                                                         CLASS_LEVEL_OPTION, CLASS_LEVEL_DEFAULT));
69                                 base.evaluate();
70                             }
71                         };
72                     }
73                 };
74 
75         @Test
test()76         public void test() {}
77     }
78 
79     /** This test simulates a test option that is used as a {@link Rule}. */
80     @Scenario
81     @RunWith(JUnit4.class)
82     public static class TestLevelExtraArgsScenario {
83         public static final String TEST_LEVEL_OPTION = "test-level-option";
84         public static final String TEST_LEVEL_DEFAULT = "test-level-default";
85         public static final String TEST_LEVEL_OVERRIDE = "test-level-override";
86 
87         @Rule
88         public TestRule classLevelOption =
89                 new TestRule() {
90                     @Override
91                     public Statement apply(Statement base, Description description) {
92                         return new Statement() {
93                             @Override
94                             public void evaluate() throws Throwable {
95                                 Assert.assertEquals(
96                                         ASSERTION_FAILURE_MESSAGE,
97                                         TEST_LEVEL_OVERRIDE,
98                                         InstrumentationRegistry.getArguments()
99                                                 .getString(TEST_LEVEL_OPTION, TEST_LEVEL_DEFAULT));
100                                 base.evaluate();
101                             }
102                         };
103                     }
104                 };
105 
106         @Test
test()107         public void test() {}
108     }
109 
110     /** This test simulates an argument that is read within a test. */
111     @Scenario
112     @RunWith(JUnit4.class)
113     public static class InTestExtraArgsScenario {
114         public static final String IN_TEST_ARG = "in-test-option";
115         public static final String IN_TEST_DEFAULT = "in-test-default";
116         public static final String IN_TEST_OVERRIDE = "in-test-override";
117 
118         @Test
testArgOverride()119         public void testArgOverride() {
120             Assert.assertEquals(
121                     ASSERTION_FAILURE_MESSAGE,
122                     IN_TEST_OVERRIDE,
123                     InstrumentationRegistry.getArguments().getString(IN_TEST_ARG, IN_TEST_DEFAULT));
124         }
125     }
126 
127     /** Tests that a particular argument is not in instrumentation args. */
128     @Scenario
129     @RunWith(JUnit4.class)
130     public static class NotInInstrumentationArgsTest {
131         public static final String ARG_TO_CHECK_OPTION = "arg-to-check";
132 
133         @Test
testArgIsAbsent()134         public void testArgIsAbsent() {
135             String argToCheck =
136                     InstrumentationRegistry.getArguments().getString(ARG_TO_CHECK_OPTION);
137             Assert.assertNotNull(ASSERTION_FAILURE_MESSAGE, argToCheck);
138             Assert.assertNull(
139                     ASSERTION_FAILURE_MESSAGE,
140                     InstrumentationRegistry.getArguments().getString(argToCheck));
141         }
142     }
143 }
144