mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Add Unit Tests for widget module (#4381)
This commit is contained in:
		
							parent
							
								
									c0c657324e
								
							
						
					
					
						commit
						2e0f8e6d68
					
				
					 2 changed files with 165 additions and 0 deletions
				
			
		|  | @ -0,0 +1,52 @@ | |||
| package fr.free.nrw.commons.widget | ||||
| 
 | ||||
| import android.app.Activity | ||||
| import fr.free.nrw.commons.TestCommonsApplication | ||||
| import org.junit.Assert | ||||
| import org.junit.Before | ||||
| import org.junit.Test | ||||
| import org.junit.runner.RunWith | ||||
| import org.robolectric.Robolectric | ||||
| import org.robolectric.RobolectricTestRunner | ||||
| import org.robolectric.android.controller.ActivityController | ||||
| import org.robolectric.annotation.Config | ||||
| import java.lang.reflect.Method | ||||
| 
 | ||||
| @RunWith(RobolectricTestRunner::class) | ||||
| @Config(sdk = [21], application = TestCommonsApplication::class) | ||||
| class HeightLimitedRecyclerViewUnitTests { | ||||
| 
 | ||||
|     private lateinit var activityController: ActivityController<Activity> | ||||
|     private lateinit var activity: Activity | ||||
|     private lateinit var recyclerView: HeightLimitedRecyclerView | ||||
| 
 | ||||
| 
 | ||||
|     @Before | ||||
|     fun setUp() { | ||||
|         activityController = Robolectric.buildActivity(Activity::class.java) | ||||
|         activity = activityController.get() | ||||
| 
 | ||||
|         recyclerView = HeightLimitedRecyclerView(activity) | ||||
|         recyclerView = HeightLimitedRecyclerView(activity, null) | ||||
|         recyclerView = HeightLimitedRecyclerView(activity, null, 0) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun checkNotNull() { | ||||
|         Assert.assertNotNull(recyclerView) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testOnMeasure() { | ||||
|         val method: Method = HeightLimitedRecyclerView::class.java.getDeclaredMethod( | ||||
|             "onMeasure", | ||||
|             Int::class.java, | ||||
|             Int::class.java | ||||
|         ) | ||||
|         method.isAccessible = true | ||||
|         method.invoke(recyclerView, 0, 0) | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,113 @@ | |||
| package fr.free.nrw.commons.widget | ||||
| 
 | ||||
| import android.appwidget.AppWidgetManager | ||||
| import android.content.Context | ||||
| import android.widget.RemoteViews | ||||
| import com.facebook.imagepipeline.core.ImagePipelineFactory | ||||
| import com.facebook.soloader.SoLoader | ||||
| import fr.free.nrw.commons.Media | ||||
| import fr.free.nrw.commons.TestAppAdapter | ||||
| import fr.free.nrw.commons.TestCommonsApplication | ||||
| import fr.free.nrw.commons.media.MediaClient | ||||
| import io.reactivex.Single | ||||
| import io.reactivex.disposables.CompositeDisposable | ||||
| import org.junit.Assert | ||||
| import org.junit.Before | ||||
| import org.junit.Test | ||||
| import org.junit.runner.RunWith | ||||
| import org.mockito.Mock | ||||
| import org.mockito.Mockito.`when` | ||||
| import org.mockito.MockitoAnnotations | ||||
| import org.powermock.reflect.Whitebox | ||||
| import org.robolectric.RobolectricTestRunner | ||||
| import org.robolectric.RuntimeEnvironment | ||||
| import org.robolectric.annotation.Config | ||||
| import org.wikipedia.AppAdapter | ||||
| import java.lang.reflect.Method | ||||
| 
 | ||||
| @RunWith(RobolectricTestRunner::class) | ||||
| @Config(sdk = [21], application = TestCommonsApplication::class) | ||||
| class PicOfDayAppWidgetUnitTests { | ||||
| 
 | ||||
|     private lateinit var widget: PicOfDayAppWidget | ||||
|     private lateinit var context: Context | ||||
| 
 | ||||
|     @Mock | ||||
|     private lateinit var views: RemoteViews | ||||
| 
 | ||||
|     @Mock | ||||
|     private lateinit var appWidgetManager: AppWidgetManager | ||||
| 
 | ||||
|     @Mock | ||||
|     private lateinit var mediaClient: MediaClient | ||||
| 
 | ||||
|     @Mock | ||||
|     private lateinit var compositeDisposable: CompositeDisposable | ||||
| 
 | ||||
|     @Before | ||||
|     fun setUp() { | ||||
|         AppAdapter.set(TestAppAdapter()) | ||||
|         context = RuntimeEnvironment.application.applicationContext | ||||
|         SoLoader.setInTestMode() | ||||
|         ImagePipelineFactory.initialize(context) | ||||
|         MockitoAnnotations.initMocks(this) | ||||
|         widget = PicOfDayAppWidget() | ||||
|         Whitebox.setInternalState(widget, "compositeDisposable", compositeDisposable) | ||||
|         Whitebox.setInternalState(widget, "mediaClient", mediaClient) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testWidgetNotNull() { | ||||
|         Assert.assertNotNull(widget) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testOnDisabled() { | ||||
|         widget.onDisabled(context) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testOnEnabled() { | ||||
|         widget.onEnabled(context) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testOnUpdate() { | ||||
|         widget.onUpdate(context, appWidgetManager, intArrayOf(1)) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testLoadImageFromUrl() { | ||||
|         val method: Method = PicOfDayAppWidget::class.java.getDeclaredMethod( | ||||
|             "loadImageFromUrl", | ||||
|             String::class.java, | ||||
|             Context::class.java, | ||||
|             RemoteViews::class.java, | ||||
|             AppWidgetManager::class.java, | ||||
|             Int::class.java | ||||
|         ) | ||||
|         method.isAccessible = true | ||||
|         method.invoke(widget, "", context, views, appWidgetManager, 1) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @Throws(Exception::class) | ||||
|     fun testLoadPictureOfTheDay() { | ||||
|         `when`(mediaClient.getPictureOfTheDay()).thenReturn(Single.just(Media())) | ||||
|         val method: Method = PicOfDayAppWidget::class.java.getDeclaredMethod( | ||||
|             "loadPictureOfTheDay", | ||||
|             Context::class.java, | ||||
|             RemoteViews::class.java, | ||||
|             AppWidgetManager::class.java, | ||||
|             Int::class.java | ||||
|         ) | ||||
|         method.isAccessible = true | ||||
|         method.invoke(widget, context, views, appWidgetManager, 1) | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Madhur Gupta
						Madhur Gupta