• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 android.platform.test.rule;
17 
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.util.Log;
21 
22 import androidx.test.InstrumentationRegistry;
23 import androidx.test.uiautomator.UiDevice;
24 
25 import java.io.IOException;
26 
27 import org.junit.AssumptionViolatedException;
28 import org.junit.rules.TestRule;
29 import org.junit.runner.Description;
30 import org.junit.runners.model.Statement;
31 
32 /**
33  * Similar to {@link org.junit.rules.TestWatcher}, but does not perform operations quietly, i.e. by
34  * delaying failures and running the underlying base {@link Statement} under all circumstances. It
35  * also has additional common support for platform testing.
36  */
37 public class TestWatcher implements TestRule {
38     private static final String LOG_TAG = TestWatcher.class.getSimpleName();
39 
40     private UiDevice mDevice;
41 
apply(final Statement base, final Description description)42     public Statement apply(final Statement base, final Description description) {
43         return new Statement() {
44             @Override
45             public void evaluate() throws Throwable {
46                 try {
47                     starting(description);
48                     base.evaluate();
49                     succeeded(description);
50                 } catch (AssumptionViolatedException e) {
51                     skipped(e, description);
52                     throw e;
53                 } catch (Throwable e) {
54                     failed(e, description);
55                     throw e;
56                 } finally {
57                     finished(description);
58                 }
59             }
60         };
61     }
62 
63     /** Invoked when a test is about to start. */
64     protected void starting(Description description) {}
65 
66     /** Invoked when a test succeeds. */
67     protected void succeeded(Description description) {}
68 
69     /** Invoked when a test is skipped due to a failed assumption. */
70     protected void skipped(AssumptionViolatedException e, Description description) {}
71 
72     /** Invoked when a test fails. */
73     protected void failed(Throwable e, Description description) {}
74 
75     /** Invoked when a test method finishes (whether passing or failing). */
76     protected void finished(Description description) {}
77 
78     /**
79      * Returns the active {@link UiDevice} to interact with.
80      *
81      * <p>Override this for unit testing device calls.
82      */
83     protected UiDevice getUiDevice() {
84         if (mDevice == null) {
85             mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
86         }
87         return mDevice;
88     }
89 
90     /**
91      * Runs a shell command, {@code cmd}, and returns the output.
92      *
93      * <p>Override this for unit testing shell commands.
94      */
95     protected String executeShellCommand(String cmd) {
96         try {
97             Log.v(LOG_TAG, String.format("Executing command from %s: %s", this.getClass(), cmd));
98             return getUiDevice().executeShellCommand(cmd);
99         } catch (IOException e) {
100             throw new RuntimeException(e);
101         }
102     }
103 
104     /**
105      * Returns the {@link Bundle} containing registered arguments.
106      *
107      * <p>Override this for unit testing device calls.
108      */
109     protected Bundle getArguments() {
110         return InstrumentationRegistry.getArguments();
111     }
112 
113     /** Returns the {@link Context} for this application. */
114     protected Context getContext() {
115         return InstrumentationRegistry.getContext();
116     }
117 }
118