• 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.ConditionalState.Phase;
8 
9 import java.util.List;
10 
11 /** Assertions specific to Public Transit. */
12 public class TransitAsserts {
13     private static final String TAG = "Transit";
14 
15     /**
16      * Asserts that the given station is the final one in a test method and no further transitions
17      * happened.
18      *
19      * <p>A different instance of the same subclass {@link TransitStation} does not count; it must
20      * be the TransitStation instance returned by the last {@link Trip} transition.
21      *
22      * @param expected the TransitStation expected to be the last
23      * @throws AssertionError if the final station is not the same as |expected|
24      */
assertFinalDestination(TransitStation expected)25     public static void assertFinalDestination(TransitStation expected) {
26         TransitStation activeStation = TrafficControl.getActiveStation();
27         if (activeStation != expected) {
28             raiseAssertion(
29                     String.format(
30                             "Expected final destination to be %s, but was %s",
31                             expected, activeStation));
32         }
33         if (expected.getPhase() != Phase.ACTIVE) {
34             raiseAssertion(
35                     String.format(
36                             "Station %s expected to be the final one, but it is not ACTIVE",
37                             expected));
38         }
39     }
40 
raiseAssertion(String message)41     private static void raiseAssertion(String message) {
42         List<TransitStation> allStations = TrafficControl.getAllStations();
43         assert false : message + "\n" + stationListToString(allStations);
44     }
45 
stationListToString(List<TransitStation> allStations)46     private static String stationListToString(List<TransitStation> allStations) {
47         StringBuilder builder = new StringBuilder();
48         int i = 1;
49         for (TransitStation station : allStations) {
50             builder.append(
51                     String.format(
52                             "  [%d] (%s) %s\n",
53                             i, ConditionalState.phaseToShortString(station.getPhase()), station));
54             i++;
55         }
56         return builder.toString();
57     }
58 }
59