Back to Repositories

Testing Android Event Delivery Cancellation in greenrobot/EventBus

This test suite evaluates EventBus’s event delivery cancellation functionality in an Android environment. It specifically focuses on testing event cancellation behavior when running on the main UI thread, ensuring proper event handling and delivery interruption.

Test Coverage Overview

The test coverage focuses on event cancellation mechanics in Android’s main thread context. Key functionality tested includes:

  • Event delivery cancellation in UI thread operations
  • Verification of event count after cancellation
  • Exception handling during cancellation
  • Thread-specific delivery behavior

Implementation Analysis

The testing approach leverages Android’s JUnit4 test runner and UI thread annotations for main thread execution validation. The implementation uses the @UiThreadTest annotation to ensure proper thread context, while incorporating EventBus’s cancellation mechanisms to verify delivery interruption patterns.

Technical Details

Testing tools and configuration include:

  • AndroidJUnit4 test runner
  • UiThreadTest annotation for main thread testing
  • EventBus subscription management
  • Latch-based synchronization
  • Assert statements for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper thread context validation
  • Clean test isolation through proper setup
  • Clear assertion patterns
  • Effective use of Android-specific test annotations
  • Structured inheritance for test organization

greenrobot/eventbus

EventBusTest/src/org/greenrobot/eventbus/EventBusAndroidCancelEventDeliveryTest.java

            
/*
 * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * 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 org.greenrobot.eventbus;

import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.runner.AndroidJUnit4;
import android.test.UiThreadTest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class EventBusAndroidCancelEventDeliveryTest extends EventBusCancelEventDeliveryTest {

    @UiThreadTest
    @Test
    public void testCancelInMainThread() {
        SubscriberMainThread subscriber = new SubscriberMainThread();
        eventBus.register(subscriber);
        eventBus.post("42");
        awaitLatch(subscriber.done, 10);
        assertEquals(0, eventCount.intValue());
        assertNotNull(failed);
    }

}