1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.documentsui.loaders 17 18 import android.os.Bundle 19 import android.platform.test.annotations.RequiresFlagsEnabled 20 import android.platform.test.flag.junit.CheckFlagsRule 21 import android.platform.test.flag.junit.DeviceFlagsValueProvider 22 import androidx.test.filters.SmallTest 23 import com.android.documentsui.ContentLock 24 import com.android.documentsui.base.DocumentInfo 25 import com.android.documentsui.flags.Flags.FLAG_USE_SEARCH_V2_READ_ONLY 26 import com.android.documentsui.testing.TestFileTypeLookup 27 import com.android.documentsui.testing.TestProvidersAccess 28 import java.time.Duration 29 import junit.framework.Assert.assertEquals 30 import org.junit.Rule 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.junit.runners.Parameterized 34 import org.junit.runners.Parameterized.Parameters 35 36 private const val TOTAL_FILE_COUNT = 10 37 38 @RunWith(Parameterized::class) 39 @SmallTest 40 class FolderLoaderTest(private val testParams: LoaderTestParams) : BaseLoaderTest() { 41 companion object { 42 @JvmStatic 43 @Parameters(name = "with parameters {0}") datanull44 fun data() = listOf( 45 LoaderTestParams("", null, Bundle(), TOTAL_FILE_COUNT), 46 // The first file is at NOW, the second at NOW - 1h, etc. 47 LoaderTestParams("", Duration.ofMinutes(1L), Bundle(), 1), 48 LoaderTestParams("", Duration.ofMinutes(60L + 1), Bundle(), 2), 49 LoaderTestParams( 50 "", 51 Duration.ofMinutes(TOTAL_FILE_COUNT * 60L + 1), 52 Bundle(), 53 TOTAL_FILE_COUNT 54 ), 55 ) 56 } 57 58 @get:Rule 59 val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule() 60 61 @Test 62 @RequiresFlagsEnabled(FLAG_USE_SEARCH_V2_READ_ONLY) 63 fun testLoadInBackground() { 64 val mockProvider = mEnv.mockProviders[TestProvidersAccess.DOWNLOADS.authority] 65 val docs = createDocuments(TOTAL_FILE_COUNT) 66 mockProvider!!.setNextChildDocumentsReturns(*docs) 67 val userIds = listOf(TestProvidersAccess.DOWNLOADS.userId) 68 val queryOptions = 69 QueryOptions( 70 TOTAL_FILE_COUNT, 71 testParams.lastModifiedDelta, 72 null, 73 true, 74 arrayOf<String>("*/*"), 75 testParams.otherArgs, 76 ) 77 val contentLock = ContentLock() 78 // TODO(majewski): Is there a better way to create Downloads root folder DocumentInfo? 79 val rootFolderInfo = DocumentInfo() 80 rootFolderInfo.authority = TestProvidersAccess.DOWNLOADS.authority 81 rootFolderInfo.userId = userIds[0] 82 83 val loader = 84 FolderLoader( 85 mActivity, 86 userIds, 87 TestFileTypeLookup(), 88 contentLock, 89 TestProvidersAccess.DOWNLOADS, 90 rootFolderInfo, 91 queryOptions, 92 mEnv.state.sortModel 93 ) 94 val directoryResult = loader.loadInBackground() 95 assertEquals(testParams.expectedCount, getFileCount(directoryResult)) 96 } 97 } 98