// Copyright (c) 2015 Intel Corporation. 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.xwalk.core.xwview.test;

import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair;

import java.util.ArrayList;
import java.util.List;

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

/**
 * Test suite for DownloadListener().
 */
public class DownloadListenerTest extends XWalkViewTestBase {
    private TestHelperBridge.OnDownloadStartHelper mDownloadStartHelper =
            mTestHelperBridge.getOnDownloadStartHelper();

    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @SmallTest
    @Feature({"onDownloadStart"})
    public void testOnDownloadStart() throws Throwable {
        final String data = "download data";
        final String contentDisposition = "attachment;filename=\"download.txt\"";
        final String mimeType = "text/plain";
        final String userAgent = "Chrome/44.0.2403.81 Crosswalk/15.44.376.0 Mobile Safari/537.36";

        List<Pair<String, String>> downloadHeaders = new ArrayList<Pair<String, String>>();
        downloadHeaders.add(Pair.create("Content-Disposition", contentDisposition));
        downloadHeaders.add(Pair.create("Content-Type", mimeType));
        downloadHeaders.add(Pair.create("Content-Length", Integer.toString(data.length())));

        setUserAgent(userAgent);
        setDownloadListener();
        TestWebServer webServer = TestWebServer.start();
        try {
            final String pageUrl = webServer.setResponse("/download.txt", data, downloadHeaders);
            final int callCount = mDownloadStartHelper.getCallCount();
            loadUrlAsync(pageUrl);
            mDownloadStartHelper.waitForCallback(callCount);

            assertEquals(pageUrl, mDownloadStartHelper.getUrl());
            assertEquals(contentDisposition, mDownloadStartHelper.getContentDisposition());
            assertEquals(mimeType, mDownloadStartHelper.getMimeType());
            assertEquals(data.length(), mDownloadStartHelper.getContentLength());
            assertEquals(userAgent, mDownloadStartHelper.getUserAgent());
        } finally {
            webServer.shutdown();
        }

    }
}

@@ -327,24 +327,60 @@
 util.List;%0A%0A
+import org.apache.http.HttpRequest;%0A
 import org.c
@@ -411,16 +411,16 @@
 eature;%0A
-
 import o
@@ -459,16 +459,58 @@
 bServer;
+%0Aimport org.xwalk.core.XWalkCookieManager;
 %0A%0A/**%0A *
@@ -1208,16 +1208,66 @@
 537.36%22;
+%0A        final String cookieValue = %22cookie data%22;
 %0A%0A      
@@ -1659,24 +1659,93 @@
 Listener();%0A
+        XWalkCookieManager cookieManager = new XWalkCookieManager();%0A
         Test
@@ -1799,24 +1799,80 @@
       try %7B%0A
+            final String requestPath = %22/download.txt%22;%0A
             
@@ -1916,31 +1916,27 @@
 esponse(
-%22/download.txt%22
+requestPath
 , data, 
@@ -2020,24 +2020,83 @@
 allCount();%0A
+            cookieManager.setCookie(pageUrl, cookieValue);%0A
             
@@ -2488,24 +2488,24 @@
 tLength());%0A
-
             
@@ -2561,24 +2561,196 @@
 serAgent());
+%0A%0A            final HttpRequest lastRequest = webServer.getLastRequest(requestPath);%0A            assertEquals(cookieValue, lastRequest.getFirstHeader(%22Cookie%22).getValue());
 %0A        %7D f

Add an assertion that cookie is added to download requests.