package com.connectsdk.service;

import android.content.Context;

import com.connectsdk.service.capability.MediaControl;
import com.connectsdk.service.command.ServiceCommand;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.config.ServiceConfig;
import com.connectsdk.service.config.ServiceDescription;

import junit.framework.Assert;

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

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Oleksii Frolov on 3/19/2015.
 */
@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class AirPlayServiceTest {

    private StubAirPlayService service;

    class StubAirPlayService extends AirPlayService {

        private Map<String, String> lastParams;
        private String lastMethod;
        private Object response;

        public StubAirPlayService(ServiceDescription serviceDescription, ServiceConfig serviceConfig) throws IOException {
            super(serviceDescription, serviceConfig);
        }

        public void setResponse(Object response) {
            this.response = response;
        }

        @Override
        public void sendCommand(ServiceCommand<?> serviceCommand) {
            serviceCommand.getResponseListener().onSuccess(response);
        }

        Context getContext() {
            return Robolectric.application;
        }

    }

    @Before
    public void setUp() throws IOException {
        service = new StubAirPlayService(Mockito.mock(ServiceDescription.class), Mockito.mock(ServiceConfig.class));
    }

    @Test
    public void testGetPlayStateFinished() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        service.setResponse(
                "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" +
                "<plist version=\"1.0\">" +
                "<dict>" +
                "</dict>" +
                "</plist>"
        );
        service.getPlayState(new MediaControl.PlayStateListener() {
            @Override
            public void onSuccess(MediaControl.PlayStateStatus object) {
                Assert.assertEquals(MediaControl.PlayStateStatus.Finished, object);
                latch.countDown();
            }

            @Override
            public void onError(ServiceCommandError error) {
                latch.countDown();
            }
        });
        latch.await();
    }

    @Test
    public void testGetPlayStatePlaying() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        service.setResponse(
                "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" +
                "<plist version=\"1.0\">" +
                "<dict>" +
                "<key>rate</key>" +
                "<real>1</real>" +
                "</dict>" +
                "</plist>"
        );
        service.getPlayState(new MediaControl.PlayStateListener() {
            @Override
            public void onSuccess(MediaControl.PlayStateStatus object) {
                Assert.assertEquals(MediaControl.PlayStateStatus.Playing, object);
                latch.countDown();
            }

            @Override
            public void onError(ServiceCommandError error) {
                latch.countDown();
            }
        });
        latch.await();
    }
}

@@ -30,41 +30,8 @@
 e;%0A%0A
-import android.content.Context;%0A%0A
 impo
@@ -449,44 +449,8 @@
 to;%0A
-import org.robolectric.Robolectric;%0A
 impo
@@ -565,74 +565,8 @@
 ion;
-%0Aimport java.util.Map;%0Aimport java.util.concurrent.CountDownLatch;
 %0A%0A/*
@@ -817,91 +817,8 @@
  %7B%0A%0A
-        private Map%3CString, String%3E lastParams;%0A        private String lastMethod;%0A
     
@@ -1306,94 +1306,8 @@
  %7D%0A%0A
-        Context getContext() %7B%0A            return Robolectric.application;%0A        %7D%0A%0A
     
@@ -1577,68 +1577,8 @@
 n %7B%0A
-        final CountDownLatch latch = new CountDownLatch(1);%0A
     
@@ -2117,43 +2117,8 @@
 t);%0A
-                latch.countDown();%0A
     
@@ -2223,39 +2223,35 @@
             
-latch.countDown
+Assert.fail
 ();%0A        
@@ -2268,39 +2268,16 @@
     %7D);%0A
-        latch.await();%0A
     %7D%0A%0A 
@@ -2361,68 +2361,8 @@
 n %7B%0A
-        final CountDownLatch latch = new CountDownLatch(1);%0A
     
@@ -2971,43 +2971,8 @@
 t);%0A
-                latch.countDown();%0A
     
@@ -3085,23 +3085,19 @@
     
-latch.countDown
+Assert.fail
 ();%0A
@@ -3126,31 +3126,8 @@
 %7D);%0A
-        latch.await();%0A
     

Make AirPlayServiceTest clearer