@SmallTest
 @Feature({"Cronet"})
 // Tests that creating a ByteBufferUploadProvider using a byte array with an
 // offset gives a ByteBuffer with position 0. crbug.com/603124.
 public void testCreateByteBufferUploadWithArrayOffset() throws Exception {
   TestUrlRequestCallback callback = new TestUrlRequestCallback();
   // This URL will trigger a rewind().
   UrlRequest.Builder builder =
       new UrlRequest.Builder(
           NativeTestServer.getRedirectToEchoBody(),
           callback,
           callback.getExecutor(),
           mTestFramework.mCronetEngine);
   builder.addHeader("Content-Type", "useless/string");
   byte[] uploadData = LOREM.getBytes("UTF-8");
   int offset = 5;
   byte[] uploadDataWithPadding = new byte[uploadData.length + offset];
   System.arraycopy(uploadData, 0, uploadDataWithPadding, offset, uploadData.length);
   UploadDataProvider dataProvider =
       UploadDataProviders.create(uploadDataWithPadding, offset, uploadData.length);
   assertEquals(uploadData.length, dataProvider.getLength());
   builder.setUploadDataProvider(dataProvider, callback.getExecutor());
   UrlRequest urlRequest = builder.build();
   urlRequest.start();
   callback.blockForDone();
   assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
   assertEquals(LOREM, callback.mResponseAsString);
 }
