@SmallTest
  @Feature({"Cronet"})
  public void testDisableCache() throws Exception {
    enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
    String url = NativeTestServer.getFileURL("/cacheable.txt");

    // When cache is disabled, making a request does not write to the cache.
    checkRequestCaching(
        url, false, true
        /** disable cache */
        );
    checkRequestCaching(url, false);

    // When cache is enabled, the second request is cached.
    checkRequestCaching(
        url, false, true
        /** disable cache */
        );
    checkRequestCaching(url, true);

    // Shut down the server, next request should have a cached response.
    NativeTestServer.shutdownNativeTestServer();
    checkRequestCaching(url, true);

    // Cache is disabled after server is shut down, request should fail.
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(url, listener, listener.getExecutor(), mActivity.mCronetEngine);
    urlRequestBuilder.disableCache();
    urlRequestBuilder.build().start();
    listener.blockForDone();
    assertNotNull(listener.mError);
    assertEquals(
        "Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED", listener.mError.getMessage());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testNetLog() throws Exception {
    Context context = getInstrumentation().getTargetContext();
    File directory = new File(PathUtils.getDataDirectory(context));
    File file = File.createTempFile("cronet", "json", directory);
    CronetEngine cronetEngine =
        new CronetUrlRequestContext(
            new CronetEngine.Builder(context).setLibraryName("cronet_tests"));
    // Start NetLog immediately after the request context is created to make
    // sure that the call won't crash the app even when the native request
    // context is not fully initialized. See crbug.com/470196.
    cronetEngine.startNetLogToFile(file.getPath(), false);

    // Start a request.
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    cronetEngine.stopNetLog();
    assertTrue(file.exists());
    assertTrue(file.length() != 0);
    assertFalse(hasBytesInNetLog(file));
    assertTrue(file.delete());
    assertTrue(!file.exists());
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testNetLogStopMultipleTimes() throws Exception {
   mActivity = launchCronetTestApp();
   File directory = new File(PathUtils.getDataDirectory(getInstrumentation().getTargetContext()));
   File file = File.createTempFile("cronet", "json", directory);
   mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
   // Start a request.
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   // Stop NetLog multiple times.
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   mActivity.mCronetEngine.stopNetLog();
   assertTrue(file.exists());
   assertTrue(file.length() != 0);
   assertFalse(hasBytesInNetLog(file));
   assertTrue(file.delete());
   assertTrue(!file.exists());
 }
Exemplo n.º 4
0
  @SmallTest
  @Feature({"Cronet"})
  public void testSimpleGet() throws Exception {
    String url = NativeTestServer.getEchoMethodURL();
    TestUrlRequestListener listener = new TestUrlRequestListener();
    listener.setAutoAdvance(false);
    UrlRequest.Builder builder =
        new UrlRequest.Builder(url, listener, listener.getExecutor(), mActivity.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(statusListener1.mStatus >= Status.IDLE);
    assertTrue(statusListener1.mStatus <= Status.READING_RESPONSE);

    listener.waitForNextStep();
    assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
    listener.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);

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

    listener.startNextRead(urlRequest);
    listener.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, listener.mResponseInfo.getHttpStatusCode());
    assertEquals("GET", listener.mResponseAsString);
  }
 @Override
 public void run() {
   mRunBlocker.block();
   CronetEngine cronetEngine = mActivity.initCronetEngine();
   mListener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(mUrl, mListener, mListener.getExecutor(), cronetEngine);
   urlRequestBuilder.build().start();
   mListener.blockForDone();
 }
 private void checkRequestCaching(String url, boolean expectCached, boolean disableCache) {
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(url, listener, listener.getExecutor(), mActivity.mCronetEngine);
   if (disableCache) {
     urlRequestBuilder.disableCache();
   }
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertEquals(expectCached, listener.mResponseInfo.wasCached());
 }
 @SmallTest
 @Feature({"Cronet"})
 public void testShutdownAfterError() throws Exception {
   mActivity = launchCronetTestApp();
   TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(
           MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertTrue(listener.mOnErrorCalled);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testDataReductionProxyEnabled() throws Exception {
    mActivity = launchCronetTestAppAndSkipFactoryInit();

    // Ensure native code is loaded before trying to start test server.
    new CronetEngine.Builder(getInstrumentation().getTargetContext())
        .setLibraryName("cronet_tests")
        .build()
        .shutdown();

    assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext()));
    if (!NativeTestServer.isDataReductionProxySupported()) {
      return;
    }
    String serverHostPort = NativeTestServer.getHostPort();

    // Enable the Data Reduction Proxy and configure it to use the test
    // server as its primary proxy, and to check successfully that this
    // proxy is OK to use.
    CronetEngine.Builder cronetEngineBuilder =
        new CronetEngine.Builder(getInstrumentation().getTargetContext());
    cronetEngineBuilder.enableDataReductionProxy("test-key");
    cronetEngineBuilder.setDataReductionProxyOptions(
        serverHostPort,
        "unused.net:9999",
        NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
    cronetEngineBuilder.setLibraryName("cronet_tests");
    mActivity.mCronetEngine = cronetEngineBuilder.build();
    TestUrlRequestListener listener = new TestUrlRequestListener();

    // Construct and start a request that can only be returned by the test
    // server. This request will fail if the configuration logic for the
    // Data Reduction Proxy is not used.
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(
            "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
            listener,
            listener.getExecutor(),
            mActivity.mCronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();

    // Verify that the request is successful and that the Data Reduction
    // Proxy logic configured to use the test server as its proxy.
    assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
    assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer());
    assertEquals(
        "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
        listener.mResponseInfo.getUrl());
  }
  @SmallTest
  @Feature({"Cronet"})
  public void testInitEngineAndStartRequest() {
    CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();

    // Immediately make a request after initializing the engine.
    CronetEngine cronetEngine = activity.initCronetEngine();
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
  }
  @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 testInitEngineStartTwoRequests() throws Exception {
    CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();

    // Make two requests after initializing the context.
    CronetEngine cronetEngine = activity.initCronetEngine();
    int[] statusCodes = {0, 0};
    String[] urls = {TEST_URL, URL_404};
    for (int i = 0; i < 2; i++) {
      TestUrlRequestListener listener = new TestUrlRequestListener();
      UrlRequest.Builder urlRequestBuilder =
          new UrlRequest.Builder(urls[i], listener, listener.getExecutor(), cronetEngine);
      urlRequestBuilder.build().start();
      listener.blockForDone();
      statusCodes[i] = listener.mResponseInfo.getHttpStatusCode();
    }
    assertEquals(200, statusCodes[0]);
    assertEquals(404, statusCodes[1]);
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testConfigUserAgent() throws Exception {
   String userAgentName = "User-Agent";
   String userAgentValue = "User-Agent-Value";
   CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(mActivity);
   cronetEngineBuilder.setUserAgent(userAgentValue);
   cronetEngineBuilder.setLibraryName("cronet_tests");
   String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, cronetEngineBuilder.toString()};
   mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, commandLineArgs);
   assertTrue(NativeTestServer.startNativeTestServer(getInstrumentation().getTargetContext()));
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(
           NativeTestServer.getEchoHeaderURL(userAgentName),
           listener,
           listener.getExecutor(),
           mActivity.mCronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   assertEquals(userAgentValue, listener.mResponseAsString);
 }
  @SmallTest
  @Feature({"Cronet"})
  public void testNetLogAfterShutdown() throws Exception {
    mActivity = launchCronetTestApp();
    TestUrlRequestListener listener = new TestUrlRequestListener();
    UrlRequest.Builder urlRequestBuilder =
        new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEngine);
    urlRequestBuilder.build().start();
    listener.blockForDone();
    mActivity.mCronetEngine.shutdown();

    File directory = new File(PathUtils.getDataDirectory(getInstrumentation().getTargetContext()));
    File file = File.createTempFile("cronet", "json", directory);
    try {
      mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
      fail("Should throw an exception.");
    } catch (Exception e) {
      assertEquals("Engine is shut down.", e.getMessage());
    }
    assertFalse(hasBytesInNetLog(file));
    assertTrue(file.delete());
    assertTrue(!file.exists());
  }
 @SmallTest
 @Feature({"Cronet"})
 public void testNetLogWithBytes() throws Exception {
   Context context = getInstrumentation().getTargetContext();
   File directory = new File(PathUtils.getDataDirectory(context));
   File file = File.createTempFile("cronet", "json", directory);
   CronetEngine cronetEngine =
       new CronetUrlRequestContext(
           new CronetEngine.Builder(context).setLibraryName("cronet_tests"));
   // Start NetLog with logAll as true.
   cronetEngine.startNetLogToFile(file.getPath(), true);
   // Start a request.
   TestUrlRequestListener listener = new TestUrlRequestListener();
   UrlRequest.Builder urlRequestBuilder =
       new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(), cronetEngine);
   urlRequestBuilder.build().start();
   listener.blockForDone();
   cronetEngine.stopNetLog();
   assertTrue(file.exists());
   assertTrue(file.length() != 0);
   assertTrue(hasBytesInNetLog(file));
   assertTrue(file.delete());
   assertTrue(!file.exists());
 }
 @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();
 }