Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alex Clarke | 90add70 | 2022-03-25 15:18:16 +0000 | [diff] [blame] | 17 | package androidx.wear.watchface |
Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 18 | |
| 19 | import android.app.Service |
| 20 | import android.content.Context |
| 21 | import android.content.Intent |
| 22 | import android.graphics.Bitmap |
| 23 | import android.graphics.Canvas |
| 24 | import android.graphics.Rect |
| 25 | import android.graphics.SurfaceTexture |
| 26 | import android.os.Build |
| 27 | import android.os.Handler |
| 28 | import android.os.IBinder |
| 29 | import android.os.Looper |
| 30 | import android.view.Surface |
| 31 | import android.view.SurfaceHolder |
| 32 | import androidx.annotation.RequiresApi |
| 33 | import androidx.test.core.app.ApplicationProvider |
| 34 | import androidx.wear.watchface.client.WatchFaceControlClient |
| 35 | import androidx.wear.watchface.control.IWatchFaceInstanceServiceStub |
| 36 | import androidx.wear.watchface.control.WatchFaceControlService |
| 37 | import kotlinx.coroutines.CoroutineScope |
| 38 | import kotlinx.coroutines.Deferred |
| 39 | import kotlinx.coroutines.android.asCoroutineDispatcher |
| 40 | import kotlinx.coroutines.launch |
| 41 | import kotlinx.coroutines.runBlocking |
| 42 | import org.junit.Before |
| 43 | import org.mockito.Mockito |
| 44 | import java.util.concurrent.CountDownLatch |
| 45 | import java.util.concurrent.TimeUnit |
| 46 | import java.util.concurrent.TimeoutException |
Alex Clarke | 12ca62e | 2022-04-25 22:33:22 +0100 | [diff] [blame^] | 47 | import kotlinx.coroutines.MainScope |
Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 48 | |
| 49 | internal const val TIMEOUT_MILLIS = 1000L |
| 50 | |
| 51 | /** |
| 52 | * Test shim to allow us to connect to WatchFaceControlService from a test and to optionally |
| 53 | * override the reported API version. |
| 54 | */ |
Alex Clarke | 90add70 | 2022-03-25 15:18:16 +0000 | [diff] [blame] | 55 | @RequiresApi(Build.VERSION_CODES.O_MR1) |
Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 56 | public class WatchFaceControlTestService : Service() { |
| 57 | public companion object { |
| 58 | /** |
| 59 | * If non-null this overrides the API version reported by [IWatchFaceInstanceServiceStub]. |
| 60 | */ |
| 61 | public var apiVersionOverride: Int? = null |
| 62 | } |
| 63 | |
| 64 | private val realService = object : WatchFaceControlService() { |
| 65 | override fun createServiceStub(): IWatchFaceInstanceServiceStub = |
Alex Clarke | b1e0b14 | 2022-02-11 19:51:30 +0000 | [diff] [blame] | 66 | object : IWatchFaceInstanceServiceStub( |
| 67 | ApplicationProvider.getApplicationContext<Context>(), |
Alex Clarke | 12ca62e | 2022-04-25 22:33:22 +0100 | [diff] [blame^] | 68 | MainScope() |
Alex Clarke | b1e0b14 | 2022-02-11 19:51:30 +0000 | [diff] [blame] | 69 | ) { |
Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 70 | @RequiresApi(Build.VERSION_CODES.O_MR1) |
| 71 | override fun getApiVersion(): Int = apiVersionOverride ?: super.getApiVersion() |
| 72 | } |
| 73 | |
| 74 | init { |
| 75 | setContext(ApplicationProvider.getApplicationContext<Context>()) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @RequiresApi(Build.VERSION_CODES.O_MR1) |
| 80 | override fun onBind(intent: Intent?): IBinder? = realService.onBind(intent) |
| 81 | } |
| 82 | |
| 83 | /** Base class for the various async tests. */ |
Alex Clarke | 90add70 | 2022-03-25 15:18:16 +0000 | [diff] [blame] | 84 | @RequiresApi(Build.VERSION_CODES.O_MR1) |
Alex Clarke | c121e64 | 2021-09-07 22:17:51 +0100 | [diff] [blame] | 85 | public open class WatchFaceControlClientServiceTest { |
| 86 | val context = ApplicationProvider.getApplicationContext<Context>() |
| 87 | val handler = Handler(Looper.getMainLooper()) |
| 88 | val handlerCoroutineScope = CoroutineScope(handler.asCoroutineDispatcher().immediate) |
| 89 | |
| 90 | val surfaceHolder = Mockito.mock(SurfaceHolder::class.java) |
| 91 | val surface = Mockito.mock(Surface::class.java) |
| 92 | val renderLatch = CountDownLatch(1) |
| 93 | |
| 94 | val surfaceTexture = SurfaceTexture(false) |
| 95 | val glSurface = Surface(surfaceTexture) |
| 96 | val glSurfaceHolder = Mockito.mock(SurfaceHolder::class.java) |
| 97 | |
| 98 | val watchFaceControlClientService = runBlocking { |
| 99 | WatchFaceControlClient.createWatchFaceControlClientImpl( |
| 100 | context, |
| 101 | Intent(context, WatchFaceControlTestService::class.java).apply { |
| 102 | action = WatchFaceControlService.ACTION_WATCHFACE_CONTROL_SERVICE |
| 103 | } |
| 104 | ) |
| 105 | } |
| 106 | |
| 107 | @Before |
| 108 | fun setUp() { |
| 109 | Mockito.`when`(surfaceHolder.surfaceFrame).thenReturn(Rect(0, 0, 100, 100)) |
| 110 | Mockito.`when`(surfaceHolder.surface).thenReturn(surface) |
| 111 | Mockito.`when`(surface.isValid).thenReturn(false) |
| 112 | val bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888) |
| 113 | val canvas = Canvas(bitmap) |
| 114 | Mockito.`when`(surfaceHolder.lockHardwareCanvas()).thenReturn(canvas) |
| 115 | |
| 116 | Mockito.`when`(surfaceHolder.unlockCanvasAndPost(canvas)).then { |
| 117 | renderLatch.countDown() |
| 118 | } |
| 119 | |
| 120 | surfaceTexture.setDefaultBufferSize(10, 10) |
| 121 | Mockito.`when`(glSurfaceHolder.surface).thenReturn(glSurface) |
| 122 | Mockito.`when`(glSurfaceHolder.surfaceFrame) |
| 123 | .thenReturn(Rect(0, 0, 10, 10)) |
| 124 | } |
| 125 | |
| 126 | fun <X> awaitWithTimeout( |
| 127 | thing: Deferred<X>, |
| 128 | timeoutMillis: Long = TIMEOUT_MILLIS |
| 129 | ): X { |
| 130 | var value: X? = null |
| 131 | val latch = CountDownLatch(1) |
| 132 | handlerCoroutineScope.launch { |
| 133 | value = thing.await() |
| 134 | latch.countDown() |
| 135 | } |
| 136 | if (!latch.await(timeoutMillis, TimeUnit.MILLISECONDS)) { |
| 137 | throw TimeoutException("Timeout waiting for thing!") |
| 138 | } |
| 139 | return value!! |
| 140 | } |
| 141 | } |