mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
BtBRQueue: Fix unit tests
Regression in the tests introduced by 891655504
These should be refactored, as they can become quite flaky due to the
mocks.
This commit is contained in:
@@ -47,6 +47,7 @@ import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.util.Log;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
|
||||
@@ -810,7 +811,8 @@ public class GBApplication extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
private void migratePrefs(int oldVersion) {
|
||||
@VisibleForTesting
|
||||
protected void migratePrefs(int oldVersion) {
|
||||
SharedPreferences.Editor editor = sharedPrefs.edit();
|
||||
|
||||
// this comes before all other migrations since the new column DeviceTypeName was added as non-null
|
||||
|
||||
@@ -129,6 +129,16 @@ public final class BtBRQueue {
|
||||
public void handleMessage(@NonNull Message msg) {
|
||||
switch (msg.what) {
|
||||
case HANDLER_SUBJECT_CONNECT: {
|
||||
if (mBtSocket == null) {
|
||||
LOG.error("Got request to connect to RFCOMM socket, but it is null");
|
||||
if (!GBApplication.getPrefs().getAutoReconnect(mGbDevice)) {
|
||||
mGbDevice.setUpdateState(GBDevice.State.NOT_CONNECTED, mContext);
|
||||
} else {
|
||||
mGbDevice.setUpdateState(GBDevice.State.WAITING_FOR_RECONNECT, mContext);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
mBtSocket.connect();
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btbr.BtBRQueue;
|
||||
|
||||
@@ -37,28 +39,28 @@ public class TestBtBRQueue {
|
||||
@Test
|
||||
public void connect() {
|
||||
GBDevice device = Mockito.mock(GBDevice.class);
|
||||
when(device.isConnected()).thenReturn(false);
|
||||
when(device.getState()).thenReturn(GBDevice.State.NOT_CONNECTED);
|
||||
|
||||
BluetoothDevice btDevice = Mockito.mock(BluetoothDevice.class);
|
||||
|
||||
BluetoothAdapter btAdapter = Mockito.mock(BluetoothAdapter.class);
|
||||
when(btAdapter.getRemoteDevice((String) any())).thenReturn(btDevice);
|
||||
|
||||
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, null, 512);
|
||||
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512);
|
||||
Assert.assertTrue(queue.connect());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reconnect() {
|
||||
GBDevice device = Mockito.mock(GBDevice.class);
|
||||
when(device.isConnected()).thenReturn(false);
|
||||
when(device.getState()).thenReturn(GBDevice.State.NOT_CONNECTED);
|
||||
|
||||
BluetoothDevice btDevice = Mockito.mock(BluetoothDevice.class);
|
||||
|
||||
BluetoothAdapter btAdapter = Mockito.mock(BluetoothAdapter.class);
|
||||
when(btAdapter.getRemoteDevice((String) any())).thenReturn(btDevice);
|
||||
|
||||
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, null, 512);
|
||||
BtBRQueue queue = new BtBRQueue(btAdapter, device, null, null, UUID.randomUUID(), 512);
|
||||
Assert.assertTrue(queue.connect());
|
||||
|
||||
queue.disconnect();
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
|
||||
public class GBTestApplication extends GBApplication {
|
||||
@Override
|
||||
protected void migratePrefs(final int oldVersion) {
|
||||
// In tests, do not migrate preferences
|
||||
// FIXME: This is not ideal. In tests, do not migrate preferences. We should be able to initialize
|
||||
// the database before the application is created so that this works and is actually testable., but
|
||||
// right now all it does is spam the logs with dozens of stack traces for failed preference migrations
|
||||
Log.i("GBTestApplication", "Skipping preference migration during tests");
|
||||
}
|
||||
}
|
||||
@@ -27,12 +27,12 @@ import static nodomain.freeyourgadget.gadgetbridge.Logging.PROP_LOGFILES_DIR;
|
||||
/**
|
||||
* Base class for all testcases in Gadgetbridge that are supposed to run locally
|
||||
* with robolectric.
|
||||
*
|
||||
* <p>
|
||||
* Important: To run them, create a run configuration and execute them in the Gadgetbridge/app/
|
||||
* directory.
|
||||
*/
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = 21)
|
||||
@Config(sdk = 21, application = GBTestApplication.class)
|
||||
public abstract class TestBase {
|
||||
protected static File logFilesDir;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user