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 
17 package android.autofillservice.cts;
18 
19 import android.view.View;
20 import android.view.accessibility.AccessibilityEvent;
21 
22 import java.util.concurrent.TimeoutException;
23 
24 /**
25  * Base class for test cases using {@link LoginActivity}.
26  */
27 public abstract class AbstractLoginActivityTestCase
28         extends AutoFillServiceTestCase.AutoActivityLaunch<LoginActivity> {
29 
30     protected LoginActivity mActivity;
31 
32     @Override
getActivityRule()33     protected AutofillActivityTestRule<LoginActivity> getActivityRule() {
34         return new AutofillActivityTestRule<LoginActivity>(
35                 LoginActivity.class) {
36             @Override
37             protected void afterActivityLaunched() {
38                 mActivity = getActivity();
39             }
40         };
41     }
42 
43     /**
44      * Requests focus on username and expect Window event happens.
45      */
46     protected void requestFocusOnUsername() throws TimeoutException {
47         mUiBot.waitForWindowChange(() -> mActivity.onUsername(View::requestFocus));
48     }
49 
50     /**
51      * Requests focus on username and expect no Window event happens.
52      */
53     protected void requestFocusOnUsernameNoWindowChange() {
54         final AccessibilityEvent event;
55         try {
56             event = mUiBot.waitForWindowChange(() -> mActivity.onUsername(View::requestFocus),
57                     Timeouts.WINDOW_CHANGE_NOT_GENERATED_NAPTIME_MS);
58         } catch (WindowChangeTimeoutException ex) {
59             // no window events! looking good
60             return;
61         }
62         throw new IllegalStateException("Expect no window event when focusing to"
63                 + " username, but event happened: " + event);
64     }
65 
66     /**
67      * Requests focus on password and expect Window event happens.
68      */
69     protected void requestFocusOnPassword() throws TimeoutException {
70         mUiBot.waitForWindowChange(() -> mActivity.onPassword(View::requestFocus));
71     }
72 
73     /**
74      * Clears focus from input fields by focusing on the parent layout.
75      */
76     protected void clearFocus() {
77         mActivity.clearFocus();
78     }
79 }
80