Convert ImageStoreManagerTest to Kotlin (#37737)

Summary:
Converts ImageStoreManagerTest.java to Kotlin, as per issue https://github.com/facebook/react-native/issues/37708

## Changelog:

[Internal] [Changed] - Convert ImageStoreManagerTest.java to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/37737

Test Plan:
Tests pass: ./gradlew :packages:react-native:ReactAndroid:test
Formatted with [KtFmt](https://facebook.github.io/ktfmt/)

Reviewed By: cipolleschi

Differential Revision: D46552483

Pulled By: cortinico

fbshipit-source-id: 98da889bd61656f930912a10a13deb0dd581df23
This commit is contained in:
Thomas Hoang
2023-06-08 07:11:44 -07:00
committed by Facebook GitHub Bot
parent 5ef5767c3c
commit e7d3662904
2 changed files with 89 additions and 81 deletions
@@ -1,81 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.modules.camera;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import android.util.Base64;
import android.util.Base64InputStream;
import com.facebook.react.bridge.ReactApplicationContext;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"})
public class ImageStoreManagerTest {
@Test
public void itDoesNotAddLineBreaks_whenBasicStringProvided() throws IOException {
byte[] exampleString = "test".getBytes();
assertEquals("dGVzdA==", invokeConversion(new ByteArrayInputStream(exampleString)));
}
@Test
public void itDoesNotAddLineBreaks_whenEmptyStringProvided() throws IOException {
byte[] exampleString = "".getBytes();
assertEquals("", invokeConversion(new ByteArrayInputStream(exampleString)));
}
@Test
public void itDoesNotAddLineBreaks_whenStringWithSpecialCharsProvided() throws IOException {
byte[] exampleString = "sdfsdf\nasdfsdfsdfsd\r\nasdas".getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(exampleString);
assertFalse(invokeConversion(inputStream).contains("\n"));
}
/**
* This test tries to test the conversion when going beyond the current buffer size (8192 bytes)
*/
@Test
public void itDoesNotAddLineBreaks_whenStringBiggerThanBuffer() throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(generateRandomByteString(10000));
assertFalse(invokeConversion(inputStream).contains("\n"));
}
/** Just to test if using the ByteArrayInputStream isn't missing something */
@Test
public void itDoesNotAddLineBreaks_whenBase64InputStream() throws IOException {
byte[] exampleString = "dGVzdA==".getBytes();
Base64InputStream inputStream =
new Base64InputStream(new ByteArrayInputStream(exampleString), Base64.NO_WRAP);
assertEquals("dGVzdA==", invokeConversion(inputStream));
}
private String invokeConversion(InputStream inputStream) throws IOException {
return new ImageStoreManager(mock(ReactApplicationContext.class))
.convertInputStreamToBase64OutputStream(inputStream);
}
private byte[] generateRandomByteString(final int length) {
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) (r.nextInt((int) (Character.MAX_VALUE)));
sb.append(c);
}
return sb.toString().getBytes();
}
}
@@ -0,0 +1,89 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.modules.camera
import android.util.Base64
import android.util.Base64InputStream
import com.facebook.react.bridge.ReactApplicationContext
import java.io.ByteArrayInputStream
import java.io.InputStream
import java.util.*
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
class ImageStoreManagerTest {
private lateinit var reactApplicationContext: ReactApplicationContext
private lateinit var imageStoreManager: ImageStoreManager
@Before
fun setUp() {
reactApplicationContext = mock(ReactApplicationContext::class.java)
imageStoreManager = ImageStoreManager(reactApplicationContext)
}
@Test
fun itDoesNotAddLineBreaks_whenBasicStringProvided() {
val exampleString = "test".toByteArray()
Assert.assertEquals("dGVzdA==", invokeConversion(ByteArrayInputStream(exampleString)))
}
@Test
fun itDoesNotAddLineBreaks_whenEmptyStringProvided() {
val exampleString = "".toByteArray()
Assert.assertEquals("", invokeConversion(ByteArrayInputStream(exampleString)))
}
@Test
fun itDoesNotAddLineBreaks_whenStringWithSpecialCharsProvided() {
val exampleString = "sdfsdf\nasdfsdfsdfsd\r\nasdas".toByteArray()
val inputStream = ByteArrayInputStream(exampleString)
val converted = invokeConversion(inputStream)
Assert.assertFalse(converted.contains("\n"))
}
/**
* This test tries to test the conversion when going beyond the current buffer size (8192 bytes)
*/
@Test
fun itDoesNotAddLineBreaks_whenStringBiggerThanBuffer() {
val inputStream = ByteArrayInputStream(generateRandomByteString(10000))
val converted = invokeConversion(inputStream)
Assert.assertFalse(converted.contains("\n"))
}
/** Just to test if using the ByteArrayInputStream isn't missing something */
@Test
fun itDoesNotAddLineBreaks_whenBase64InputStream() {
val exampleString = "dGVzdA==".toByteArray()
val inputStream = Base64InputStream(ByteArrayInputStream(exampleString), Base64.NO_WRAP)
Assert.assertEquals("dGVzdA==", invokeConversion(inputStream))
}
private fun invokeConversion(inputStream: InputStream): String {
return ImageStoreManager(reactApplicationContext)
.convertInputStreamToBase64OutputStream(inputStream)
}
private fun generateRandomByteString(length: Int): ByteArray {
val r = Random()
val sb = StringBuilder()
repeat(length) {
val c = r.nextInt(Char.MAX_VALUE.code).toChar()
sb.append(c)
}
return sb.toString().toByteArray()
}
}