Skip to content

Commit f897af0

Browse files
authored
Fix upload tests (commons-app#2130)
1 parent 8c083f3 commit f897af0

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ public String getSHA1(InputStream is) {
3939
public FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
4040
return FileUtils.getFileInputStream(filePath);
4141
}
42+
43+
public String getGeolocationOfFile(String filePath) {
44+
return FileUtils.getGeolocationOfFile(filePath);
45+
}
4246
}

app/src/main/java/fr/free/nrw/commons/upload/UploadModel.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import android.graphics.BitmapRegionDecoder;
99
import android.net.Uri;
1010
import android.support.annotation.Nullable;
11-
import android.util.Log;
1211

1312
import java.io.File;
14-
import java.io.FileInputStream;
1513
import java.io.IOException;
1614
import java.util.ArrayList;
1715
import java.util.Date;
@@ -26,7 +24,9 @@
2624
import fr.free.nrw.commons.contributions.Contribution;
2725
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2826
import fr.free.nrw.commons.settings.Prefs;
27+
import fr.free.nrw.commons.utils.BitmapRegionDecoderWrapper;
2928
import fr.free.nrw.commons.utils.ImageUtils;
29+
import fr.free.nrw.commons.utils.ImageUtilsWrapper;
3030
import io.reactivex.Observable;
3131
import io.reactivex.Single;
3232
import io.reactivex.disposables.Disposable;
@@ -57,6 +57,8 @@ public class UploadModel {
5757
private SessionManager sessionManager;
5858
private Uri currentMediaUri;
5959
private FileUtilsWrapper fileUtilsWrapper;
60+
private ImageUtilsWrapper imageUtilsWrapper;
61+
private BitmapRegionDecoderWrapper bitmapRegionDecoderWrapper;
6062
private FileProcessor fileProcessor;
6163

6264
@Inject
@@ -67,9 +69,12 @@ public class UploadModel {
6769
MediaWikiApi mwApi,
6870
SessionManager sessionManager,
6971
FileUtilsWrapper fileUtilsWrapper,
72+
ImageUtilsWrapper imageUtilsWrapper,
73+
BitmapRegionDecoderWrapper bitmapRegionDecoderWrapper,
7074
FileProcessor fileProcessor) {
7175
this.licenses = licenses;
7276
this.prefs = prefs;
77+
this.bitmapRegionDecoderWrapper = bitmapRegionDecoderWrapper;
7378
this.license = Prefs.Licenses.CC_BY_SA_3;
7479
this.licensesByName = licensesByName;
7580
this.context = context;
@@ -78,6 +83,7 @@ public class UploadModel {
7883
this.sessionManager = sessionManager;
7984
this.fileUtilsWrapper = fileUtilsWrapper;
8085
this.fileProcessor = fileProcessor;
86+
this.imageUtilsWrapper = imageUtilsWrapper;
8187
useExtStorage = this.prefs.getBoolean("useExternalStorage", false);
8288
}
8389

@@ -103,8 +109,8 @@ void receive(List<Uri> mediaUri, String mimeType, String source, SimilarImageInt
103109
.map(b -> b ? ImageUtils.IMAGE_DUPLICATE : ImageUtils.IMAGE_OK),
104110
Single.fromCallable(() ->
105111
fileUtilsWrapper.getFileInputStream(filePath))
106-
.map(file -> BitmapRegionDecoder.newInstance(file, false))
107-
.map(ImageUtils::checkIfImageIsTooDark), //Returns IMAGE_DARK or IMAGE_OK
112+
.map(file -> bitmapRegionDecoderWrapper.newInstance(file, false))
113+
.map(imageUtilsWrapper::checkIfImageIsTooDark), //Returns IMAGE_DARK or IMAGE_OK
108114
(dupe, dark) -> dupe | dark)
109115
.observeOn(Schedulers.io())
110116
.subscribe(item.imageQuality::onNext, Timber::e);
@@ -134,15 +140,14 @@ void receiveDirect(Uri media, String mimeType, String source, String wikidataEnt
134140
.map(fileUtilsWrapper::getSHA1)
135141
.map(mwApi::existingFile)
136142
.map(b -> b ? ImageUtils.IMAGE_DUPLICATE : ImageUtils.IMAGE_OK),
137-
Single.fromCallable(() ->
138-
filePath)
139-
.map(FileUtils::getGeolocationOfFile)
140-
.map(geoLocation -> ImageUtils.checkImageGeolocationIsDifferent(geoLocation,wikidataItemLocation))
143+
Single.fromCallable(() -> filePath)
144+
.map(fileUtilsWrapper::getGeolocationOfFile)
145+
.map(geoLocation -> imageUtilsWrapper.checkImageGeolocationIsDifferent(geoLocation,wikidataItemLocation))
141146
.map(r -> r ? ImageUtils.IMAGE_GEOLOCATION_DIFFERENT : ImageUtils.IMAGE_OK),
142147
Single.fromCallable(() ->
143148
fileUtilsWrapper.getFileInputStream(filePath))
144-
.map(file -> BitmapRegionDecoder.newInstance(file, false))
145-
.map(ImageUtils::checkIfImageIsTooDark), //Returns IMAGE_DARK or IMAGE_OK
149+
.map(file -> bitmapRegionDecoderWrapper.newInstance(file, false))
150+
.map(imageUtilsWrapper::checkIfImageIsTooDark), //Returns IMAGE_DARK or IMAGE_OK
146151
(dupe, wrongGeo, dark) -> dupe | wrongGeo | dark).subscribe(item.imageQuality::onNext);
147152
items.add(item);
148153
items.get(0).selected = true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
import android.graphics.BitmapRegionDecoder;
4+
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
8+
import javax.inject.Inject;
9+
import javax.inject.Singleton;
10+
11+
@Singleton
12+
public class BitmapRegionDecoderWrapper {
13+
14+
@Inject
15+
public BitmapRegionDecoderWrapper() {
16+
17+
}
18+
19+
public BitmapRegionDecoder newInstance(FileInputStream file, boolean isSharable) throws IOException {
20+
return BitmapRegionDecoder.newInstance(file, isSharable);
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
import android.graphics.BitmapRegionDecoder;
4+
5+
import javax.inject.Inject;
6+
import javax.inject.Singleton;
7+
8+
import static fr.free.nrw.commons.utils.ImageUtils.*;
9+
10+
@Singleton
11+
public class ImageUtilsWrapper {
12+
13+
@Inject
14+
public ImageUtilsWrapper() {
15+
16+
}
17+
18+
public @Result int checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecoder) {
19+
return ImageUtils.checkIfImageIsTooDark(bitmapRegionDecoder);
20+
}
21+
22+
public boolean checkImageGeolocationIsDifferent(String geolocationOfFileString, String wikidataItemLocationString) {
23+
return ImageUtils.checkImageGeolocationIsDifferent(geolocationOfFileString, wikidataItemLocationString);
24+
}
25+
}

app/src/test/kotlin/fr/free/nrw/commons/upload/UploadModelTest.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import android.app.Application
44
import android.content.ContentResolver
55
import android.content.Context
66
import android.content.SharedPreferences
7+
import android.graphics.BitmapRegionDecoder
78
import android.net.Uri
89
import fr.free.nrw.commons.auth.SessionManager
910
import fr.free.nrw.commons.mwapi.MediaWikiApi
11+
import fr.free.nrw.commons.utils.BitmapRegionDecoderWrapper
12+
import fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK
13+
import fr.free.nrw.commons.utils.ImageUtilsWrapper
1014
import org.junit.After
1115
import org.junit.Assert.assertFalse
1216
import org.junit.Assert.assertTrue
1317
import org.junit.Before
1418
import org.junit.Test
15-
import org.mockito.ArgumentMatchers.any
16-
import org.mockito.ArgumentMatchers.anyString
19+
import org.mockito.ArgumentMatchers.*
1720
import org.mockito.InjectMocks
1821
import org.mockito.Mock
1922
import org.mockito.Mockito.`when`
@@ -45,6 +48,10 @@ class UploadModelTest {
4548
@Mock
4649
internal var fileUtilsWrapper: FileUtilsWrapper? = null
4750
@Mock
51+
internal var imageUtilsWrapper: ImageUtilsWrapper? = null
52+
@Mock
53+
internal var bitmapRegionDecoderWrapper: BitmapRegionDecoderWrapper? = null
54+
@Mock
4855
internal var fileProcessor: FileProcessor? = null
4956

5057
@InjectMocks
@@ -67,6 +74,14 @@ class UploadModelTest {
6774
.thenReturn("sha")
6875
`when`(fileUtilsWrapper!!.getFileInputStream(anyString()))
6976
.thenReturn(mock(FileInputStream::class.java))
77+
`when`(fileUtilsWrapper!!.getGeolocationOfFile(anyString()))
78+
.thenReturn("")
79+
`when`(imageUtilsWrapper!!.checkIfImageIsTooDark(any(BitmapRegionDecoder::class.java)))
80+
.thenReturn(IMAGE_OK)
81+
`when`(imageUtilsWrapper!!.checkImageGeolocationIsDifferent(anyString(), anyString()))
82+
.thenReturn(false)
83+
`when`(bitmapRegionDecoderWrapper!!.newInstance(any(FileInputStream::class.java), anyBoolean()))
84+
.thenReturn(mock(BitmapRegionDecoder::class.java))
7085

7186
}
7287

0 commit comments

Comments
 (0)