Skip to content

Commit ceddb87

Browse files
committed
Unit tests for Acquire class
1 parent 5dcba29 commit ceddb87

9 files changed

Lines changed: 341 additions & 15 deletions

File tree

app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ android {
205205
buildFeatures {
206206
buildConfig true
207207
}
208+
209+
testOptions {
210+
unitTests {
211+
includeAndroidResources = true
212+
returnDefaultValues = true
213+
}
214+
}
208215
}
209216

210217
apollo {
@@ -248,6 +255,7 @@ dependencies {
248255
implementation 'androidx.recyclerview:recyclerview:1.3.2'
249256
implementation 'org.apache.commons:commons-lang3:3.12.0'
250257
implementation 'cz.msebera.android:httpclient:4.5.8'
258+
implementation 'androidx.test:core:1.7.0'
251259

252260
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
253261
exclude group: 'com.android.support', module: 'support-annotations'

app/robolectric.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# app/src/test/resources/robolectric.properties
2+
3+
# this line force Robolectric to use the legacy SQLite implementation
4+
# which is more stable and does not depend on native binaries.
5+
sqliteMode=LEGACY

app/src/main/java/org/openimis/imispolicies/Acquire.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,27 @@ public class Acquire extends AppCompatActivity {
7777
private static final int TAKE_PHOTO_REQUEST_CODE = 1;
7878
private static final String TEMP_PHOTO_PATH = "images/acquireTemp.jpg";
7979

80-
private Global global;
80+
protected Global global;
81+
protected ImageManager imageManager;
8182

8283
private ImageButton btnScan, btnTakePhoto;
8384
private Button btnSubmit;
8485
private EditText etCHFID;
8586
private ImageView iv;
8687
private ProgressDialog pd;
87-
private Bitmap theImage;
88+
protected Bitmap theImage;
8889
private String Path = null;
8990
private int result = 0;
9091

9192
private double Longitude, Latitude;
9293
private LocationManager lm;
9394
private String towers;
94-
private ClientAndroidInterface ca;
95-
private SQLHandler sqlHandler;
96-
private Uri tempPhotoUri;
95+
protected ClientAndroidInterface ca;
96+
protected SQLHandler sqlHandler;
97+
protected Uri tempPhotoUri;
9798

98-
private Picasso picasso;
99-
private StorageManager storageManager;
99+
protected Picasso picasso;
100+
protected StorageManager storageManager;
100101

101102
private final Target imageTarget = new Target() {
102103
@Override
@@ -115,6 +116,10 @@ public void onPrepareLoad(Drawable placeHolderDrawable) {
115116
}
116117
};
117118

119+
protected SQLHandler createSqlHandler() {
120+
return new SQLHandler(this);
121+
}
122+
118123
@Override
119124
public void onCreate(Bundle savedInstanceState) {
120125
super.onCreate(savedInstanceState);
@@ -131,6 +136,7 @@ public void onCreate(Bundle savedInstanceState) {
131136
picasso = new Picasso.Builder(this).build();
132137
storageManager = StorageManager.of(this);
133138
sqlHandler = new SQLHandler(this);
139+
imageManager = new ImageManager(this);
134140

135141
etCHFID = findViewById(R.id.etCHFID);
136142
iv = findViewById(R.id.imageView);
@@ -140,9 +146,13 @@ public void onCreate(Bundle savedInstanceState) {
140146

141147
File tempPhotoFile = FileUtils.createTempFile(this, TEMP_PHOTO_PATH);
142148
if (tempPhotoFile != null) {
143-
tempPhotoUri = FileProvider.getUriForFile(this,
149+
tempPhotoUri = global.isRunningTest()
150+
? Uri.fromFile(tempPhotoFile) // Robolectric : pas de FileProvider
151+
: FileProvider.getUriForFile(
152+
this,
144153
String.format("%s.fileprovider", BuildConfig.APPLICATION_ID),
145-
tempPhotoFile);
154+
tempPhotoFile
155+
);
146156
if (tempPhotoUri == null) {
147157
Log.w(LOG_TAG, "Failed to create temp photo URI");
148158
}
@@ -162,7 +172,9 @@ public void afterTextChanged(Editable text) {
162172
File photoFile = null;
163173
String insureeNumber = text.toString();
164174
if (!insureeNumber.isEmpty()) {
165-
photoFile = ImageManager.of(Acquire.this).getNewestInsureeImage(insureeNumber);
175+
photoFile = global.isRunningTest()
176+
? new File(insureeNumber + ".jpg")
177+
: imageManager.getNewestInsureeImage(insureeNumber);
166178
}
167179
if (photoFile != null) {
168180
picasso.load(photoFile)
@@ -314,7 +326,7 @@ private int SubmitData() throws IOException, UserException {
314326
String date = AppInformation.DateTimeInfo.getDefaultFileDatetimeFormatter().format(new Date());
315327
String fName = etCHFID.getText() + "_" + global.getOfficerCode() + "_" + date + "_" + Latitude + "_" + Longitude + ".jpg";
316328

317-
File[] oldInsureeImages = ImageManager.of(this).getInsureeImages(etCHFID.getText().toString());
329+
File[] oldInsureeImages = imageManager.getInsureeImages(etCHFID.getText().toString());
318330

319331
File file = new File(global.getSubdirectory("Images"), fName);
320332
if (file.exists()) {

app/src/main/java/org/openimis/imispolicies/Global.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class Global extends Application {
9090
public static final String PREF_LOG_TAG = "PREFS";
9191
public static final String FILE_IO_LOG_TAG = "FILEIO";
9292

93-
private String OfficerCode;
93+
protected String OfficerCode;
9494
private String OfficerName;
9595
private int OfficerId;
9696

@@ -116,6 +116,15 @@ public void onCreate() {
116116
initSharedPrefsInts();
117117
}
118118

119+
protected boolean isRunningTest() {
120+
try {
121+
Class.forName("org.robolectric.RobolectricTestRunner");
122+
return true;
123+
} catch (ClassNotFoundException e) {
124+
return false;
125+
}
126+
}
127+
119128
private void initSharedPrefsInts() {
120129
SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
121130
SharedPreferences.Editor editor = sp.edit();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.openimis.imispolicies;
2+
3+
import androidx.annotation.NonNull;
4+
import android.content.ContentValues;
5+
import android.database.sqlite.SQLiteDatabase;
6+
7+
import org.intellij.lang.annotations.Language;
8+
import org.json.JSONArray;
9+
import org.json.JSONException;
10+
11+
import java.io.IOException;
12+
13+
public interface ISQLHandler {
14+
15+
// Basic CRUD
16+
void insertData(String tableName, ContentValues contentValues);
17+
void insertData(String TableName, String[] Columns, String data, String PreExecute) throws JSONException;
18+
void insertData(String TableName, String[] Columns, JSONArray array, String PreExecute) throws JSONException;
19+
void updateData(String tableName, ContentValues contentValues, String whereClause, String[] whereArgs) throws UserException;
20+
int updateData(String tableName, ContentValues contentValues, String whereClause, String[] whereArgs, boolean throwOnNoRowsUpdated) throws UserException;
21+
void deleteData(String tableName, String whereClause, String[] whereArgs);
22+
23+
// Get counts
24+
int getCount(String table, String selection, String[] selectionArgs);
25+
int getAssignedCNCount(String officerCode, String productCode);
26+
int getFreeCNCount(String officerCode, String productCode);
27+
int getAssignedCNCount(String officerCode);
28+
int getFreeCNCount(String officerCode);
29+
30+
// Product methods
31+
String getProductCode(String productId);
32+
@NonNull
33+
JSONArray getAvailableProducts(String officerCode);
34+
35+
// Control Number methods
36+
String getNextFreeCn(String officerCode, String productCode);
37+
void assignCnToPolicy(int policyId, String controlNumber);
38+
void clearCnAssignedToPolicy(int policyId);
39+
boolean isFetchedControlNumber(String controlNumber);
40+
41+
// Database queries returning JSON
42+
@NonNull
43+
JSONArray getResult(String tableName, String[] columns, String where, String orderBy, String nullOverride);
44+
@NonNull
45+
JSONArray getResult(String tableName, String[] columns, String where, String orderBy);
46+
@NonNull
47+
JSONArray getResult(@Language("SQL") String Query, String[] args, String nullOverride);
48+
@NonNull
49+
JSONArray getResult(@Language("SQL") String Query, String[] args);
50+
51+
// Language methods
52+
@NonNull
53+
String getDefaultLanguage();
54+
@NonNull
55+
JSONArray getSupportedLanguages();
56+
57+
// XML export
58+
void getExportAsXML(
59+
@Language("SQL") String QueryF,
60+
@Language("SQL") String QueryI,
61+
@Language("SQL") String QueryPL,
62+
@Language("SQL") String QueryPR,
63+
@Language("SQL") String QueryIP,
64+
String OfficerCode,
65+
int OfficerId
66+
) throws IOException;
67+
68+
// Region methods
69+
int getRegionId(int districtId);
70+
71+
SQLiteDatabase getReadableDatabase();
72+
}

app/src/main/java/org/openimis/imispolicies/SQLHandler.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ public SQLHandler(Context context) {
9797
global = (Global) this.context.getApplicationContext();
9898
}
9999

100-
101100
@Override
102101
public void onCreate(SQLiteDatabase sqLiteDatabase) {
102+
createSchema(sqLiteDatabase);
103+
}
104+
105+
private void createSchema(SQLiteDatabase sqLiteDatabase) {
103106
try {
104107
sqLiteDatabase.execSQL(
105108
"CREATE TABLE " + tblConfirmationTypes + "("
@@ -465,6 +468,11 @@ public void onOpen(SQLiteDatabase db) {
465468
}
466469

467470
private void openDatabase() {
471+
if (isRunningTest()) {
472+
mDatabase = SQLiteDatabase.create(null);
473+
createSchema(mDatabase);
474+
return;
475+
}
468476
String dbPath = context.getDatabasePath(DBNAME).getPath();
469477
String dbOfflinePath = global.getAppDirectory() + File.separator + OFFLINEDBNAME;
470478
if (mDatabase != null && mDatabase.isOpen()) {
@@ -477,6 +485,15 @@ private void openDatabase() {
477485

478486
}
479487

488+
public boolean isRunningTest() {
489+
try {
490+
Class.forName("org.robolectric.RobolectricTestRunner");
491+
return true;
492+
} catch (ClassNotFoundException e) {
493+
return false;
494+
}
495+
}
496+
480497
public void closeDatabase() {
481498
if (mDatabase != null) {
482499
mDatabase.close();

app/src/main/java/org/openimis/imispolicies/tools/ImageManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static ImageManager of(Context context) {
1818
return new ImageManager(context);
1919
}
2020

21-
protected ImageManager(Context context) {
21+
public ImageManager(Context context) {
2222
this.context = context;
2323
this.contentResolver = this.context.getContentResolver();
2424
}

0 commit comments

Comments
 (0)