package com.marverenic.music.viewmodel;

import com.marverenic.music.BuildConfig;
import com.marverenic.music.RobolectricJockeyApplication;
import com.marverenic.music.model.Song;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertSame;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 23, constants = BuildConfig.class, application = RobolectricJockeyApplication.class)
public class SongViewModelTest {

    private RxFragmentActivity mActivity;
    private Song mModel;
    private List<Song> mSurroundingContents;
    private SongViewModel mSubject;

    @Before
    public void setup() {
        mActivity = Robolectric.buildActivity(RxFragmentActivity.class)
                .create()
                .start()
                .resume()
                .get();

        mSurroundingContents = new ArrayList<>();
        mModel = new Song.Builder()
                .setSongName("Song")
                .setSongId(1)
                .setArtistName("Artist")
                .setArtistId(5)
                .setAlbumName("Album")
                .setAlbumId(10)
                .setSongDuration(TimeUnit.MILLISECONDS.convert(3, TimeUnit.MINUTES))
                .setYear(2016)
                .setDateAdded(System.currentTimeMillis())
                .setTrackNumber(1)
                .setInLibrary(true)
                .build();

        mSubject = new SongViewModel(mActivity, mActivity.getSupportFragmentManager(),
                mActivity.bindToLifecycle(), mSurroundingContents);
    }

    @Test
    public void testCorrectText() {
        mSurroundingContents.add(mModel);
        mSubject.setIndex(0);

        assertEquals("Song", mSubject.getTitle());
        assertEquals("Artist - Album", mSubject.getDetail());
    }

    @Test
    public void testReferenceInSingletonList() {
        mSurroundingContents.add(mModel);
        mSubject.setIndex(0);

        assertEquals(0, mSubject.getIndex());
        assertSame(mSurroundingContents, mSubject.getSongs());
        assertEquals(mModel, mSubject.getReference());
    }

    @Test
    public void testReferenceInGeneralList() {
        mSurroundingContents.add(null);
        mSurroundingContents.add(null);
        mSurroundingContents.add(null);
        mSurroundingContents.add(mModel);
        mSurroundingContents.add(null);
        mSubject.setIndex(3);

        assertEquals(3, mSubject.getIndex());
        assertSame(mSurroundingContents, mSubject.getSongs());
        assertEquals(mModel, mSubject.getReference());
    }

}

@@ -34,16 +34,43 @@
 model;%0A%0A
+import android.view.View;%0A%0A
 import c
@@ -102,16 +102,16 @@
 Config;%0A
-
 import c
@@ -200,16 +200,69 @@
 l.Song;%0A
+import com.marverenic.music.player.PlayerController;%0A
 import c
@@ -958,16 +958,46 @@
 mModel;%0A
+    private Song mOtherModel;%0A
     priv
@@ -1847,24 +1847,572 @@
  .build();%0A%0A
+        mOtherModel = new Song.Builder()%0A                .setSongName(%22Another Song%22)%0A                .setSongId(2)%0A                .setArtistName(%22Another Artist%22)%0A                .setArtistId(6)%0A                .setAlbumName(%22Another Album%22)%0A                .setAlbumId(11)%0A                .setSongDuration(TimeUnit.MILLISECONDS.convert(3, TimeUnit.MINUTES))%0A                .setYear(2017)%0A                .setDateAdded(System.currentTimeMillis())%0A                .setTrackNumber(6)%0A                .setInLibrary(true)%0A                .build();%0A%0A
         mSub
@@ -3494,32 +3494,32 @@
 ct.getSongs());%0A
-
         assertEq
@@ -3564,10 +3564,1218 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testNowPlayingIndicatorWithEmptyQueue() %7B%0A        PlayerController playerController = mSubject.mPlayerController;%0A        playerController.clearQueue();%0A%0A        mSurroundingContents.add(null);%0A        mSurroundingContents.add(null);%0A        mSurroundingContents.add(null);%0A        mSurroundingContents.add(mModel);%0A        mSurroundingContents.add(null);%0A        mSubject.setIndex(3);%0A%0A        assertEquals(View.GONE, mSubject.getNowPlayingIndicatorVisibility());%0A    %7D%0A%0A    @Test%0A    public void testNowPlayingIndicatorWithGeneralQueue() %7B%0A        PlayerController playerController = mSubject.mPlayerController;%0A        playerController.clearQueue();%0A%0A        mSurroundingContents.add(mOtherModel);%0A        mSurroundingContents.add(mOtherModel);%0A        mSurroundingContents.add(mOtherModel);%0A        mSurroundingContents.add(mModel);%0A        mSurroundingContents.add(mOtherModel);%0A        mSubject.setIndex(3);%0A%0A        playerController.setQueue(mSurroundingContents, 3);%0A        assertEquals(View.VISIBLE, mSubject.getNowPlayingIndicatorVisibility());%0A%0A        playerController.changeSong(2);%0A        assertEquals(View.GONE, mSubject.getNowPlayingIndicatorVisibility());%0A    %7D%0A%0A
 %7D%0A

Add unit tests for playing indicator visibility