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 
17 package android.autofillservice.cts.testcore;
18 
19 import static android.autofillservice.cts.testcore.AugmentedHelper.getContentDescriptionForUi;
20 import static android.autofillservice.cts.testcore.AugmentedTimeouts.AUGMENTED_FILL_TIMEOUT;
21 import static android.autofillservice.cts.testcore.AugmentedTimeouts.AUGMENTED_UI_NOT_SHOWN_NAPTIME_MS;
22 
23 import static com.google.common.truth.Truth.assertWithMessage;
24 
25 import android.autofillservice.cts.R;
26 import android.view.autofill.AutofillId;
27 
28 import androidx.annotation.NonNull;
29 import androidx.test.uiautomator.UiObject2;
30 
31 import com.google.common.base.Preconditions;
32 
33 import java.util.Objects;
34 
35 /**
36  * Helper for UI-related needs.
37  */
38 public final class AugmentedUiBot {
39 
40     private final UiBot mUiBot;
41     private boolean mOkToCallAssertUiGone;
42 
AugmentedUiBot(@onNull UiBot uiBot)43     public AugmentedUiBot(@NonNull UiBot uiBot) {
44         mUiBot = uiBot;
45     }
46 
47     /**
48      * Asserts the augmented autofill UI was never shown.
49      *
50      * <p>This method is slower than {@link #assertUiGone()} and should only be called in the
51      * cases where the dataset picker was not previous shown.
52      */
assertUiNeverShown()53     public void assertUiNeverShown() throws Exception {
54         mUiBot.assertNeverShownByRelativeId("augmented autofil UI", R.id.augmentedAutofillUi,
55                 AUGMENTED_UI_NOT_SHOWN_NAPTIME_MS);
56     }
57 
58     /**
59      * Asserts the augmented autofill UI was shown.
60      *
61      * @param focusedId where it should have been shown
62      * @param expectedText the expected text in the UI
63      */
assertUiShown(@onNull AutofillId focusedId, @NonNull String expectedText)64     public UiObject2 assertUiShown(@NonNull AutofillId focusedId,
65             @NonNull String expectedText) throws Exception {
66         Objects.requireNonNull(focusedId);
67         Objects.requireNonNull(expectedText);
68 
69         final UiObject2 ui = mUiBot.assertShownByRelativeId(R.id.augmentedAutofillUi);
70 
71         assertWithMessage("Wrong text on UI").that(ui.getText()).isEqualTo(expectedText);
72 
73         final String expectedContentDescription = getContentDescriptionForUi(focusedId);
74         assertWithMessage("Wrong content description on UI")
75                 .that(ui.getContentDescription()).isEqualTo(expectedContentDescription);
76 
77         mOkToCallAssertUiGone = true;
78 
79         return ui;
80     }
81 
82     /**
83      * Asserts the augmented autofill UI is gone AFTER it was previously shown.
84      *
85      * @throws IllegalStateException if this method is called without calling
86      * {@link #assertUiShown(AutofillId, String)} before.
87      */
assertUiGone()88     public void assertUiGone() {
89         Preconditions.checkState(mOkToCallAssertUiGone, "must call assertUiShown() first");
90         mUiBot.assertGoneByRelativeId(R.id.augmentedAutofillUi, AUGMENTED_FILL_TIMEOUT);
91     }
92 }
93