Trying to get some automated tests working....

This commit is contained in:
Graham Jones
2023-03-10 12:10:39 +00:00
parent 033c41a675
commit 908e9a8563
7 changed files with 163 additions and 254 deletions

View File

@@ -46,11 +46,6 @@ dependencies {
implementation 'androidx.test:core:1.4.0' implementation 'androidx.test:core:1.4.0'
implementation 'com.google.android.gms:play-services-tflite-java:16.0.0' implementation 'com.google.android.gms:play-services-tflite-java:16.0.0'
implementation 'com.google.android.gms:play-services-tflite-support:16.0.0' implementation 'com.google.android.gms:play-services-tflite-support:16.0.0'
testImplementation 'junit:junit:4.13.2'
// Set this dependency if you want to use Mockito
testImplementation 'org.mockito:mockito-core:4.3.1'
// Set this dependency if you want to use Hamcrest matching
testImplementation 'org.hamcrest:hamcrest-library:2.2'
implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'org.apache.commons:commons-math3:3.6.1' implementation 'org.apache.commons:commons-math3:3.6.1'
@@ -60,13 +55,24 @@ dependencies {
implementation 'com.github.wendykierp:JTransforms:3.1' implementation 'com.github.wendykierp:JTransforms:3.1'
implementation 'com.google.android.gms:play-services-location:+' implementation 'com.google.android.gms:play-services-location:+'
//implementation 'com.github.RohitSurwase.UCE-Handler:uce_handler:1.3' //implementation 'com.github.RohitSurwase.UCE-Handler:uce_handler:1.3'
testImplementation 'org.robolectric:robolectric:4.7.3'
implementation 'com.android.volley:volley:1.2.1' implementation 'com.android.volley:volley:1.2.1'
implementation platform('com.google.firebase:firebase-bom:29.2.0') implementation platform('com.google.firebase:firebase-bom:29.2.0')
implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-analytics'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0' implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
implementation 'com.google.firebase:firebase-firestore' implementation 'com.google.firebase:firebase-firestore'
testImplementation 'junit:junit:4.13.2'
testImplementation "androidx.test:core"
testImplementation 'org.mockito:mockito-core:4.3.1'
//testImplementation 'org.hamcrest:hamcrest-library:2.2'
//testImplementation 'org.robolectric:robolectric:4.7.3'
//androidTestImplementation 'androidx.test:core:1.1.0'
//androidTestImplementation 'androidx.test:runner:1.1.1'
//androidTestImplementation 'androidx.test:rules:1.1.1'
//androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
//androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
} }
repositories { repositories {

View File

@@ -6,10 +6,11 @@ import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
public class SdAlgHr { public class SdAlgHr {
private final static String TAG = "SdAlgHr"; private final static String TAG = "SdAlgHr";
private Context mContext; private Context mContext;
private OsdUtil mUtil;
private boolean mSimpleHrAlarmActive; private boolean mSimpleHrAlarmActive;
private double mSimpleHrAlarmThreshMin; private double mSimpleHrAlarmThreshMin;
private double mSimpleHrAlarmThreshMax; private double mSimpleHrAlarmThreshMax;
@@ -24,11 +25,13 @@ public class SdAlgHr {
private double mAverageHrAlarmThreshMin; private double mAverageHrAlarmThreshMin;
private double mAverageHrAlarmThreshMax; private double mAverageHrAlarmThreshMax;
private ArrayList<Double> mHrHist;
public SdAlgHr(Context context) { public SdAlgHr(Context context) {
Log.d(TAG, "SdAlgHr Constructor"); Log.i(TAG, "SdAlgHr Constructor");
mContext = context; mContext = context;
mUtil = new OsdUtil(mContext, new Handler()); mHrHist = new ArrayList<Double>();
updatePrefs();
} }
public void close() { public void close() {
@@ -47,8 +50,7 @@ public class SdAlgHr {
retVal = Double.parseDouble(prefValStr); retVal = Double.parseDouble(prefValStr);
} catch (Exception ex) { } catch (Exception ex) {
Log.v(TAG, "readDoublePref() - Problem with preference!"); Log.v(TAG, "readDoublePref() - Problem with preference!");
mUtil.writeToSysLogFile(TAG+".readDoublePref() - Problem with preference!"); //mUtil.showToast(TAG+":"+mContext.getString(R.string.problem_parsing_preferences));
mUtil.showToast(TAG+":"+mContext.getString(R.string.problem_parsing_preferences));
} }
return retVal; return retVal;
} }
@@ -59,25 +61,104 @@ public class SdAlgHr {
* - defined in res/xml/prefs.xml * - defined in res/xml/prefs.xml
*/ */
Log.i(TAG, "updatePrefs()"); Log.i(TAG, "updatePrefs()");
mUtil.writeToSysLogFile(TAG+".updatePrefs()");
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(mContext); .getDefaultSharedPreferences(mContext);
mSimpleHrAlarmActive = SP.getBoolean("HRAlarmActive", false); mSimpleHrAlarmActive = SP.getBoolean("HRAlarmActive", false);
mSimpleHrAlarmThreshMin = readDoublePref(SP, "HRThreshMin", "20"); mSimpleHrAlarmThreshMin = readDoublePref(SP, "HRThreshMin", "20");
mSimpleHrAlarmThreshMax = readDoublePref(SP, "HRThreshMax", "150"); mSimpleHrAlarmThreshMax = readDoublePref(SP, "HRThreshMax", "150");
Log.d(TAG,"updatePrefs(): mSimpleHrAlarmActive="+mSimpleHrAlarmActive);
Log.d(TAG,"updatePrefs(): mSimpleHrAlarmThreshMin="+mSimpleHrAlarmThreshMin);
Log.d(TAG,"updatePrefs(): mSimpleHrAlarmThreshMax="+mSimpleHrAlarmThreshMax);
mAdaptiveHrAlarmActive = SP.getBoolean("HRAdaptiveAlarmActive", false); mAdaptiveHrAlarmActive = SP.getBoolean("HRAdaptiveAlarmActive", false);
mAdaptiveHrAlarmWindowSecs = readDoublePref(SP, "HRAdaptiveAlarmWindowSecs", "30"); mAdaptiveHrAlarmWindowSecs = readDoublePref(SP, "HRAdaptiveAlarmWindowSecs", "30");
mAdaptiveHrAlarmWindowDp = (int)Math.round(mAdaptiveHrAlarmWindowSecs/5.0); mAdaptiveHrAlarmWindowDp = (int)Math.round(mAdaptiveHrAlarmWindowSecs/5.0);
mAdaptiveHrAlarmThresh = readDoublePref(SP, "HRAdaptiveAlarmThresh", "20"); mAdaptiveHrAlarmThresh = readDoublePref(SP, "HRAdaptiveAlarmThresh", "20");
Log.d(TAG,"updatePrefs(): mAdaptiveHrAlarmActive="+mAdaptiveHrAlarmActive);
Log.d(TAG,"updatePrefs(): mAdaptiveHrWindowSecs="+mAdaptiveHrAlarmWindowSecs);
Log.d(TAG,"updatePrefs(): mAdaptiveHrWindowDp="+mAdaptiveHrAlarmWindowDp);
Log.d(TAG,"updatePrefs(): mAdaptiveHrAlarmThresh="+mAdaptiveHrAlarmThresh);
mAverageHrAlarmActive = SP.getBoolean("HRAverageAlarmActive", false); mAverageHrAlarmActive = SP.getBoolean("HRAverageAlarmActive", false);
mAverageHrAlarmWindowSecs = readDoublePref(SP, "HRAverageAlarmWindowSecs", "120"); mAverageHrAlarmWindowSecs = readDoublePref(SP, "HRAverageAlarmWindowSecs", "120");
mAverageHrAlarmWindowDp = (int)Math.round(mAverageHrAlarmWindowSecs/5.0); mAverageHrAlarmWindowDp = (int)Math.round(mAverageHrAlarmWindowSecs/5.0);
mAverageHrAlarmThreshMin = readDoublePref(SP, "HRAverageAlarmThreshMin", "40"); mAverageHrAlarmThreshMin = readDoublePref(SP, "HRAverageAlarmThreshMin", "40");
mAverageHrAlarmThreshMax = readDoublePref(SP, "HRAverageAlarmThreshMax", "120"); mAverageHrAlarmThreshMax = readDoublePref(SP, "HRAverageAlarmThreshMax", "120");
Log.d(TAG,"updatePrefs(): mAverageHrAlarmActive="+mAverageHrAlarmActive);
Log.d(TAG,"updatePrefs(): mAverageHrAlarmWindowSecs="+mAverageHrAlarmWindowSecs);
Log.d(TAG,"updatePrefs(): mAverageHrAlarmWindowDp="+mAverageHrAlarmWindowDp);
Log.d(TAG,"updatePrefs(): mAverageHrAlarmThreshMin="+mAverageHrAlarmThreshMin);
Log.d(TAG,"updatePrefs(): mAverageHrAlarmThreshMax="+mAverageHrAlarmThreshMax);
} }
private void addToHist(double hrVal) {
/**
* Add value hrVal to the heart rate history list, truncating the list if it is
* longer than the required length.
*/
Log.d(TAG,"addToHist() - length before="+mHrHist.size());
mHrHist.add(hrVal);
Log.d(TAG,"addToHist() - length before="+mHrHist.size());
}
private boolean checkSimpleHr(double hrVal) {
/**
* Check heart rate value against simple thresholds
*/
boolean retVal = false;
if (mSimpleHrAlarmActive) {
if ((hrVal > mSimpleHrAlarmThreshMax)
|| (hrVal <mSimpleHrAlarmThreshMin)) {
retVal = true;
}
}
return(retVal);
}
private boolean checkAdaptiveHr(double hrVal) {
// FIXME Make this do something
return(false);
}
private boolean checkAverageHr(double hrVal) {
// FIXME Make this do something
return(false);
}
public double getAverageHrVal() {
double hrSum = 0.;
int hrCount = 0;
double retVal;
for (int n=0; n<mHrHist.size(); n++) {
if (mHrHist.get(n) > -1) {
hrSum += mHrHist.get(n);
hrCount++;
}
}
if (hrCount>0) {
retVal = hrSum / hrCount;
} else {
retVal = -1;
}
return(retVal);
}
public ArrayList<Boolean> checkHr(double hrVal) {
/**
* Checks the current Heart Rate reading hrVal against the
* three possible heart rate alarm algorithms (simple, adaptive, average)
* and returns an ArrayList of the alarm status of each algorithm in the above order.
* true=ALARM, false=OK.
*/
Log.v(TAG, "checkHr("+hrVal+")");
addToHist(hrVal);
ArrayList<Boolean> retVal = new ArrayList<Boolean>();
retVal.add(checkSimpleHr(hrVal));
retVal.add(checkAdaptiveHr(hrVal));
retVal.add(checkAverageHr(hrVal));
return(retVal);
}
} }

View File

@@ -1,95 +0,0 @@
package uk.org.openseizuredetector;
import android.os.Build;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.O_MR1}, packageName = "uk.org.openseizuredetector")
public class LogManagerTest extends TestCase {
public void tearDown() throws Exception {
}
SdData getFakeSdData() {
return null;
}
@Test
public void testWriteToLocalDb() {
//SdData sd1 = getFakeSdData();
//mLm.writeToLocalDb(sd1);
assertTrue(true);
}
public void testGetDatapointById() {
}
public void testSetDatapointToUploaded() {
}
public void testSetDatapointStatus() {
}
public void testGetDatapointsByDate() {
}
public void testGetEventsList() {
}
public void testPruneLocalDb() {
}
public void testGetNextEventToUpload() {
}
public void testGetNearestDatapointToDate() {
}
public void testGetLocalEventsCount() {
}
public void testGetLocalDatapointsCount() {
}
public void testWriteToRemoteServer() {
}
public void testUploadSdData() {
}
public void testAuthCallback() {
}
public void testFinishUpload() {
}
public void testEventCallback() {
}
public void testUploadNextDatapoint() {
}
public void testDatapointCallback() {
}
public void testClose() {
}
public void testStopRemoteLogTimer() {
}
public void testStopAutoPruneTimer() {
}
}

