/*
 * Copyright (C) 2016 grandcentrix GmbH
 * 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 net.grandcentrix.thirtyinch;


import org.junit.Test;
import org.mockito.InOrder;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class SendToViewTest {

    private class TestPresenter extends TiPresenter<TestView> {

    }

    private interface TestView extends TiView {

        void doSomething1();

        void doSomething2();

        void doSomething3();
    }

    @Test
    public void sendToViewInOrder() throws Exception {
        final TestPresenter presenter = new TestPresenter();
        presenter.create();
        assertThat(presenter.getQueuedViewActions()).hasSize(0);

        presenter.sendToView(new ViewAction<TestView>() {
            @Override
            public void call(final TestView view) {
                view.doSomething3();
            }
        });
        presenter.sendToView(new ViewAction<TestView>() {
            @Override
            public void call(final TestView view) {
                view.doSomething1();
            }
        });
        presenter.sendToView(new ViewAction<TestView>() {
            @Override
            public void call(final TestView view) {
                view.doSomething2();
            }
        });
        assertThat(presenter.getQueuedViewActions()).hasSize(3);

        final TestView view = mock(TestView.class);
        presenter.attachView(view);

        assertThat(presenter.getQueuedViewActions()).hasSize(0);

        final InOrder inOrder = inOrder(view);
        inOrder.verify(view).doSomething3();
        inOrder.verify(view).doSomething1();
        inOrder.verify(view).doSomething2();
    }

    @Test
    public void viewAttached() throws Exception {
        final TestPresenter presenter = new TestPresenter();
        presenter.create();
        assertThat(presenter.getQueuedViewActions()).hasSize(0);

        final TestView view = mock(TestView.class);
        presenter.attachView(view);

        presenter.sendToView(new ViewAction<TestView>() {
            @Override
            public void call(final TestView view) {
                view.doSomething1();
            }
        });
        assertThat(presenter.getQueuedViewActions()).hasSize(0);
        verify(view).doSomething1();
    }

    @Test
    public void viewDetached() throws Exception {
        final TestPresenter presenter = new TestPresenter();
        presenter.create();
        assertThat(presenter.getQueuedViewActions()).hasSize(0);

        presenter.sendToView(new ViewAction<TestView>() {
            @Override
            public void call(final TestView view) {
                view.doSomething1();
            }
        });
        assertThat(presenter.getQueuedViewActions()).hasSize(1);

        final TestView view = mock(TestView.class);
        presenter.attachView(view);
        verify(view).doSomething1();

        assertThat(presenter.getQueuedViewActions()).hasSize(0);
    }
}

@@ -872,16 +872,134 @@
 .verify;
+%0Aimport static org.mockito.Mockito.verifyNoMoreInteractions;%0Aimport static org.mockito.Mockito.verifyZeroInteractions;
 %0A%0Apublic
@@ -3735,11 +3735,1156 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void viewReceivesNoInteractionsAfterDetaching() throws Exception %7B%0A        final TestPresenter presenter = new TestPresenter();%0A        presenter.create();%0A        assertThat(presenter.getQueuedViewActions()).hasSize(0);%0A%0A        final TestView view = mock(TestView.class);%0A        presenter.attachView(view);%0A        presenter.detachView();%0A%0A        presenter.sendToView(new ViewAction%3CTestView%3E() %7B%0A            @Override%0A            public void call(final TestView view) %7B%0A                view.doSomething1();%0A            %7D%0A        %7D);%0A        assertThat(presenter.getQueuedViewActions()).hasSize(1);%0A        verifyZeroInteractions(view);%0A%0A        presenter.attachView(view);%0A%0A        verify(view).doSomething1();%0A        assertThat(presenter.getQueuedViewActions()).hasSize(0);%0A%0A%0A        presenter.detachView();%0A%0A        presenter.sendToView(new ViewAction%3CTestView%3E() %7B%0A            @Override%0A            public void call(final TestView view) %7B%0A                view.doSomething1();%0A            %7D%0A        %7D);%0A        assertThat(presenter.getQueuedViewActions()).hasSize(1);%0A%0A        verifyNoMoreInteractions(view);%0A    %7D
 %0A%7D%0A

Check view doesn't receive events after detaching