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