// Copyright (c) 2014 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.embedding.test;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.internal.XWalkClient;
import org.xwalk.embedding.MainActivity;
import org.xwalk.embedding.base.XWalkViewTestBase;

import android.annotation.SuppressLint;
import android.test.suitebuilder.annotation.SmallTest;
import android.webkit.ValueCallback;

public class XWalkResourceClientTest extends XWalkViewTestBase {


    public XWalkResourceClientTest() {
        super(MainActivity.class);
    }


    @SmallTest
    public void testOnLoadStarted() {
        try {
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run() {
                    XWalkResourceClient client = new XWalkResourceClient(getXWalkView());
                    mXWalkView.setResourceClient(client);
                    client.onLoadStarted(mXWalkView, "http://www.baidu.com");
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testOnLoadFinished() {
        try {
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run()  {
                    XWalkResourceClient client = new XWalkResourceClient(getXWalkView());
                    mXWalkView.setResourceClient(client);
                    client.onLoadFinished(mXWalkView, "http://www.baidu.com");
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testOnProgressChanged() {
        try {
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run() {
                    XWalkResourceClient client = new XWalkResourceClient(getXWalkView());
                    mXWalkView.setResourceClient(client);
                    client.onProgressChanged(mXWalkView, NUM_NAVIGATIONS);
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testShouldInterceptLoadRequest() {
        try {
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run()  {
                    XWalkResourceClient client = new XWalkResourceClient(mXWalkView);
                    mXWalkView.setResourceClient(client);
                    client.shouldInterceptLoadRequest(mXWalkView, "http://www.baidu.com/");
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testOnReceivedLoadError() {
        try {
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run() {
                    XWalkResourceClient client = new XWalkResourceClient(mXWalkView);
                    mXWalkView.setResourceClient(client);
                    client.onReceivedLoadError(mXWalkView, NUM_NAVIGATIONS, null, "http://www.baidu.com/");
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testShouldOverrideUrlLoading() {
        try {
            loadUrlSync("about:blank");
            getInstrumentation().runOnMainSync(new Runnable() {

                @Override
                public void run() {
                    XWalkResourceClient client = new XWalkResourceClient(mXWalkView);
                    assertFalse(client.shouldOverrideUrlLoading(mXWalkView,"about:blank"));
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }

    @SmallTest
    public void testOnReceivedSslError() {
        try {
            loadUrlSync("about:blank");
            getInstrumentation().runOnMainSync(new Runnable() {

                @SuppressLint("NewApi")
                @Override
                public void run() {
                    XWalkClient client = new XWalkClient(mXWalkView);
                    ValueCallback<Boolean> callback =
                            new ValueCallback<Boolean>() {

                        @Override
                        public void onReceiveValue(Boolean arg0) {

                        }
                    };
                    client.onReceivedSslError(mXWalkView, callback, null);
                }
            });
            assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
            assertTrue(false);
        }
    }
}

@@ -242,52 +242,8 @@
 nt;%0A
-import org.xwalk.core.internal.XWalkClient;%0A
 impo
@@ -335,48 +335,8 @@
 e;%0A%0A
-import android.annotation.SuppressLint;%0A
 impo
@@ -389,45 +389,8 @@
 est;
-%0Aimport android.webkit.ValueCallback;
 %0A%0Apu
@@ -4154,873 +4154,7 @@
    %7D
-%0A%0A    @SmallTest%0A    public void testOnReceivedSslError() %7B%0A        try %7B%0A            loadUrlSync(%22about:blank%22);%0A            getInstrumentation().runOnMainSync(new Runnable() %7B%0A%0A                @SuppressLint(%22NewApi%22)%0A                @Override%0A                public void run() %7B%0A                    XWalkClient client = new XWalkClient(mXWalkView);%0A                    ValueCallback%3CBoolean%3E callback =%0A                            new ValueCallback%3CBoolean%3E() %7B%0A%0A                        @Override%0A                        public void onReceiveValue(Boolean arg0) %7B%0A%0A                        %7D%0A                    %7D;%0A                    client.onReceivedSslError(mXWalkView, callback, null);%0A                %7D%0A            %7D);%0A            assertTrue(true);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            assertTrue(false);%0A        %7D%0A    %7D
 %0A%7D%0A

Delete one unnecessary test case