Permalink
Browse files

Set webViewString directly to bypass event from blocks (#1320)

Setting the WebViewString from blocks will also trigger a
WebViewStringChange event. This forces users to have to work around
the extra change event when they are typically expecting the change
from within JavaScript code only. This change sets the internal value
directly to bypass firing the event.

Change-Id: Ic8f38bfb34faad1f67ee0bcc29289417ef780eb3
  • Loading branch information...
ewpatton authored and jisqyv committed Aug 12, 2018
1 parent 37c4a0c commit 2869a13e01c1fbcfdf5e27640c15ca36e7cde912
@@ -153,7 +153,7 @@ public String WebViewString() {
*/
@SimpleProperty(category = PropertyCategory.BEHAVIOR)
public void WebViewString(String newString) {
wvInterface.setWebViewString(newString);
wvInterface.setWebViewStringFromBlocks(newString);
}

@Override
@@ -484,6 +484,10 @@ public void run() {
});
}

public void setWebViewStringFromBlocks(final String newString) {
webViewString = newString;
}

}
}

@@ -0,0 +1,36 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright © 2018 Massachusetts Institute of Technology, All rights reserved.
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.components.runtime;

import com.google.appinventor.components.runtime.shadows.ShadowEventDispatcher;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class WebViewerTest extends RobolectricTestBase {

private WebViewer webViewer;

@Before
public void setUp() {
super.setUp();
webViewer = new WebViewer(getForm());
}

/**
* Tests that setting a WebViewer's WebViewString property does not cause
*/
@Test
public void testWebViewStringChange() {
final String TEST_STRING = "teststring";
webViewer.WebViewString(TEST_STRING);
runAllEvents();
ShadowEventDispatcher.assertEventNotFired(webViewer, "WebViewStringChange");
assertEquals(TEST_STRING, webViewer.WebViewString());
}

}
@@ -7,7 +7,7 @@

import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.EventDispatcher;
import org.easymock.internal.AssertionErrorWrapper;
import org.junit.Assert;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@@ -60,6 +60,23 @@ public static void assertEventFired(Component component, String eventName, Objec
throw new AssertionError(String.format("Component %s did not receive event %s", component, eventName));
}

/**
* Checks whether or not the given {@code eventName} has fired for {@code component}. If so, the
* test fails.
* @param component The component to check for events
* @param eventName An event name to check that should not have fired
*/
public static void assertEventNotFired(Component component, String eventName) {
Set<EventWithArgs> events = firedEvents.get(component);
if (events != null) {
for (EventWithArgs e : events) {
if (e.eventName.equals(eventName)) {
Assert.fail("Expected " + eventName + " of " + component + " to not fire, but it did.");
}
}
}
}

public static void assertEventFiredAny(Component component, String eventName) {
Set<EventWithArgs> events = firedEvents.get(component);
if (events != null) {

0 comments on commit 2869a13

Please sign in to comment.