Android Open Source - LeBlue Mocker Object






From Project

Back to project page LeBlue.

License

The source code is released under:

Apache License

If you think the Android project LeBlue listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package houtbecke.rs.le.session;
/*from ww  w.j  av a  2s.c  o m*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import houtbecke.rs.le.LeCharacteristicListener;
import houtbecke.rs.le.LeDeviceListener;
import houtbecke.rs.le.LeRemoteDeviceListener;
import houtbecke.rs.le.mock.LeMockController;

public class MockerObject implements Mocker {

    Map<Integer, Map<EventType, List<MockedResponse>>> mocks = new HashMap<>();

    Map<Integer, Set<Integer>> listeners = new HashMap<>();
    volatile static int sourceCounter = 0;

    boolean mockDeviceListeners = false;
    boolean mockRemoteDeviceListeners = false;
    boolean mockCharacteristicsListeners = false;

    int defaultDelay = -1;
    int getDelay() {
        return defaultDelay == -1 ? sessionObject.getDefaultDelay() : defaultDelay;

    }

    final int sessionSource;
    SessionObject sessionObject;
    public final SessionObject and;
    public SessionObject end() {
        return sessionObject;
    }

    public MockerObject(SessionObject sessionObject, int sessionSource) {
        this.sessionObject = sessionObject;
        this.sessionSource = sessionSource;
        this.and = sessionObject;
    }

    public static MockerObject newMocker(SessionObject sessionObject, int defaultSource) {
        return new MockerObject(sessionObject, defaultSource);
    }

    /**
     * Mock responses followed by a mocked event defined after calling this method will delay the mock event with the supplied.
     *
     * @param delay delay in ms
     * @return the same instance of this MockerObject
     */
    public MockerObject withDelay(int delay) {
        this.defaultDelay = delay;
        return this;

    }

    Map<EventType, List<MockedResponse>> getMocks(int source) {
        Map<EventType, List<MockedResponse>> ret = mocks.get(source);
        if (ret == null) {
            ret = new HashMap<>();
            mocks.put(source, ret);
        }
        return ret;
    }

     List<MockedResponse> getMocksList(int source, EventType type) {
        Map<EventType, List<MockedResponse>> mocksMap = getMocks(source);
        List<MockedResponse> ret = mocksMap.get(type);
        if (ret == null) {
            ret = new ArrayList<>();
            mocksMap.put(type, ret);
        }
        return ret;
    }


    @Override
    public MockedResponse mock(LeMockController controller, EventType type, int source, String... arguments) {

        boolean mock = false;

        switch(type) {
            case deviceAddListener:
                mock |= mockDeviceListeners;
            case remoteDeviceAddListener:
                mock |= mockRemoteDeviceListeners;
            case remoteDeviceSetCharacteristicListener:
                mock |= mockCharacteristicsListeners;
                if (mock)
                    return new MockedResponseObject(addListener(source));
                break;

        }

        if (mocks.get(source) != null) {
            List<MockedResponse> responses = mocks.get(source).get(type);
            if (responses != null)
                for (MockedResponse response: responses) {
                    if (response.isForArguments(arguments)) {
                        if (response.isSelfDestroying())
                            responses.remove(response);
                        return response;
                    }

                }
        }
        return null;
    }

    public MockerObject withSelfDestroyingMock(EventType type, String... values) {
        return withSelfDestroyingMock(sessionSource, type, values);
    }
    public MockerObject withSelfDestroyingMock(int source, EventType type, String... values) {
        MockedResponseObject response = new MockedResponseObject(values);
        response.destroyAfterUse();
        getMocksList(source, type).add(response);
        return this;
    }

    public MockerObject withMock(EventType type, String argument, int argumentPos, String... values) {
        return withMock(sessionSource, type, argument, argumentPos, values);
    }
    public MockerObject withMock(int source, EventType type, String argument, int argumentPos, String... values) {
        MockedResponseObject response = new MockedResponseObject(values);
        response.forArguments(argument, argumentPos);
        getMocksList(source, type).add(response);
        return this;
    }

    public MockerObject withSelfDestroyingMock(EventType type, String argument, int argumentPos, String... values) {
        return withSelfDestroyingMock(sessionSource, type, argument, argumentPos, values);
    }
    public MockerObject withSelfDestroyingMock(int source, EventType type, String argument, int argumentPos, String... values) {
        MockedResponseObject mockedResponse = new MockedResponseObject(values);
        mockedResponse.destroyAfterUse();
        mockedResponse.forArguments(argument, argumentPos);
        getMocksList(source, type).add(mockedResponse);
        return this;
    }

    public MockerObject withSelfDestroyingMock(EventType method, MockedResponseObject response) {
        return withSelfDestroyingMock(sessionSource, method, response);
    }
    public MockerObject withSelfDestroyingMock(int source, EventType method, MockedResponseObject response) {
        response.destroyAfterUse();
        getMocksList(source, method).add(response);
        return this;
    }


    public MockerObject withMock(EventType type, String... values) {
        return withMock(sessionSource, type, values);
    }
    public MockerObject withMock(int source, EventType type, String... values) {
        getMocksList(source, type).add(new MockedResponseObject(values));
        return this;
    }

    public MockerObject withMock(EventType type, Event event, String... values) {
        return withMock(sessionSource, type, event, values);
    }
    public MockerObject withMock(int source, EventType type, Event event, String... values) {
        getMocksList(source, type).add(new MockedResponseObject(event, values));
        return this;
    }

    public MockerObject withMock(EventType method, String value) {
        return withMock(sessionSource, method, value);
    }
    public MockerObject withMock(int source, EventType method, String value) {
        getMocksList(source, method).add(new MockedResponseObject(new String[] {value}));
        return this;
    }

    public MockerObject withMock(EventType method, MockedResponse response) {
        return withMock(sessionSource, method, response);
    }
    public MockerObject withMock(int source, EventType method, MockedResponse response) {
        getMocksList(source, method).add(response);
        return this;
    }

    public MockedResponseObject getFirstMockObject(EventType method) {
        return getFirstMockObject(sessionSource, method);
    }

    public MockedResponseObject getFirstMockObject(int source, EventType method) {
        if (getMocksList(source, method).size() == 0)
            return null;
        MockedResponse mr = getMocksList(source, method).get(0);
        return mr instanceof MockedResponseObject ? (MockedResponseObject) mr : null;

    }

    Collection<Integer> getListenersSet(int source) {
        Set<Integer> listenersForSource = listeners.get(source);
        if (listenersForSource == null) {
            listenersForSource = new HashSet<>();
            listeners.put(source, listenersForSource);
        }
        return  listenersForSource;
    }

    String addListener(int source) {
        int id = ++sourceCounter;
        getListenersSet(source).add(id);
        return id+"";
    }

    @Override
    public LeDeviceListener[] getDeviceListeners(LeMockController controller, int device) {
        Collection<Integer> deviceListeners = getListenersSet(device);
        LeDeviceListener[] leDeviceListeners = new LeDeviceListener[deviceListeners.size()];
        int k = 0;
        for (int key: deviceListeners) {
            leDeviceListeners[k] = controller.getDeviceListener(key);
            k++;
        }
        return leDeviceListeners;
    }

    @Override
    public LeRemoteDeviceListener[] getRemoteDeviceListeners(LeMockController controller, int remoteDevice) {
        Collection<Integer> remoteDeviceListeners = getListenersSet(remoteDevice);

        List<LeRemoteDeviceListener> ret = new ArrayList<>();
        for (int key: remoteDeviceListeners) {
            LeRemoteDeviceListener listener = controller.getRemoteDeviceListener(key);
            if (listener != null)
                ret.add(listener);

        }
        return ret.toArray(new LeRemoteDeviceListener[ret.size()]);
    }

    @Override
    public LeCharacteristicListener[] getCharacteristicListeners(LeMockController controller, int characteristic) {
        Collection<Integer> characteristicsListeners = getListenersSet(characteristic);

        List<LeCharacteristicListener> ret = new ArrayList<>();
        for (int key: characteristicsListeners) {
            LeCharacteristicListener listener = controller.getCharacteristicListener(key);
            if (listener != null)
                ret.add(listener);
        }
        return ret.toArray(new LeCharacteristicListener[ret.size()]);
    }





}




