mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-30 22:34:02 +01:00
With lazy loading of contributions (#3566)
This commit is contained in:
parent
c216fdf0d4
commit
d863a404f1
34 changed files with 1397 additions and 928 deletions
|
|
@ -6,6 +6,7 @@ import android.net.ConnectivityManager;
|
|||
import android.net.NetworkInfo;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -28,34 +29,30 @@ public class NetworkUtilsTest {
|
|||
|
||||
@Test
|
||||
public void testInternetConnectionEstablished() {
|
||||
Context mockContext = mock(Context.class);
|
||||
Application mockApplication = mock(Application.class);
|
||||
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
|
||||
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
|
||||
when(mockNetworkInfo.isConnectedOrConnecting())
|
||||
.thenReturn(true);
|
||||
when(mockConnectivityManager.getActiveNetworkInfo())
|
||||
.thenReturn(mockNetworkInfo);
|
||||
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(mockConnectivityManager);
|
||||
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
|
||||
Context mockContext = getContext(true);
|
||||
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
|
||||
assertTrue(internetConnectionEstablished);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInternetConnectionNotEstablished() {
|
||||
@NotNull
|
||||
public static Context getContext(boolean connectionEstablished) {
|
||||
Context mockContext = mock(Context.class);
|
||||
Application mockApplication = mock(Application.class);
|
||||
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
|
||||
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
|
||||
when(mockNetworkInfo.isConnectedOrConnecting())
|
||||
.thenReturn(false);
|
||||
.thenReturn(connectionEstablished);
|
||||
when(mockConnectivityManager.getActiveNetworkInfo())
|
||||
.thenReturn(mockNetworkInfo);
|
||||
.thenReturn(mockNetworkInfo);
|
||||
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(mockConnectivityManager);
|
||||
.thenReturn(mockConnectivityManager);
|
||||
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
|
||||
return mockContext;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInternetConnectionNotEstablished() {
|
||||
Context mockContext = getContext(false);
|
||||
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
|
||||
assertFalse(internetConnectionEstablished);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
package fr.free.nrw.commons.utils
|
||||
|
||||
|
||||
import android.database.Cursor
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.paging.DataSource
|
||||
import androidx.paging.LivePagedListBuilder
|
||||
import androidx.paging.PagedList
|
||||
import androidx.room.InvalidationTracker
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.RoomSQLiteQuery
|
||||
import androidx.room.paging.LimitOffsetDataSource
|
||||
import com.nhaarman.mockitokotlin2.whenever
|
||||
import org.mockito.Mockito.mock
|
||||
|
||||
fun <T> List<T>.asPagedList(config: PagedList.Config? = null): LiveData<PagedList<T>> {
|
||||
val defaultConfig = PagedList.Config.Builder()
|
||||
.setEnablePlaceholders(false)
|
||||
.setPageSize(size)
|
||||
.setMaxSize(size + 2)
|
||||
.setPrefetchDistance(1)
|
||||
.build()
|
||||
return LivePagedListBuilder<Int, T>(
|
||||
createMockDataSourceFactory(this),
|
||||
config ?: defaultConfig
|
||||
).build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a mocked instance of the data source factory
|
||||
*/
|
||||
fun <T> createMockDataSourceFactory(itemList: List<T>): DataSource.Factory<Int, T> =
|
||||
object : DataSource.Factory<Int, T>() {
|
||||
override fun create(): DataSource<Int, T> = MockLimitDataSource(itemList)
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a mocked Room SQL query
|
||||
*/
|
||||
private fun mockQuery(): RoomSQLiteQuery? {
|
||||
val query = mock(RoomSQLiteQuery::class.java);
|
||||
whenever(query.sql).thenReturn("");
|
||||
return query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a mocked Room DB
|
||||
*/
|
||||
private fun mockDb(): RoomDatabase? {
|
||||
val roomDatabase = mock(RoomDatabase::class.java);
|
||||
val invalidationTracker = mock(InvalidationTracker::class.java)
|
||||
whenever(roomDatabase.invalidationTracker).thenReturn(invalidationTracker);
|
||||
return roomDatabase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class that defines the mocked data source
|
||||
*/
|
||||
class MockLimitDataSource<T>(private val itemList: List<T>) :
|
||||
LimitOffsetDataSource<T>(mockDb(), mockQuery(), false, null) {
|
||||
override fun convertRows(cursor: Cursor?): MutableList<T> = itemList.toMutableList()
|
||||
override fun countItems(): Int = itemList.count()
|
||||
override fun isInvalid(): Boolean = false
|
||||
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
|
||||
}
|
||||
|
||||
override fun loadRange(startPosition: Int, loadCount: Int): MutableList<T> {
|
||||
return itemList.subList(startPosition, startPosition + loadCount).toMutableList()
|
||||
}
|
||||
|
||||
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
|
||||
callback.onResult(itemList, 0)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue