Skip to content

Commit

Permalink
Convert com.facebook.testutils.shadows to Kotlin (#47487)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47487

Changelog: [Android][Deprecated] Deprecated shadows for ReadableNative[Map|Array].[Readable|Writable]

Reviewed By: rshest

Differential Revision: D65597417

fbshipit-source-id: 61f649c32fb91a13af075aa65869093f31d218e3
  • Loading branch information
javache authored and facebook-github-bot committed Nov 22, 2024
1 parent eb5fbdf commit d424bb9
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.facebook.testutils.shadows.ShadowArguments
import com.facebook.testutils.shadows.ShadowNativeArray
import com.facebook.testutils.shadows.ShadowNativeLoader
import com.facebook.testutils.shadows.ShadowSoLoader
import com.facebook.testutils.shadows.ShadowWritableNativeArray
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
Expand All @@ -41,7 +42,7 @@ import org.robolectric.annotation.Config
ShadowSoLoader::class,
ShadowNativeLoader::class,
ShadowArguments::class,
ShadowNativeArray.Writable::class])
ShadowWritableNativeArray::class])
class BridgelessReactContextTest {
private lateinit var context: Context
private lateinit var reactHost: ReactHostImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ShadowArguments {
@Implementation
fun fromJavaArgs(args: Array<Any?>): WritableNativeArray =
WritableNativeArray().apply {
(Shadow.extract(this) as ShadowNativeArray).contents = args.toList()
(Shadow.extract(this) as ShadowNativeArray).backingArray = JavaOnlyArray.of(*args)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.testutils.shadows

import com.facebook.react.bridge.JavaOnlyArray
import com.facebook.react.bridge.NativeArray
import com.facebook.react.bridge.ReadableNativeArray
import com.facebook.react.bridge.WritableNativeArray
Expand All @@ -16,14 +17,24 @@ import org.robolectric.shadow.api.Shadow
// Mockito can't mock native methods, so shadow the entire class instead
@Implements(NativeArray::class)
public open class ShadowNativeArray {
public var contents: List<Any?> = mutableListOf()
var backingArray: JavaOnlyArray = JavaOnlyArray()

@Implements(ReadableNativeArray::class) public class Readable : ShadowNativeArray() {}
@Deprecated(
"Use ShadowReadableNativeArray",
ReplaceWith(
"ShadowReadableNativeArray", "com.facebook.testutils.shadows.ShadowReadableNativeArray"))
@Implements(ReadableNativeArray::class)
public class Readable : ShadowNativeArray() {}

@Implements(WritableNativeArray::class) public class Writable : ShadowNativeArray() {}
@Deprecated(
"Use ShadowWritableNativeArray",
ReplaceWith(
"ShadowWritableNativeArray", "com.facebook.testutils.shadows.ShadowWritableNativeArray"))
@Implements(WritableNativeArray::class)
public class Writable : ShadowNativeArray() {}

public companion object {
public fun getContents(array: NativeArray): List<Any?> =
(Shadow.extract(array) as ShadowNativeArray).contents
(Shadow.extract(array) as ShadowNativeArray).backingArray.toArrayList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.testutils.shadows

import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.bridge.NativeMap
import com.facebook.react.bridge.ReadableNativeMap
import com.facebook.react.bridge.WritableNativeMap
Expand All @@ -15,15 +16,25 @@ import org.robolectric.shadow.api.Shadow

// Mockito can't mock native methods, so shadow the entire class instead
@Implements(NativeMap::class)
public open class ShadowNativeMap {
public var contents: Map<String, Any?> = HashMap()
open class ShadowNativeMap {
var backingMap: JavaOnlyMap = JavaOnlyMap()

@Implements(ReadableNativeMap::class) public class Readable : ShadowNativeMap() {}
@Deprecated(
"Use ShadowReadableNativeMap",
ReplaceWith(
"ShadowReadableNativeMap", "com.facebook.testutils.shadows.ShadowReadableNativeMap"))
@Implements(ReadableNativeMap::class)
public class Readable : ShadowNativeMap() {}

@Implements(WritableNativeMap::class) public class Writable : ShadowNativeMap() {}
@Deprecated(
"Use ShadowWritableNativeMap",
ReplaceWith(
"ShadowWritableNativeMap", "com.facebook.testutils.shadows.ShadowWritableNativeMap"))
@Implements(WritableNativeMap::class)
public class Writable : ShadowNativeMap() {}

public companion object {
public fun getContents(map: NativeMap): Map<String, Any?> =
(Shadow.extract(map) as ShadowNativeMap).contents
companion object {
fun getContents(map: NativeMap): Map<String, Any?> =
(Shadow.extract(map) as ShadowNativeMap).backingMap.toHashMap()
}
}

This file was deleted.

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

import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableNativeArray
import com.facebook.react.bridge.ReadableType
import java.util.ArrayList
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements

@Implements(ReadableNativeArray::class)
open class ShadowReadableNativeArray : ShadowNativeArray(), ReadableArray {

@Implementation override fun size(): Int = backingArray.size()

@Implementation override fun isNull(index: Int): Boolean = backingArray.isNull(index)

@Implementation override fun getBoolean(index: Int): Boolean = backingArray.getBoolean(index)

@Implementation override fun getDouble(index: Int): Double = backingArray.getDouble(index)

@Implementation override fun getInt(index: Int): Int = backingArray.getInt(index)

@Implementation override fun getLong(index: Int): Long = backingArray.getLong(index)

@Implementation override fun getString(index: Int): String? = backingArray.getString(index)

@Implementation override fun getArray(index: Int): ReadableArray? = backingArray.getArray(index)

@Implementation override fun getMap(index: Int): ReadableMap? = backingArray.getMap(index)

@Implementation override fun getDynamic(index: Int): Dynamic = backingArray.getDynamic(index)

@Implementation override fun getType(index: Int): ReadableType = backingArray.getType(index)

@Implementation override fun toArrayList(): ArrayList<Any?> = backingArray.toArrayList()
}

This file was deleted.

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

import com.facebook.react.bridge.Dynamic
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableMapKeySetIterator
import com.facebook.react.bridge.ReadableNativeMap
import com.facebook.react.bridge.ReadableType
import java.util.HashMap
import org.robolectric.annotation.Implementation
import org.robolectric.annotation.Implements

@Implements(ReadableNativeMap::class)
open class ShadowReadableNativeMap : ShadowNativeMap(), ReadableMap {

@Implementation override fun hasKey(name: String): Boolean = backingMap.hasKey(name)

@Implementation override fun isNull(name: String): Boolean = backingMap.isNull(name)

@Implementation override fun getBoolean(name: String): Boolean = backingMap.getBoolean(name)

@Implementation override fun getDouble(name: String): Double = backingMap.getDouble(name)

@Implementation override fun getInt(name: String): Int = backingMap.getInt(name)

@Implementation override fun getLong(name: String): Long = backingMap.getLong(name)

@Implementation override fun getString(name: String): String? = backingMap.getString(name)

@Implementation override fun getArray(name: String): ReadableArray? = backingMap.getArray(name)

@Implementation override fun getMap(name: String): ReadableMap? = backingMap.getMap(name)

@Implementation override fun getDynamic(name: String): Dynamic = backingMap.getDynamic(name)

@Implementation override fun getType(name: String): ReadableType = backingMap.getType(name)

@get:Implementation
override val entryIterator: Iterator<Map.Entry<String, Any?>>
get() = backingMap.entryIterator

@Implementation
override fun keySetIterator(): ReadableMapKeySetIterator = backingMap.keySetIterator()

@Implementation override fun toHashMap(): HashMap<String, Any?> = backingMap.toHashMap()
}
Loading

0 comments on commit d424bb9

Please sign in to comment.