mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
(refactor): kotlin-ify ShareModuleTest.java (#37746)
Summary: This PR converts the java logic inside of `ShareModuleTest.java` to Kotlin as requested in https://github.com/facebook/react-native/issues/37708 We also swap `ShareModuleTest.java` for `ShareModuleTest.kt` ## Changelog: [Internal][Changed]: Convert ShareModuleTest to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/37746 Test Plan: `./gradlew :packages:react-native:ReactAndroid:test` must pass and CI must be green ## Screenshot of passing tests locally  Reviewed By: cortinico Differential Revision: D46514083 Pulled By: rshest fbshipit-source-id: 7eb49fb4ccf5b889bfe38aa6274454e1633a8b0e
This commit is contained in:
committed by
Facebook GitHub Bot
parent
8cf5da481d
commit
e1206da06d
-200
@@ -1,200 +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.share;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.react.bridge.JavaOnlyMap;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactTestHelper;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.testutils.shadows.ShadowArguments;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(shadows = {ShadowArguments.class})
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ShareModuleTest {
|
||||
|
||||
private Activity mActivity;
|
||||
private ShareModule mShareModule;
|
||||
|
||||
@Before
|
||||
public void prepareModules() throws Exception {
|
||||
mActivity = Robolectric.setupActivity(Activity.class);
|
||||
|
||||
ReactApplicationContext applicationContext = ReactTestHelper.createCatalystContextForTest();
|
||||
applicationContext.onNewIntent(mActivity, new Intent());
|
||||
mShareModule = new ShareModule(applicationContext);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
mActivity = null;
|
||||
mShareModule = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShareDialog() {
|
||||
final String title = "Title";
|
||||
final String message = "Message";
|
||||
final String dialogTitle = "Dialog Title";
|
||||
|
||||
JavaOnlyMap content = new JavaOnlyMap();
|
||||
content.putString("title", title);
|
||||
content.putString("message", message);
|
||||
|
||||
final SimplePromise promise = new SimplePromise();
|
||||
|
||||
mShareModule.share(content, dialogTitle, promise);
|
||||
|
||||
final Intent chooserIntent = shadowOf(RuntimeEnvironment.application).getNextStartedActivity();
|
||||
assertNotNull("Dialog was not displayed", chooserIntent);
|
||||
assertEquals(Intent.ACTION_CHOOSER, chooserIntent.getAction());
|
||||
assertEquals(dialogTitle, chooserIntent.getExtras().get(Intent.EXTRA_TITLE));
|
||||
|
||||
final Intent contentIntent = (Intent) chooserIntent.getExtras().get(Intent.EXTRA_INTENT);
|
||||
assertNotNull("Intent was not built correctly", contentIntent);
|
||||
assertEquals(Intent.ACTION_SEND, contentIntent.getAction());
|
||||
assertEquals(title, contentIntent.getExtras().get(Intent.EXTRA_SUBJECT));
|
||||
assertEquals(message, contentIntent.getExtras().get(Intent.EXTRA_TEXT));
|
||||
|
||||
assertEquals(1, promise.getResolved());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidContent() {
|
||||
final String dialogTitle = "Dialog Title";
|
||||
|
||||
final SimplePromise promise = new SimplePromise();
|
||||
|
||||
mShareModule.share(null, dialogTitle, promise);
|
||||
|
||||
assertEquals(1, promise.getRejected());
|
||||
assertEquals(ShareModule.ERROR_INVALID_CONTENT, promise.getErrorCode());
|
||||
}
|
||||
|
||||
static final class SimplePromise implements Promise {
|
||||
private static final String ERROR_DEFAULT_CODE = "EUNSPECIFIED";
|
||||
private static final String ERROR_DEFAULT_MESSAGE = "Error not specified.";
|
||||
|
||||
private int mResolved;
|
||||
private int mRejected;
|
||||
private Object mValue;
|
||||
private String mErrorCode;
|
||||
private String mErrorMessage;
|
||||
|
||||
public int getResolved() {
|
||||
return mResolved;
|
||||
}
|
||||
|
||||
public int getRejected() {
|
||||
return mRejected;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return mValue;
|
||||
}
|
||||
|
||||
public String getErrorCode() {
|
||||
return mErrorCode;
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
return mErrorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resolve(Object value) {
|
||||
mResolved++;
|
||||
mValue = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, String message) {
|
||||
reject(code, message, /*Throwable*/ null, /*WritableMap*/ null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, Throwable throwable) {
|
||||
reject(code, /*Message*/ null, throwable, /*WritableMap*/ null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, String message, Throwable throwable) {
|
||||
reject(code, message, throwable, /*WritableMap*/ null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(Throwable throwable) {
|
||||
reject(/*Code*/ null, /*Message*/ null, throwable, /*WritableMap*/ null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(Throwable throwable, WritableMap userInfo) {
|
||||
reject(/*Code*/ null, /*Message*/ null, throwable, userInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, @NonNull WritableMap userInfo) {
|
||||
reject(code, /*Message*/ null, /*Throwable*/ null, userInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, Throwable throwable, WritableMap userInfo) {
|
||||
reject(code, /*Message*/ null, throwable, userInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(String code, String message, @NonNull WritableMap userInfo) {
|
||||
reject(code, message, /*Throwable*/ null, userInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reject(
|
||||
String code,
|
||||
String message,
|
||||
@Nullable Throwable throwable,
|
||||
@Nullable WritableMap userInfo) {
|
||||
mRejected++;
|
||||
|
||||
if (code == null) {
|
||||
mErrorCode = ERROR_DEFAULT_CODE;
|
||||
} else {
|
||||
mErrorCode = code;
|
||||
}
|
||||
|
||||
if (message != null) {
|
||||
mErrorMessage = message;
|
||||
} else if (throwable != null) {
|
||||
mErrorMessage = throwable.getMessage();
|
||||
} else {
|
||||
mErrorMessage = ERROR_DEFAULT_MESSAGE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public void reject(String message) {
|
||||
reject(/*Code*/ null, message, /*Throwable*/ null, /*WritableMap*/ null);
|
||||
}
|
||||
}
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* 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.share
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import com.facebook.react.bridge.JavaOnlyMap
|
||||
import com.facebook.react.bridge.Promise
|
||||
import com.facebook.react.bridge.ReactTestHelper
|
||||
import com.facebook.react.bridge.WritableMap
|
||||
import com.facebook.testutils.shadows.ShadowArguments
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@Config(shadows = [ShadowArguments::class])
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class ShareModuleTest {
|
||||
|
||||
private lateinit var activity: Activity
|
||||
private lateinit var shareModule: ShareModule
|
||||
|
||||
@Before
|
||||
fun prepareModules() {
|
||||
activity = Robolectric.setupActivity(Activity::class.java)
|
||||
|
||||
val applicationContext = ReactTestHelper.createCatalystContextForTest()
|
||||
applicationContext.onNewIntent(activity, Intent())
|
||||
shareModule = ShareModule(applicationContext)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShareDialog() {
|
||||
val title = "Title"
|
||||
val message = "Message"
|
||||
val dialogTitle = "Dialog Title"
|
||||
|
||||
val content = JavaOnlyMap()
|
||||
content.putString("title", title)
|
||||
content.putString("message", message)
|
||||
|
||||
val promise = SimplePromise()
|
||||
|
||||
shareModule.share(content, dialogTitle, promise)
|
||||
|
||||
val chooserIntent = shadowOf(RuntimeEnvironment.application).nextStartedActivity
|
||||
assertNotNull("Dialog was not displayed", chooserIntent)
|
||||
assertEquals(Intent.ACTION_CHOOSER, chooserIntent.action)
|
||||
assertEquals(dialogTitle, chooserIntent.extras?.get(Intent.EXTRA_TITLE))
|
||||
|
||||
val contentIntent = chooserIntent.extras?.get(Intent.EXTRA_INTENT) as Intent?
|
||||
assertNotNull("Intent was not built correctly", contentIntent)
|
||||
assertEquals(Intent.ACTION_SEND, contentIntent?.action)
|
||||
assertEquals(title, contentIntent?.extras?.get(Intent.EXTRA_SUBJECT))
|
||||
assertEquals(message, contentIntent?.extras?.get(Intent.EXTRA_TEXT))
|
||||
|
||||
assertEquals(1, promise.resolved)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInvalidContent() {
|
||||
val dialogTitle = "Dialog Title"
|
||||
|
||||
val promise = SimplePromise()
|
||||
|
||||
shareModule.share(null, dialogTitle, promise)
|
||||
|
||||
assertEquals(1, promise.rejected)
|
||||
assertEquals(ShareModule.ERROR_INVALID_CONTENT, promise.errorCode)
|
||||
}
|
||||
|
||||
internal class SimplePromise : Promise {
|
||||
companion object {
|
||||
private const val ERROR_DEFAULT_CODE = "EUNSPECIFIED"
|
||||
private const val ERROR_DEFAULT_MESSAGE = "Error not specified."
|
||||
}
|
||||
|
||||
var resolved = 0
|
||||
private set
|
||||
|
||||
var rejected = 0
|
||||
private set
|
||||
|
||||
var value: Any? = null
|
||||
private set
|
||||
|
||||
var errorCode: String? = null
|
||||
private set
|
||||
|
||||
var errorMessage: String? = null
|
||||
private set
|
||||
|
||||
override fun resolve(value: Any?) {
|
||||
resolved++
|
||||
this.value = value
|
||||
}
|
||||
|
||||
override fun reject(code: String?, message: String?) {
|
||||
reject(code, message, null, null)
|
||||
}
|
||||
|
||||
override fun reject(code: String?, throwable: Throwable?) {
|
||||
reject(code, null, throwable, null)
|
||||
}
|
||||
|
||||
override fun reject(code: String?, message: String?, throwable: Throwable?) {
|
||||
reject(code, message, throwable, null)
|
||||
}
|
||||
|
||||
override fun reject(throwable: Throwable?) {
|
||||
reject(null, null, throwable, null)
|
||||
}
|
||||
|
||||
override fun reject(throwable: Throwable?, userInfo: WritableMap?) {
|
||||
reject(null, null, throwable, userInfo)
|
||||
}
|
||||
|
||||
override fun reject(code: String?, userInfo: WritableMap) {
|
||||
reject(code, null, null, userInfo)
|
||||
}
|
||||
|
||||
override fun reject(code: String?, throwable: Throwable?, userInfo: WritableMap?) {
|
||||
reject(code, null, throwable, userInfo)
|
||||
}
|
||||
|
||||
override fun reject(code: String?, message: String?, userInfo: WritableMap) {
|
||||
reject(code, message, null, userInfo)
|
||||
}
|
||||
|
||||
override fun reject(
|
||||
code: String?,
|
||||
message: String?,
|
||||
throwable: Throwable?,
|
||||
userInfo: WritableMap?
|
||||
) {
|
||||
rejected++
|
||||
|
||||
errorCode = code ?: ERROR_DEFAULT_CODE
|
||||
errorMessage = message ?: throwable?.message ?: ERROR_DEFAULT_MESSAGE
|
||||
}
|
||||
|
||||
@Deprecated("Method deprecated")
|
||||
override fun reject(message: String?) {
|
||||
reject(null, message, null, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user