package com.avg.zaap.crashlibrary.file.observer;

import android.os.FileObserver;

import org.junit.Before;
import org.junit.Test;

import java.io.File;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * A test class for {@link FilePathObserver}.
 * <p>
 * Created by marek.osvald on 21.4.2016.
 */
public class FilePathObserverTests {

    private static final String FILE_PATH = File.separatorChar + "foo" + File.separatorChar + "bar" + File.separatorChar;
    private static final String FILE_NAME = "file";
    private static final String FILE_EXTENSION = ".ext";
    private static final String FILE_NAME_WITH_EXTENSION = FILE_NAME + FILE_EXTENSION;
    private static final int EVENT_CLOSE_WRITE = FileObserver.CLOSE_WRITE;

    private FilePathObserver mFileObserver;
    private IFilePathObserverCallback mCallback;

    @Before
    public void setUp() throws Exception {
        mCallback = mock(IFilePathObserverCallback.class);
        mFileObserver = new FilePathObserver(FILE_PATH, EVENT_CLOSE_WRITE, mCallback);
    }

    @Test
    public void onEvent_fileNameNullEventCloseWrite_doesNothing() throws Exception {
        // act
        mFileObserver.onEvent(EVENT_CLOSE_WRITE, null);

        // assert
        verify(mCallback, never()).onNewFileCreated(anyString(), anyString());
    }

    @Test
    public void onEvent_fileNameNotNullEventNewFile_callsCallback() throws Exception {
        // act
        mFileObserver.onEvent(EVENT_CLOSE_WRITE, FILE_NAME_WITH_EXTENSION);

        // assert
        verify(mCallback, times(1)).onNewFileCreated(FILE_NAME, FILE_PATH + FILE_NAME_WITH_EXTENSION);
    }

}
