| /* |
| * Copyright (C) 2021 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.car.ui.actions; |
| |
| import static androidx.test.espresso.matcher.ViewMatchers.isRoot; |
| |
| import android.view.View; |
| |
| import androidx.test.espresso.PerformException; |
| import androidx.test.espresso.UiController; |
| import androidx.test.espresso.ViewAction; |
| import androidx.test.espresso.util.HumanReadables; |
| import androidx.test.espresso.util.TreeIterables; |
| |
| import org.hamcrest.Description; |
| import org.hamcrest.Matcher; |
| import org.hamcrest.StringDescription; |
| |
| import java.util.concurrent.TimeoutException; |
| |
| public class WaitForNoMatchingViewAction implements ViewAction { |
| private Matcher<View> mMatcher; |
| private long mWaitTimeMillis; |
| |
| public WaitForNoMatchingViewAction(Matcher<View> matcher, long waitTimeMillis) { |
| mMatcher = matcher; |
| mWaitTimeMillis = waitTimeMillis; |
| } |
| |
| @Override |
| public Matcher<View> getConstraints() { |
| return isRoot(); |
| } |
| |
| @Override |
| public String getDescription() { |
| Description description = new StringDescription(); |
| mMatcher.describeTo(description); |
| return "wait at most " + mWaitTimeMillis + " milliseconds for no views matching " |
| + description.toString(); |
| } |
| |
| @Override |
| public void perform(UiController uiController, View view) { |
| uiController.loopMainThreadUntilIdle(); |
| final long startTime = System.currentTimeMillis(); |
| final long endTime = startTime + mWaitTimeMillis; |
| |
| do { |
| boolean isViewFound = false; |
| for (View child : TreeIterables.breadthFirstViewTraversal(view)) { |
| if (mMatcher.matches(child)) { |
| isViewFound = true; |
| break; |
| } |
| } |
| |
| if (!isViewFound) { |
| return; |
| } |
| |
| uiController.loopMainThreadForAtLeast(50); |
| } |
| while (System.currentTimeMillis() < endTime); |
| |
| throw new PerformException.Builder() |
| .withActionDescription(this.getDescription()) |
| .withViewDescription(HumanReadables.describe(view)) |
| .withCause(new TimeoutException()) |
| .build(); |
| } |
| } |