diff options
Diffstat (limited to 'chromium/base/android/junit/src/org/chromium/base/supplier/OneshotSupplierImplTest.java')
-rw-r--r-- | chromium/base/android/junit/src/org/chromium/base/supplier/OneshotSupplierImplTest.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/chromium/base/android/junit/src/org/chromium/base/supplier/OneshotSupplierImplTest.java b/chromium/base/android/junit/src/org/chromium/base/supplier/OneshotSupplierImplTest.java new file mode 100644 index 00000000000..b00cb18fe87 --- /dev/null +++ b/chromium/base/android/junit/src/org/chromium/base/supplier/OneshotSupplierImplTest.java @@ -0,0 +1,79 @@ +// Copyright 2020 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base.supplier; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.verify; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.LooperMode; +import org.robolectric.shadows.ShadowProcess; + +import org.chromium.base.Callback; +import org.chromium.base.test.BaseRobolectricTestRunner; + +/** + * Unit tests for {@link OneshotSupplierImpl}. + */ +@RunWith(BaseRobolectricTestRunner.class) +@Config(shadows = {ShadowProcess.class}) +@LooperMode(LooperMode.Mode.LEGACY) +public class OneshotSupplierImplTest { + private OneshotSupplierImpl<String> mSupplier = new OneshotSupplierImpl<>(); + + @Spy + private Callback<String> mCallback1; + @Spy + private Callback<String> mCallback2; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testSet() { + assertNull(mSupplier.onAvailable(mCallback1)); + assertNull(mSupplier.onAvailable(mCallback2)); + mSupplier.set("answer"); + + verify(mCallback1).onResult("answer"); + verify(mCallback2).onResult("answer"); + } + + @Test + public void testSetBeforeAddObserver() { + mSupplier.set("answer"); + + assertEquals("answer", mSupplier.onAvailable(mCallback1)); + assertEquals("answer", mSupplier.onAvailable(mCallback2)); + + verify(mCallback1).onResult("answer"); + verify(mCallback2).onResult("answer"); + } + + @Test + public void testInterleaved() { + assertNull(mSupplier.onAvailable(mCallback1)); + mSupplier.set("answer"); + assertEquals("answer", mSupplier.onAvailable(mCallback2)); + + verify(mCallback1).onResult("answer"); + verify(mCallback2).onResult("answer"); + } + + @Test + public void testGet() { + assertNull(mSupplier.get()); + mSupplier.set("answer"); + assertEquals("answer", mSupplier.get()); + } +} |