Java Source Code List

houtbecke.rs.le.BleException.java
houtbecke.rs.le.LeCharacteristicListener.java
houtbecke.rs.le.LeDefinedUUIDs.java
houtbecke.rs.le.LeDeviceListener.java
houtbecke.rs.le.LeDeviceState.java
houtbecke.rs.le.LeDevice.java
houtbecke.rs.le.LeFormat.java
houtbecke.rs.le.LeGattCharacteristic.java
houtbecke.rs.le.LeGattDescriptorValue.java
houtbecke.rs.le.LeGattDescriptor.java
houtbecke.rs.le.LeGattService.java
houtbecke.rs.le.LeGattStatus.java
houtbecke.rs.le.LeRecord.java
houtbecke.rs.le.LeRemoteDeviceListener.java
houtbecke.rs.le.LeRemoteDevice.java
houtbecke.rs.le.LeScanRecordImpl.java
houtbecke.rs.le.LeScanRecord.java
houtbecke.rs.le.LeUtil.java
houtbecke.rs.le.dummy.DummyLeRemoteDevice.java
houtbecke.rs.le.example.MainActivity.java
houtbecke.rs.le.fourdotthree.LeDevice43.java
houtbecke.rs.le.fourdotthree.LeGattCharacteristic43.java
houtbecke.rs.le.fourdotthree.LeGattService43.java
houtbecke.rs.le.fourdotthree.LeRemoteDevice43.java
houtbecke.rs.le.interceptor.AndroidEventSinkUtil.java
houtbecke.rs.le.interceptor.BaseIntercepting.java
houtbecke.rs.le.interceptor.EventSinkUtil.java
houtbecke.rs.le.interceptor.InterceptingLeCharacteristicListener.java
houtbecke.rs.le.interceptor.InterceptingLeDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeDevice.java
houtbecke.rs.le.interceptor.InterceptingLeGattCharacteristic.java
houtbecke.rs.le.interceptor.InterceptingLeGattService.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDevice.java
houtbecke.rs.le.interceptor.LeInterceptor.java
houtbecke.rs.le.interceptor.LeLogCatInterceptor.java
houtbecke.rs.le.interceptor.LeSessionInterceptor.java
houtbecke.rs.le.mock.LeDeviceMock.java
houtbecke.rs.le.mock.LeGattCharacteristicMock.java
houtbecke.rs.le.mock.LeGattServiceMock.java
houtbecke.rs.le.mock.LeMockController.java
houtbecke.rs.le.mock.LeRemoteDeviceMock.java
houtbecke.rs.le.mock.LeSessionController.java
houtbecke.rs.le.mock.NativeWaitNotify.java
houtbecke.rs.le.mock.SimpleWaitNotify.java
houtbecke.rs.le.mock.WaitNotify.java
houtbecke.rs.le.session.CharacteristicsMockerObject.java
houtbecke.rs.le.session.DeviceMockerObject.java
houtbecke.rs.le.session.EventSinkFiller.java
houtbecke.rs.le.session.EventSink.java
houtbecke.rs.le.session.EventSource.java
houtbecke.rs.le.session.EventType.java
houtbecke.rs.le.session.Event.java
houtbecke.rs.le.session.GattServiceMockerObject.java
houtbecke.rs.le.session.ListEventSinkSource.java
houtbecke.rs.le.session.MockedResponseObject.java
houtbecke.rs.le.session.MockedResponse.java
houtbecke.rs.le.session.MockerObject.java
houtbecke.rs.le.session.Mocker.java
houtbecke.rs.le.session.RemoteDeviceMockerObject.java
houtbecke.rs.le.session.SessionObject.java
houtbecke.rs.le.session.Session.java