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 import androidx.annotation.Nullable; 8 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.List; 12 13 /** A transition into and/or out of {@link ConditionalState}s. */ 14 public class Transition { 15 /** 16 * A trigger that will be executed to start the transition after all Conditions are in place and 17 * states are set to TRANSITIONING_*. 18 */ 19 public interface Trigger { 20 /** 21 * Code to trigger the transition, e.g. click a View. 22 * 23 * @param transition the Transition that will be triggered; Conditions can be added to it. 24 */ triggerTransition(Transition transition)25 void triggerTransition(Transition transition); 26 } 27 28 @Nullable private final Trigger mTrigger; 29 30 @Nullable private List<Condition> mConditions; 31 Transition(@ullable Trigger trigger)32 Transition(@Nullable Trigger trigger) { 33 mTrigger = trigger; 34 } 35 36 /** 37 * Add a |condition| to the Transition that is not in the exit or enter conditions of the states 38 * involved. The condition will be waited in parallel with the exit and enter conditions of the 39 * states. 40 */ addCondition(Condition condition)41 public void addCondition(Condition condition) { 42 if (mConditions == null) { 43 mConditions = new ArrayList<>(); 44 } 45 mConditions.add(condition); 46 } 47 triggerTransition()48 protected void triggerTransition() { 49 if (mTrigger != null) { 50 mTrigger.triggerTransition(this); 51 } 52 } 53 getTransitionConditions()54 protected List<Condition> getTransitionConditions() { 55 if (mConditions == null) { 56 return Collections.EMPTY_LIST; 57 } else { 58 return mConditions; 59 } 60 } 61 } 62