1 /*
2  * Copyright (C) 2023 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.launcher3.util.rule;
17 
18 import androidx.annotation.NonNull;
19 import androidx.test.InstrumentationRegistry;
20 import androidx.test.uiautomator.UiDevice;
21 
22 import com.android.launcher3.tapl.LauncherInstrumentation;
23 import com.android.launcher3.ui.AbstractLauncherUiTest;
24 
25 import org.junit.rules.TestRule;
26 import org.junit.runner.Description;
27 import org.junit.runners.model.Statement;
28 
29 /**
30  * Isolates tests from some of the state created by the previous test.
31  */
32 public class TestIsolationRule implements TestRule {
33     private final LauncherInstrumentation mLauncher;
34     private final boolean mRequireOneActiveActivity;
35 
TestIsolationRule(LauncherInstrumentation launcher, boolean requireOneActiveActivity)36     public TestIsolationRule(LauncherInstrumentation launcher, boolean requireOneActiveActivity) {
37         mLauncher = launcher;
38         mRequireOneActiveActivity = requireOneActiveActivity;
39     }
40 
41     @NonNull
42     @Override
apply(@onNull Statement base, @NonNull Description description)43     public Statement apply(@NonNull Statement base, @NonNull Description description) {
44         return new Statement() {
45             @Override
46             public void evaluate() throws Throwable {
47                 base.evaluate();
48                 // Make sure that Launcher workspace looks correct.
49 
50                 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressHome();
51                 AbstractLauncherUiTest.checkDetectedLeaks(mLauncher, mRequireOneActiveActivity);
52             }
53         };
54     }
55 }
56