Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
49
android/tests/plugins/console/ConsoleSonarPluginTest.java
Normal file
49
android/tests/plugins/console/ConsoleSonarPluginTest.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.console;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.testing.SonarConnectionMock;
|
||||
import com.facebook.sonar.testing.SonarResponderMock;
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.class)
|
||||
public class ConsoleSonarPluginTest {
|
||||
|
||||
SonarConnectionMock connection;
|
||||
SonarResponderMock responder;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
JavascriptEnvironment jsEnvironment = new JavascriptEnvironment();
|
||||
final ConsoleSonarPlugin plugin = new ConsoleSonarPlugin(jsEnvironment);
|
||||
connection = new SonarConnectionMock();
|
||||
responder = new SonarResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleExpressionShouldEvaluateCorrectly() throws Exception {
|
||||
|
||||
receiveScript("2 + 2");
|
||||
assertThat(
|
||||
responder.successes,
|
||||
hasItem(new SonarObject.Builder().put("value", 4).put("type", "json").build()));
|
||||
}
|
||||
|
||||
private void receiveScript(String a) throws Exception {
|
||||
SonarObject getValue = new SonarObject.Builder().put("command", a).build();
|
||||
connection.receivers.get("executeCommand").onReceive(getValue, responder);
|
||||
}
|
||||
}
|
||||
109
android/tests/plugins/console/JavascriptSessionTest.java
Normal file
109
android/tests/plugins/console/JavascriptSessionTest.java
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.console;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.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 {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(
|
||||
mContextFactory,
|
||||
ImmutableMap.<String, Object>of(
|
||||
"a", 2,
|
||||
"b", 2));
|
||||
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 {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(
|
||||
mContextFactory,
|
||||
ImmutableMap.<String, Object>of("object", new HashMap<String, String>()));
|
||||
JSONObject json = session.evaluateCommand("object");
|
||||
assertEquals("javaObject", json.getString("type"));
|
||||
assertEquals("{}", json.getJSONObject("value").getString("toString"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavaMethodEvaluation() throws Exception {
|
||||
JavascriptSession session =
|
||||
new JavascriptSession(
|
||||
mContextFactory,
|
||||
ImmutableMap.<String, Object>of("object", new HashMap<String, String>()));
|
||||
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);
|
||||
}
|
||||
}
|
||||
90
android/tests/plugins/inspector/ApplicationWrapperTest.java
Normal file
90
android/tests/plugins/inspector/ApplicationWrapperTest.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.inspector;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.app.Application.ActivityLifecycleCallbacks;
|
||||
import android.os.Bundle;
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.class)
|
||||
public class ApplicationWrapperTest {
|
||||
|
||||
private ApplicationWrapper mWrapper;
|
||||
private ActivityLifecycleCallbacks mCallbacks;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Application app = Mockito.mock(Application.class);
|
||||
Mockito.doAnswer(
|
||||
new Answer() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
mCallbacks = (ActivityLifecycleCallbacks) invocation.getArguments()[0];
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.when(app)
|
||||
.registerActivityLifecycleCallbacks(Mockito.any(ActivityLifecycleCallbacks.class));
|
||||
|
||||
mWrapper = new ApplicationWrapper(app);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivityCreated() {
|
||||
final Activity activity1 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity1, Mockito.mock(Bundle.class));
|
||||
|
||||
final Activity activity2 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity2, Mockito.mock(Bundle.class));
|
||||
|
||||
assertThat(mWrapper.getActivityStack().size(), equalTo(2));
|
||||
assertThat(mWrapper.getActivityStack().get(0), equalTo(activity1));
|
||||
assertThat(mWrapper.getActivityStack().get(1), equalTo(activity2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivityPaused() {
|
||||
final Activity activity1 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity1, Mockito.mock(Bundle.class));
|
||||
|
||||
final Activity activity2 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity2, Mockito.mock(Bundle.class));
|
||||
|
||||
mCallbacks.onActivityPaused(activity2);
|
||||
|
||||
assertThat(mWrapper.getActivityStack().size(), equalTo(2));
|
||||
assertThat(mWrapper.getActivityStack().get(0), equalTo(activity1));
|
||||
assertThat(mWrapper.getActivityStack().get(1), equalTo(activity2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinishingActivityPaused() {
|
||||
final Activity activity1 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity1, Mockito.mock(Bundle.class));
|
||||
|
||||
final Activity activity2 = Mockito.mock(Activity.class);
|
||||
mCallbacks.onActivityCreated(activity2, Mockito.mock(Bundle.class));
|
||||
|
||||
Mockito.when(activity2.isFinishing()).thenReturn(true);
|
||||
mCallbacks.onActivityPaused(activity2);
|
||||
|
||||
assertThat(mWrapper.getActivityStack().size(), equalTo(1));
|
||||
assertThat(mWrapper.getActivityStack().get(0), equalTo(activity1));
|
||||
}
|
||||
}
|
||||
129
android/tests/plugins/inspector/DescriptorMappingTest.java
Normal file
129
android/tests/plugins/inspector/DescriptorMappingTest.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.inspector;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import com.facebook.sonar.core.SonarConnection;
|
||||
import com.facebook.sonar.core.SonarDynamic;
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.testing.SonarConnectionMock;
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.class)
|
||||
public class DescriptorMappingTest {
|
||||
|
||||
private class TestClass {}
|
||||
|
||||
private class TestSubClass extends TestClass {}
|
||||
|
||||
private class TestDescriptor<T> extends NodeDescriptor<T> {
|
||||
@Override
|
||||
public void init(T node) {}
|
||||
|
||||
@Override
|
||||
public String getId(T node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName(T node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount(T node) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getChildAt(T node, int index) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Named<SonarObject>> getData(T node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(T node, String[] path, SonarDynamic value) throws Exception {}
|
||||
|
||||
@Override
|
||||
public List<Named<String>> getAttributes(T node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHighlighted(T node, boolean selected) {}
|
||||
|
||||
@Override
|
||||
public void hitTest(T node, Touch touch) {}
|
||||
|
||||
@Override
|
||||
public String getDecoration(T obj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(String query, T obj) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescriptorForRegisteredClass() {
|
||||
final DescriptorMapping descriptorMapping = new DescriptorMapping();
|
||||
final NodeDescriptor descriptor1 = new TestDescriptor<>();
|
||||
final NodeDescriptor descriptor2 = new TestDescriptor<>();
|
||||
|
||||
descriptorMapping.register(TestClass.class, descriptor1);
|
||||
descriptorMapping.register(TestSubClass.class, descriptor2);
|
||||
|
||||
assertThat(descriptorMapping.descriptorForClass(TestSubClass.class), equalTo(descriptor2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescriptorForRegisteredSuperClass() {
|
||||
final DescriptorMapping descriptorMapping = new DescriptorMapping();
|
||||
final NodeDescriptor descriptor = new TestDescriptor<>();
|
||||
|
||||
descriptorMapping.register(TestClass.class, descriptor);
|
||||
|
||||
assertThat(descriptorMapping.descriptorForClass(TestSubClass.class), equalTo(descriptor));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnConnect() {
|
||||
final DescriptorMapping descriptorMapping = new DescriptorMapping();
|
||||
final NodeDescriptor descriptor = new TestDescriptor<>();
|
||||
descriptorMapping.register(TestClass.class, descriptor);
|
||||
|
||||
final SonarConnection connection = new SonarConnectionMock();
|
||||
descriptorMapping.onConnect(connection);
|
||||
|
||||
assertThat(descriptor.connected(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnDisconnect() {
|
||||
final DescriptorMapping descriptorMapping = new DescriptorMapping();
|
||||
final NodeDescriptor descriptor = new TestDescriptor<>();
|
||||
descriptorMapping.register(TestClass.class, descriptor);
|
||||
|
||||
final SonarConnection connection = new SonarConnectionMock();
|
||||
descriptorMapping.onConnect(connection);
|
||||
descriptorMapping.onDisconnect();
|
||||
|
||||
assertThat(descriptor.connected(), equalTo(false));
|
||||
}
|
||||
}
|
||||
424
android/tests/plugins/inspector/InspectorSonarPluginTest.java
Normal file
424
android/tests/plugins/inspector/InspectorSonarPluginTest.java
Normal file
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.inspector;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import android.app.Application;
|
||||
import android.graphics.Rect;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
import com.facebook.sonar.core.SonarArray;
|
||||
import com.facebook.sonar.core.SonarConnection;
|
||||
import com.facebook.sonar.core.SonarDynamic;
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.plugins.console.iface.NullScriptingEnvironment;
|
||||
import com.facebook.sonar.plugins.console.iface.ScriptingEnvironment;
|
||||
import com.facebook.sonar.plugins.inspector.InspectorSonarPlugin.TouchOverlayView;
|
||||
import com.facebook.sonar.plugins.inspector.descriptors.ApplicationDescriptor;
|
||||
import com.facebook.sonar.testing.SonarConnectionMock;
|
||||
import com.facebook.sonar.testing.SonarResponderMock;
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.class)
|
||||
public class InspectorSonarPluginTest {
|
||||
|
||||
private MockApplicationDescriptor mApplicationDescriptor;
|
||||
private DescriptorMapping mDescriptorMapping;
|
||||
private ApplicationWrapper mApp;
|
||||
private ScriptingEnvironment mScriptingEnvironment;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Application app = Mockito.spy(RuntimeEnvironment.application);
|
||||
Mockito.when(app.getApplicationContext()).thenReturn(app);
|
||||
Mockito.when(app.getPackageName()).thenReturn("com.facebook.sonar");
|
||||
|
||||
mDescriptorMapping = new DescriptorMapping();
|
||||
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 InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnection connection = new SonarConnectionMock();
|
||||
|
||||
plugin.onConnect(connection);
|
||||
assertThat(mApplicationDescriptor.connected(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnDisconnect() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnection connection = new SonarConnectionMock();
|
||||
|
||||
plugin.onConnect(connection);
|
||||
plugin.onDisconnect();
|
||||
assertThat(mApplicationDescriptor.connected(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRoot() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
mApplicationDescriptor.root = root;
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
|
||||
assertThat(
|
||||
responder.successes,
|
||||
hasItem(
|
||||
new SonarObject.Builder()
|
||||
.put("id", "com.facebook.sonar")
|
||||
.put("name", "com.facebook.sonar")
|
||||
.put("data", new SonarObject.Builder())
|
||||
.put("children", new SonarArray.Builder().put("test"))
|
||||
.put("attributes", new SonarArray.Builder())
|
||||
.put("decoration", (String) null)
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodes() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
root.name = "test";
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
plugin.mGetNodes.onReceive(
|
||||
new SonarObject.Builder().put("ids", new SonarArray.Builder().put("test")).build(),
|
||||
responder);
|
||||
|
||||
assertThat(
|
||||
responder.successes,
|
||||
hasItem(
|
||||
new SonarObject.Builder()
|
||||
.put(
|
||||
"elements",
|
||||
new SonarArray.Builder()
|
||||
.put(
|
||||
new SonarObject.Builder()
|
||||
.put("id", "test")
|
||||
.put("name", "test")
|
||||
.put("data", new SonarObject.Builder())
|
||||
.put("children", new SonarArray.Builder())
|
||||
.put("attributes", new SonarArray.Builder())
|
||||
.put("decoration", (String) null)))
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNodesThatDontExist() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
plugin.mGetNodes.onReceive(
|
||||
new SonarObject.Builder().put("ids", new SonarArray.Builder().put("notest")).build(),
|
||||
responder);
|
||||
|
||||
assertThat(
|
||||
responder.errors,
|
||||
hasItem(
|
||||
new SonarObject.Builder()
|
||||
.put("message", "No node with given id")
|
||||
.put("id", "notest")
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetData() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
root.data = new SonarObject.Builder().put("prop", "value").build();
|
||||
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
plugin.mSetData.onReceive(
|
||||
new SonarObject.Builder()
|
||||
.put("id", "test")
|
||||
.put("path", new SonarArray.Builder().put("data"))
|
||||
.put("value", new SonarObject.Builder().put("prop", "updated_value"))
|
||||
.build(),
|
||||
responder);
|
||||
|
||||
assertThat(root.data.getString("prop"), equalTo("updated_value"));
|
||||
assertThat(
|
||||
connection.sent.get("invalidate"),
|
||||
hasItem(
|
||||
new SonarObject.Builder()
|
||||
.put(
|
||||
"nodes",
|
||||
new SonarArray.Builder()
|
||||
.put(new SonarObject.Builder().put("id", "test").build())
|
||||
.build())
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHighlighted() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
plugin.mSetHighlighted.onReceive(
|
||||
new SonarObject.Builder().put("id", "com.facebook.sonar").build(), responder);
|
||||
|
||||
assertThat(mApplicationDescriptor.highlighted, equalTo(true));
|
||||
|
||||
plugin.mSetHighlighted.onReceive(
|
||||
new SonarObject.Builder().put("id", "test").build(), responder);
|
||||
|
||||
assertThat(mApplicationDescriptor.highlighted, equalTo(false));
|
||||
assertThat(root.highlighted, equalTo(true));
|
||||
|
||||
plugin.onDisconnect();
|
||||
|
||||
assertThat(root.highlighted, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHitTest() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode one = new TestNode();
|
||||
one.id = "1";
|
||||
one.bounds.set(5, 5, 20, 20);
|
||||
|
||||
final TestNode two = new TestNode();
|
||||
two.id = "2";
|
||||
two.bounds.set(20, 20, 100, 100);
|
||||
|
||||
final TestNode three = new TestNode();
|
||||
three.id = "3";
|
||||
three.bounds.set(0, 0, 20, 20);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
root.children.add(one);
|
||||
root.children.add(two);
|
||||
root.children.add(three);
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.hitTest(10, 10);
|
||||
|
||||
assertThat(
|
||||
connection.sent.get("select"),
|
||||
hasItem(
|
||||
new SonarObject.Builder()
|
||||
.put(
|
||||
"path", new SonarArray.Builder().put("com.facebook.sonar").put("test").put("3"))
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSearchActive() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final ViewGroup decorView = Mockito.spy(new FrameLayout(mApp.getApplication()));
|
||||
Mockito.when(mApp.getViewRoots()).thenReturn(Arrays.<View>asList(decorView));
|
||||
|
||||
plugin.mSetSearchActive.onReceive(
|
||||
new SonarObject.Builder().put("active", true).build(), responder);
|
||||
|
||||
Mockito.verify(decorView, Mockito.times(1)).addView(Mockito.any(TouchOverlayView.class));
|
||||
|
||||
plugin.mSetSearchActive.onReceive(
|
||||
new SonarObject.Builder().put("active", false).build(), responder);
|
||||
|
||||
Mockito.verify(decorView, Mockito.times(1)).removeView(Mockito.any(TouchOverlayView.class));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void testNullChildThrows() throws Exception {
|
||||
final InspectorSonarPlugin plugin =
|
||||
new InspectorSonarPlugin(mApp, mDescriptorMapping, mScriptingEnvironment);
|
||||
final SonarResponderMock responder = new SonarResponderMock();
|
||||
final SonarConnectionMock connection = new SonarConnectionMock();
|
||||
plugin.onConnect(connection);
|
||||
|
||||
final TestNode root = new TestNode();
|
||||
root.id = "test";
|
||||
root.name = "test";
|
||||
root.children = new ArrayList<>();
|
||||
root.children.add(null);
|
||||
mApplicationDescriptor.root = root;
|
||||
|
||||
plugin.mGetRoot.onReceive(null, responder);
|
||||
plugin.mGetNodes.onReceive(
|
||||
new SonarObject.Builder().put("ids", new SonarArray.Builder().put("test")).build(),
|
||||
responder);
|
||||
}
|
||||
|
||||
private class TestNode {
|
||||
String id;
|
||||
String name;
|
||||
List<TestNode> children = new ArrayList<>();
|
||||
SonarObject data;
|
||||
List<Named<String>> atttributes = new ArrayList<>();
|
||||
String decoration;
|
||||
boolean highlighted;
|
||||
Rect bounds = new Rect();
|
||||
}
|
||||
|
||||
private class TestNodeDescriptor extends NodeDescriptor<TestNode> {
|
||||
|
||||
@Override
|
||||
public void init(TestNode node) {}
|
||||
|
||||
@Override
|
||||
public String getId(TestNode node) {
|
||||
return node.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName(TestNode node) {
|
||||
return node.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount(TestNode node) {
|
||||
return node.children.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getChildAt(TestNode node, int index) {
|
||||
return node.children.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Named<SonarObject>> getData(TestNode node) {
|
||||
return Collections.singletonList(new Named<>("data", node.data));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(TestNode node, String[] path, SonarDynamic value) throws Exception {
|
||||
if (path[0].equals("data")) {
|
||||
node.data = value.asObject();
|
||||
}
|
||||
invalidate(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Named<String>> getAttributes(TestNode node) {
|
||||
return node.atttributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHighlighted(TestNode node, boolean selected) {
|
||||
node.highlighted = selected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hitTest(TestNode node, Touch touch) {
|
||||
for (int i = node.children.size() - 1; i >= 0; i--) {
|
||||
final TestNode child = node.children.get(i);
|
||||
final Rect bounds = child.bounds;
|
||||
if (touch.containedIn(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
|
||||
touch.continueWithOffset(i, bounds.left, bounds.top);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
touch.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDecoration(TestNode node) {
|
||||
return node.decoration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(String query, TestNode node) {
|
||||
return getName(node).contains(query);
|
||||
}
|
||||
}
|
||||
|
||||
private class MockApplicationDescriptor extends ApplicationDescriptor {
|
||||
TestNode root;
|
||||
boolean highlighted;
|
||||
|
||||
@Override
|
||||
public int getChildCount(ApplicationWrapper node) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getChildAt(ApplicationWrapper node, int index) {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHighlighted(ApplicationWrapper node, boolean selected) {
|
||||
highlighted = selected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hitTest(ApplicationWrapper node, Touch touch) {
|
||||
touch.continueWithOffset(0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* 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.sonar.plugins.inspector.descriptors;
|
||||
|
||||
import static android.view.View.MeasureSpec.EXACTLY;
|
||||
import static android.view.View.MeasureSpec.makeMeasureSpec;
|
||||
import static org.mockito.Matchers.any;
|
||||
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
import com.facebook.sonar.plugins.inspector.Touch;
|
||||
import com.facebook.testing.robolectric.v3.WithTestDefaultsRunner;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(WithTestDefaultsRunner.class)
|
||||
public class ViewGroupDescriptorTest {
|
||||
|
||||
@Test
|
||||
public void testHitTestVisibleChild() {
|
||||
final ViewGroupDescriptor descriptor = new ViewGroupDescriptor();
|
||||
|
||||
final ViewGroup root = new FrameLayout(RuntimeEnvironment.application);
|
||||
final View child = new View(RuntimeEnvironment.application);
|
||||
root.addView(child);
|
||||
|
||||
root.measure(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY));
|
||||
root.layout(0, 0, 100, 100);
|
||||
|
||||
final Touch touch = Mockito.mock(Touch.class);
|
||||
Mockito.when(touch.containedIn(any(int.class), any(int.class), any(int.class), any(int.class)))
|
||||
.thenReturn(true);
|
||||
descriptor.hitTest(root, touch);
|
||||
Mockito.verify(touch, Mockito.times(1)).continueWithOffset(0, 0, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHitTestInvisibleChild() {
|
||||
final ViewGroupDescriptor descriptor = new ViewGroupDescriptor();
|
||||
|
||||
final ViewGroup root = new FrameLayout(RuntimeEnvironment.application);
|
||||
final View child = new View(RuntimeEnvironment.application);
|
||||
child.setVisibility(View.GONE);
|
||||
root.addView(child);
|
||||
|
||||
root.measure(makeMeasureSpec(100, EXACTLY), makeMeasureSpec(100, EXACTLY));
|
||||
root.layout(0, 0, 100, 100);
|
||||
|
||||
final Touch touch = Mockito.mock(Touch.class);
|
||||
Mockito.when(touch.containedIn(any(int.class), any(int.class), any(int.class), any(int.class)))
|
||||
.thenReturn(true);
|
||||
descriptor.hitTest(root, touch);
|
||||
Mockito.verify(touch, Mockito.times(1)).finish();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user