• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNull;
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.verify;
13 
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Spy;
18 import org.mockito.junit.MockitoJUnit;
19 import org.mockito.junit.MockitoRule;
20 import org.robolectric.shadows.ShadowLooper;
21 
22 import org.chromium.base.Callback;
23 import org.chromium.base.test.BaseRobolectricTestRunner;
24 
25 /** Unit tests for {@link LazyOneshotSupplier}. */
26 @RunWith(BaseRobolectricTestRunner.class)
27 public class LazyOneshotSupplierTest {
28     @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule();
29 
30     // TODO(https://crbug.com/1498561): Switch to @Mock once the default #bind works on mocks.
31     @Spy private Callback<Object> mOnAvailable;
32 
33     @Test
testFromValueObject()34     public void testFromValueObject() {
35         Object foo = new Object();
36         LazyOneshotSupplier lazyOneshotSupplier = LazyOneshotSupplier.fromValue(foo);
37         lazyOneshotSupplier.onAvailable(mOnAvailable);
38         assertFalse(lazyOneshotSupplier.hasValue());
39 
40         assertEquals(foo, lazyOneshotSupplier.get());
41         assertTrue(lazyOneshotSupplier.hasValue());
42 
43         ShadowLooper.idleMainLooper();
44         verify(mOnAvailable).onResult(eq(foo));
45     }
46 
47     @Test
testFromValueNull()48     public void testFromValueNull() {
49         LazyOneshotSupplier lazyOneshotSupplier = LazyOneshotSupplier.fromValue(null);
50         lazyOneshotSupplier.onAvailable(mOnAvailable);
51         assertFalse(lazyOneshotSupplier.hasValue());
52 
53         assertNull(lazyOneshotSupplier.get());
54         assertTrue(lazyOneshotSupplier.hasValue());
55 
56         ShadowLooper.idleMainLooper();
57         verify(mOnAvailable).onResult(eq(null));
58     }
59 
60     @Test
testFromSupplierObject()61     public void testFromSupplierObject() {
62         Object foo = new Object();
63         LazyOneshotSupplier lazyOneshotSupplier = LazyOneshotSupplier.fromSupplier(() -> foo);
64         lazyOneshotSupplier.onAvailable(mOnAvailable);
65         assertFalse(lazyOneshotSupplier.hasValue());
66 
67         assertEquals(foo, lazyOneshotSupplier.get());
68         assertTrue(lazyOneshotSupplier.hasValue());
69 
70         ShadowLooper.idleMainLooper();
71         verify(mOnAvailable).onResult(eq(foo));
72     }
73 
74     @Test
testFromSupplierNull()75     public void testFromSupplierNull() {
76         LazyOneshotSupplier lazyOneshotSupplier = LazyOneshotSupplier.fromSupplier(() -> null);
77         lazyOneshotSupplier.onAvailable(mOnAvailable);
78         assertFalse(lazyOneshotSupplier.hasValue());
79 
80         assertNull(lazyOneshotSupplier.get());
81         assertTrue(lazyOneshotSupplier.hasValue());
82 
83         ShadowLooper.idleMainLooper();
84         verify(mOnAvailable).onResult(eq(null));
85     }
86 }
87