001package react4j;
002
003import java.lang.reflect.Field;
004import javax.annotation.Nonnull;
005
006/**
007 * Utility class for interacting with React4j in tests.
008 */
009@GwtIncompatible
010public final class ReactTestUtil
011{
012  private ReactTestUtil()
013  {
014  }
015
016  /**
017   * Reset the state of React config to either production or development state.
018   *
019   * @param productionMode true to set it to production mode configuration, false to set it to development mode config.
020   */
021  public static void resetConfig( final boolean productionMode )
022  {
023    if ( ReactConfig.isProductionEnvironment() )
024    {
025      throw new IllegalStateException( "Unable to reset config as React is in production mode" );
026    }
027
028    if ( productionMode )
029    {
030      disableViewNames();
031      noCheckInvariants();
032      minimizeInputKeys();
033      noValidateInputValues();
034      noStoreDebugDataAsState();
035    }
036    else
037    {
038      enableViewNames();
039      checkInvariants();
040      noMinimizeInputKeys();
041      validateInputValues();
042      storeDebugDataAsState();
043    }
044    Contexts.setContextProvider( new Contexts.DefaultContextProvider() );
045    Contexts.clear();
046  }
047
048  /**
049   * Set the `react4j.enable_view_names` setting to {@code true}.
050   */
051  public static void enableViewNames()
052  {
053    setViewEnableNames( true );
054  }
055
056  /**
057   * Set the `react4j.enable_view_names` setting to {@code false}.
058   */
059  public static void disableViewNames()
060  {
061    setViewEnableNames( true );
062  }
063
064  /**
065   * Configure the `react4j.enable_view_names` setting.
066   *
067   * @param setting the setting.
068   */
069  public static void setViewEnableNames( final boolean setting )
070  {
071    setConstant( "ENABLE_VIEW_NAMES", setting );
072  }
073
074  /**
075   * Set the `react4j.minimize_input_keys` setting to {@code true}.
076   */
077  public static void minimizeInputKeys()
078  {
079    setMinimizeInputKeys( true );
080  }
081
082  /**
083   * Set the `react4j.minimize_input_keys` setting to {@code false}.
084   */
085  public static void noMinimizeInputKeys()
086  {
087    setMinimizeInputKeys( false );
088  }
089
090  /**
091   * Configure the `react4j.minimize_input_keys` setting.
092   *
093   * @param setting the setting.
094   */
095  private static void setMinimizeInputKeys( final boolean setting )
096  {
097    setConstant( "MINIMIZE_INPUT_KEYS", setting );
098  }
099
100  /**
101   * Set the `react4j.validate_input_values` setting to {@code true}.
102   */
103  public static void validateInputValues()
104  {
105    setValidateInputValues( true );
106  }
107
108  /**
109   * Set the `react4j.validate_input_values` setting to {@code false}.
110   */
111  public static void noValidateInputValues()
112  {
113    setValidateInputValues( false );
114  }
115
116  /**
117   * Configure the `react4j.validate_input_values` setting.
118   *
119   * @param setting the setting.
120   */
121  private static void setValidateInputValues( final boolean setting )
122  {
123    setConstant( "SHOULD_VALIDATE_INPUT_VALUES", setting );
124  }
125
126  /**
127   * Set the `react4j.store_debug_data_as_state` setting to {@code true}.
128   */
129  public static void storeDebugDataAsState()
130  {
131    setStoreDebugDataAsState( true );
132  }
133
134  /**
135   * Set the `react4j.store_debug_data_as_state` setting to {@code false}.
136   */
137  public static void noStoreDebugDataAsState()
138  {
139    setStoreDebugDataAsState( false );
140  }
141
142  /**
143   * Configure the `react4j.store_debug_data_as_state` setting.
144   *
145   * @param setting the setting.
146   */
147  private static void setStoreDebugDataAsState( final boolean setting )
148  {
149    setConstant( "SHOULD_STORE_DEBUG_DATA_AS_STATE", setting );
150  }
151
152  /**
153   * Set the `react4j.check_invariants` setting to {@code true}.
154   */
155  public static void checkInvariants()
156  {
157    setCheckInvariants( true );
158  }
159
160  /**
161   * Set the `react4j.check_invariants` setting to {@code false}.
162   */
163  public static void noCheckInvariants()
164  {
165    setCheckInvariants( false );
166  }
167
168  /**
169   * Configure the `react4j.check_invariants` setting.
170   *
171   * @param setting the setting.
172   */
173  private static void setCheckInvariants( final boolean setting )
174  {
175    setConstant( "CHECK_INVARIANTS", setting );
176  }
177
178  /**
179   * Set the specified field name on BrainCheckConfig.
180   */
181  @SuppressWarnings( "NonJREEmulationClassesInClientCode" )
182  private static void setConstant( @Nonnull final String fieldName, final boolean value )
183  {
184    try
185    {
186      final Field field = ReactConfig.class.getDeclaredField( fieldName );
187      field.setAccessible( true );
188      field.set( null, value );
189    }
190    catch ( NoSuchFieldException | IllegalAccessException e )
191    {
192      throw new IllegalStateException( "Unable to change constant " + fieldName, e );
193    }
194  }
195}