Skip to content

Commit

Permalink
Convert BlobModuleTest class to Kotlin (#37719)
Browse files Browse the repository at this point in the history
Summary:
[Migrate](#37708) BlobModuleTest class to Kotlin

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[INTERNAL] [CHANGED] `BlobModuleTest` class migrated from Java to Kotlin

Pull Request resolved: #37719

Test Plan:
Tests should pass

```sh
./gradlew :packages:react-native:ReactAndroid:test
```

Reviewed By: yungsters

Differential Revision: D46485981

Pulled By: cortinico

fbshipit-source-id: 69858fedfd84b59d6c1c2aca99f6fa9c0a8832fa
  • Loading branch information
mateusz1913 authored and facebook-github-bot committed Jun 7, 2023
1 parent 08dc0a6 commit 22b39d7
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 156 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.blob

import android.net.Uri
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.JavaOnlyArray
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.bridge.ReactTestHelper
import java.nio.ByteBuffer
import java.util.UUID
import kotlin.random.Random
import org.junit.After
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito.mockStatic
import org.powermock.api.mockito.PowerMockito.`when` as whenever
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@PrepareForTest(Arguments::class)
@RunWith(RobolectricTestRunner::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
@Config(manifest = Config.NONE)
class BlobModuleTest {
private lateinit var bytes: ByteArray
private lateinit var blobId: String
private lateinit var blobModule: BlobModule

@get:Rule var rule = PowerMockRule()

@Before
fun prepareModules() {
mockStatic(Arguments::class.java)
whenever(Arguments.createMap()).thenAnswer { JavaOnlyMap() }

bytes = ByteArray(120)
Random.Default.nextBytes(bytes)

blobModule = BlobModule(ReactTestHelper.createCatalystContextForTest())
blobId = blobModule.store(bytes)
}

@After
fun cleanUp() {
blobModule.remove(blobId)
}

@Test
fun testResolve() {
assertArrayEquals(bytes, blobModule.resolve(blobId, 0, bytes.size))
val expectedRange = bytes.copyOfRange(30, bytes.size)
assertArrayEquals(expectedRange, blobModule.resolve(blobId, 30, bytes.size - 30))
}

@Test
fun testResolveUri() {
val uri =
Uri.Builder()
.appendPath(blobId)
.appendQueryParameter("offset", "0")
.appendQueryParameter("size", bytes.size.toString())
.build()

assertArrayEquals(bytes, blobModule.resolve(uri))
}

@Test
fun testResolveMap() {
val blob =
JavaOnlyMap().apply {
putString("blobId", blobId)
putInt("offset", 0)
putInt("size", bytes.size)
}

assertArrayEquals(bytes, blobModule.resolve(blob))
}

@Test
fun testRemove() {
assertNotNull(blobModule.resolve(blobId, 0, bytes.size))

blobModule.remove(blobId)

assertNull(blobModule.resolve(blobId, 0, bytes.size))
}

@Test
fun testCreateFromParts() {
val id = UUID.randomUUID().toString()

val blobData =
JavaOnlyMap().apply {
putString("blobId", blobId)
putInt("offset", 0)
putInt("size", bytes.size)
}
val blob =
JavaOnlyMap().apply {
putMap("data", blobData)
putString("type", "blob")
}

val stringData = "i \u2665 dogs"
val stringBytes = stringData.encodeToByteArray()
val string =
JavaOnlyMap().apply {
putString("data", stringData)
putString("type", "string")
}

val parts =
JavaOnlyArray().apply {
pushMap(blob)
pushMap(string)
}

blobModule.createFromParts(parts, id)

val resultSize = bytes.size + stringBytes.size

val result = blobModule.resolve(id, 0, resultSize)

val buffer =
ByteBuffer.allocate(resultSize).apply {
put(bytes)
put(stringBytes)
}

assertArrayEquals(result, buffer.array())
}

@Test
fun testRelease() {
assertNotNull(blobModule.resolve(blobId, 0, bytes.size))

blobModule.release(blobId)

assertNull(blobModule.resolve(blobId, 0, bytes.size))
}
}

0 comments on commit 22b39d7

Please sign in to comment.