|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines.experimental.android |
| 6 | + |
| 7 | +import android.os.Build |
| 8 | +import android.os.Looper |
| 9 | +import android.os.Message |
| 10 | +import android.os.MessageQueue |
| 11 | +import kotlinx.coroutines.experimental.Dispatchers |
| 12 | +import kotlinx.coroutines.experimental.GlobalScope |
| 13 | +import kotlinx.coroutines.experimental.launch |
| 14 | +import org.junit.Test |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.robolectric.RobolectricTestRunner |
| 17 | +import org.robolectric.Shadows.shadowOf |
| 18 | +import org.robolectric.annotation.Config |
| 19 | +import org.robolectric.shadows.ShadowLooper |
| 20 | +import org.robolectric.util.ReflectionHelpers |
| 21 | +import kotlin.test.assertFalse |
| 22 | +import kotlin.test.assertTrue |
| 23 | + |
| 24 | +@RunWith(RobolectricTestRunner::class) |
| 25 | +@Config(manifest = Config.NONE, sdk = [28]) |
| 26 | +class HandlerDispatcherTest { |
| 27 | + |
| 28 | + /** |
| 29 | + * Because [Dispatchers.Main] is a singleton, we cannot vary its initialization behavior. As a |
| 30 | + * result we only test its behavior on the newest API level and assert that it uses async |
| 31 | + * messages. We rely on the other tests to exercise the variance of the mechanism that the main |
| 32 | + * dispatcher uses to ensure it has correct behavior on all API levels. |
| 33 | + */ |
| 34 | + @Test fun mainIsAsync() { |
| 35 | + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) |
| 36 | + |
| 37 | + val mainLooper = ShadowLooper.getShadowMainLooper() |
| 38 | + mainLooper.pause() |
| 39 | + val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) |
| 40 | + |
| 41 | + val job = GlobalScope.launch(Dispatchers.Main) {} |
| 42 | + |
| 43 | + val message = mainMessageQueue.head |
| 44 | + assertTrue(message.isAsynchronous) |
| 45 | + |
| 46 | + job.cancel() |
| 47 | + } |
| 48 | + |
| 49 | + @Test fun asyncMessagesApi14() { |
| 50 | + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 14) |
| 51 | + |
| 52 | + val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() |
| 53 | + |
| 54 | + val mainLooper = ShadowLooper.getShadowMainLooper() |
| 55 | + mainLooper.pause() |
| 56 | + val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) |
| 57 | + |
| 58 | + val job = GlobalScope.launch(main) {} |
| 59 | + |
| 60 | + val message = mainMessageQueue.head |
| 61 | + assertFalse(message.isAsynchronous) |
| 62 | + |
| 63 | + job.cancel() |
| 64 | + } |
| 65 | + |
| 66 | + @Test fun asyncMessagesApi16() { |
| 67 | + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 16) |
| 68 | + |
| 69 | + val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() |
| 70 | + |
| 71 | + val mainLooper = ShadowLooper.getShadowMainLooper() |
| 72 | + mainLooper.pause() |
| 73 | + val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) |
| 74 | + |
| 75 | + val job = GlobalScope.launch(main) {} |
| 76 | + |
| 77 | + val message = mainMessageQueue.head |
| 78 | + assertTrue(message.isAsynchronous) |
| 79 | + |
| 80 | + job.cancel() |
| 81 | + } |
| 82 | + |
| 83 | + @Test fun asyncMessagesApi28() { |
| 84 | + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) |
| 85 | + |
| 86 | + val main = Looper.getMainLooper().asHandler(async = true).asCoroutineDispatcher() |
| 87 | + |
| 88 | + val mainLooper = ShadowLooper.getShadowMainLooper() |
| 89 | + mainLooper.pause() |
| 90 | + val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) |
| 91 | + |
| 92 | + val job = GlobalScope.launch(main) {} |
| 93 | + |
| 94 | + val message = mainMessageQueue.head |
| 95 | + assertTrue(message.isAsynchronous) |
| 96 | + |
| 97 | + job.cancel() |
| 98 | + } |
| 99 | + |
| 100 | + @Test fun noAsyncMessagesIfNotRequested() { |
| 101 | + ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28) |
| 102 | + |
| 103 | + val main = Looper.getMainLooper().asHandler(async = false).asCoroutineDispatcher() |
| 104 | + |
| 105 | + val mainLooper = ShadowLooper.getShadowMainLooper() |
| 106 | + mainLooper.pause() |
| 107 | + val mainMessageQueue = shadowOf(Looper.getMainLooper().queue) |
| 108 | + |
| 109 | + val job = GlobalScope.launch(main) {} |
| 110 | + |
| 111 | + val message = mainMessageQueue.head |
| 112 | + assertFalse(message.isAsynchronous) |
| 113 | + |
| 114 | + job.cancel() |
| 115 | + } |
| 116 | + |
| 117 | + // TODO compile against API 23+ so this can be invoked without reflection. |
| 118 | + private val Looper.queue: MessageQueue |
| 119 | + get() = Looper::class.java.getDeclaredMethod("getQueue").invoke(this) as MessageQueue |
| 120 | + |
| 121 | + // TODO compile against API 22+ so this can be invoked without reflection. |
| 122 | + private val Message.isAsynchronous: Boolean |
| 123 | + get() = Message::class.java.getDeclaredMethod("isAsynchronous").invoke(this) as Boolean |
| 124 | +} |
0 commit comments