Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / test / javatests / src / org / chromium / cronet_test_apk / HttpUrlRequestFactoryTest.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.cronet_test_apk;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import org.chromium.base.test.util.Feature;
10 import org.chromium.net.HttpUrlRequest;
11 import org.chromium.net.HttpUrlRequestFactory;
12 import org.chromium.net.HttpUrlRequestFactoryConfig;
13
14 import java.util.HashMap;
15 import java.util.regex.Pattern;
16
17 /**
18  * Tests for {@link HttpUrlRequestFactory}
19  */
20 public class HttpUrlRequestFactoryTest extends CronetTestBase {
21     // URL used for base tests.
22     private static final String URL = "http://127.0.0.1:8000";
23
24     @SmallTest
25     @Feature({"Cronet"})
26     public void testCreateFactory() throws Throwable {
27         HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
28         config.enableQUIC(true);
29         config.addQuicHint("www.google.com", 443, 443);
30         config.addQuicHint("www.youtube.com", 443, 443);
31         config.setLibraryName("cronet_tests");
32         String[] commandLineArgs = {
33                 CronetTestActivity.CONFIG_KEY, config.toString() };
34         CronetTestActivity activity =
35                 launchCronetTestAppWithUrlAndCommandLineArgs(URL,
36                                                              commandLineArgs);
37         // Make sure the activity was created as expected.
38         assertNotNull(activity);
39         waitForActiveShellToBeDoneLoading();
40         HttpUrlRequestFactory factory = activity.mRequestFactory;
41         assertNotNull("Factory should be created", factory);
42         assertTrue("Factory should be Chromium/n.n.n.n@r but is "
43                            + factory.getName(),
44                    Pattern.matches("Chromium/\\d+\\.\\d+\\.\\d+\\.\\d+@\\w+",
45                            factory.getName()));
46     }
47
48     @SmallTest
49     @Feature({"Cronet"})
50     public void testCreateLegacyFactory() {
51         HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
52         config.enableLegacyMode(true);
53
54         HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory(
55                 getInstrumentation().getTargetContext(), config);
56         assertNotNull("Factory should be created", factory);
57         assertTrue("Factory should be HttpUrlConnection/n.n.n.n@r but is "
58                            + factory.getName(),
59                    Pattern.matches(
60                            "HttpUrlConnection/\\d+\\.\\d+\\.\\d+\\.\\d+@\\w+",
61                            factory.getName()));
62         HashMap<String, String> headers = new HashMap<String, String>();
63         TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();
64         HttpUrlRequest request = factory.createRequest(
65                 URL, 0, headers, listener);
66         request.start();
67         listener.blockForComplete();
68         assertEquals(200, listener.mHttpStatusCode);
69         assertEquals("OK", listener.mHttpStatusText);
70     }
71
72     @SmallTest
73     @Feature({"Cronet"})
74     public void testQuicHintHost() {
75         HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
76         config.addQuicHint("www.google.com", 443, 443);
77         try {
78             config.addQuicHint("https://www.google.com", 443, 443);
79         } catch (IllegalArgumentException e) {
80             return;
81         }
82         fail("IllegalArgumentException must be thrown");
83     }
84 }