summaryrefslogtreecommitdiffstats
path: root/libraries/health/runners/longevity/platform/tests/src/android/platform/test/longevity/ScheduledScenarioRunnerTest.java
blob: 78bc467c1221e4be10efd1191011408dac272e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.platform.test.longevity;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.longThat;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;
import static java.lang.Math.abs;

import android.os.Bundle;
import android.platform.test.longevity.proto.Configuration.Scenario;
import android.platform.test.longevity.proto.Configuration.Scenario.AfterTest;
import android.platform.test.longevity.proto.Configuration.Scenario.ExtraArg;
import android.platform.test.longevity.samples.testing.SampleTimedProfileSuite;
import androidx.test.InstrumentationRegistry;

import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestTimedOutException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoAssertionError;

import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

/** Unit tests for the {@link ScheduledScenarioRunner} runner. */
@RunWith(JUnit4.class)
public class ScheduledScenarioRunnerTest {

    @Mock private RunNotifier mRunNotifier;

    private static final String ASSERTION_FAILURE_MESSAGE = "Test assertion failed";

    public static class ArgumentTest {
        public static final String TEST_ARG = "test-arg-test-only";
        public static final String TEST_ARG_DEFAULT = "default";
        public static final String TEST_ARG_OVERRIDE = "not default";

        @Before
        public void setUp() {
            // The actual argument testing happens here as this is where instrumentation args are
            // parsed in the CUJs.
            String argValue =
                    InstrumentationRegistry.getArguments().getString(TEST_ARG, TEST_ARG_DEFAULT);
            Assert.assertEquals(ASSERTION_FAILURE_MESSAGE, argValue, TEST_ARG_OVERRIDE);
        }

        @Test
        public void dummyTest() {
            // Does nothing; always passes.
        }
    }

    // Threshold above which missing the expected timing is considered a failure.
    private static final long TIMING_LEEWAY_MS = 500;

    // Holds the state of the instrumentation args before each test for restoring after, as one test
    // might affect the state of another otherwise.
    // TODO(b/124239142): Avoid manipulating the instrumentation args here.
    private Bundle mArgumentsBeforeTest;

    @Before
    public void setUpSuite() throws InitializationError {
        initMocks(this);
        mArgumentsBeforeTest = InstrumentationRegistry.getArguments();
    }

    @After
    public void restoreSuite() {
        InstrumentationRegistry.registerInstance(
                InstrumentationRegistry.getInstrumentation(), mArgumentsBeforeTest);
    }

