• 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 org.junit.Assert;
8 import org.junit.Test;
9 import org.junit.runner.RunWith;
10 import org.robolectric.shadows.ShadowLooper;
11 
12 import org.chromium.base.test.BaseRobolectricTestRunner;
13 import org.chromium.base.test.util.CallbackHelper;
14 
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.TimeoutException;
18 
19 /** Unit tests for {@link SupplierUtils}. */
20 @RunWith(BaseRobolectricTestRunner.class)
21 public class SupplierUtilsTest {
22 
23     @Test
testWaitForAll_NoSuppliers()24     public void testWaitForAll_NoSuppliers() throws TimeoutException {
25         CallbackHelper callbackHelper = new CallbackHelper();
26         SupplierUtils.waitForAll(callbackHelper::notifyCalled);
27         callbackHelper.waitForFirst();
28     }
29 
30     @Test
testWaitForAll_AllSuppliersAlreadyHaveValues()31     public void testWaitForAll_AllSuppliersAlreadyHaveValues() throws TimeoutException {
32         Supplier<Integer> baseSupplier = () -> 4;
33         OneshotSupplierImpl<String> oneshotSupplier = new OneshotSupplierImpl<>();
34         oneshotSupplier.set("foo");
35         ObservableSupplierImpl<Object> observableSupplier = new ObservableSupplierImpl<>();
36         observableSupplier.set(new Object());
37         SyncOneshotSupplierImpl<List<?>> syncOneshotSupplier = new SyncOneshotSupplierImpl<>();
38         syncOneshotSupplier.set(new ArrayList<>());
39 
40         CallbackHelper callbackHelper = new CallbackHelper();
41         SupplierUtils.waitForAll(
42                 callbackHelper::notifyCalled,
43                 baseSupplier,
44                 oneshotSupplier,
45                 observableSupplier,
46                 syncOneshotSupplier);
47         callbackHelper.waitForFirst();
48     }
49 
50     @Test
testWaitForAll_SomeSuppliersAlreadyHaveValues()51     public void testWaitForAll_SomeSuppliersAlreadyHaveValues() throws TimeoutException {
52         Supplier<Integer> baseSupplier = () -> 4;
53         OneshotSupplierImpl<String> oneshotSupplier = new OneshotSupplierImpl<>();
54 
55         ObservableSupplierImpl<Object> observableSupplier = new ObservableSupplierImpl<>();
56         observableSupplier.set(new Object());
57 
58         SyncOneshotSupplierImpl<List<?>> syncOneshotSupplier = new SyncOneshotSupplierImpl<>();
59 
60         CallbackHelper callbackHelper = new CallbackHelper();
61         SupplierUtils.waitForAll(
62                 callbackHelper::notifyCalled,
63                 baseSupplier,
64                 oneshotSupplier,
65                 observableSupplier,
66                 syncOneshotSupplier);
67 
68         Assert.assertEquals(0, callbackHelper.getCallCount());
69         oneshotSupplier.set("foo");
70         Assert.assertEquals(0, callbackHelper.getCallCount());
71         syncOneshotSupplier.set(new ArrayList<>());
72         ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
73         callbackHelper.waitForFirst();
74     }
75 
76     @Test
testWaitForAll_NoSuppliersAlreadyHaveValues()77     public void testWaitForAll_NoSuppliersAlreadyHaveValues() throws TimeoutException {
78         OneshotSupplierImpl<String> oneshotSupplier = new OneshotSupplierImpl<>();
79         ObservableSupplierImpl<Object> observableSupplier = new ObservableSupplierImpl<>();
80         SyncOneshotSupplierImpl<List<?>> syncOneshotSupplier = new SyncOneshotSupplierImpl<>();
81 
82         CallbackHelper callbackHelper = new CallbackHelper();
83         SupplierUtils.waitForAll(
84                 callbackHelper::notifyCalled,
85                 oneshotSupplier,
86                 observableSupplier,
87                 syncOneshotSupplier);
88 
89         Assert.assertEquals(0, callbackHelper.getCallCount());
90         observableSupplier.set(new Object());
91         Assert.assertEquals(0, callbackHelper.getCallCount());
92         oneshotSupplier.set("foo");
93         Assert.assertEquals(0, callbackHelper.getCallCount());
94         syncOneshotSupplier.set(new ArrayList<>());
95         ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
96         callbackHelper.waitForFirst();
97     }
98 
99     @Test
testWaitForAll_WaitForOneshotSupplier()100     public void testWaitForAll_WaitForOneshotSupplier() throws TimeoutException {
101         OneshotSupplierImpl<Object> supplier = new OneshotSupplierImpl<>();
102 
103         CallbackHelper callbackHelper = new CallbackHelper();
104         SupplierUtils.waitForAll(callbackHelper::notifyCalled, supplier);
105 
106         Assert.assertEquals(0, callbackHelper.getCallCount());
107         supplier.set(new Object());
108         ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
109         callbackHelper.waitForFirst();
110     }
111 
112     @Test
testWaitForAll_WaitForObservableSupplier()113     public void testWaitForAll_WaitForObservableSupplier() throws TimeoutException {
114         ObservableSupplierImpl<Object> supplier = new ObservableSupplierImpl<>();
115 
116         CallbackHelper callbackHelper = new CallbackHelper();
117         SupplierUtils.waitForAll(callbackHelper::notifyCalled, supplier);
118 
119         Assert.assertEquals(0, callbackHelper.getCallCount());
120         supplier.set(new Object());
121         callbackHelper.waitForFirst();
122     }
123 
124     @Test
testWaitForAll_WaitForSyncOneshotSupplier()125     public void testWaitForAll_WaitForSyncOneshotSupplier() throws TimeoutException {
126         SyncOneshotSupplierImpl<Object> supplier = new SyncOneshotSupplierImpl<>();
127 
128         CallbackHelper callbackHelper = new CallbackHelper();
129         SupplierUtils.waitForAll(callbackHelper::notifyCalled, supplier);
130 
131         Assert.assertEquals(0, callbackHelper.getCallCount());
132         supplier.set(new Object());
133         callbackHelper.waitForFirst();
134     }
135 }
136