Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/ContextInitTest.java |
diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/ContextInitTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/ContextInitTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..29cb9fe468e5253e8120de41e565bc1ca1ccc0d1 |
--- /dev/null |
+++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/ContextInitTest.java |
@@ -0,0 +1,105 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.cronet_test_apk; |
+ |
+import android.test.suitebuilder.annotation.SmallTest; |
+ |
+import org.chromium.base.test.util.Feature; |
+import org.chromium.net.HttpUrlRequest; |
+import org.chromium.net.HttpUrlRequestFactory; |
+ |
+import java.util.HashMap; |
+ |
+/** |
+ * Tests that make sure ChromiumUrlRequestContext initialization will not |
+ * affect embedders' ability to make requests. |
+ */ |
+public class ContextInitTest extends CronetTestBase { |
+ // URL used for base tests. |
+ private static final String URL = "http://127.0.0.1:8000"; |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
+ public void testInitFactoryAndStartRequest() { |
+ CronetTestActivity activity = skipFactoryInitInOnCreate(); |
+ // Make sure the activity was created as expected. |
+ assertNotNull(activity); |
+ // Make sure the factory is not created. |
+ assertNull(activity.mRequestFactory); |
+ |
+ // Immediately make a request after initializing the factory. |
+ activity.initRequestFactory(); |
+ TestHttpUrlRequestListener listener = |
+ makeRequest(activity.mRequestFactory, URL); |
+ listener.blockForComplete(); |
+ assertEquals(200, listener.mHttpStatusCode); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
+ public void testInitFactoryStartRequestAndCancel() { |
+ CronetTestActivity activity = skipFactoryInitInOnCreate(); |
+ // Make sure the activity was created as expected. |
+ assertNotNull(activity); |
+ // Make sure the factory is not created. |
+ assertNull(activity.mRequestFactory); |
+ |
+ // Make a request and cancel it after initializing the factory. |
+ activity.initRequestFactory(); |
+ HashMap<String, String> headers = new HashMap<String, String>(); |
+ TestHttpUrlRequestListener listener = |
+ new TestHttpUrlRequestListener(); |
+ HttpUrlRequest request = activity.mRequestFactory.createRequest( |
+ URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
+ request.start(); |
+ request.cancel(); |
+ listener.blockForComplete(); |
+ assertEquals(0, listener.mHttpStatusCode); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
+ public void testInitFactoryStartTwoRequests() throws Exception { |
+ CronetTestActivity activity = skipFactoryInitInOnCreate(); |
+ // Make sure the activity was created as expected. |
+ assertNotNull(activity); |
+ // Make sure the factory is not created. |
+ assertNull(activity.mRequestFactory); |
+ |
+ // Make two request right after initializing the factory. |
+ int[] statusCodes = {0, 0}; |
+ String[] urls = {URL, "http://127.0.0.1:8000/test"}; |
+ activity.initRequestFactory(); |
+ for (int i = 0; i < 2; i++) { |
+ TestHttpUrlRequestListener listener = |
+ makeRequest(activity.mRequestFactory, urls[i]); |
+ listener.blockForComplete(); |
+ statusCodes[i] = listener.mHttpStatusCode; |
+ } |
+ assertEquals(200, statusCodes[0]); |
+ assertEquals(404, statusCodes[1]); |
mmenke
2014/10/15 17:44:17
nit: Fix indent.
xunjieli
2014/10/15 19:48:08
Done.
|
+ } |
+ |
+ // Helper method to tell the activity to skip factory init in onCreate(). |
+ private CronetTestActivity skipFactoryInitInOnCreate() { |
+ String[] commandLineArgs = { |
+ CronetTestActivity.SKIP_FACTORY_INIT_KEY, "skip" }; |
+ CronetTestActivity activity = |
+ launchCronetTestAppWithUrlAndCommandLineArgs(null, |
+ commandLineArgs); |
+ return activity; |
+ } |
+ |
+ // Helper function to make a request. |
+ private TestHttpUrlRequestListener makeRequest( |
+ HttpUrlRequestFactory factory, String url) { |
+ HashMap<String, String> headers = new HashMap<String, String>(); |
+ TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
+ HttpUrlRequest request = factory.createRequest( |
+ url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
+ request.start(); |
+ return listener; |
+ } |
+} |