1 // Copyright 2020 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.supplier; 6 7 import static org.mockito.ArgumentMatchers.any; 8 import static org.mockito.Mockito.never; 9 import static org.mockito.Mockito.times; 10 import static org.mockito.Mockito.verify; 11 import static org.mockito.Mockito.verifyNoMoreInteractions; 12 13 import android.os.Handler; 14 15 import org.junit.Rule; 16 import org.junit.Test; 17 import org.junit.runner.RunWith; 18 import org.mockito.Mock; 19 import org.mockito.junit.MockitoJUnit; 20 import org.mockito.junit.MockitoRule; 21 import org.robolectric.annotation.Config; 22 23 import org.chromium.base.Callback; 24 import org.chromium.base.test.BaseRobolectricTestRunner; 25 26 /** Unit tests for {@link OneShotCallback}. */ 27 @RunWith(BaseRobolectricTestRunner.class) 28 @Config(manifest = Config.NONE) 29 public class OneShotCallbackTest { 30 @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule(); 31 32 @Mock Callback<Integer> mCallbackMock; 33 34 @Test testNotCalledWithNoValue()35 public void testNotCalledWithNoValue() { 36 Handler handler = new Handler(); 37 ObservableSupplierImpl<Integer> supplier = new ObservableSupplierImpl<>(); 38 39 handler.post(() -> new OneShotCallback<>(supplier, mCallbackMock)); 40 41 handler.post( 42 () -> { 43 verify(mCallbackMock, never()).onResult(any()); 44 }); 45 } 46 47 @Test testCalledWithPresetValue()48 public void testCalledWithPresetValue() { 49 Handler handler = new Handler(); 50 ObservableSupplierImpl<Integer> supplier = new ObservableSupplierImpl<>(); 51 supplier.set(5); 52 53 handler.post( 54 () -> { 55 new OneShotCallback<>(supplier, mCallbackMock); 56 }); 57 58 handler.post( 59 () -> { 60 verify(mCallbackMock, times(1)).onResult(5); 61 }); 62 } 63 64 @Test testCalledWithSet()65 public void testCalledWithSet() { 66 Handler handler = new Handler(); 67 ObservableSupplierImpl<Integer> supplier = new ObservableSupplierImpl<>(); 68 69 handler.post(() -> new OneShotCallback<>(supplier, mCallbackMock)); 70 handler.post( 71 () -> { 72 verify(mCallbackMock, never()).onResult(any()); 73 }); 74 75 supplier.set(5); 76 handler.post( 77 () -> { 78 verify(mCallbackMock, times(1)).onResult(5); 79 }); 80 } 81 82 @Test testNotCalledWithPresetValueOnlyOnce()83 public void testNotCalledWithPresetValueOnlyOnce() { 84 Handler handler = new Handler(); 85 ObservableSupplierImpl<Integer> supplier = new ObservableSupplierImpl<>(); 86 supplier.set(5); 87 supplier.set(10); 88 89 handler.post( 90 () -> { 91 new OneShotCallback<>(supplier, mCallbackMock); 92 }); 93 94 handler.post( 95 () -> { 96 verify(mCallbackMock, times(1)).onResult(10); 97 }); 98 } 99 100 @Test testCalledWithSetOnlyOnce()101 public void testCalledWithSetOnlyOnce() { 102 Handler handler = new Handler(); 103 ObservableSupplierImpl<Integer> supplier = new ObservableSupplierImpl<>(); 104 105 handler.post(() -> new OneShotCallback<>(supplier, mCallbackMock)); 106 handler.post( 107 () -> { 108 verify(mCallbackMock, never()).onResult(any()); 109 }); 110 111 supplier.set(5); 112 handler.post( 113 () -> { 114 verify(mCallbackMock, times(1)).onResult(5); 115 }); 116 117 supplier.set(10); 118 verifyNoMoreInteractions(mCallbackMock); 119 } 120 } 121