Sunday, March 11, 2018

Re: Testing native @JsType in GWT tests

JsInterop doesn't work in legacy DevMode; you have to run the tests in production mode: https://gwt-maven-plugin.github.io/gwt-maven-plugin/test-mojo.html#productionMode
(this is the default in GWT 2.8, but Mojo's Maven plugin for GWT forces legacy DevMode by default; may I suggest that you switch to net.ltgt.gwt.maven:gwt-maven-plugin as suggested at https://gwt-maven-plugin.github.io/gwt-maven-plugin/ ? you'll have fewer of such surprises –disclosure: I'm the author of that latter plugin, and former maintainer of Mojo's one)

On Sunday, March 11, 2018 at 8:12:39 PM UTC+1, Mincong Huang wrote:
Hi Vassilis,

Thanks for your help. After having specified the TOP_WINDOW as you suggested, the test
passed in IntelliJ IDEA. However, it is still failing via Maven plugin [1]. I don't understand any
more...

Mincong


On Sun, Mar 11, 2018 at 6:47 PM, Vassilis Virvilis <vasvir2@gmail.com> wrote:
I would guess that script injector injects by default inside gwt frame.

You need to specify TOP_WINDOW because jsinterop are mapped to $wnd by default.

Something like that (can't test right now)

ScriptInjector.fromString(js).setWindow(ScriptInjector.TOP_WINDOW).inject();


If you do this then you need to change your jsni test to

  var p = new $wnd.ns.Person();

 Hope that helps.

    Vassilis


On Sun, Mar 11, 2018 at 3:44 PM, Mincong Huang <mincong.h@gmail.com> wrote:
Hi,

I've a simple JavaScript:

var ns = {
  Person: function() {
    this.sayHello = function() {
      return 'Hi';
    };
  }
}

And I used it to test JsInterop annotation `@JsType` in my GWT test. I injected JS code to
GWTTestCase and tried to retrieve the definition via `@JsType`. However, it doesn't
work, it seems that GWT cannot find the method (see Travis CI [1]):

> [INFO] [ERROR] function @io.mincongh.client.interop.GwtTestJsInterop$Person::sayHello() NOT FOUND, thisObj: com.google.gwt.dev.shell.JavaObject@43182ce3, methodName: @io.mincongh.client.interop.GwtTestJsInterop$Person::sayHello()
> [INFO] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 11.342 sec <<< FAILURE!

Actually, my code is pretty simple:

public class GwtTestJsInterop extends GWTTestCase {
  @Override
  public void gwtSetUp() {
    String js = "var ns = { Person: function() { this.sayHello = function() { return 'Hi'; }; } }";
    ScriptInjector.fromString(js).inject();
  }

  @Override
  public String getModuleName() {
    return R.JUNIT_MODULE;
  }

  public void testJsni() {
    assertEquals("Hi", sayHello());
  }

  private native String sayHello() /*-{
    var p = new ns.Person();
    return p.sayHello();
  }-*/;

  public void testJsType() {
    Person p = new Person();
    String words = p.sayHello();
    assertEquals("Hi", words);
  }

  @JsType(namespace = "ns", isNative = true)
  private static class Person {
    public native String sayHello();
  }
}


Note: The 2nd test method, `testJsni()`, runs successfully, so the JS should be
injected correctly. I also have the flag `generateJsInteropExports` enabled in the
Maven GWT plugin in the POM [2]. So what am I missing?

I'm using GWT 2.8.2 and Maven GWT plugin 2.8.2. Any help will be appreciated!

Thank you,
Mincong HUANG

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.



--
Vassilis Virvilis

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment