1 /*
2  * Copyright (C) 2019 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.autofillservice.cts.commontests;
17 
18 import static android.autofillservice.cts.testcore.Helper.allowOverlays;
19 import static android.autofillservice.cts.testcore.Helper.disallowOverlays;
20 
21 import android.autofillservice.cts.activities.AbstractAutoFillActivity;
22 import android.autofillservice.cts.testcore.AugmentedHelper;
23 import android.autofillservice.cts.testcore.AugmentedUiBot;
24 import android.autofillservice.cts.testcore.CtsAugmentedAutofillService;
25 import android.autofillservice.cts.testcore.CtsAugmentedAutofillService.AugmentedReplier;
26 import android.autofillservice.cts.testcore.UiBot;
27 import android.content.AutofillOptions;
28 import android.view.autofill.AutofillManager;
29 
30 import com.android.compatibility.common.util.RequiredSystemResourceRule;
31 
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.rules.RuleChain;
36 import org.junit.rules.TestRule;
37 
38 /////
39 ///// NOTE: changes in this class should also be applied to
40 /////       AugmentedAutofillManualActivityLaunchTestCase, which is exactly the same as this except
41 /////       by which class it extends.
42 
43 // Must be public because of the @ClassRule
44 public abstract class AugmentedAutofillAutoActivityLaunchTestCase
45         <A extends AbstractAutoFillActivity> extends AutoFillServiceTestCase.AutoActivityLaunch<A> {
46 
47     protected static AugmentedReplier sAugmentedReplier;
48     protected AugmentedUiBot mAugmentedUiBot;
49 
50     private CtsAugmentedAutofillService.ServiceWatcher mServiceWatcher;
51 
52     private static final RequiredSystemResourceRule sRequiredResource =
53             new RequiredSystemResourceRule("config_defaultAugmentedAutofillService");
54 
55     private static final RuleChain sRequiredFeatures = RuleChain
56             .outerRule(sRequiredFeaturesRule)
57             .around(sRequiredResource);
58 
AugmentedAutofillAutoActivityLaunchTestCase()59     public AugmentedAutofillAutoActivityLaunchTestCase() {}
60 
AugmentedAutofillAutoActivityLaunchTestCase(UiBot uiBot)61     public AugmentedAutofillAutoActivityLaunchTestCase(UiBot uiBot) {
62         super(uiBot);
63     }
64 
65     @BeforeClass
allowAugmentedAutofill()66     public static void allowAugmentedAutofill() {
67         sContext.getApplicationContext()
68                 .setAutofillOptions(AutofillOptions.forWhitelistingItself());
69         allowOverlays();
70     }
71 
72     @AfterClass
resetAllowAugmentedAutofill()73     public static void resetAllowAugmentedAutofill() {
74         sContext.getApplicationContext().setAutofillOptions(null);
75         disallowOverlays();
76     }
77 
78     @Before
setFixtures()79     public void setFixtures() {
80         mServiceWatcher = null;
81         sAugmentedReplier = CtsAugmentedAutofillService.getAugmentedReplier();
82         sAugmentedReplier.reset();
83         CtsAugmentedAutofillService.resetStaticState();
84         mAugmentedUiBot = new AugmentedUiBot(mUiBot);
85         mSafeCleanerRule
86                 .run(() -> sAugmentedReplier.assertNoUnhandledFillRequests())
87                 .run(() -> {
88                     AugmentedHelper.resetAugmentedService(sContext);
89                     if (mServiceWatcher != null) {
90                         mServiceWatcher.waitOnDisconnected();
91                     }
92                 })
93                 .add(() -> {
94                     return sAugmentedReplier.getExceptions();
95                 });
96     }
97 
98     @Override
getNumberRetries()99     protected int getNumberRetries() {
100         return 0; // A.K.A. "Optimistic Thinking"
101     }
102 
103     @Override
getSmartSuggestionMode()104     protected int getSmartSuggestionMode() {
105         return AutofillManager.FLAG_SMART_SUGGESTION_SYSTEM;
106     }
107 
108     @Override
getRequiredFeaturesRule()109     protected TestRule getRequiredFeaturesRule() {
110         return sRequiredFeatures;
111     }
112 
enableAugmentedService()113     protected CtsAugmentedAutofillService enableAugmentedService() throws InterruptedException {
114         if (mServiceWatcher != null) {
115             throw new IllegalStateException("There Can Be Only One!");
116         }
117 
118         mServiceWatcher = CtsAugmentedAutofillService.setServiceWatcher();
119         AugmentedHelper.setAugmentedService(CtsAugmentedAutofillService.SERVICE_NAME, sContext);
120 
121         CtsAugmentedAutofillService service = mServiceWatcher.waitOnConnected();
122         return service;
123     }
124 
waitUntilDisconnected()125     protected void waitUntilDisconnected() throws Exception {
126         if (mServiceWatcher != null) {
127             mServiceWatcher.waitOnDisconnected();
128             // Prevent SafeCleanerRule calls it again
129             mServiceWatcher = null;
130         }
131     }
132 }
133