1 /*
2  * Copyright (C) 2017 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 static com.google.common.truth.Truth.assertWithMessage;
20 
21 import android.app.Activity;
22 import android.view.autofill.AutofillManager;
23 
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 
27 /**
28   * Base class for all activities in this test suite
29   */
30 abstract class AbstractAutoFillActivity extends Activity {
31 
32     private MyAutofillCallback mCallback;
33 
34     /**
35      * Run an action in the UI thread, and blocks caller until the action is finished.
36      */
syncRunOnUiThread(Runnable action)37     public final void syncRunOnUiThread(Runnable action) {
38         syncRunOnUiThread(action, Helper.UI_TIMEOUT_MS);
39     }
40 
41     /**
42      * Run an action in the UI thread, and blocks caller until the action is finished or it times
43      * out.
44      */
syncRunOnUiThread(Runnable action, int timeoutMs)45     public final void syncRunOnUiThread(Runnable action, int timeoutMs) {
46         final CountDownLatch latch = new CountDownLatch(1);
47         runOnUiThread(() -> {
48             action.run();
49             latch.countDown();
50         });
51         try {
52             if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) {
53                 throw new RetryableException("action on UI thread timed out after %d ms",
54                         timeoutMs);
55             }
56         } catch (InterruptedException e) {
57             Thread.currentThread().interrupt();
58             throw new RuntimeException("Interrupted", e);
59         }
60     }
61 
getAutofillManager()62     protected AutofillManager getAutofillManager() {
63         return getSystemService(AutofillManager.class);
64     }
65 
66     /**
67      * Registers and returns a custom callback for autofill events.
68      */
registerCallback()69     protected MyAutofillCallback registerCallback() {
70         assertWithMessage("already registered").that(mCallback).isNull();
71         mCallback = new MyAutofillCallback();
72         getAutofillManager().registerCallback(mCallback);
73         return mCallback;
74     }
75 
76     /**
77      * Unregister the callback from the {@link AutofillManager}.
78      */
unregisterCallback()79     protected void unregisterCallback() {
80         assertWithMessage("not registered").that(mCallback).isNotNull();
81         getAutofillManager().unregisterCallback(mCallback);
82         mCallback = null;
83     }
84 }
85