Exemplo n.º 1
0
  /** Tests simulation of successful {@link XMLHttpRequest} through {@link MockXhr}. */
  public void testSuccess() {
    final String url = "no-place";
    final String data = "my-data";
    final String contentType = "my-content-type";

    final TestDelegate delegate =
        new TestDelegate("POST", url, data, contentType) {
          @Override
          protected void issueResponse(XMLHttpRequest xhr, String data) {
            respond(xhr, XMLHttpRequest.DONE, 200, "Yay!", "response-data");
          }
        };

    final MockXhr.Restorer restorer = MockXhr.setDelegate(WindowExt.getHostWindow(), delegate);
    try {
      final boolean[] didSucceed = new boolean[1];
      Xhr.post(
          WindowExt.getHostWindow(),
          url,
          data,
          contentType,
          new Xhr.XhrCallback() {
            public void onFail(XMLHttpRequest xhr) {
              fail("onFail should not be called.");
            }

            public void onSuccess(XMLHttpRequest xhr) {
              assertEquals("response-data", xhr.getResponseText());
              didSucceed[0] = true;
            }
          });
      assertTrue("onCreate not called", delegate.didCreate);
      assertTrue("onOpen not called", delegate.didOpen);
      assertTrue("onSend not called", delegate.didSend);
      assertTrue("onSetHeader not called", delegate.didSetHeader);
      assertTrue("onSuccess not called", didSucceed[0]);
    } finally {
      restorer.restore();
    }

    // Setup/Restore one more time to ensure it was properly restored the first
    // time.
    MockXhr.setDelegate(
            WindowExt.getHostWindow(),
            new MockXhr.Delegate() {
              @Override
              public void onSend(XMLHttpRequest xhr, String data) {}
            })
        .restore();
  }
Exemplo n.º 2
0
  /** Tests simulation of failed {@link XMLHttpRequest} through {@link MockXhr}. */
  public void testFailure() {
    final String url = "no-place-2";
    final String data = "my-data-2";
    final String contentType = "my-content-type-2";

    final TestDelegate delegate =
        new TestDelegate("POST", url, data, contentType) {
          @Override
          protected void issueResponse(XMLHttpRequest xhr, String data) {
            respond(xhr, XMLHttpRequest.DONE, 500, "", "");
          }
        };

    MockXhr.Restorer restorer = MockXhr.setDelegate(WindowExt.getHostWindow(), delegate);
    try {
      final boolean[] didFail = new boolean[1];
      Xhr.post(
          WindowExt.getHostWindow(),
          url,
          data,
          contentType,
          new Xhr.XhrCallback() {
            public void onFail(XMLHttpRequest xhr) {
              didFail[0] = true;
            }

            public void onSuccess(XMLHttpRequest xhr) {
              fail("onSuccess should not be called.");
            }
          });
      assertTrue("onCreate not called", delegate.didCreate);
      assertTrue("onOpen not called", delegate.didOpen);
      assertTrue("onSend not called", delegate.didSend);
      assertTrue("onSetHeader not called", delegate.didSetHeader);
      assertTrue("onFail not called", didFail[0]);
    } finally {
      restorer.restore();
    }
  }