package nodomain.freeyourgadget.gadgetbridge.service;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;

import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.test.TestBase;

import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_BODY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class DeviceCommunicationServiceTestCase extends TestBase {
    private static final java.lang.String TEST_DEVICE_ADDRESS = TestDeviceSupport.class.getName();

    /**
     * Factory that always returns the mockSupport instance
     */
    private class TestDeviceSupportFactory extends DeviceSupportFactory {
        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();
    }

    @Override
    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);
        DeviceCommunicationService.setDeviceSupportFactory(new TestDeviceSupportFactory(getContext()));

        mDeviceService = new TestDeviceService(getContext());
    }

    private GBDevice getDevice() {
        return realSupport.getDevice();
    }

    @Override
    public void tearDown() throws Exception {
        mDeviceService.stopService(mDeviceService.createIntent());
        super.tearDown();
    }

    @Test
    public void testNotConnected() {
        GBDevice device = getDevice();
        assertEquals(GBDevice.State.NOT_CONNECTED, device.getState());

        // verify that the events like onFindDevice do not reach the DeviceSupport instance,
        // because not connected
        InOrder inOrder = Mockito.inOrder(mockSupport);
        mDeviceService.onFindDevice(true);
        inOrder.verify(mockSupport, Mockito.times(0)).onFindDevice(true);
        inOrder.verifyNoMoreInteractions();
    }

    @Test
    public void ensureConnected() {
        mDeviceService.start();
        // connection goes synchronously here
        mDeviceService.connect(getDevice());
        Mockito.verify(mockSupport, Mockito.times(1)).connect();
        assertTrue(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();
    }

    @Test
    public void testTransliterationSupport() {
        SharedPreferences settings = GBApplication.getPrefs().getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("transliteration", true);
        editor.commit();

        Intent intent = mDeviceService.createIntent().putExtra(EXTRA_NOTIFICATION_BODY, "Прõсто текčт");
        mDeviceService.invokeService(intent);
        String result = intent.getStringExtra(EXTRA_NOTIFICATION_BODY);

        assertEquals("Transliteration support fail!", "Prosto tekct", result);
    }
}

@@ -4067,11 +4067,569 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testRtlSupport() %7B%0A        SharedPreferences settings = GBApplication.getPrefs().getPreferences();%0A        SharedPreferences.Editor editor = settings.edit();%0A        editor.putBoolean(%22rtl%22, true);%0A        editor.commit();%0A%0A        Intent intent = mDeviceService.createIntent().putExtra(EXTRA_NOTIFICATION_BODY, %22English and %D7%A2%D7%91%D7%A8%D7%99%D7%AA%22);%0A        mDeviceService.invokeService(intent);%0A        String result = intent.getStringExtra(EXTRA_NOTIFICATION_BODY);%0A%0A        assertEquals(%22Rtl support fail!%22, %22%D7%AA%D7%99%D7%A8%D7%91%D7%A2 English and%22, result);%0A    %7D
 %0A%7D%0A

Add Unittest for Rtl + some small fixes