-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert BlobModuleTest class to Kotlin (#37719)
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
1 parent
08dc0a6
commit 22b39d7
Showing
2 changed files
with
155 additions
and
156 deletions.
There are no files selected for viewing
156 changes: 0 additions & 156 deletions
156
...act-native/ReactAndroid/src/test/java/com/facebook/react/modules/blob/BlobModuleTest.java
This file was deleted.
Oops, something went wrong.
155 changes: 155 additions & 0 deletions
155
...react-native/ReactAndroid/src/test/java/com/facebook/react/modules/blob/BlobModuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |