mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-09-03 21:15:37 +02:00
Revert "Revert "Adjust test case setup and fix failing tests""
This reverts commit b0384e90d5
.
This commit is contained in:
parent
c4f8f86c00
commit
f1fbab7dd9
@ -88,12 +88,19 @@ public class GBApplication extends Application {
|
|||||||
|
|
||||||
public static final String ACTION_QUIT
|
public static final String ACTION_QUIT
|
||||||
= "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit";
|
= "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit";
|
||||||
|
|
||||||
|
private static GBApplication app;
|
||||||
|
|
||||||
private static Logging logging = new Logging() {
|
private static Logging logging = new Logging() {
|
||||||
@Override
|
@Override
|
||||||
protected String createLogDirectory() throws IOException {
|
protected String createLogDirectory() throws IOException {
|
||||||
|
if (GBEnvironment.env().isLocalTest()) {
|
||||||
|
return System.getProperty(Logging.PROP_LOGFILES_DIR);
|
||||||
|
} else {
|
||||||
File dir = FileUtils.getExternalFilesDir();
|
File dir = FileUtils.getExternalFilesDir();
|
||||||
return dir.getAbsolutePath();
|
return dir.getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private DeviceManager deviceManager;
|
private DeviceManager deviceManager;
|
||||||
@ -110,12 +117,17 @@ public class GBApplication extends Application {
|
|||||||
// don't do anything here, add it to onCreate instead
|
// don't do anything here, add it to onCreate instead
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Logging getLogging() {
|
||||||
|
return logging;
|
||||||
|
}
|
||||||
|
|
||||||
protected DeviceService createDeviceService() {
|
protected DeviceService createDeviceService() {
|
||||||
return new GBDeviceService(this);
|
return new GBDeviceService(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
app = this;
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
if (lockHandler != null) {
|
if (lockHandler != null) {
|
||||||
@ -137,9 +149,12 @@ public class GBApplication extends Application {
|
|||||||
|
|
||||||
setupExceptionHandler();
|
setupExceptionHandler();
|
||||||
|
|
||||||
GB.environment = GBEnvironment.createDeviceEnvironment();
|
if (!GBEnvironment.isEnvironmentSetup()) {
|
||||||
|
GBEnvironment.setupEnvironment(GBEnvironment.createDeviceEnvironment());
|
||||||
setupDatabase(this);
|
// setup db after the environment is set up, but don't do it in test mode
|
||||||
|
// in test mode, it's done individually, see TestBase
|
||||||
|
setupDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
deviceManager = new DeviceManager(this);
|
deviceManager = new DeviceManager(this);
|
||||||
|
|
||||||
@ -195,8 +210,14 @@ public class GBApplication extends Application {
|
|||||||
return prefs.getBoolean("minimize_priority", false);
|
return prefs.getBoolean("minimize_priority", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setupDatabase(Context context) {
|
public void setupDatabase() {
|
||||||
DBOpenHelper helper = new DBOpenHelper(context, DATABASE_NAME, null);
|
DaoMaster.OpenHelper helper;
|
||||||
|
GBEnvironment env = GBEnvironment.env();
|
||||||
|
if (env.isTest()) {
|
||||||
|
helper = new DaoMaster.DevOpenHelper(this, null, null);
|
||||||
|
} else {
|
||||||
|
helper = new DBOpenHelper(this, DATABASE_NAME, null);
|
||||||
|
}
|
||||||
SQLiteDatabase db = helper.getWritableDatabase();
|
SQLiteDatabase db = helper.getWritableDatabase();
|
||||||
DaoMaster daoMaster = new DaoMaster(db);
|
DaoMaster daoMaster = new DaoMaster(db);
|
||||||
if (lockHandler == null) {
|
if (lockHandler == null) {
|
||||||
@ -471,4 +492,8 @@ public class GBApplication extends Application {
|
|||||||
public DeviceManager getDeviceManager() {
|
public DeviceManager getDeviceManager() {
|
||||||
return deviceManager;
|
return deviceManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static GBApplication app() {
|
||||||
|
return app;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,10 @@ package nodomain.freeyourgadget.gadgetbridge;
|
|||||||
* Some more or less useful utility methods to aid local (non-device) testing.
|
* Some more or less useful utility methods to aid local (non-device) testing.
|
||||||
*/
|
*/
|
||||||
public class GBEnvironment {
|
public class GBEnvironment {
|
||||||
|
// DO NOT USE A LOGGER HERE. Will break LoggingTest!
|
||||||
|
// private static final Logger LOG = LoggerFactory.getLogger(GBEnvironment.class);
|
||||||
|
|
||||||
|
private static GBEnvironment environment;
|
||||||
private boolean localTest;
|
private boolean localTest;
|
||||||
private boolean deviceTest;
|
private boolean deviceTest;
|
||||||
|
|
||||||
@ -41,4 +45,15 @@ public class GBEnvironment {
|
|||||||
return localTest;
|
return localTest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static synchronized GBEnvironment env() {
|
||||||
|
return environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
static synchronized boolean isEnvironmentSetup() {
|
||||||
|
return environment != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized static void setupEnvironment(GBEnvironment env) {
|
||||||
|
environment = env;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import android.database.sqlite.SQLiteDatabase;
|
|||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.database.DBOpenHelper;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
|
|
||||||
@ -36,7 +35,7 @@ public class LockHandler implements DBHandler {
|
|||||||
public LockHandler() {
|
public LockHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(DaoMaster daoMaster, DBOpenHelper helper) {
|
public void init(DaoMaster daoMaster, DaoMaster.OpenHelper helper) {
|
||||||
if (isValid()) {
|
if (isValid()) {
|
||||||
throw new IllegalStateException("DB must be closed before initializing it again");
|
throw new IllegalStateException("DB must be closed before initializing it again");
|
||||||
}
|
}
|
||||||
@ -82,7 +81,7 @@ public class LockHandler implements DBHandler {
|
|||||||
throw new IllegalStateException("session must be null");
|
throw new IllegalStateException("session must be null");
|
||||||
}
|
}
|
||||||
// this will create completely new db instances and in turn update this handler through #init()
|
// this will create completely new db instances and in turn update this handler through #init()
|
||||||
GBApplication.setupDatabase(GBApplication.getContext());
|
GBApplication.app().setupDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,6 +34,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import de.greenrobot.dao.query.QueryBuilder;
|
import de.greenrobot.dao.query.QueryBuilder;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBException;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncState;
|
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncState;
|
||||||
@ -97,13 +98,18 @@ public class CalendarReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void syncCalendar(List<CalendarEvents.CalendarEvent> eventList) {
|
public void syncCalendar(List<CalendarEvents.CalendarEvent> eventList) {
|
||||||
LOG.info("Syncing with calendar.");
|
|
||||||
Hashtable<Long, CalendarEvents.CalendarEvent> eventTable = new Hashtable<>();
|
|
||||||
|
|
||||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||||
DaoSession session = dbHandler.getDaoSession();
|
DaoSession session = dbHandler.getDaoSession();
|
||||||
Long deviceId = DBHelper.getDevice(mGBDevice, session).getId();
|
syncCalendar(eventList, session);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
GB.toast("Database Error while syncing Calendar", Toast.LENGTH_SHORT, GB.ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void syncCalendar(List<CalendarEvents.CalendarEvent> eventList, DaoSession session) {
|
||||||
|
LOG.info("Syncing with calendar.");
|
||||||
|
Hashtable<Long, CalendarEvents.CalendarEvent> eventTable = new Hashtable<>();
|
||||||
|
Long deviceId = DBHelper.getDevice(mGBDevice, session).getId();
|
||||||
QueryBuilder<CalendarSyncState> qb = session.getCalendarSyncStateDao().queryBuilder();
|
QueryBuilder<CalendarSyncState> qb = session.getCalendarSyncStateDao().queryBuilder();
|
||||||
|
|
||||||
|
|
||||||
@ -162,11 +168,6 @@ public class CalendarReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
updateEvents(deviceId, session);
|
updateEvents(deviceId, session);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
GB.toast("Database Error while syncing Calendar", Toast.LENGTH_SHORT, GB.ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateEvents(Long deviceId, DaoSession session) {
|
private void updateEvents(Long deviceId, DaoSession session) {
|
||||||
|
@ -38,6 +38,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
// Don't use slf4j here -- would be a bootstrapping problem
|
// Don't use slf4j here -- would be a bootstrapping problem
|
||||||
@ -209,10 +210,12 @@ public class FileUtils {
|
|||||||
|
|
||||||
// the first directory is also the primary external storage, i.e. the same as Environment.getExternalFilesDir()
|
// the first directory is also the primary external storage, i.e. the same as Environment.getExternalFilesDir()
|
||||||
// TODO: check the mount state of *all* dirs when switching to later API level
|
// TODO: check the mount state of *all* dirs when switching to later API level
|
||||||
|
if (!GBEnvironment.env().isLocalTest()) { // don't do this with robolectric
|
||||||
if (i == 0 && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
if (i == 0 && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
|
||||||
GB.log("ignoring unmounted external storage dir: " + dir, GB.INFO, null);
|
GB.log("ignoring unmounted external storage dir: " + dir, GB.INFO, null);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
result.add(dir); // add last
|
result.add(dir); // add last
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -60,10 +60,9 @@ public class GB {
|
|||||||
public static final String DISPLAY_MESSAGE_MESSAGE = "message";
|
public static final String DISPLAY_MESSAGE_MESSAGE = "message";
|
||||||
public static final String DISPLAY_MESSAGE_DURATION = "duration";
|
public static final String DISPLAY_MESSAGE_DURATION = "duration";
|
||||||
public static final String DISPLAY_MESSAGE_SEVERITY = "severity";
|
public static final String DISPLAY_MESSAGE_SEVERITY = "severity";
|
||||||
public static GBEnvironment environment;
|
|
||||||
|
|
||||||
public static Notification createNotification(String text, boolean connected, Context context) {
|
public static Notification createNotification(String text, boolean connected, Context context) {
|
||||||
if (env().isLocalTest()) {
|
if (GBEnvironment.env().isLocalTest()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
|
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
|
||||||
@ -227,7 +226,7 @@ public class GB {
|
|||||||
*/
|
*/
|
||||||
public static void toast(final Context context, final String message, final int displayTime, final int severity, final Throwable ex) {
|
public static void toast(final Context context, final String message, final int displayTime, final int severity, final Throwable ex) {
|
||||||
log(message, severity, ex); // log immediately, not delayed
|
log(message, severity, ex); // log immediately, not delayed
|
||||||
if (env().isLocalTest()) {
|
if (GBEnvironment.env().isLocalTest()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Looper mainLooper = Looper.getMainLooper();
|
Looper mainLooper = Looper.getMainLooper();
|
||||||
@ -358,7 +357,7 @@ public class GB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void updateBatteryNotification(String text, String bigText, Context context) {
|
public static void updateBatteryNotification(String text, String bigText, Context context) {
|
||||||
if (env().isLocalTest()) {
|
if (GBEnvironment.env().isLocalTest()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Notification notification = createBatteryNotification(text, bigText, context);
|
Notification notification = createBatteryNotification(text, bigText, context);
|
||||||
@ -369,10 +368,6 @@ public class GB {
|
|||||||
removeNotification(NOTIFICATION_ID_LOW_BATTERY, context);
|
removeNotification(NOTIFICATION_ID_LOW_BATTERY, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GBEnvironment env() {
|
|
||||||
return environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void assertThat(boolean condition, String errorMessage) {
|
public static void assertThat(boolean condition, String errorMessage) {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
throw new AssertionError(errorMessage);
|
throw new AssertionError(errorMessage);
|
||||||
|
@ -6,7 +6,9 @@ import org.junit.Test;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncStateDao;
|
import nodomain.freeyourgadget.gadgetbridge.entities.CalendarSyncStateDao;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.CalendarReceiver;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents;
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents;
|
||||||
|
@ -6,8 +6,8 @@ import org.junit.After;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||||
|
|
||||||
@ -25,16 +25,12 @@ public class LoggingTest extends TestBase {
|
|||||||
public LoggingTest() throws Exception {
|
public LoggingTest() throws Exception {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Logging logging = new Logging() {
|
private Logging logging = GBApplication.getLogging();
|
||||||
@Override
|
|
||||||
protected String createLogDirectory() throws IOException {
|
|
||||||
return logFilesDir.getAbsolutePath();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() throws Exception {
|
||||||
|
super.tearDown();
|
||||||
assertTrue(FileUtils.deleteRecursively(getLogFilesDir()));
|
assertTrue(FileUtils.deleteRecursively(getLogFilesDir()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.test;
|
package nodomain.freeyourgadget.gadgetbridge.test;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -16,9 +15,9 @@ import java.io.File;
|
|||||||
import ch.qos.logback.classic.util.ContextInitializer;
|
import ch.qos.logback.classic.util.ContextInitializer;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.GBEnvironment;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
import nodomain.freeyourgadget.gadgetbridge.Logging;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
|
||||||
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
||||||
@ -46,9 +45,10 @@ public abstract class TestBase {
|
|||||||
// Make sure logging is set up for all testcases, so that we can debug problems
|
// Make sure logging is set up for all testcases, so that we can debug problems
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setupSuite() throws Exception {
|
public static void setupSuite() throws Exception {
|
||||||
|
GBEnvironment.setupEnvironment(GBEnvironment.createLocalTestEnvironment());
|
||||||
|
|
||||||
// print everything going to android.util.Log to System.out
|
// print everything going to android.util.Log to System.out
|
||||||
System.setProperty("robolectric.logging", "stdout");
|
System.setProperty("robolectric.logging", "stdout");
|
||||||
// ShadowLog.stream = System.out;
|
|
||||||
|
|
||||||
// properties might be preconfigured in build.gradle because of test ordering problems
|
// properties might be preconfigured in build.gradle because of test ordering problems
|
||||||
String logDir = System.getProperty(Logging.PROP_LOGFILES_DIR);
|
String logDir = System.getProperty(Logging.PROP_LOGFILES_DIR);
|
||||||
@ -69,20 +69,19 @@ public abstract class TestBase {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
app = (GBApplication) RuntimeEnvironment.application;
|
||||||
assertNotNull(app);
|
assertNotNull(app);
|
||||||
assertNotNull(getContext());
|
assertNotNull(getContext());
|
||||||
// doesn't work with Robolectric yet
|
app.setupDatabase();
|
||||||
// dbHandler = GBApplication.acquireDB();
|
dbHandler = GBApplication.acquireDB();
|
||||||
// daoSession = dbHandler.getDaoSession();
|
daoSession = dbHandler.getDaoSession();
|
||||||
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(app, null, null);
|
|
||||||
SQLiteDatabase db = openHelper.getWritableDatabase();
|
|
||||||
daoSession = new DaoMaster(db).newSession();
|
|
||||||
assertNotNull(daoSession);
|
assertNotNull(daoSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
// GBApplication.releaseDB();
|
dbHandler.closeDb();
|
||||||
|
GBApplication.releaseDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected GBDevice createDummyGDevice(String macAddress) {
|
protected GBDevice createDummyGDevice(String macAddress) {
|
||||||
|
Loading…
Reference in New Issue
Block a user