// Copyright 2015 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.net;

import android.test.suitebuilder.annotation.SmallTest;

import org.chromium.base.test.util.Feature;

import java.util.HashMap;

/**
 * Tests making requests using QUIC.
 */
public class QuicTest extends CronetTestBase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // Loads library first since native functions are used to retrieve
        // QuicTestServer info before config is constructed.
        System.loadLibrary("cronet_tests");
    }

    @SmallTest
    @Feature({"Cronet"})
    public void disabled_testQuicLoadUrl() throws Exception {
        HttpUrlRequestFactoryConfig config = new HttpUrlRequestFactoryConfig();
        String quicURL = QuicTestServer.getServerURL() + "/simple.txt";
        config.enableQUIC(true);
        config.setLibraryName("cronet_tests");
        config.addQuicHint(QuicTestServer.getServerHost(),
                QuicTestServer.getServerPort(), QuicTestServer.getServerPort());
        config.setExperimentalQuicConnectionOptions("PACE,IW10,FOO,DEADBEEF");

        String[] commandLineArgs = {
                CronetTestActivity.CONFIG_KEY, config.toString() };
        CronetTestActivity activity =
                launchCronetTestAppWithUrlAndCommandLineArgs(null,
                                                             commandLineArgs);
        QuicTestServer.startQuicTestServer(activity.getApplicationContext());
        activity.startNetLog();

        HashMap<String, String> headers = new HashMap<String, String>();
        TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener();

        HttpUrlRequest request =
                activity.mRequestFactory.createRequest(
                        quicURL,
                        HttpUrlRequest.REQUEST_PRIORITY_MEDIUM,
                        headers,
                        listener);
        request.start();
        listener.blockForComplete();
        assertEquals(200, listener.mHttpStatusCode);
        assertEquals(
                "This is a simple text file served by QUIC.\n", listener.mResponseAsString);
        assertEquals("quic/1+spdy/3", listener.mNegotiatedProtocol);
        activity.stopNetLog();
        QuicTestServer.shutdownQuicTestServer();
    }
}

@@ -737,17 +737,8 @@
 oid 
-disabled_
 test
@@ -1772,24 +1772,247 @@
 istener();%0A%0A
+        // Quic always races the first request with SPDY. Since our test server%0A        // only supports Quic, request will either succeed with a 200 or fail%0A        // with a 500.%0A        for (int i = 0; i %3C 2; i++) %7B%0A    
         Http
@@ -2040,32 +2040,36 @@
                 
+    
 activity.mReques
@@ -2116,16 +2116,20 @@
         
+   
+ 
 quicURL,
@@ -2129,16 +2129,20 @@
 uicURL,%0A
+    
         
@@ -2221,16 +2221,20 @@
         
+  
+  
 headers,
@@ -2234,16 +2234,20 @@
 eaders,%0A
+    
         
@@ -2281,16 +2281,20 @@
         
+    
 request.
@@ -2298,24 +2298,28 @@
 st.start();%0A
+    
         list
@@ -2339,24 +2339,202 @@
 Complete();%0A
+            assertTrue(listener.mHttpStatusCode == 200%0A                    %7C%7C listener.mHttpStatusCode == 500);%0A            if (listener.mHttpStatusCode == 200) break;%0A        %7D%0A
         asse
@@ -2659,16 +2659,32 @@
 UIC.%5Cn%22,
+%0A               
  listene

Enable QuicTest#testQuicLoadUrl