Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5129c925 authored by Bo Majewski's avatar Bo Majewski Committed by Android (Google) Code Review
Browse files

Merge "[DocsUI, Search]: Check validity of constructor arguments." into main

parents 265f2649 63fb47f2
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -49,7 +49,11 @@ import kotlin.time.measureTime
 *  - Query options such as maximum number of results, last modified time delta, etc.
 *  - a lookup from file extension to file type
 *  - The model capable of sorting results
 *  - An acceptable mime types
 *  - An executor for running searches across multiple roots in parallel
 *
 *  SearchLoader requires that either a query is not null and not empty or that QueryOptions
 *  specify a last modified time restriction. This is to prevent searching for every file
 *  across every specified root.
 */
class SearchLoader(
    context: Context,
@@ -63,6 +67,12 @@ class SearchLoader(
    private val mExecutorService: ExecutorService,
) : BaseFileLoader(context, userIdList, mimeTypeLookup) {

    init {
        require((mQuery !== null && !mQuery.isBlank()) || mOptions.maxLastModifiedDelta !== null) {
            "Either the query or the last modified time must not be null"
        }
    }

    /**
     * Helper class that runs query on a single user for the given parameter. This class implements
     * an abstract future so that if the task is completed, we can retrieve the cursor via the get
+42 −5
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.time.Duration
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import junit.framework.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -36,7 +37,9 @@ private const val TOTAL_FILE_COUNT = 8
@RunWith(Parameterized::class)
@SmallTest
class SearchLoaderTest(private val testParams: LoaderTestParams) : BaseLoaderTest() {
    private lateinit var mExecutor: ExecutorService
    lateinit var mExecutor: ExecutorService
    val mContentLock = ContentLock()
    val mContentObserver = LockingContentObserver(mContentLock) {}

    companion object {
        @JvmStatic
@@ -71,10 +74,7 @@ class SearchLoaderTest(private val testParams: LoaderTestParams) : BaseLoaderTes
                true,
                arrayOf("*/*")
            )
        val contentLock = ContentLock()
        val rootIds = listOf(TestProvidersAccess.DOWNLOADS)
        val observer = LockingContentObserver(contentLock) {
        }

        // TODO(majewski): Is there a better way to create Downloads root folder DocumentInfo?
        val rootFolderInfo = DocumentInfo()
@@ -86,7 +86,7 @@ class SearchLoaderTest(private val testParams: LoaderTestParams) : BaseLoaderTes
                mActivity,
                userIds,
                TestFileTypeLookup(),
                observer,
                mContentObserver,
                rootIds,
                testParams.query,
                queryOptions,
@@ -96,4 +96,41 @@ class SearchLoaderTest(private val testParams: LoaderTestParams) : BaseLoaderTes
        val directoryResult = loader.loadInBackground()
        assertEquals(testParams.expectedCount, getFileCount(directoryResult))
    }

    @Test
    fun testBlankQueryAndRecency() {
        val userIds = listOf(TestProvidersAccess.DOWNLOADS.userId)
        val rootIds = listOf(TestProvidersAccess.DOWNLOADS)
        val noLastModifiedQueryOptions = QueryOptions(10, null, null, true, arrayOf("*/*"))

        // Blank query and no last modified duration is invalid.
        assertThrows(IllegalArgumentException::class.java) {
            SearchLoader(
                mActivity,
                userIds,
                TestFileTypeLookup(),
                mContentObserver,
                rootIds,
                "",
                noLastModifiedQueryOptions,
                mEnv.state.sortModel,
                mExecutor,
            )
        }

        // Null query and no last modified duration is invalid.
        assertThrows(IllegalArgumentException::class.java) {
            SearchLoader(
                mActivity,
                userIds,
                TestFileTypeLookup(),
                mContentObserver,
                rootIds,
                null,
                noLastModifiedQueryOptions,
                mEnv.state.sortModel,
                mExecutor,
            )
        }
    }
}