1 /*
<lambda>null2  * Copyright 2023 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 androidx.camera.video.internal
18 
19 import android.media.CamcorderProfile.QUALITY_1080P
20 import android.media.CamcorderProfile.QUALITY_2160P
21 import android.media.CamcorderProfile.QUALITY_480P
22 import android.media.CamcorderProfile.QUALITY_720P
23 import android.media.EncoderProfiles.VideoProfile.HDR_HLG
24 import android.os.Build
25 import androidx.camera.core.impl.EncoderProfilesProvider
26 import androidx.camera.core.impl.EncoderProfilesProxy
27 import androidx.camera.core.impl.EncoderProfilesProxy.VideoProfileProxy.BIT_DEPTH_10
28 import androidx.camera.testing.impl.EncoderProfilesUtil.PROFILES_1080P
29 import androidx.camera.testing.impl.EncoderProfilesUtil.PROFILES_2160P
30 import androidx.camera.testing.impl.EncoderProfilesUtil.PROFILES_480P
31 import androidx.camera.testing.impl.EncoderProfilesUtil.PROFILES_720P
32 import androidx.camera.testing.impl.fakes.FakeEncoderProfilesProvider
33 import androidx.camera.testing.impl.fakes.FakeVideoEncoderInfo
34 import androidx.camera.video.internal.encoder.VideoEncoderInfo
35 import com.google.common.truth.Truth.assertThat
36 import org.junit.Test
37 import org.junit.runner.RunWith
38 import org.robolectric.RobolectricTestRunner
39 import org.robolectric.annotation.Config
40 import org.robolectric.annotation.internal.DoNotInstrument
41 
42 @RunWith(RobolectricTestRunner::class)
43 @DoNotInstrument
44 @Config(minSdk = Build.VERSION_CODES.LOLLIPOP)
45 class BackupHdrProfileEncoderProfilesProviderTest {
46 
47     private val defaultProvider =
48         createFakeEncoderProfilesProvider(
49             mapOf(
50                 QUALITY_2160P to PROFILES_2160P,
51                 QUALITY_1080P to PROFILES_1080P,
52                 QUALITY_720P to PROFILES_720P,
53                 QUALITY_480P to PROFILES_480P
54             )
55         )
56     private val videoEncoderFinder = VideoEncoderInfo.Finder { FakeVideoEncoderInfo() }
57 
58     @Test
59     fun hasNoProfile_canNotGetProfiles() {
60         val emptyProvider = createFakeEncoderProfilesProvider()
61         val provider = BackupHdrProfileEncoderProfilesProvider(emptyProvider, videoEncoderFinder)
62 
63         assertThat(provider.hasProfile(QUALITY_2160P)).isFalse()
64         assertThat(provider.hasProfile(QUALITY_1080P)).isFalse()
65         assertThat(provider.hasProfile(QUALITY_720P)).isFalse()
66         assertThat(provider.hasProfile(QUALITY_480P)).isFalse()
67         assertThat(provider.getAll(QUALITY_2160P)).isNull()
68         assertThat(provider.getAll(QUALITY_1080P)).isNull()
69         assertThat(provider.getAll(QUALITY_720P)).isNull()
70         assertThat(provider.getAll(QUALITY_480P)).isNull()
71     }
72 
73     @Test
74     fun hasProfile_backupHdrProfileAppended() {
75         val qualities = listOf(QUALITY_2160P, QUALITY_1080P, QUALITY_720P, QUALITY_480P)
76         for (quality in qualities) {
77             val baseVideoProfilesSize = defaultProvider.getAll(quality)!!.videoProfiles.size
78             val provider =
79                 BackupHdrProfileEncoderProfilesProvider(defaultProvider, videoEncoderFinder)
80 
81             assertThat(provider.hasProfile(quality)).isTrue()
82             val videoProfiles = provider.getAll(quality)!!.videoProfiles
83             assertThat(videoProfiles.size).isEqualTo(baseVideoProfilesSize + 1)
84             assertThat(videoProfiles.last().hdrFormat).isEqualTo(HDR_HLG)
85             assertThat(videoProfiles.last().bitDepth).isEqualTo(BIT_DEPTH_10)
86         }
87     }
88 
89     private fun createFakeEncoderProfilesProvider(
90         qualityToProfilesMap: Map<Int, EncoderProfilesProxy> = emptyMap()
91     ): EncoderProfilesProvider {
92         return FakeEncoderProfilesProvider.Builder()
93             .also { builder ->
94                 for ((quality, profiles) in qualityToProfilesMap) {
95                     builder.add(quality, profiles)
96                 }
97             }
98             .build()
99     }
100 }
101