1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.compatibility.common.util; 18 19 import androidx.annotation.NonNull; 20 import androidx.annotation.Nullable; 21 22 import org.junit.rules.TestRule; 23 import org.junit.runner.Description; 24 import org.junit.runners.model.Statement; 25 26 import java.util.Objects; 27 28 /** 29 * JUnit rule used to prepare a state before the test is run. 30 * 31 * <p>It stores the current state before the test, changes it (if necessary), then restores it after 32 * the test (if necessary). 33 * 34 * @param <T> value type 35 */ 36 public class StateChangerRule<T> implements TestRule { 37 38 private final StateManager<T> mStateManager; 39 private final T mValue; 40 41 /** 42 * Default constructor. 43 * 44 * @param stateManager abstraction used to mange the state. 45 * @param value value to be set before the test is run. 46 */ StateChangerRule(@onNull StateManager<T> stateManager, @Nullable T value)47 public StateChangerRule(@NonNull StateManager<T> stateManager, @Nullable T value) { 48 mStateManager = Objects.requireNonNull(stateManager); 49 mValue = value; 50 } 51 52 @Override apply(Statement base, Description description)53 public Statement apply(Statement base, Description description) { 54 return new Statement() { 55 56 @Override 57 public void evaluate() throws Throwable { 58 final T previousValue = mStateManager.get(); 59 if (!Objects.equals(previousValue, mValue)) { 60 mStateManager.set(mValue); 61 } 62 try { 63 base.evaluate(); 64 } finally { 65 final T currentValue = mStateManager.get(); 66 if (!Objects.equals(currentValue, previousValue)) { 67 mStateManager.set(previousValue); 68 // TODO: if set() throws a RuntimeException, JUnit will silently catch it 69 // and re-run the test case; it should fail instead. 70 } 71 } 72 } 73 }; 74 } 75 } 76