mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-14 11:01:06 +01:00
f258e62633
- created and provided by DeviceHelper - passed from UI to service - without UI, service uses DeviceHelper directly => Cleaner and less duplicated code
82 lines
2.7 KiB
Java
82 lines
2.7 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.service;
|
|
|
|
import android.content.Context;
|
|
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.mockito.InOrder;
|
|
import org.mockito.Mock;
|
|
import org.mockito.Mockito;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
public class DeviceCommunicationServiceTestCase extends AbstractServiceTestCase<DeviceCommunicationService> {
|
|
private static final java.lang.String TEST_DEVICE_ADDRESS = TestDeviceSupport.class.getName();
|
|
|
|
/**
|
|
* Factory that always returns the mockSupport instance
|
|
*/
|
|
private class TestDeviceSupportFactory extends DeviceSupportFactory {
|
|
public TestDeviceSupportFactory(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
@Override
|
|
public synchronized DeviceSupport createDeviceSupport(GBDevice device) throws GBException {
|
|
return mockSupport;
|
|
}
|
|
}
|
|
|
|
private TestDeviceService mDeviceService;
|
|
@Mock
|
|
private TestDeviceSupport realSupport;
|
|
private TestDeviceSupport mockSupport;
|
|
|
|
public DeviceCommunicationServiceTestCase() {
|
|
super(DeviceCommunicationService.class);
|
|
}
|
|
|
|
@Before
|
|
public void setUp() throws Exception {
|
|
super.setUp();
|
|
mockSupport = null;
|
|
realSupport = new TestDeviceSupport();
|
|
realSupport.setContext(new GBDevice(TEST_DEVICE_ADDRESS, "Test Device", DeviceType.TEST), null, getContext());
|
|
mockSupport = Mockito.spy(realSupport);
|
|
getServiceInstance().setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));
|
|
|
|
mDeviceService = new TestDeviceService(this);
|
|
}
|
|
|
|
@Test
|
|
public void testStart() {
|
|
assertFalse("Service was already", getServiceInstance().isStarted());
|
|
mDeviceService.start();
|
|
assertTrue("Service should be started", getServiceInstance().isStarted());
|
|
}
|
|
|
|
@Test
|
|
public void ensureConnected() {
|
|
mDeviceService.connect(realSupport.getDevice());
|
|
Mockito.verify(mockSupport, Mockito.times(1)).connect();
|
|
assertTrue(realSupport.getDevice().isInitialized());
|
|
}
|
|
|
|
@Test
|
|
public void testFindDevice() {
|
|
ensureConnected();
|
|
|
|
InOrder inOrder = Mockito.inOrder(mockSupport);
|
|
mDeviceService.onFindDevice(true);
|
|
mDeviceService.onFindDevice(false);
|
|
inOrder.verify(mockSupport, Mockito.times(1)).onFindDevice(true);
|
|
inOrder.verify(mockSupport, Mockito.times(1)).onFindDevice(false);
|
|
inOrder.verifyNoMoreInteractions();
|
|
}
|
|
}
|