1 /* 2 * Copyright (C) 2022 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.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import android.content.res.Resources; 23 import android.support.test.uiautomator.By; 24 import android.support.test.uiautomator.BySelector; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.Until; 27 28 import androidx.test.InstrumentationRegistry; 29 30 import com.android.internal.R; 31 32 final class FloatingToolbarUtils { 33 34 private final UiDevice mDevice; 35 private static final BySelector TOOLBAR_CONTAINER_SELECTOR = 36 By.res("android", "floating_popup_container"); 37 FloatingToolbarUtils()38 FloatingToolbarUtils() { 39 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 40 } 41 waitForFloatingToolbarPopup()42 void waitForFloatingToolbarPopup() { 43 mDevice.wait(Until.findObject(TOOLBAR_CONTAINER_SELECTOR), 500); 44 } 45 assertFloatingToolbarIsDisplayed()46 void assertFloatingToolbarIsDisplayed() { 47 waitForFloatingToolbarPopup(); 48 assertThat(mDevice.hasObject(TOOLBAR_CONTAINER_SELECTOR)).isTrue(); 49 } 50 assertFloatingToolbarContainsItem(String itemLabel)51 void assertFloatingToolbarContainsItem(String itemLabel) { 52 waitForFloatingToolbarPopup(); 53 assertWithMessage("Expected to find item labelled [" + itemLabel + "]") 54 .that(mDevice.hasObject( 55 TOOLBAR_CONTAINER_SELECTOR.hasDescendant(By.text(itemLabel)))) 56 .isTrue(); 57 } 58 assertFloatingToolbarDoesNotContainItem(String itemLabel)59 void assertFloatingToolbarDoesNotContainItem(String itemLabel) { 60 waitForFloatingToolbarPopup(); 61 assertWithMessage("Expected to not find item labelled [" + itemLabel + "]") 62 .that(mDevice.hasObject( 63 TOOLBAR_CONTAINER_SELECTOR.hasDescendant(By.text(itemLabel)))) 64 .isFalse(); 65 } 66 assertFloatingToolbarContainsItemAtIndex(String itemLabel, int index)67 void assertFloatingToolbarContainsItemAtIndex(String itemLabel, int index) { 68 waitForFloatingToolbarPopup(); 69 assertWithMessage("Expected to find item labelled [" + itemLabel + "] at index " + index) 70 .that(mDevice.findObject(TOOLBAR_CONTAINER_SELECTOR) 71 .findObjects(By.clickable(true)) 72 .get(index) 73 .getChildren() 74 .get(1) 75 .getText()) 76 .isEqualTo(itemLabel); 77 } 78 clickFloatingToolbarItem(String label)79 void clickFloatingToolbarItem(String label) { 80 waitForFloatingToolbarPopup(); 81 mDevice.findObject(TOOLBAR_CONTAINER_SELECTOR) 82 .findObject(By.text(label)) 83 .click(); 84 } 85 clickFloatingToolbarOverflowItem(String label)86 void clickFloatingToolbarOverflowItem(String label) { 87 // TODO: There might be a benefit to combining this with "clickFloatingToolbarItem" method. 88 waitForFloatingToolbarPopup(); 89 mDevice.findObject(TOOLBAR_CONTAINER_SELECTOR) 90 .findObject(By.desc(str(R.string.floating_toolbar_open_overflow_description))) 91 .click(); 92 mDevice.wait( 93 Until.findObject(TOOLBAR_CONTAINER_SELECTOR.hasDescendant(By.text(label))), 94 1000); 95 mDevice.findObject(TOOLBAR_CONTAINER_SELECTOR) 96 .findObject(By.text(label)) 97 .click(); 98 } 99 str(int id)100 private static String str(int id) { 101 return Resources.getSystem().getString(id); 102 } 103 } 104