View File

@@ -1,39 +0,0 @@
package uk.org.openseizuredetector;
import android.app.Activity;
import android.os.Handler;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
/**
* Created by graham on 01/01/16.
*/
public class OsdUtilTest {
@Test
public void testIsServerRunning() throws Exception {
}
@Test
public void testStartServer() throws Exception {
//Activity a = new Activity();
Handler handler = new Handler();
OsdUtil util = new OsdUtil(null,handler);
assertThat(util.isServerRunning(), is(true));
assertThat(true, is (true));
//assertThat(true, is(false));
}
@Test
public void testStopServer() throws Exception {
}
}

View File

@@ -0,0 +1,63 @@
package uk.org.openseizuredetector;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class SdAlgHrTest extends TestCase {
private SdAlgHr mSdAlgHr = null;
//@Mock
private Context mContext;
private SharedPreferences sharedPrefs;
private static PreferenceManager mPreferenceManager;
@Before
public void setUp() throws Exception {
super.setUp();
//mContext = ApplicationProvider.getApplicationContext();
this.sharedPrefs = Mockito.mock(SharedPreferences.class);
this.mContext = Mockito.mock(Context.class);
mPreferenceManager = Mockito.mock(PreferenceManager.class);
//Mockito.when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
Mockito.when(mPreferenceManager.getDefaultSharedPreferences(any())).thenReturn(sharedPrefs);
mSdAlgHr = new SdAlgHr(mContext);
assertNotNull(mSdAlgHr);
}
public void tearDown() throws Exception {
}
@Test
public void testCheckHr() throws Exception{
setUp();
assertNotNull(mSdAlgHr);
mSdAlgHr.checkHr(60.);
mSdAlgHr.checkHr(70.);
mSdAlgHr.checkHr(80.);
double hrAv = mSdAlgHr.getAverageHrVal();
assertEquals(hrAv, 70);
}
}

View File

@@ -1,45 +0,0 @@
package uk.org.openseizuredetector;
import android.os.Build;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.O_MR1}, packageName = "uk.org.openseizuredetector")
public class SdDataTest extends TestCase {
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
}
@Test
public void testConstructor() {
SdData sd = new SdData();
assertTrue(true);
}
public void testFromJSON() {
}
public void testTestToString() {
}
public void testToJSON() {
}
public void testToDataString() {
}
public void testToCSVString() {
}
}

View File

@@ -1,62 +0,0 @@
package uk.org.openseizuredetector;
import static org.junit.Assert.*;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;
import com.google.firebase.FirebaseApp;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class WebApiConnectionTest {
WebApiConnection mWac;
@Before
public void setUp() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
FirebaseApp.initializeApp(context);
mWac = new WebApiConnection(context);
}
@After
public void tearDown() throws Exception {
}
@Test
public void isLoggedIn() {
assertTrue(mWac.isLoggedIn());
assertFalse(mWac.isLoggedIn());
}
@Test
public void createEvent() {
}
@Test
public void getEvent() {
}
@Test
public void getEvents() {
}
@Test
public void updateEvent() {
}
@Test
public void createDatapoint() {
}
@Test
public void getUserProfile() {
}
}