1 /* 2 * Copyright (C) 2024 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 17 package com.google.android.wallpaper.weathereffects.graphics 18 19 import android.graphics.Bitmap 20 import android.graphics.Color 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import com.google.android.wallpaper.weathereffects.graphics.FrameBuffer.Companion.RESULT_FENCE_TIME_OUT 23 import com.google.common.truth.Truth.assertThat 24 import com.google.common.util.concurrent.MoreExecutors 25 import java.util.concurrent.CountDownLatch 26 import java.util.concurrent.TimeUnit 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @RunWith(AndroidJUnit4::class) 31 class FrameBufferTest { 32 33 private val executor = MoreExecutors.directExecutor() 34 35 @Test onImageReady_invokesCallbacknull36 fun onImageReady_invokesCallback() { 37 val expectedWidth = 1 38 val expectedHeight = 1 39 val expectedColor = Color.RED 40 41 val buffer = FrameBuffer(expectedWidth, expectedHeight) 42 buffer.beginDrawing().drawColor(expectedColor) 43 buffer.endDrawing() 44 45 val latch = CountDownLatch(1) 46 var bitmap: Bitmap? = null 47 48 buffer.tryObtainingImage( 49 { 50 bitmap = it 51 latch.countDown() 52 }, 53 executor 54 ) 55 56 assertThat(latch.await(RESULT_FENCE_TIME_OUT, TimeUnit.MILLISECONDS)).isTrue() 57 58 assertThat(bitmap).isNotNull() 59 val resultBitmap = bitmap!! 60 assertThat(resultBitmap.width).isEqualTo(expectedWidth) 61 assertThat(resultBitmap.height).isEqualTo(expectedHeight) 62 assertThat(resultBitmap.colorSpace).isEqualTo(buffer.colorSpace) 63 64 // Color sampling only works on software bitmap. 65 val softwareBitmap = resultBitmap.copy(Bitmap.Config.ARGB_8888, false) 66 assertThat(softwareBitmap.getPixel(0, 0)).isEqualTo(expectedColor) 67 } 68 69 @Test close_onImageReady_doesNotInvokeCallbacknull70 fun close_onImageReady_doesNotInvokeCallback() { 71 val buffer = FrameBuffer(width = 1, height = 1) 72 buffer.beginDrawing().drawColor(Color.RED) 73 buffer.endDrawing() 74 75 // Call close before we obtain image. 76 buffer.close() 77 78 val latch = CountDownLatch(1) 79 var bitmap: Bitmap? = null 80 81 buffer.tryObtainingImage( 82 { 83 bitmap = it 84 latch.countDown() 85 }, 86 executor 87 ) 88 89 assertThat(latch.await(RESULT_FENCE_TIME_OUT, TimeUnit.MILLISECONDS)).isFalse() 90 assertThat(bitmap).isNull() 91 } 92 } 93