    /**
     * Test that an over time test causes a JUnit TestTimedOutException with the correct exception
     * timeout.
     */
    @Test
    public void testOverTimeTest_throwsTestTimedOutException() throws InitializationError {
        ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
        // Set a over time test with a 6-second window that will idle until the end of the window is
        // reached.
        long timeoutMs = TimeUnit.SECONDS.toMillis(6);
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.LongIdleTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.LongIdleTest.class,
                                testScenario,
                                timeoutMs,
                                true));
        runner.run(mRunNotifier);
        // Verify that a TestTimedOutException is fired and that the timeout is correct.
        verify(mRunNotifier, atLeastOnce()).fireTestFailure(failureCaptor.capture());
        List<Failure> failures = failureCaptor.getAllValues();
        boolean correctTestTimedOutExceptionFired =
                failures.stream()
                        .anyMatch(
                                f -> {
                                    if (!(f.getException() instanceof TestTimedOutException)) {
                                        return false;
                                    }
                                    TestTimedOutException exception =
                                            (TestTimedOutException) f.getException();
                                    long exceptionTimeout =
                                            exception
                                                    .getTimeUnit()
                                                    .toMillis(exception.getTimeout());
                                    long expectedTimeout = timeoutMs - runner.getTeardownLeeway();
                                    return abs(exceptionTimeout - expectedTimeout)
                                            <= TIMING_LEEWAY_MS;
                                });
        Assert.assertTrue(correctTestTimedOutExceptionFired);
    }

    /** Test that an over time test does not idle before teardown. */
    @Test
    public void testOverTimeTest_doesNotIdleBeforeTeardown() throws InitializationError {
        // Set a over time test with a 6-second window that will idle until the end of the window is
        // reached.
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.LongIdleTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.LongIdleTest.class,
                                testScenario,
                                TimeUnit.SECONDS.toMillis(6),
                                true));
        runner.run(mRunNotifier);
        // There should not be idle before teardown as the test should not have left itself enough
        // time for that.
        verify(runner, never()).performIdleBeforeTeardown(anyLong());
    }

    /** Test that an over time test still idles until tne next scenario is supposed to begin. */
    @Test
    public void testOverTimeTest_idlesAfterTeardownUntilNextScenario() throws InitializationError {
        // Set a over time test with a 6-second window that will idle until the end of the window is
        // reached.
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.LongIdleTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.LongIdleTest.class,
                                testScenario,
                                TimeUnit.SECONDS.toMillis(6),
                                true));
        runner.run(mRunNotifier);
        // Verify that it still idles until the next scenario; duration should be roughly equal to
        // the leeway set in @{link ScheduledScenarioRunner}.
        verify(runner, times(1))
                .performIdleBeforeNextScenario(
                        getWithinMarginMatcher(runner.getTeardownLeeway(), TIMING_LEEWAY_MS));
    }

    /** Test that a test set to stay in the app after the test idles after its @Test method. */
    @Test
    public void testRespectsAfterTestPolicy_stayInApp() throws InitializationError {
        // Set a passing test with a 6-second timeout that will idle after its @Test method and
        // idle until the end of the timeout is reached.
        long timeoutMs = TimeUnit.SECONDS.toMillis(6);
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.PassingTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.PassingTest.class,
                                testScenario,
                                timeoutMs,
                                true));
        runner.run(mRunNotifier);
        // Idles before teardown; duration should be roughly equal to the timeout minus two times
        // the leeway set in {@link ScheduledScenarioRunner}. Please see that class to see why two
        // times the leeway is expected here.
        verify(runner, times(1))
                .performIdleBeforeTeardown(
                        getWithinMarginMatcher(
                                timeoutMs - 2 * runner.getTeardownLeeway(), TIMING_LEEWAY_MS));
        // Test should have passed.
        verify(mRunNotifier, never()).fireTestFailure(any(Failure.class));
    }

    /** Test that a test set to exit the app after the test does not idle after its @Test method. */
    @Test
    public void testRespectsAfterTestPolicy_exit() throws InitializationError {
        // Set a passing test with a 6-second timeout that does not idle after its @Test method and
        // will idle until the end of the timeout is reached.
        long timeoutMs = TimeUnit.SECONDS.toMillis(6);
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.PassingTest.class.getName())
                        .setAfterTest(AfterTest.EXIT)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.PassingTest.class,
                                testScenario,
                                timeoutMs,
                                true));
        runner.run(mRunNotifier);
        // There should not be idle before teardown.
        verify(runner, never()).performIdleBeforeTeardown(anyLong());
        // Idles before the next scenario; duration should be roughly equal to the timeout.
        verify(runner, times(1))
                .performIdleBeforeNextScenario(getWithinMarginMatcher(timeoutMs, TIMING_LEEWAY_MS));
        // Test should have passed.
        verify(mRunNotifier, never()).fireTestFailure(any(Failure.class));
    }

    /** Test that an ignored scenario still includes the timeout dictated in a profile. */
    @Test
    public void testIgnoredScenario_doesIdle() throws InitializationError, Exception {
        long timeoutMs = TimeUnit.SECONDS.toMillis(6);
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.PassingTest.class.getName())
                        .setAfterTest(AfterTest.EXIT)
                        .build();
        Bundle ignores = new Bundle();
        ignores.putString(
                LongevityClassRunner.FILTER_OPTION,
                SampleTimedProfileSuite.PassingTest.class.getCanonicalName());
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.PassingTest.class,
                                testScenario,
                                timeoutMs,
                                true,
                                ignores));
        RunNotifier notifier = spy(new RunNotifier());
        RunListener listener = mock(RunListener.class);
        notifier.addListener(listener);
        runner.run(notifier);
        // There should not be idle before teardown.
        verify(runner, never()).performIdleBeforeTeardown(anyLong());
        // Ensure the test was ignored via listener.
        verify(listener, times(1)).testIgnored(any());
        // Idles before the next scenario; duration should be roughly equal to the timeout.
        verify(runner, times(1))
                .performIdleBeforeNextScenario(getWithinMarginMatcher(timeoutMs, TIMING_LEEWAY_MS));
    }

    /** Test that the last test does not have idle after it, regardless of its AfterTest policy. */
    @Test
    public void testLastScenarioDoesNotIdle() throws InitializationError {
        // Set a passing test with a 6-second timeout that is set to idle after its @Test method and
        // but should not idle as it will be the last test in practice.
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(SampleTimedProfileSuite.PassingTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                SampleTimedProfileSuite.PassingTest.class,
                                testScenario,
                                TimeUnit.SECONDS.toMillis(6),
                                false));
        runner.run(mRunNotifier);
        // There should not be idle of any form.
        verify(runner, never()).performIdleBeforeTeardown(anyLong());
        verify(runner, never()).performIdleBeforeNextScenario(anyLong());
    }


    /** Test that the "extras" in a scenario is properly registered before the test. */
    @Test
    public void testExtraArgs_registeredBeforeTest() throws Throwable {
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(ArgumentTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .addExtras(
                                ExtraArg.newBuilder()
                                        .setKey(ArgumentTest.TEST_ARG)
                                        .setValue(ArgumentTest.TEST_ARG_OVERRIDE))
                        .build();
        ScheduledScenarioRunner runner =
                spy(
                        new ScheduledScenarioRunner(
                                ArgumentTest.class,
                                testScenario,
                                TimeUnit.SECONDS.toMillis(6),
                                false));
        runner.run(mRunNotifier);
        verifyForAssertionFailures(mRunNotifier);
    }

    /** Test that the "extras" in a scenario is properly un-registered after the test. */
    @Test
    public void testExtraArgs_unregisteredAfterTest() throws Throwable {
        Bundle argsBeforeTest = InstrumentationRegistry.getArguments();
        Scenario testScenario =
                Scenario.newBuilder()
                        .setAt("00:00:00")
                        .setJourney(ArgumentTest.class.getName())
                        .setAfterTest(AfterTest.STAY_IN_APP)
                        .addExtras(
                                ExtraArg.newBuilder()
                                        .setKey(ArgumentTest.TEST_ARG)
                                        .setValue(ArgumentTest.TEST_ARG_OVERRIDE))
                        .build();
        ScheduledScenarioRunner runner =
                new ScheduledScenarioRunner(
                        ArgumentTest.class, testScenario, TimeUnit.SECONDS.toMillis(6), false);
        runner.run(mRunNotifier);
        Bundle argsAfterTest = InstrumentationRegistry.getArguments();
        Assert.assertTrue(bundlesContainSameStringKeyValuePairs(argsBeforeTest, argsAfterTest));
    }

    /** Test that suspension-aware sleep will sleep for the expected duration. */
    @Test
    public void testSuspensionAwareSleep_sleepsForExpectedDuration() {
        long expectedSleepMillis = TimeUnit.SECONDS.toMillis(5);
        long timestampBeforeSleep = System.currentTimeMillis();
        ScheduledScenarioRunner.suspensionAwareSleep(expectedSleepMillis);
        long actualSleepDuration = System.currentTimeMillis() - timestampBeforeSleep;
        Assert.assertTrue(abs(actualSleepDuration - expectedSleepMillis) <= TIMING_LEEWAY_MS);
    }

    /** Test that suspension-aware sleep will end due to alarm going off. */
    @Test
    public void testSuspensionAwareSleep_isWokenUpByAlarm() {
        long expectedSleepMillis = TimeUnit.SECONDS.toMillis(5);
        long timestampBeforeSleep = System.currentTimeMillis();
        // Supply a longer CountDownLatch timeout so that the alarm will fire before the timeout is
        // reached.
        ScheduledScenarioRunner.suspensionAwareSleep(expectedSleepMillis, expectedSleepMillis * 2);
        long actualSleepDuration = System.currentTimeMillis() - timestampBeforeSleep;
        Assert.assertTrue(abs(actualSleepDuration - expectedSleepMillis) <= TIMING_LEEWAY_MS);
    }

    /** Test that the teardown leeway override works. */
    @Test
    public void testTeardownLeewayOverride() throws Throwable {
        Bundle args = new Bundle();
        long leewayOverride = 1000L;
        args.putString(
                ScheduledScenarioRunner.TEARDOWN_LEEWAY_OPTION, String.valueOf(leewayOverride));
        ScheduledScenarioRunner runner =
                new ScheduledScenarioRunner(
                        ArgumentTest.class,
                        Scenario.newBuilder().build(),
                        TimeUnit.SECONDS.toMillis(6),
                        false,
                        args);
        Assert.assertEquals(leewayOverride, runner.getTeardownLeeway());
    }

    /**
     * Helper method to get an argument matcher that checks whether the input value is equal to
     * expected value within a margin.
     */
    private long getWithinMarginMatcher(long expected, long margin) {
        return longThat(duration -> abs(duration - expected) <= margin);
    }

    /**
     * Verify that no test failure is fired because of an assertion failure in the stubbed methods.
     * If the verification fails, check whether it's due the injected assertions failing. If yes,
     * throw that exception out; otherwise, throw the first exception.
     */
    private void verifyForAssertionFailures(final RunNotifier notifier) throws Throwable {
        try {
            verify(notifier, never()).fireTestFailure(any());
        } catch (MockitoAssertionError e) {
            ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
            verify(notifier, atLeastOnce()).fireTestFailure(failureCaptor.capture());
            List<Failure> failures = failureCaptor.getAllValues();
            // Go through the failures, look for an known failure case from the above exceptions
            // and throw the exception in the first one out if any.
            for (Failure failure : failures) {
                if (failure.getException().getMessage().contains(ASSERTION_FAILURE_MESSAGE)) {
                    throw failure.getException();
                }
            }
            // Otherwise, throw the exception from the first failure reported.
            throw failures.get(0).getException();
        }
    }

    /**
     * Helper method to check whether two {@link Bundle}s are equal since the built-in {@code
     * equals} is not properly overridden.
     */
    private boolean bundlesContainSameStringKeyValuePairs(Bundle b1, Bundle b2) {
        if (b1.size() != b2.size()) {
            return false;
        }
        HashSet<String> allKeys = new HashSet<String>(b1.keySet());
        allKeys.addAll(b2.keySet());
        for (String key : allKeys) {
            if (b1.getString(key) != null) {
                // If key is in b1 and corresponds to a string, check whether this key corresponds
                // to the same value in b2.
                if (!b1.getString(key).equals(b2.getString(key))) {
                    return false;
                }
            } else if (b2.getString(key) != null) {
                // Otherwise if b2 has a string at this key, return false since we know that b1 does
                // not have a string at this key.
                return false;
            }
        }
        return true;
    }
}