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 import com.android.documentsui.base.EventListener;
25 
26 import javax.annotation.Nullable;
27 
28 /**
29  * Test {@link EventHandler} that can be used to spy on,  control responses from,
30  * and make assertions against values tested.
31  */
32 public class TestEventListener<T> implements EventListener<T> {
33 
34     private @Nullable T lastValue;
35     private boolean called;
36 
37     @Override
accept(T event)38     public void accept(T event) {
39         called = true;
40         lastValue = event;
41     }
42 
assertLastArgument(@ullable T expected)43     public void assertLastArgument(@Nullable T expected) {
44         assertEquals(expected, lastValue);
45     }
46 
assertCalled()47     public void assertCalled() {
48         assertTrue(called);
49     }
50 
assertNotCalled()51     public void assertNotCalled() {
52         assertFalse(called);
53     }
54 
getLastValue()55     public T getLastValue() {
56         return lastValue;
57     }
58 }
59