1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base.test.transit; 6 7 /** 8 * A condition that needs to be fulfilled for a state transition to be considered done. 9 * 10 * <p>{@link ConditionWaiter} waits for multiple Conditions to be fulfilled. {@link 11 * ConditionChecker} performs one-time checks for whether multiple Conditions are fulfilled. 12 */ 13 public abstract class Condition { 14 private String mDescription; 15 16 private boolean mIsRunOnUiThread; 17 18 /** 19 * @param isRunOnUiThread true if the Condition should be checked on the UI Thread, false if it 20 * should be checked on the Instrumentation Thread. 21 */ Condition(boolean isRunOnUiThread)22 public Condition(boolean isRunOnUiThread) { 23 mIsRunOnUiThread = isRunOnUiThread; 24 } 25 26 /** 27 * Called on the instrumentation thread, depending on #shouldRunOnUiThread(). 28 * 29 * @return whether the condition has been fulfilled. 30 */ check()31 public abstract boolean check(); 32 33 /** 34 * @return a short description to be printed as part of a list of conditions. Use {@link 35 * #getDescription()} to get a description as it caches the description until {@link 36 * #rebuildDescription()} invalidates it. 37 */ buildDescription()38 public abstract String buildDescription(); 39 40 /** 41 * Hook run right before the condition starts being monitored. Used, for example, to get initial 42 * callback counts. 43 */ onStartMonitoring()44 public void onStartMonitoring() {} 45 46 /** 47 * @return a short description to be printed as part of a list of conditions. 48 */ getDescription()49 public String getDescription() { 50 if (mDescription == null) { 51 rebuildDescription(); 52 } 53 return mDescription; 54 } 55 56 /** 57 * Invalidates last description; the next time {@link #getDescription()}, it will get a new one 58 * from {@link #buildDescription()}. 59 */ rebuildDescription()60 protected void rebuildDescription() { 61 mDescription = buildDescription(); 62 } 63 64 /** 65 * @return true if the check is intended to be run on the UI Thread, false if it should be run 66 * on the instrumentation thread. 67 */ isRunOnUiThread()68 public boolean isRunOnUiThread() { 69 return mIsRunOnUiThread; 70 } 71 } 72