Sunday, March 11, 2018

Re: Testing native @JsType in GWT tests

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.

No comments:

Post a Comment