/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.firebase.ui.auth.testhelpers;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;

import java.util.concurrent.Executor;

public class AutoCompleteTask<TResult> extends Task<TResult> {
    private TResult mResult;
    private boolean mComplete;
    private boolean mSuccess;
    private Exception mException;

    public AutoCompleteTask(TResult result, boolean complete, @Nullable Exception exception) {
        mResult = result;
        mComplete = complete;
        mSuccess = exception == null;
        mException = exception;
    }

    @Override
    public boolean isComplete() {
        return mComplete;
    }

    @Override
    public boolean isSuccessful() {
        return mSuccess;
    }

    @Override
    public TResult getResult() {
        return mResult;
    }

    @Nullable
    @Override
    public Exception getException() {
        return mException;
    }

    @NonNull
    @Override
    public Task<TResult> addOnCompleteListener(@NonNull OnCompleteListener<TResult> onCompleteListener) {
        if (mComplete) {
            onCompleteListener.onComplete(this);
        }
        return this;
    }

    @NonNull
    @Override
    public Task<TResult> addOnSuccessListener(@NonNull OnSuccessListener<? super TResult> onSuccessListener) {
        if (mSuccess) {
            onSuccessListener.onSuccess(mResult);
        }
        return this;
    }

    @NonNull
    @Override
    public Task<TResult> addOnSuccessListener(@NonNull Executor executor, @NonNull OnSuccessListener
            onSuccessListener) {
        throw new RuntimeException("Method not implemented");
    }

    @NonNull
    @Override
    public Task<TResult> addOnSuccessListener(@NonNull Activity activity,
                                              @NonNull OnSuccessListener onSuccessListener) {
        if (mSuccess) {
            onSuccessListener.onSuccess(mResult);
        }
        return this;
    }

    @NonNull
    @Override
    public Task<TResult> addOnFailureListener(@NonNull OnFailureListener onFailureListener) {
        if (!mSuccess) {
            onFailureListener.onFailure(mException);
        }
        return this;
    }

    @NonNull
    @Override
    public Task<TResult> addOnFailureListener(@NonNull Executor executor,
                                              @NonNull OnFailureListener onFailureListener) {
        throw new RuntimeException("Method not implemented");
    }

    @NonNull
    @Override
    public Task<TResult> addOnFailureListener(@NonNull Activity activity,
                                              @NonNull OnFailureListener onFailureListener) {
        return addOnFailureListener(onFailureListener);
    }

    @NonNull
    @Override
    public <TContinuationResult> Task<TContinuationResult> continueWith(
            @NonNull Continuation<TResult, TContinuationResult> continuation) {
        try {
            return Tasks.forResult(continuation.then(Tasks.forResult(mResult)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TResult getResult(@NonNull Class aClass) throws Throwable {
        throw new RuntimeException("Method not implemented");
    }
}

@@ -2756,16 +2756,33 @@
 Listener
+%3C? super TResult%3E
  onSucce
@@ -4025,32 +4025,392 @@
        %7D%0A    %7D%0A%0A
+    @NonNull%0A    @Override%0A    public %3CTContinuationResult%3E Task%3CTContinuationResult%3E continueWithTask(%0A            @NonNull Continuation%3CTResult, Task%3CTContinuationResult%3E%3E continuation) %7B%0A        try %7B%0A            return continuation.then(Tasks.forResult(mResult));%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

Fix broken tests