Back to Repositories

Testing Android Event Handling Implementation in greenrobot/EventBus

This test suite implements Android-specific functionality for EventBus event handling and posting mechanisms. It provides a foundation for testing event dispatching across different Android threads while ensuring proper Looper and Handler implementations.

Test Coverage Overview

The test suite covers Android-specific event posting mechanisms and thread handling in EventBus.

  • Tests event posting through Android’s main thread Looper
  • Validates Handler implementation for event dispatching
  • Ensures proper thread separation between test and main threads
  • Covers initialization of Android-specific EventBus components

Implementation Analysis

The testing approach utilizes AndroidJUnit4 runner for Android-specific test execution. It implements an abstract base class pattern that extends AbstractEventBusTest while adding Android-specific functionality.

Key implementation features include:
  • Custom EventPostHandler extending Android Handler
  • Main thread Looper integration
  • Thread safety validation between test and main threads

Technical Details

Testing infrastructure includes:

  • AndroidJUnit4 test runner
  • Android Support Test libraries
  • Android Handler and Looper mechanisms
  • SuppressLint annotations for Handler implementation
  • JUnit Assert utilities

Best Practices Demonstrated

The test implementation showcases several Android testing best practices.

  • Proper separation of concerns between Android and base EventBus testing
  • Thread-safe event posting mechanisms
  • Clean inheritance structure for test organization
  • Proper Android component lifecycle management
  • Efficient message handling patterns

greenrobot/eventbus

EventBusTest/src/org/greenrobot/eventbus/AbstractAndroidEventBusTest.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 android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.runner.RunWith;


import static org.junit.Assert.assertFalse;

/**
 * @author Markus Junginger, greenrobot
 */
@RunWith(AndroidJUnit4.class)
public abstract class AbstractAndroidEventBusTest extends AbstractEventBusTest {
    private EventPostHandler mainPoster;

    public AbstractAndroidEventBusTest() {
        this(false);
    }

    public AbstractAndroidEventBusTest(boolean collectEventsReceived) {
        super(collectEventsReceived);
    }

    @Before
    public void setUpAndroid() throws Exception {
        mainPoster = new EventPostHandler(Looper.getMainLooper());
        assertFalse(Looper.getMainLooper().getThread().equals(Thread.currentThread()));
    }

    protected void postInMainThread(Object event) {
        mainPoster.post(event);
    }

    @SuppressLint("HandlerLeak")
    class EventPostHandler extends Handler {
        public EventPostHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            eventBus.post(msg.obj);
        }

        void post(Object event) {
            sendMessage(obtainMessage(0, event));
        }

    }

}