/*
 * Copyright (C) 2013 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * 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 de.greenrobot.event.test;

/**
 * @author Markus Junginger, greenrobot
 */
public class EventBusAbortEventDeliveryTest extends AbstractEventBusTest {

    public void testAbort() {
        Subscriber aborter = new Subscriber(true);
        eventBus.register(new Subscriber(false));
        eventBus.register(aborter, 1);
        eventBus.register(new Subscriber(false));
        eventBus.post("42");
        assertEquals(1, eventCount.intValue());

        eventBus.unregister(aborter);
        eventBus.post("42");
        assertEquals(1 + 2, eventCount.intValue());
    }

    public void testAbortInBetween() {
        Subscriber aborter = new Subscriber(true);
        eventBus.register(aborter, 2);
        eventBus.register(new Subscriber(false), 1);
        eventBus.register(new Subscriber(false), 3);
        eventBus.post("42");
        assertEquals(2, eventCount.intValue());
    }

    class Subscriber {
        private boolean abort;

        public Subscriber(boolean abort) {
            this.abort = abort;
        }

        public void onEvent(String event) {
            trackEvent(event);
            if (abort) {
                eventBus.abortEventDelivery(event);
            }
        }
    }

}

@@ -668,16 +668,142 @@
 .test;%0A%0A
+import java.util.concurrent.CountDownLatch;%0A%0Aimport android.test.UiThreadTest;%0Aimport de.greenrobot.event.EventBusException;%0A%0A
 /**%0A * @
@@ -918,16 +918,47 @@
 Test %7B%0A%0A
+    private Throwable failed;%0A%0A
     publ
@@ -1700,310 +1700,1678 @@
     
-class Subscriber %7B%0A        private boolean abort;%0A%0A        public Subscriber(boolean abort) %7B%0A            this.abort = abort;%0A        %7D%0A%0A        public void onEvent(String event) %7B%0A            trackEvent(event);%0A            if (abort) %7B%0A                eventBus.abortEventDelivery(event);%0A            %7D
+public void testAbortOutsideEventHandler() %7B%0A        try %7B%0A            eventBus.abortEventDelivery(this);%0A            fail(%22Should have thrown%22);%0A        %7D catch (EventBusException e) %7B%0A            // Expected%0A        %7D%0A    %7D%0A%0A    public void testAbortWrongEvent() %7B%0A        eventBus.register(new SubscriberAbortOtherEvent());%0A        eventBus.post(%2242%22);%0A        assertEquals(0, eventCount.intValue());%0A        assertNotNull(failed);%0A    %7D%0A%0A    @UiThreadTest%0A    public void testAbortInMainThread() %7B%0A        SubscriberMainThread subscriber = new SubscriberMainThread();%0A        eventBus.register(subscriber);%0A        eventBus.post(%2242%22);%0A        awaitLatch(subscriber.done, 10);%0A        assertEquals(0, eventCount.intValue());%0A        assertNotNull(failed);%0A    %7D%0A%0A    class Subscriber %7B%0A        private boolean abort;%0A%0A        public Subscriber(boolean abort) %7B%0A            this.abort = abort;%0A        %7D%0A%0A        public void onEvent(String event) %7B%0A            trackEvent(event);%0A            if (abort) %7B%0A                eventBus.abortEventDelivery(event);%0A            %7D%0A        %7D%0A    %7D%0A%0A    class SubscriberAbortOtherEvent %7B%0A        public void onEvent(String event) %7B%0A            try %7B%0A                eventBus.abortEventDelivery(this);%0A            %7D catch (EventBusException e) %7B%0A                failed = e;%0A            %7D%0A        %7D%0A    %7D%0A%0A    class SubscriberMainThread %7B%0A        CountDownLatch done = new CountDownLatch(1);%0A        public void onEventMainThread(String event) %7B%0A            try %7B%0A                eventBus.abortEventDelivery(event);%0A            %7D catch (EventBusException e) %7B%0A                failed = e;%0A            %7D%0A            done.countDown();
 %0A   

test illegal abort situations