Remove JS console plugin
Summary: This is to unblock our mobile lab build target that adds Flipper to a redex-able build. Redex currently crashes when it tries to optimise Rhino. This could probably be fixed but we don't have any real use-cases for the console plugin right now. It should be fairly easy to unland this even partially if we want to revive this in the future. Reviewed By: danielbuechele Differential Revision: D15044539 fbshipit-source-id: f0857274aa046f5be935a342cf91b6a390fcb3dc
This commit is contained in:
committed by
Facebook Github Bot
parent
8684812907
commit
84e4b916ee
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console;
|
||||
|
||||
import com.facebook.flipper.core.FlipperConnection;
|
||||
import com.facebook.flipper.core.FlipperPlugin;
|
||||
import com.facebook.flipper.plugins.console.iface.ConsoleCommandReceiver;
|
||||
|
||||
public class ConsoleFlipperPlugin implements FlipperPlugin {
|
||||
|
||||
private final JavascriptEnvironment mJavascriptEnvironment;
|
||||
private JavascriptSession mJavascriptSession;
|
||||
|
||||
public ConsoleFlipperPlugin(JavascriptEnvironment jsEnvironment) {
|
||||
this.mJavascriptEnvironment = jsEnvironment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "Console";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnect(FlipperConnection connection) throws Exception {
|
||||
ConsoleCommandReceiver.listenForCommands(connection, mJavascriptEnvironment);
|
||||
}
|
||||
|
||||
public void onDisconnect() throws Exception {}
|
||||
|
||||
@Override
|
||||
public boolean runInBackground() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console;
|
||||
|
||||
import com.facebook.flipper.plugins.console.iface.ScriptingEnvironment;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
|
||||
public class JavascriptEnvironment implements ScriptingEnvironment {
|
||||
|
||||
private final Map<String, Object> mBoundVariables;
|
||||
private final ContextFactory mContextFactory;
|
||||
|
||||
public JavascriptEnvironment() {
|
||||
mBoundVariables = new HashMap<>();
|
||||
mContextFactory =
|
||||
new ContextFactory() {
|
||||
@Override
|
||||
public boolean hasFeature(Context cx, int featureIndex) {
|
||||
return featureIndex == Context.FEATURE_ENHANCED_JAVA_ACCESS;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavascriptSession startSession() {
|
||||
return new JavascriptSession(mContextFactory, mBoundVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for other plugins to register objects to a name, so that they can be accessed in all
|
||||
* console sessions.
|
||||
*
|
||||
* @param name The variable name to bind the object to.
|
||||
* @param object The reference to bind.
|
||||
*/
|
||||
@Override
|
||||
public void registerGlobalObject(String name, Object object) {
|
||||
if (mBoundVariables.containsKey(name)) {
|
||||
throw new IllegalStateException(
|
||||
String.format("Variable %s is already reserved for %s", name, mBoundVariables.get(name)));
|
||||
}
|
||||
mBoundVariables.put(name, object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console;
|
||||
|
||||
import com.facebook.flipper.plugins.console.iface.ScriptingSession;
|
||||
import java.io.Closeable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.Function;
|
||||
import org.mozilla.javascript.NativeJSON;
|
||||
import org.mozilla.javascript.NativeJavaMethod;
|
||||
import org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
import org.mozilla.javascript.Undefined;
|
||||
|
||||
public class JavascriptSession implements Closeable, ScriptingSession {
|
||||
|
||||
private static final String TYPE = "type";
|
||||
private static final String VALUE = "value";
|
||||
public static final String JSON = "json";
|
||||
private final Context mContext;
|
||||
private final ContextFactory mContextFactory;
|
||||
private final Scriptable mScope;
|
||||
private final AtomicInteger lineNumber = new AtomicInteger(0);
|
||||
|
||||
JavascriptSession(ContextFactory contextFactory, Map<String, Object> globals) {
|
||||
mContextFactory = contextFactory;
|
||||
mContext = contextFactory.enterContext();
|
||||
|
||||
// Interpreted mode, or it will produce Dalvik incompatible bytecode.
|
||||
mContext.setOptimizationLevel(-1);
|
||||
mScope = mContext.initStandardObjects();
|
||||
|
||||
for (Map.Entry<String, Object> entry : globals.entrySet()) {
|
||||
final Object value = entry.getValue();
|
||||
|
||||
if (value instanceof Number || value instanceof String) {
|
||||
ScriptableObject.putConstProperty(mScope, entry.getKey(), entry.getValue());
|
||||
} else {
|
||||
// Calling java methods in the VM produces objects wrapped in NativeJava*.
|
||||
// So passing in wrapped objects keeps them consistent.
|
||||
ScriptableObject.putConstProperty(
|
||||
mScope,
|
||||
entry.getKey(),
|
||||
new NativeJavaObject(mScope, entry.getValue(), entry.getValue().getClass()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript) throws JSONException {
|
||||
return evaluateCommand(userScript, mScope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript, Object context) throws JSONException {
|
||||
Scriptable scope = new NativeJavaObject(mScope, context, context.getClass());
|
||||
return evaluateCommand(userScript, scope);
|
||||
}
|
||||
|
||||
private JSONObject evaluateCommand(String command, Scriptable scope) throws JSONException {
|
||||
try {
|
||||
// This may be called by any thread, and contexts have to be entered in the current thread
|
||||
// before being used, so enter/exit every time.
|
||||
mContextFactory.enterContext();
|
||||
return toJson(
|
||||
mContext.evaluateString(
|
||||
scope, command, "flipper-console", lineNumber.incrementAndGet(), null));
|
||||
} finally {
|
||||
Context.exit();
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject toJson(Object result) throws JSONException {
|
||||
|
||||
if (result instanceof String) {
|
||||
return new JSONObject().put(TYPE, JSON).put(VALUE, result);
|
||||
}
|
||||
|
||||
if (result instanceof Class) {
|
||||
return new JSONObject().put(TYPE, "class").put(VALUE, ((Class) result).getName());
|
||||
}
|
||||
|
||||
if (result instanceof NativeJavaObject
|
||||
&& ((NativeJavaObject) result).unwrap() instanceof String) {
|
||||
return new JSONObject().put(TYPE, JSON).put(VALUE, ((NativeJavaObject) result).unwrap());
|
||||
}
|
||||
|
||||
if (result instanceof NativeJavaObject
|
||||
&& ((NativeJavaObject) result).unwrap() instanceof Class) {
|
||||
return new JSONObject()
|
||||
.put(TYPE, "class")
|
||||
.put(VALUE, ((NativeJavaObject) result).unwrap().toString());
|
||||
}
|
||||
|
||||
if (result instanceof NativeJavaObject) {
|
||||
final JSONObject o = new JSONObject();
|
||||
o.put("toString", ((NativeJavaObject) result).unwrap().toString());
|
||||
for (Object id : ((NativeJavaObject) result).getIds()) {
|
||||
if (id instanceof String) {
|
||||
final String name = (String) id;
|
||||
final Object value = ((NativeJavaObject) result).get(name, (NativeJavaObject) result);
|
||||
if (value != null && value instanceof NativeJavaMethod) {
|
||||
continue;
|
||||
}
|
||||
final String valueString = value == null ? null : safeUnwrap(value).toString();
|
||||
o.put(name, valueString);
|
||||
}
|
||||
}
|
||||
return new JSONObject().put(TYPE, "javaObject").put(VALUE, o);
|
||||
}
|
||||
|
||||
if (result instanceof NativeJavaMethod) {
|
||||
final JSONObject o = new JSONObject();
|
||||
o.put(TYPE, "method");
|
||||
o.put("name", ((NativeJavaMethod) result).getFunctionName());
|
||||
return o;
|
||||
}
|
||||
|
||||
if (result == null || result instanceof Undefined) {
|
||||
return new JSONObject().put(TYPE, "null");
|
||||
}
|
||||
|
||||
if (result instanceof Function) {
|
||||
final JSONObject o = new JSONObject();
|
||||
o.put(TYPE, "function");
|
||||
o.put(VALUE, Context.toString(result));
|
||||
return o;
|
||||
}
|
||||
|
||||
if (result instanceof ScriptableObject) {
|
||||
return new JSONObject()
|
||||
.put(TYPE, JSON)
|
||||
.put(
|
||||
VALUE,
|
||||
new JSONTokener(NativeJSON.stringify(mContext, mScope, result, null, null).toString())
|
||||
.nextValue());
|
||||
}
|
||||
|
||||
if (result instanceof Number) {
|
||||
return new JSONObject().put(TYPE, JSON).put(VALUE, result);
|
||||
}
|
||||
|
||||
return new JSONObject().put(TYPE, "unknown").put(VALUE, result.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
private static Object safeUnwrap(Object o) {
|
||||
if (o instanceof NativeJavaObject) {
|
||||
return ((NativeJavaObject) o).unwrap();
|
||||
}
|
||||
return o;
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console.iface;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.flipper.core.FlipperConnection;
|
||||
import com.facebook.flipper.core.FlipperObject;
|
||||
import com.facebook.flipper.core.FlipperReceiver;
|
||||
import com.facebook.flipper.core.FlipperResponder;
|
||||
import com.facebook.flipper.plugins.common.MainThreadFlipperReceiver;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Convenience class for adding console execution to a Flipper Plugin. Calling {@link
|
||||
* ConsoleCommandReceiver#listenForCommands(FlipperConnection, ScriptingEnvironment,
|
||||
* ContextProvider)} will add the necessary listeners for responding to command execution calls.
|
||||
*/
|
||||
public class ConsoleCommandReceiver {
|
||||
|
||||
/**
|
||||
* Incoming command execution calls may reference a context ID that means something to your
|
||||
* plugin. Implement {@link ContextProvider} to provide a mapping from context ID to java object.
|
||||
* This will allow your Flipper plugin to control the execution context of the command.
|
||||
*/
|
||||
public interface ContextProvider {
|
||||
@Nullable
|
||||
Object getObjectForId(String id);
|
||||
}
|
||||
|
||||
public static void listenForCommands(
|
||||
final FlipperConnection connection,
|
||||
final ScriptingEnvironment scriptingEnvironment,
|
||||
final ContextProvider contextProvider) {
|
||||
|
||||
final ScriptingSession session = scriptingEnvironment.startSession();
|
||||
final FlipperReceiver executeCommandReceiver =
|
||||
new MainThreadFlipperReceiver() {
|
||||
@Override
|
||||
public void onReceiveOnMainThread(FlipperObject params, FlipperResponder responder)
|
||||
throws Exception {
|
||||
final String command = params.getString("command");
|
||||
final String contextObjectId = params.getString("context");
|
||||
final Object contextObject = contextProvider.getObjectForId(contextObjectId);
|
||||
try {
|
||||
JSONObject o =
|
||||
contextObject == null
|
||||
? session.evaluateCommand(command)
|
||||
: session.evaluateCommand(command, contextObject);
|
||||
responder.success(new FlipperObject(o));
|
||||
} catch (Exception e) {
|
||||
responder.error(new FlipperObject.Builder().put("message", e.getMessage()).build());
|
||||
}
|
||||
}
|
||||
};
|
||||
final FlipperReceiver isEnabledReceiver =
|
||||
new FlipperReceiver() {
|
||||
@Override
|
||||
public void onReceive(FlipperObject params, FlipperResponder responder) throws Exception {
|
||||
responder.success(
|
||||
new FlipperObject.Builder()
|
||||
.put("isEnabled", scriptingEnvironment.isEnabled())
|
||||
.build());
|
||||
}
|
||||
};
|
||||
|
||||
connection.receive("executeCommand", executeCommandReceiver);
|
||||
connection.receive("isConsoleEnabled", isEnabledReceiver);
|
||||
}
|
||||
|
||||
public static void listenForCommands(
|
||||
FlipperConnection connection, ScriptingEnvironment scriptingEnvironment) {
|
||||
listenForCommands(connection, scriptingEnvironment, nullContextProvider);
|
||||
}
|
||||
|
||||
private static final ContextProvider nullContextProvider =
|
||||
new ContextProvider() {
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getObjectForId(String id) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console.iface;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class NullScriptingEnvironment implements ScriptingEnvironment {
|
||||
|
||||
@Override
|
||||
public ScriptingSession startSession() {
|
||||
return new NoOpScriptingSession();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerGlobalObject(String name, Object object) {}
|
||||
|
||||
static class NoOpScriptingSession implements ScriptingSession {
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript) throws JSONException {
|
||||
throw new UnsupportedOperationException("Console plugin not enabled in this app");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript, Object context) throws JSONException {
|
||||
return evaluateCommand(userScript);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console.iface;
|
||||
|
||||
public interface ScriptingEnvironment {
|
||||
|
||||
ScriptingSession startSession();
|
||||
|
||||
void registerGlobalObject(String name, Object object);
|
||||
|
||||
boolean isEnabled();
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console.iface;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public interface ScriptingSession {
|
||||
|
||||
JSONObject evaluateCommand(String userScript) throws JSONException;
|
||||
|
||||
JSONObject evaluateCommand(String userScript, Object context) throws JSONException;
|
||||
|
||||
void close();
|
||||
}
|
||||
@@ -21,9 +21,6 @@ import com.facebook.flipper.core.FlipperPlugin;
|
||||
import com.facebook.flipper.core.FlipperReceiver;
|
||||
import com.facebook.flipper.core.FlipperResponder;
|
||||
import com.facebook.flipper.plugins.common.MainThreadFlipperReceiver;
|
||||
import com.facebook.flipper.plugins.console.iface.ConsoleCommandReceiver;
|
||||
import com.facebook.flipper.plugins.console.iface.NullScriptingEnvironment;
|
||||
import com.facebook.flipper.plugins.console.iface.ScriptingEnvironment;
|
||||
import com.facebook.flipper.plugins.inspector.descriptors.ApplicationDescriptor;
|
||||
import com.facebook.flipper.plugins.inspector.descriptors.utils.AccessibilityUtil;
|
||||
import java.util.ArrayList;
|
||||
@@ -35,7 +32,6 @@ public class InspectorFlipperPlugin implements FlipperPlugin {
|
||||
private ApplicationWrapper mApplication;
|
||||
private DescriptorMapping mDescriptorMapping;
|
||||
private ObjectTracker mObjectTracker;
|
||||
private ScriptingEnvironment mScriptingEnvironment;
|
||||
private String mHighlightedId;
|
||||
private TouchOverlayView mTouchOverlay;
|
||||
private FlipperConnection mConnection;
|
||||
@@ -57,17 +53,9 @@ public class InspectorFlipperPlugin implements FlipperPlugin {
|
||||
}
|
||||
|
||||
public InspectorFlipperPlugin(Context context, DescriptorMapping descriptorMapping) {
|
||||
this(getAppContextFromContext(context), descriptorMapping, new NullScriptingEnvironment());
|
||||
}
|
||||
|
||||
public InspectorFlipperPlugin(
|
||||
Context context,
|
||||
DescriptorMapping descriptorMapping,
|
||||
ScriptingEnvironment scriptingEnvironment) {
|
||||
this(
|
||||
new ApplicationWrapper(getAppContextFromContext(context)),
|
||||
descriptorMapping,
|
||||
scriptingEnvironment,
|
||||
null);
|
||||
}
|
||||
|
||||
@@ -75,23 +63,10 @@ public class InspectorFlipperPlugin implements FlipperPlugin {
|
||||
Context context,
|
||||
DescriptorMapping descriptorMapping,
|
||||
@Nullable List<ExtensionCommand> extensions) {
|
||||
this(
|
||||
new ApplicationWrapper(getAppContextFromContext(context)),
|
||||
descriptorMapping,
|
||||
new NullScriptingEnvironment(),
|
||||
extensions);
|
||||
}
|
||||
|
||||
public InspectorFlipperPlugin(
|
||||
Context context,
|
||||
DescriptorMapping descriptorMapping,
|
||||
ScriptingEnvironment scriptingEnvironment,
|
||||
@Nullable List<ExtensionCommand> extensions) {
|
||||
|
||||
this(
|
||||
new ApplicationWrapper(getAppContextFromContext(context)),
|
||||
descriptorMapping,
|
||||
scriptingEnvironment,
|
||||
extensions);
|
||||
}
|
||||
|
||||
@@ -99,13 +74,11 @@ public class InspectorFlipperPlugin implements FlipperPlugin {
|
||||
InspectorFlipperPlugin(
|
||||
ApplicationWrapper wrapper,
|
||||
DescriptorMapping descriptorMapping,
|
||||
ScriptingEnvironment scriptingEnvironment,
|
||||
@Nullable List<ExtensionCommand> extensions) {
|
||||
mDescriptorMapping = descriptorMapping;
|
||||
|
||||
mObjectTracker = new ObjectTracker();
|
||||
mApplication = wrapper;
|
||||
mScriptingEnvironment = scriptingEnvironment;
|
||||
mExtensionCommands = extensions;
|
||||
mShowLithoAccessibilitySettings = false;
|
||||
}
|
||||
@@ -120,16 +93,6 @@ public class InspectorFlipperPlugin implements FlipperPlugin {
|
||||
mConnection = connection;
|
||||
mDescriptorMapping.onConnect(connection);
|
||||
|
||||
ConsoleCommandReceiver.listenForCommands(
|
||||
connection,
|
||||
mScriptingEnvironment,
|
||||
new ConsoleCommandReceiver.ContextProvider() {
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getObjectForId(String id) {
|
||||
return mObjectTracker.get(id);
|
||||
}
|
||||
});
|
||||
connection.receive("getRoot", mGetRoot);
|
||||
connection.receive("getAllNodes", mGetAllNodes);
|
||||
connection.receive("getNodes", mGetNodes);
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import com.facebook.flipper.core.FlipperObject;
|
||||
import com.facebook.flipper.testing.FlipperConnectionMock;
|
||||
import com.facebook.flipper.testing.FlipperResponderMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ConsoleSonarPluginTest {
|
||||
|
||||
FlipperConnectionMock connection;
|
||||
FlipperResponderMock responder;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
JavascriptEnvironment jsEnvironment = new JavascriptEnvironment();
|
||||
final ConsoleFlipperPlugin plugin = new ConsoleFlipperPlugin(jsEnvironment);
|
||||
connection = new FlipperConnectionMock();
|
||||
responder = new FlipperResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleExpressionShouldEvaluateCorrectly() throws Exception {
|
||||
|
||||
receiveScript("2 + 2");
|
||||
assertThat(
|
||||
responder.successes,
|
||||
hasItem(new FlipperObject.Builder().put("value", 4).put("type", "json").build()));
|
||||
}
|
||||
|
||||
private void receiveScript(String a) throws Exception {
|
||||
FlipperObject getValue = new FlipperObject.Builder().put("command", a).build();
|
||||
connection.receivers.get("executeCommand").onReceive(getValue, responder);
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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.flipper.plugins.console;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class JavascriptSessionTest {
|
||||
|
||||
ContextFactory mContextFactory = new ContextFactory();
|
||||
|
||||
@Test
|
||||
public void testSimpleExpressionsEvaluate() throws Exception {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(mContextFactory, Collections.<String, Object>emptyMap());
|
||||
JSONObject json = session.evaluateCommand("2+2-1");
|
||||
assertEquals(3, json.getInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatePersistsBetweenCommands() throws Exception {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(mContextFactory, Collections.<String, Object>emptyMap());
|
||||
session.evaluateCommand("var x = 10;");
|
||||
JSONObject json = session.evaluateCommand("x");
|
||||
assertEquals(10, json.getInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVariablesGetBoundCorrectly() throws Exception {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("a", 2);
|
||||
data.put("b", 2);
|
||||
JavascriptSession session = new JavascriptSession(mContextFactory, data);
|
||||
JSONObject json = session.evaluateCommand("a+b");
|
||||
assertEquals("json", json.getString("type"));
|
||||
assertEquals(4, json.getInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberEvaluation() throws Exception {
|
||||
assertEquals(4, evaluateWithNoGlobals("4").getInt("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEvaluation() throws Exception {
|
||||
assertEquals("hello", evaluateWithNoGlobals("\"hello\"").getString("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaObjectEvaluation() throws Exception {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("object", new HashMap<String, String>());
|
||||
JavascriptSession session = new JavascriptSession(mContextFactory, data);
|
||||
JSONObject json = session.evaluateCommand("object");
|
||||
assertEquals("javaObject", json.getString("type"));
|
||||
assertEquals("{}", json.getJSONObject("value").getString("toString"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaMethodEvaluation() throws Exception {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("object", new HashMap<String, String>());
|
||||
JavascriptSession session = new JavascriptSession(mContextFactory, data);
|
||||
JSONObject json = session.evaluateCommand("object.get");
|
||||
assertEquals("method", json.getString("type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsFunctionEvaluation() throws Exception {
|
||||
JSONObject json = evaluateWithNoGlobals("function() {}");
|
||||
assertEquals("function", json.getString("type"));
|
||||
assertEquals("function(){}", removeWhitespace(json.getString("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullEvaluation() throws Exception {
|
||||
assertEquals("null", evaluateWithNoGlobals("null").getString("type"));
|
||||
assertEquals("null", evaluateWithNoGlobals("undefined").getString("type"));
|
||||
}
|
||||
|
||||
private static String removeWhitespace(String input) {
|
||||
return input.replaceAll("\\s", "");
|
||||
}
|
||||
|
||||
private JSONObject evaluateWithNoGlobals(String input) throws Exception {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(mContextFactory, new HashMap<String, Object>());
|
||||
return session.evaluateCommand(input);
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,6 @@ import com.facebook.flipper.core.FlipperArray;
|
||||
import com.facebook.flipper.core.FlipperConnection;
|
||||
import com.facebook.flipper.core.FlipperDynamic;
|
||||
import com.facebook.flipper.core.FlipperObject;
|
||||
import com.facebook.flipper.plugins.console.iface.NullScriptingEnvironment;
|
||||
import com.facebook.flipper.plugins.console.iface.ScriptingEnvironment;
|
||||
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin.TouchOverlayView;
|
||||
import com.facebook.flipper.plugins.inspector.descriptors.ApplicationDescriptor;
|
||||
import com.facebook.flipper.testing.FlipperConnectionMock;
|
||||
@@ -45,7 +43,6 @@ public class InspectorFlipperPluginTest {
|
||||
private MockApplicationDescriptor mApplicationDescriptor;
|
||||
private DescriptorMapping mDescriptorMapping;
|
||||
private ApplicationWrapper mApp;
|
||||
private ScriptingEnvironment mScriptingEnvironment;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@@ -57,14 +54,13 @@ public class InspectorFlipperPluginTest {
|
||||
mApplicationDescriptor = new MockApplicationDescriptor();
|
||||
mDescriptorMapping.register(ApplicationWrapper.class, mApplicationDescriptor);
|
||||
mDescriptorMapping.register(TestNode.class, new TestNodeDescriptor());
|
||||
mScriptingEnvironment = new NullScriptingEnvironment();
|
||||
mApp = Mockito.spy(new ApplicationWrapper(app));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnConnect() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnection connection = new FlipperConnectionMock();
|
||||
|
||||
plugin.onConnect(connection);
|
||||
@@ -74,7 +70,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testOnDisconnect() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnection connection = new FlipperConnectionMock();
|
||||
|
||||
plugin.onConnect(connection);
|
||||
@@ -85,7 +81,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testGetRoot() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -112,7 +108,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testGetNodes() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -149,7 +145,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testGetNodesThatDontExist() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -175,7 +171,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testSetData() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -211,7 +207,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testSetHighlighted() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -240,7 +236,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testHitTest() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
@@ -278,7 +274,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testSetSearchActive() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
@@ -300,7 +296,7 @@ public class InspectorFlipperPluginTest {
|
||||
@Test
|
||||
public void testNullChildThrows() throws Exception {
|
||||
final InspectorFlipperPlugin plugin =
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, mScriptingEnvironment, null);
|
||||
new InspectorFlipperPlugin(mApp, mDescriptorMapping, null);
|
||||
final FlipperResponderMock responder = new FlipperResponderMock();
|
||||
final FlipperConnectionMock connection = new FlipperConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
Reference in New Issue
Block a user