/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * 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.liulishuo.filedownloader.download;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

import java.io.IOException;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

@SuppressWarnings("ALL")
@RunWith(RobolectricTestRunner.class)
public class DownloadRunnableTest {

    @Mock
    private ConnectTask mockConnectTask;

    private ProcessCallback mockCallback;

    @SuppressWarnings("ThrowableInstanceNeverThrown")
    private Exception mockIOException = new IOException("test");

    private DownloadRunnable downloadRunnable;

    @Before
    public void setUp() {
        initMocks(this);

        mockCallback = spy(new MockProcessCallback());
        downloadRunnable = new DownloadRunnable.Builder()
                .setCallback(mockCallback)
                .buildForTest(mockConnectTask);

        when(mockConnectTask.getProfile()).thenReturn(mock(ConnectionProfile.class));
    }

    @Test
    public void run_withConnectFailed_retry() throws IOException {
        when(mockConnectTask.connect()).thenThrow(mockIOException);

        downloadRunnable.run();

        verify(mockCallback).onRetry(mockIOException, 0);
    }

    @Test
    public void run_withConnectButCreateFetchTaskFailed_error() {
        // create fetch-data-task would be crash because of arguments not ready

        downloadRunnable.run();

        verify(mockCallback, times(0)).onRetry(any(Exception.class), anyLong());

        verify(mockCallback).onError(any(Exception.class));
    }

    private static class MockProcessCallback implements ProcessCallback {

        @Override
        public void onProgress(long increaseBytes) {
        }

        @Override
        public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long endOffset) throws IOException {
        }

        boolean isFirstTime = true;

        @Override
        public boolean isRetry(Exception exception) {
            if (isFirstTime) {
                isFirstTime = false;
                return true;
            }

            return false;
        }

        @Override
        public void onError(Exception exception) {
        }


        @Override
        public void onRetry(Exception exception, long invalidIncreaseBytes) {
        }

        @Override
        public void syncProgressFromCache() {
        }
    }
}
@@ -643,16 +643,88 @@
 nload;%0A%0A
+import com.liulishuo.filedownloader.connection.FileDownloadConnection;%0A%0A
 import o
@@ -740,16 +740,16 @@
 Before;%0A
-
 import o
@@ -891,24 +891,59 @@
 IOException;
+%0Aimport java.net.HttpURLConnection;
 %0A%0Aimport sta
@@ -1068,24 +1068,24 @@
 ckito.mock;%0A
+
 import stati
@@ -1115,49 +1115,8 @@
 py;%0A
-import static org.mockito.Mockito.times;%0A
 impo
@@ -2253,133 +2253,298 @@
 run_
-withConnectButCreateFetchTaskFailed_error() %7B%0A        // create fetch-data-task would be crash because of arguments not ready
+responseCodeNotMet_error() throws IOException %7B%0A        final FileDownloadConnection connection = mock(FileDownloadConnection.class);%0A        when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_PRECON_FAILED);%0A        when(mockConnectTask.connect()).thenReturn(connection);
 %0A%0A  
@@ -2566,32 +2566,56 @@
 unnable.run();%0A%0A
+        // retry first.%0A
         verify(m
@@ -2629,18 +2629,8 @@
 back
-, times(0)
 ).on
@@ -2666,24 +2666,56 @@
 nyLong());%0A%0A
+        // then callback error.%0A
         veri

add unit test to testing the case of response code is not 200 or 206