1+ package fr.free.nrw.commons.utils
2+
3+ import android.app.ProgressDialog
4+ import android.content.Context
5+ import android.graphics.Bitmap
6+ import fr.free.nrw.commons.TestCommonsApplication
7+ import fr.free.nrw.commons.location.LatLng
8+ import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
9+ import io.reactivex.disposables.CompositeDisposable
10+ import org.junit.Assert
11+ import org.junit.Before
12+ import org.junit.Test
13+ import org.junit.runner.RunWith
14+ import org.mockito.Mock
15+ import org.mockito.Mockito.`when`
16+ import org.mockito.Mockito.mock
17+ import org.mockito.MockitoAnnotations
18+ import org.robolectric.RobolectricTestRunner
19+ import org.robolectric.RuntimeEnvironment
20+ import org.robolectric.annotation.Config
21+ import org.robolectric.annotation.LooperMode
22+ import java.io.File
23+ import java.lang.reflect.Field
24+ import java.lang.reflect.Method
25+
26+
27+ @RunWith(RobolectricTestRunner ::class )
28+ @Config(sdk = [21 ], application = TestCommonsApplication ::class )
29+ @LooperMode(LooperMode .Mode .PAUSED )
30+ class ImageUtilsTest {
31+
32+ private lateinit var context: Context
33+
34+ @Mock
35+ private lateinit var bitmap: Bitmap
36+
37+ @Mock
38+ private lateinit var progressDialogWallpaper: ProgressDialog
39+
40+ @Mock
41+ private lateinit var okHttpJsonApiClient: OkHttpJsonApiClient
42+
43+ @Mock
44+ private lateinit var compositeDisposable: CompositeDisposable
45+
46+ @Before
47+ fun setup () {
48+ MockitoAnnotations .initMocks(this )
49+ context = RuntimeEnvironment .application.applicationContext
50+ }
51+
52+ @Test
53+ fun testCheckIfImageIsTooDarkCaseException () {
54+ Assert .assertEquals(ImageUtils .checkIfImageIsTooDark(" " ), ImageUtils .IMAGE_OK )
55+ }
56+
57+ @Test
58+ fun testCheckIfImageIsTooDark () {
59+ val tempFile = File .createTempFile(" prefix" , " suffix" )
60+ ImageUtils .checkIfImageIsTooDark(tempFile.absolutePath)
61+ }
62+
63+ @Test
64+ fun testSetWallpaper () {
65+ val mockImageUtils = mock(ImageUtils ::class .java)
66+ val method: Method = ImageUtils ::class .java.getDeclaredMethod(
67+ " setWallpaper" ,
68+ Context ::class .java,
69+ Bitmap ::class .java
70+ )
71+ method.isAccessible = true
72+
73+ `when `(progressDialogWallpaper.isShowing).thenReturn(true )
74+
75+ val progressDialogWallpaperField: Field =
76+ ImageUtils ::class .java.getDeclaredField(" progressDialogWallpaper" )
77+ progressDialogWallpaperField.isAccessible = true
78+ progressDialogWallpaperField.set(mockImageUtils, progressDialogWallpaper)
79+
80+ method.invoke(mockImageUtils, context, bitmap)
81+ }
82+
83+ @Test
84+ fun testShowSettingAvatarProgressBar () {
85+ val mockImageUtils = mock(ImageUtils ::class .java)
86+ val method: Method = ImageUtils ::class .java.getDeclaredMethod(
87+ " showSettingAvatarProgressBar" ,
88+ Context ::class .java
89+ )
90+ method.isAccessible = true
91+ method.invoke(mockImageUtils, context)
92+ }
93+
94+ @Test
95+ fun testGetErrorMessageForResultCase0 () {
96+ ImageUtils .getErrorMessageForResult(context, 0 )
97+ }
98+
99+ @Test
100+ fun testGetErrorMessageForResultCaseIMAGE_DARK () {
101+ ImageUtils .getErrorMessageForResult(context, 1 )
102+ }
103+
104+ @Test
105+ fun testGetErrorMessageForResultCaseIMAGE_BLURRY () {
106+ ImageUtils .getErrorMessageForResult(context, 2 )
107+ }
108+
109+ @Test
110+ fun testGetErrorMessageForResultCaseIMAGE_DUPLICATE () {
111+ ImageUtils .getErrorMessageForResult(context, 4 )
112+ }
113+
114+ @Test
115+ fun testGetErrorMessageForResultCaseIMAGE_GEOLOCATION_DIFFERENT () {
116+ ImageUtils .getErrorMessageForResult(context, 8 )
117+ }
118+
119+ @Test
120+ fun testGetErrorMessageForResultCaseFILE_FBMD () {
121+ ImageUtils .getErrorMessageForResult(context, 16 )
122+ }
123+
124+ @Test
125+ fun testGetErrorMessageForResultCaseFILE_NO_EXIF () {
126+ ImageUtils .getErrorMessageForResult(context, 32 )
127+ }
128+
129+ @Test
130+ fun testSetAvatarFromImageUrl () {
131+ ImageUtils .setAvatarFromImageUrl(
132+ context,
133+ " " ,
134+ " " ,
135+ okHttpJsonApiClient,
136+ compositeDisposable
137+ )
138+ }
139+
140+ @Test
141+ fun testCheckImageGeolocationIsDifferentCaseNull () {
142+ Assert .assertEquals(
143+ ImageUtils .checkImageGeolocationIsDifferent(
144+ " test" ,
145+ null
146+ ), false
147+ )
148+ }
149+
150+ @Test
151+ fun testCheckImageGeolocationIsDifferent () {
152+ Assert .assertEquals(
153+ ImageUtils .checkImageGeolocationIsDifferent(
154+ " 0.0|0.0" ,
155+ LatLng (0.0 , 0.0 , 0f )
156+ ), false
157+ )
158+ }
159+
160+ @Test
161+ fun testCheckIfImageIsDark () {
162+ val mockImageUtils = mock(ImageUtils ::class .java)
163+ val method: Method = ImageUtils ::class .java.getDeclaredMethod(
164+ " checkIfImageIsDark" ,
165+ Bitmap ::class .java
166+ )
167+ method.isAccessible = true
168+ method.invoke(mockImageUtils, null )
169+ }
170+
171+ }
0 commit comments