Exemplo n.º 2
0
  @SmallTest
  @Feature({"Cronet"})
  public void testSimpleGet() throws Exception {
    String url = NativeTestServer.getEchoMethodURL();
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    callback.setAutoAdvance(false);
    UrlRequest.Builder builder =
        new UrlRequest.Builder(url, callback, callback.getExecutor(), mTestFramework.mCronetEngine);
    UrlRequest urlRequest = builder.build();
    // Calling before request is started should give Status.INVALID,
    // since the native adapter is not created.
    TestStatusListener statusListener0 = new TestStatusListener();
    urlRequest.getStatus(statusListener0);
    statusListener0.waitUntilOnStatusCalled();
    assertTrue(statusListener0.mOnStatusCalled);
    assertEquals(Status.INVALID, statusListener0.mStatus);

    urlRequest.start();

    // Should receive a valid status.
    TestStatusListener statusListener1 = new TestStatusListener();
    urlRequest.getStatus(statusListener1);
    statusListener1.waitUntilOnStatusCalled();
    assertTrue(statusListener1.mOnStatusCalled);
    assertTrue("Status is :" + statusListener1.mStatus, statusListener1.mStatus >= Status.IDLE);
    assertTrue(
        "Status is :" + statusListener1.mStatus,
        statusListener1.mStatus <= Status.READING_RESPONSE);

    callback.waitForNextStep();
    assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep);
    callback.startNextRead(urlRequest);

    // Should receive a valid status.
    TestStatusListener statusListener2 = new TestStatusListener();
    urlRequest.getStatus(statusListener2);
    statusListener2.waitUntilOnStatusCalled();
    assertTrue(statusListener2.mOnStatusCalled);
    assertTrue(statusListener1.mStatus >= Status.IDLE);
    assertTrue(statusListener1.mStatus <= Status.READING_RESPONSE);

    callback.waitForNextStep();
    assertEquals(ResponseStep.ON_READ_COMPLETED, callback.mResponseStep);

    callback.startNextRead(urlRequest);
    callback.blockForDone();

    // Calling after request done should give Status.INVALID, since
    // the native adapter is destroyed.
    TestStatusListener statusListener3 = new TestStatusListener();
    urlRequest.getStatus(statusListener3);
    statusListener3.waitUntilOnStatusCalled();
    assertTrue(statusListener3.mOnStatusCalled);
    assertEquals(Status.INVALID, statusListener3.mStatus);

    assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
    assertEquals("GET", callback.mResponseAsString);
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testShutdown() throws Exception {
    mActivity = launchCronetTestApp();
    TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
    // Block listener when response starts to verify that shutdown fails
    // if there are active requests.
    listener.setAutoAdvance(false);
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
    UrlRequest urlRequest = urlRequestBuilder.build();
    urlRequest.start();
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }

    listener.waitForNextStep();
    assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }
    listener.startNextRead(urlRequest);

    listener.waitForNextStep();
    assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep);
    try {
      mActivity.mCronetEngine.shutdown();
      fail("Should throw an exception");
    } catch (Exception e) {
      assertEquals("Cannot shutdown with active requests.", e.getMessage());
    }

    // May not have read all the data, in theory. Just enable auto-advance
    // and finish the request.
    listener.setAutoAdvance(true);
    listener.startNextRead(urlRequest);
    listener.blockForDone();
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testRealTimeNetworkQualityObservations() throws Exception {
   mActivity = launchCronetTestApp();
   TestExecutor testExecutor = new TestExecutor();
   TestNetworkQualityListener networkQualityListener = new TestNetworkQualityListener();
   mActivity.mCronetEngine.enableNetworkQualityEstimatorForTesting(true, true, testExecutor);
   mActivity.mCronetEngine.addRttListener(networkQualityListener);
   mActivity.mCronetEngine.addThroughputListener(networkQualityListener);
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest urlRequest =
       mActivity.mCronetEngine.createRequest(TEST_URL, listener, listener.getExecutor());
   urlRequest.start();
   listener.blockForDone();
   testExecutor.runAllTasks();
   assertTrue(networkQualityListener.rttObservationCount() > 0);
   assertTrue(networkQualityListener.throughputObservationCount() > 0);
   mActivity.mCronetEngine.shutdown();
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testNoErrorWhenExceptionDuringStart() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getEchoBodyURL(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    final ConditionVariable first = new ConditionVariable();
    final String exceptionMessage = "Bad Length";
    builder.addHeader("Content-Type", "useless/string");
    builder.setUploadDataProvider(
        new UploadDataProvider() {
          @Override
          public long getLength() throws IOException {
            first.open();
            throw new IOException(exceptionMessage);
          }

          @Override
          public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
              throws IOException {}

          @Override
          public void rewind(UploadDataSink uploadDataSink) throws IOException {}
        },
        callback.getExecutor());
    UrlRequest urlRequest = builder.build();
    urlRequest.start();
    first.block();
    callback.blockForDone();
    assertFalse(callback.mOnCanceledCalled);
    assertEquals(UrlRequestError.LISTENER_EXCEPTION_THROWN, callback.mError.getErrorCode());
    assertEquals("Exception received from UploadDataProvider", callback.mError.getMessage());
    assertEquals(exceptionMessage, callback.mError.getCause().getMessage());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testNoErrorWhenCanceledDuringStart() throws Exception {
    TestUrlRequestCallback callback = new TestUrlRequestCallback();
    UrlRequest.Builder builder =
        new UrlRequest.Builder(
            NativeTestServer.getEchoBodyURL(),
            callback,
            callback.getExecutor(),
            mTestFramework.mCronetEngine);
    final ConditionVariable first = new ConditionVariable();
    final ConditionVariable second = new ConditionVariable();
    builder.addHeader("Content-Type", "useless/string");
    builder.setUploadDataProvider(
        new UploadDataProvider() {
          @Override
          public long getLength() throws IOException {
            first.open();
            second.block();
            return 0;
          }

          @Override
          public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer)
              throws IOException {}

          @Override
          public void rewind(UploadDataSink uploadDataSink) throws IOException {}
        },
        callback.getExecutor());
    UrlRequest urlRequest = builder.build();
    urlRequest.start();
    first.block();
    urlRequest.cancel();
    second.open();
    callback.blockForDone();
    assertTrue(callback.mOnCanceledCalled);
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testShutdownAfterCancel() throws Exception {
   mActivity = launchCronetTestApp();
   TestUrlRequestListener listener = new TestUrlRequestListener();
   // Block listener when response starts to verify that shutdown fails
   // if there are active requests.
   listener.setAutoAdvance(false);
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   UrlRequest urlRequest = urlRequestBuilder.build();
   urlRequest.start();
   try {
     mActivity.mCronetEngine.shutdown();
     fail("Should throw an exception");
   } catch (Exception e) {
     assertEquals("Cannot shutdown with active requests.", e.getMessage());
   }
   listener.waitForNextStep();
   assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
   urlRequest.cancel();
   mActivity.mCronetEngine.shutdown();
 }
 @SmallTest
 @Feature({"Cronet"})
 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Exception {
   mActivity = launchCronetTestApp();
   TestNetworkQualityListener networkQualityListener = new TestNetworkQualityListener();
   try {
     mActivity.mCronetEngine.addRttListener(networkQualityListener);
     fail("Should throw an exception.");
   } catch (IllegalStateException e) {
   }
   try {
     mActivity.mCronetEngine.addThroughputListener(networkQualityListener);
     fail("Should throw an exception.");
   } catch (IllegalStateException e) {
   }
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest urlRequest =
       mActivity.mCronetEngine.createRequest(TEST_URL, listener, listener.getExecutor());
   urlRequest.start();
   listener.blockForDone();
   assertEquals(0, networkQualityListener.rttObservationCount());
   assertEquals(0, networkQualityListener.throughputObservationCount());
   mActivity.mCronetEngine.shutdown();
 }