summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.kt
blob: 41602d94935f6ed4dade23ac0bb3dac187da3029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
package org.mozilla.geckoview.test

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.* // ktlint-disable no-wildcard-imports
import org.junit.Test
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.test.util.Environment

val env = Environment()

fun <T> GeckoResult<T>.pollDefault(): T? =
    this.poll(env.defaultTimeoutMillis)

class GeckoResultTestKotlin {
    class MockException : RuntimeException()

    @Test fun pollIncompleteWithValue() {
        val result = GeckoResult<Int>()
        val thread = Thread { result.complete(42) }

        thread.start()
        assertThat("Value should match", result.pollDefault(), equalTo(42))
    }

    @Test(expected = MockException::class)
    fun pollIncompleteWithError() {
        val result = GeckoResult<Void>()

        val thread = Thread { result.completeExceptionally(MockException()) }
        thread.start()

        result.pollDefault()
    }
}