• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.test;
6 
7 import org.junit.rules.TestRule;
8 import org.junit.runner.Description;
9 import org.junit.runners.model.Statement;
10 
11 import org.chromium.base.LifetimeAssert;
12 import org.chromium.base.test.util.Batch;
13 import org.chromium.base.test.util.RequiresRestart;
14 
15 /** TestRule used to ensure we don't leak LifetimeAsserts in unit tests. */
16 public final class UnitTestLifetimeAssertRule implements TestRule {
17     @Override
apply(Statement base, Description description)18     public Statement apply(Statement base, Description description) {
19         return new Statement() {
20             @Override
21             public void evaluate() throws Throwable {
22                 base.evaluate();
23                 Batch annotation = description.getTestClass().getAnnotation(Batch.class);
24                 if (annotation != null && annotation.value().equals(Batch.UNIT_TESTS)) {
25                     if (description.getAnnotation(RequiresRestart.class) != null) return;
26                     LifetimeAssert.assertAllInstancesDestroyedForTesting();
27                 }
28             }
29         };
30     }
31 }
32