package rx.android.eventbus;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class DefaultEventBusTest {

    private static final Queue<String> TEST_DEFAULT_QUEUE = Queue.of(String.class).get();
    private static final Queue<String> TEST_REPLAY_QUEUE = Queue.of(String.class).replay().get();
    private static final Queue<String> TEST_REPLAY_QUEUE_WITH_DEFAULT = Queue.of(String.class).replay("first!").get();

    private DefaultEventBus eventBus = new DefaultEventBus();

    @Mock private Observer<String> observer1;
    @Mock private Observer<String> observer2;
    @Mock private Observer<Integer> intObserver;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldLazilyCreateEventQueuesWhenFirstAccessingThem() {
        assertThat(eventBus.queue(TEST_DEFAULT_QUEUE), is(notNullValue()));
    }

    @Test
    public void shouldPublishEventsToSubscribers() {
        eventBus.subscribe(TEST_DEFAULT_QUEUE, observer1);
        eventBus.subscribe(TEST_DEFAULT_QUEUE, observer2);
        eventBus.publish(TEST_DEFAULT_QUEUE, "hello!");

        verify(observer1).onNext("hello!");
        verify(observer2).onNext("hello!");
    }

    @Test
    public void shouldNotPublishEventsToSubscribersAfterUnsubscribing() {
        final Subscription subscription = eventBus.subscribe(TEST_DEFAULT_QUEUE, observer1);
        eventBus.subscribe(TEST_DEFAULT_QUEUE, observer2);

        eventBus.publish(TEST_DEFAULT_QUEUE, "hello!");
        subscription.unsubscribe();

        eventBus.publish(TEST_DEFAULT_QUEUE, "world!");

        verify(observer1).onNext("hello!");
        verifyNoMoreInteractions(observer1);
        verify(observer2).onNext("hello!");
        verify(observer2).onNext("world!");
    }

    @Test
    public void shouldAllowEventDataTransformation() {
        eventBus.subscribe(TEST_DEFAULT_QUEUE, observer1);
        eventBus.queue(TEST_DEFAULT_QUEUE).map(new Func1<String, Integer>() {
            @Override
            public Integer call(String s) {
                return Integer.parseInt(s);
            }
        }).subscribe(intObserver);

        eventBus.publish(TEST_DEFAULT_QUEUE, "1");

        verify(observer1).onNext("1");
        verify(intObserver).onNext(1);
    }

    @Test
    public void shouldEmitDefaultEventOnConnectionToReplayQueue() {
        eventBus.subscribe(TEST_REPLAY_QUEUE_WITH_DEFAULT, observer1);
        verify(observer1).onNext("first!");
        verifyNoMoreInteractions(observer1);
    }

    @Test
    public void shouldEmitLastValueOnConnectionToReplayQueue() throws Exception {
        eventBus.publish(TEST_REPLAY_QUEUE, "1");
        eventBus.publish(TEST_REPLAY_QUEUE, "2");
        eventBus.subscribe(TEST_REPLAY_QUEUE, observer1);
        verify(observer1).onNext("2");
        verifyNoMoreInteractions(observer1);
    }

    @Test
    public void shouldEmitSubsequentValuesWhenConnectedToReplayQueue() throws Exception {
        eventBus.publish(TEST_REPLAY_QUEUE, "1");
        eventBus.subscribe(TEST_REPLAY_QUEUE, observer1);
        eventBus.publish(TEST_REPLAY_QUEUE, "2");
        verify(observer1).onNext("2");
    }
}

@@ -547,35 +547,8 @@
 ion;
-%0Aimport rx.functions.Func1;
 %0A%0A@R
@@ -2326,507 +2326,8 @@
  %7D%0A%0A
-    @Test%0A    public void shouldAllowEventDataTransformation() %7B%0A        eventBus.subscribe(TEST_DEFAULT_QUEUE, observer1);%0A        eventBus.queue(TEST_DEFAULT_QUEUE).map(new Func1%3CString, Integer%3E() %7B%0A            @Override%0A            public Integer call(String s) %7B%0A                return Integer.parseInt(s);%0A            %7D%0A        %7D).subscribe(intObserver);%0A%0A        eventBus.publish(TEST_DEFAULT_QUEUE, %221%22);%0A%0A        verify(observer1).onNext(%221%22);%0A        verify(intObserver).onNext(1);%0A    %7D%0A%0A
     

Remove unit test that was testing Rx core functionality