// Copyright 2012 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.android_webview.test;

import android.graphics.Bitmap;
import android.graphics.Picture;
import android.net.http.SslError;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;

import org.chromium.android_webview.AwContentsClient;
import org.chromium.android_webview.AwContentsClientBridge;
import org.chromium.android_webview.AwHttpAuthHandler;
import org.chromium.android_webview.InterceptedRequestData;
import org.chromium.android_webview.JsPromptResultReceiver;
import org.chromium.android_webview.JsResultReceiver;
import org.chromium.base.ThreadUtils;

import java.security.Principal;

/**
 * As a convience for tests that only care about specefic callbacks, this class provides
 * empty implementations of all abstract methods.
 */
public class NullContentsClient extends AwContentsClient {
    public NullContentsClient() {
        this(ThreadUtils.getUiThreadLooper());
    }

    public NullContentsClient(Looper looper) {
        super(looper);  // "...beams are gonna blind me".
    }

    @Override
    public boolean shouldOverrideUrlLoading(String url) {
        return false;
    }

    @Override
    public void onUnhandledKeyEvent(KeyEvent event) {
    }

    @Override
    public void getVisitedHistory(ValueCallback<String[]> callback) {
    }

    @Override
    public void doUpdateVisitedHistory(String url, boolean isReload) {
    }

    @Override
    public void onProgressChanged(int progress) {
    }

    @Override
    public InterceptedRequestData shouldInterceptRequest(String url) {
        return null;
    }

    @Override
    public boolean shouldOverrideKeyEvent(KeyEvent event) {
        return false;
    }

    @Override
    public void onLoadResource(String url) {
    }

    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        return false;
    }

    @Override
    public void onReceivedHttpAuthRequest(AwHttpAuthHandler handler, String host, String realm) {
        handler.cancel();
    }

    @Override
    public void onReceivedSslError(ValueCallback<Boolean> callback, SslError error) {
        callback.onReceiveValue(false);
    }

    @Override
    public void onReceivedClientCertRequest(
            final AwContentsClientBridge.ClientCertificateRequestCallback callback,
            final String[] keyTypes, final Principal[] principals, final String host,
            final int port) {
        callback.proceed(null, null);
    }

    @Override
    public void onReceivedLoginRequest(String realm, String account, String args) {
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
            GeolocationPermissions.Callback callback) {
    }

    @Override
    public void onGeolocationPermissionsHidePrompt() {
    }

    @Override
    public void handleJsAlert(String url, String message, JsResultReceiver receiver) {
    }

    @Override
    public void handleJsBeforeUnload(String url, String message, JsResultReceiver receiver) {
    }

    @Override
    public void handleJsConfirm(String url, String message, JsResultReceiver receiver) {
    }

    @Override
    public void handleJsPrompt(
            String url, String message, String defaultValue, JsPromptResultReceiver receiver) {
    }

    @Override
    public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
            boolean isDoneCounting) {
    }

    @Override
    public void onNewPicture(Picture picture) {
    }

    @Override
    public void onPageStarted(String url) {
    }

    @Override
    public void onPageFinished(String url) {
    }

    @Override
    public void onReceivedError(int errorCode, String description, String failingUrl) {
    }

    @Override
    public void onFormResubmission(Message dontResend, Message resend) {
        dontResend.sendToTarget();
    }

    @Override
    public void onDownloadStart(String url,
                                String userAgent,
                                String contentDisposition,
                                String mimeType,
                                long contentLength) {
    }

    @Override
    public boolean onCreateWindow(boolean isDialog, boolean isUserGesture) {
        return false;
    }

    @Override
    public void onCloseWindow() {
    }

    @Override
    public void onRequestFocus() {
    }

    @Override
    public void onReceivedTouchIconUrl(String url, boolean precomposed) {
    }

    @Override
    public void onReceivedIcon(Bitmap bitmap) {
    }

    @Override
    public void onReceivedTitle(String title) {
    }

    @Override
    public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
    }

    @Override
    public void onHideCustomView() {
    }

    @Override
    public void onScaleChangedScaled(float oldScale, float newScale) {
    }

    @Override
    protected View getVideoLoadingProgressView() {
        return null;
    }

    @Override
    public Bitmap getDefaultVideoPoster() {
        return null;
    }
}

@@ -347,32 +347,57 @@
 oid.os.Message;%0A
+import android.util.Log;%0A
 import android.v
@@ -1218,16 +1218,78 @@
 lient %7B%0A
+%0A    private static final String TAG = %22NullContentsClient%22;%0A%0A
     publ
@@ -3317,32 +3317,126 @@
 ver receiver) %7B%0A
+        Log.i(TAG, %22handleJsAlert(%22 + url + %22, %22 + message + %22)%22);%0A        receiver.cancel();%0A
     %7D%0A%0A    @Over
@@ -3526,32 +3526,134 @@
 ver receiver) %7B%0A
+        Log.i(TAG, %22handleJsBeforeUnload(%22 + url + %22, %22 + message + %22)%22);%0A        receiver.confirm();%0A
     %7D%0A%0A    @Over
@@ -3738,32 +3738,128 @@
 ver receiver) %7B%0A
+        Log.i(TAG, %22handleJsConfirm(%22 + url + %22, %22 + message + %22)%22);%0A        receiver.cancel();%0A
     %7D%0A%0A    @Over
@@ -3987,24 +3987,119 @@
 receiver) %7B%0A
+        Log.i(TAG, %22handleJsPrompt(%22 + url + %22, %22 + message + %22)%22);%0A        receiver.cancel();%0A
     %7D%0A%0A    @

Stop JS alerts hanging the WebView Test Shell