• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
10  * StationFacility is a {@link ConditionalState} scoped to a single {@link TransitStation} instance.
11  *
12  * <p>This should be used for example for popup dialogs, menus, temporary messages. A transit-layer
13  * class should be derived from it and instantiated. It should expose facility-specific methods for
14  * the test-layer to use.
15  *
16  * <p>As a {@link ConditionalState}, it has a defined lifecycle and must declare {@link Elements}
17  * that determine its enter and exit {@link Condition}s.
18  *
19  * <p>Leaving the TransitStation causes this state to be left as well, and exit Conditions will be
20  * waited upon for the TransitStation transition to be complete.
21  *
22  * <p>Transitions into and out of a StationFacility while the TransitStation is ACTIVE should be
23  * done with {@link #enterSync(StationFacility, Trigger)} and {@link #exitSync(StationFacility,
24  * Trigger)}.
25  *
26  * @param <T> the type of TransitStation this is scoped to.
27  */
28 public abstract class StationFacility<T extends TransitStation> extends ConditionalState {
29     protected final T mStation;
30     private final int mId;
31     private static int sLastFacilityId = 1000;
32 
33     /**
34      * Constructor.
35      *
36      * <p>Instantiate a subclass, then call {@link #enterSync(StationFacility, Trigger)} to enter
37      * it.
38      *
39      * @param station the TransitStation this StationFacility is scoped to.
40      */
StationFacility(T station)41     protected StationFacility(T station) {
42         mId = ++sLastFacilityId;
43         mStation = station;
44         mStation.registerFacility(this);
45     }
46 
47     @Override
toString()48     public String toString() {
49         return String.format("<S%d|F%d: %s>", mStation.getId(), mId, getClass().getSimpleName());
50     }
51 
52     /**
53      * Starts a transition into the StationFacility, runs the transition |trigger| and blocks until
54      * the facility is considered ACTIVE (enter Conditions are fulfilled).
55      *
56      * @param facility the StationFacility to enter.
57      * @param trigger the trigger to start the transition (e.g. clicking a view).
58      * @return the StationFacility entered.
59      * @param <F> the type of StationFacility entered.
60      */
enterSync(F facility, Trigger trigger)61     public static <F extends StationFacility> F enterSync(F facility, Trigger trigger) {
62         FacilityCheckIn checkIn = new FacilityCheckIn(facility, trigger);
63         checkIn.enterSync();
64         return facility;
65     }
66 
67     /**
68      * Starts a transition out of the StationFacility, runs the transition |trigger| and blocks
69      * until the facility is considered FINISHED (exit Conditions are fulfilled).
70      *
71      * @param facility the StationFacility to exit.
72      * @param trigger the trigger to start the transition (e.g. clicking a view).
73      * @return the StationFacility exited.
74      * @param <F> the type of StationFacility exited.
75      */
exitSync(F facility, Trigger trigger)76     public static <F extends StationFacility> F exitSync(F facility, Trigger trigger) {
77         FacilityCheckOut checkOut = new FacilityCheckOut(facility, trigger);
78         checkOut.exitSync();
79         return facility;
80     }
81 }
82