Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2016 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.documentsui.testing;
     18 
     19 import static junit.framework.Assert.assertFalse;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import com.android.documentsui.base.EventHandler;
     24 
     25 import javax.annotation.Nullable;
     26 
     27 /**
     28  * Test {@link EventHandler} that can be used to spy on,  control responses from,
     29  * and make assertions against values tested.
     30  */
     31 public class TestEventHandler<T> implements EventHandler<T> {
     32 
     33     private @Nullable T lastValue;
     34     private boolean nextReturnValue;
     35     private boolean called;
     36 
     37     @Override
     38     public boolean accept(T event) {
     39         called = true;
     40         lastValue = event;
     41         return nextReturnValue;
     42     }
     43 
     44     public void assertLastArgument(@Nullable T expected) {
     45         assertEquals(expected, lastValue);
     46     }
     47 
     48     public void assertCalled() {
     49         assertTrue(called);
     50     }
     51 
     52     public void assertNotCalled() {
     53         assertFalse(called);
     54     }
     55 
     56     public void nextReturn(boolean value) {
     57         nextReturnValue = value;
     58     }
     59 
     60     public @Nullable T getLastValue() {
     61         return lastValue;
     62     }
     63 
     64     public void reset() {
     65         called = false;
     66         lastValue = null;
     67         nextReturnValue = false;
     68     }
     69 }
     70