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 org.chromium.base.test.transit.Transition.Trigger; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 /** 13 * A major {@link ConditionalState}, a "screen" the app can be in. Only one can be active at a time. 14 * 15 * <p>A transit-layer class should be derived from it and instantiated. 16 * 17 * <p>As a {@link ConditionalState}, it has a defined lifecycle and must declare {@link Elements} 18 * that determine its enter and exit {@link Condition}s. 19 * 20 * <p>Transitions should be done with {@link Trip#travelSync(TransitStation, TransitStation, 21 * Trigger)}. The transit-layer derived class should expose screen-specific methods for the 22 * test-layer to use. 23 */ 24 public abstract class TransitStation extends ConditionalState { 25 private static final String TAG = "Transit"; 26 private final int mId; 27 private static int sLastStationId; 28 private List<StationFacility> mFacilities = new ArrayList<>(); 29 TransitStation()30 protected TransitStation() { 31 mId = ++sLastStationId; 32 TrafficControl.notifyCreatedStation(this); 33 } 34 getActiveFacilityExitConditions()35 List<Condition> getActiveFacilityExitConditions() { 36 List<Condition> conditions = new ArrayList<>(); 37 for (StationFacility facility : mFacilities) { 38 if (facility.getPhase() == Phase.ACTIVE) { 39 conditions.addAll(facility.getExitConditions()); 40 } 41 } 42 return conditions; 43 } 44 registerFacility(StationFacility facility)45 void registerFacility(StationFacility facility) { 46 mFacilities.add(facility); 47 } 48 49 @Override toString()50 public String toString() { 51 return String.format("<S%d: %s>", mId, getClass().getSimpleName()); 52 } 53 54 /** 55 * @return the self-incrementing id for logging purposes. 56 */ getId()57 public int getId() { 58 return mId; 59 } 60 } 61