• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.helpers.common.test;
18 
19 import android.os.Bundle;
20 import android.platform.test.helpers.HelperManager;
21 import android.platform.test.helpers.IStandardAppHelper;
22 import android.platform.test.helpers.listeners.FailureTestWatcher;
23 import android.support.test.InstrumentationRegistry;
24 import android.support.test.uiautomator.UiDevice;
25 import android.util.Log;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TestRule;
32 import org.junit.rules.Timeout;
33 
34 import java.time.Duration;
35 import java.util.HashMap;
36 import java.util.Map;
37 
38 import com.android.permissionutils.GrantPermissionUtil;
39 
40 /**
41  * A base-class for testing app helper implementations.
42  *
43  * @param T the helper interface under test.
44  */
45 public abstract class HelperTest<T extends IStandardAppHelper> {
46     private static final String LOG_TAG = HelperTest.class.getSimpleName();
47     private static final String SKIP_INIT_PARAM = "skip-init";
48 
49     // Keep track (across tests) of the initialized applications.
50     private static Map<Class, Boolean> mInitMap = new HashMap<Class, Boolean>();
51 
52     // Global 5-minute test timeout
53     @Rule
54     public final TestRule timeout = Timeout.millis(Duration.ofMinutes(5).toMillis());
55 
56     // Global screenshot capture on failures
57     public FailureTestWatcher watcher = new FailureTestWatcher();
58 
59     @Rule
setUpWatcher()60     public FailureTestWatcher setUpWatcher() {
61         watcher.setHelper(getHelper());
62         return watcher;
63     }
64 
65     protected UiDevice mDevice;
66     protected T mHelper;
67 
68     /**
69      * Set up the target application before each test case starts.
70      */
71     @Before
setUp()72     public void setUp() {
73         // Initialize each application once on the first open unless skipped.
74         if (!mInitMap.containsKey(getHelperClass()) &&
75                 !"true".equals(getArguments().get(SKIP_INIT_PARAM))) {
76             initialize();
77             mInitMap.put(getHelperClass(), true);
78         }
79         resetApp();
80         openApp();
81     }
82 
83     /**
84      * Tear down the target application after each test case completes.
85      */
86     @After
tearDown()87     public void tearDown() {
88         exitApp();
89     }
90 
91     /**
92      * An empty test that ensures setup and initialization work properly.
93      */
94     @Test
testDismissDialogs()95     public void testDismissDialogs() { }
96 
97     /**
98      * Initialize the target application once before the test suite starts.
99      */
initialize()100     public void initialize() {
101         openApp();
102         getHelper().dismissInitialDialogs();
103         exitApp();
104     }
105 
106     /**
107      * Reset the target application.
108      */
resetApp()109     public void resetApp() { }
110 
111     /**
112      * Open the target application.
113      */
openApp()114     public void openApp() {
115         // Open the application.
116         getHelper().open();
117     }
118 
119     /**
120      * Exit the target application.
121      */
exitApp()122     public void exitApp() {
123         // Exit the application.
124         getHelper().exit();
125     }
126 
127     /**
128      * @return the connected {@link UiDevice}
129      */
getDevice()130     public UiDevice getDevice() {
131         if (mDevice == null) {
132             mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
133         }
134 
135         return mDevice;
136     }
137 
138     /**
139      * @return the {@link Bundle} of arguments
140      */
getArguments()141     public Bundle getArguments() {
142         return InstrumentationRegistry.getArguments();
143     }
144 
145     /**
146      * @return an implementation for {@code T}
147      */
getHelper()148     public T getHelper() {
149         if (mHelper == null) {
150             mHelper = HelperManager.getInstance(
151                     InstrumentationRegistry.getContext(),
152                     InstrumentationRegistry.getInstrumentation())
153                         .get(getHelperClass());
154         }
155 
156         return mHelper;
157     }
158 
getHelperClass()159     protected abstract Class<T> getHelperClass();
160 }
161