Skip to content

Commit e23b2e7

Browse files
authored
Merge pull request #5227 from element-hq/feature/bma/addUnitTest
Add unit test on VideoCompressorHelper
2 parents fa755e5 + c66de6a commit e23b2e7

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

libraries/androidutils/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies {
3838
testImplementation(libs.test.junit)
3939
testImplementation(libs.test.truth)
4040
testImplementation(libs.test.robolectric)
41+
testImplementation(libs.androidx.test.ext.junit)
4142
testImplementation(libs.coroutines.core)
4243
testImplementation(libs.coroutines.test)
4344
testImplementation(projects.services.toolbox.test)

libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/media/VideoCompressorHelper.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VideoCompressorHelper(
2727
fun getOutputSize(inputSize: Size): Size {
2828
val resultMajor = min(inputSize.major(), maxSize)
2929
val aspectRatio = inputSize.major().toFloat() / inputSize.minor().toFloat()
30-
return if (inputSize.width >= inputSize.height) {
30+
return if (inputSize.isLandscape()) {
3131
Size(resultMajor, (resultMajor / aspectRatio).roundToInt())
3232
} else {
3333
Size((resultMajor / aspectRatio).roundToInt(), resultMajor)
@@ -46,5 +46,6 @@ class VideoCompressorHelper(
4646
}
4747
}
4848

49-
internal fun Size.major(): Int = if (width > height) width else height
50-
internal fun Size.minor(): Int = if (width < height) width else height
49+
private fun Size.isLandscape(): Boolean = width > height
50+
private fun Size.major(): Int = if (isLandscape()) width else height
51+
private fun Size.minor(): Int = if (isLandscape()) height else width
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.androidutils.media
9+
10+
import android.util.Size
11+
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import com.google.common.truth.Truth.assertThat
13+
import org.junit.Test
14+
import org.junit.runner.RunWith
15+
16+
@RunWith(AndroidJUnit4::class)
17+
class VideoCompressorHelperTest {
18+
@Test
19+
fun `test getOutputSize`() {
20+
val helper = VideoCompressorHelper(maxSize = 720)
21+
22+
// Landscape input
23+
var inputSize = Size(1920, 1080)
24+
var outputSize = helper.getOutputSize(inputSize)
25+
assertThat(outputSize).isEqualTo(Size(720, 405))
26+
27+
// Landscape input small height
28+
inputSize = Size(1920, 200)
29+
outputSize = helper.getOutputSize(inputSize)
30+
assertThat(outputSize).isEqualTo(Size(720, 75))
31+
32+
// Portrait input
33+
inputSize = Size(1080, 1920)
34+
outputSize = helper.getOutputSize(inputSize)
35+
assertThat(outputSize).isEqualTo(Size(405, 720))
36+
37+
// Portrait input small width
38+
inputSize = Size(200, 1920)
39+
outputSize = helper.getOutputSize(inputSize)
40+
assertThat(outputSize).isEqualTo(Size(75, 720))
41+
42+
// Square input
43+
inputSize = Size(1000, 1000)
44+
outputSize = helper.getOutputSize(inputSize)
45+
assertThat(outputSize).isEqualTo(Size(720, 720))
46+
47+
// Square input same size
48+
inputSize = Size(720, 720)
49+
outputSize = helper.getOutputSize(inputSize)
50+
assertThat(outputSize).isEqualTo(Size(720, 720))
51+
52+
// Square input no downscaling
53+
inputSize = Size(240, 240)
54+
outputSize = helper.getOutputSize(inputSize)
55+
assertThat(outputSize).isEqualTo(Size(240, 240))
56+
57+
// Small input landscape (no downscaling)
58+
inputSize = Size(640, 480)
59+
outputSize = helper.getOutputSize(inputSize)
60+
assertThat(outputSize).isEqualTo(Size(640, 480))
61+
62+
// Small input portrait (no downscaling)
63+
inputSize = Size(480, 640)
64+
outputSize = helper.getOutputSize(inputSize)
65+
assertThat(outputSize).isEqualTo(Size(480, 640))
66+
}
67+
68+
@Test
69+
fun `test calculateOptimalBitrate`() {
70+
val helper = VideoCompressorHelper(maxSize = 720)
71+
val inputSize = Size(1920, 1080)
72+
var bitrate = helper.calculateOptimalBitrate(inputSize, frameRate = 30)
73+
// Output size will be 720x405, so bitrate = 720*405*0.1*30 = 874800
74+
assertThat(bitrate).isEqualTo(874_800L)
75+
// Half frame rate, half bitrate
76+
bitrate = helper.calculateOptimalBitrate(inputSize, frameRate = 15)
77+
assertThat(bitrate).isEqualTo(437_400L)
78+
}
79+
}

0 commit comments

Comments
 (0)