• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.os.Parcel
20 import android.provider.DocumentsContract
21 import com.android.documentsui.DirectoryResult
22 import com.android.documentsui.TestActivity
23 import com.android.documentsui.TestConfigStore
24 import com.android.documentsui.base.DocumentInfo
25 import com.android.documentsui.base.UserId
26 import com.android.documentsui.sorting.SortModel
27 import com.android.documentsui.testing.ActivityManagers
28 import com.android.documentsui.testing.TestEnv
29 import com.android.documentsui.testing.TestModel
30 import com.android.documentsui.testing.UserManagers
31 import java.time.Duration
32 import java.util.Locale
33 import kotlin.time.Duration.Companion.hours
34 import org.junit.Before
35 
36 /**
37  * Returns the number of matched files, or -1.
38  */
39 fun getFileCount(result: DirectoryResult?) = result?.cursor?.count ?: -1
40 
41 /**
42  * A data class that holds parameters that can be varied for the loader test. The last
43  * value, expectedCount, can be used for simple tests that check that the number of
44  * returned files matches the expectations.
45  */
46 data class LoaderTestParams(
47     // A query, matched against file names. May be empty.
48     val query: String,
49     // The delta from now that indicates maximum age of matched files.
50     val lastModifiedDelta: Duration?,
51     // The extra arguments typically supplied by search view manager.
52     val otherArgs: Bundle,
53     // The number of files that are expected, for the above parameters, to be found by a loader.
54     val expectedCount: Int,
55 )
56 
57 /**
58  * Common base class for search and folder loaders.
59  */
60 open class BaseLoaderTest {
61     lateinit var mEnv: TestEnv
62     lateinit var mActivity: TestActivity
63     lateinit var mTestConfigStore: TestConfigStore
64 
65     @Before
66     open fun setUp() {
67         mEnv = TestEnv.create()
68         mTestConfigStore = TestConfigStore()
69         mEnv.state.configStore = mTestConfigStore
70         mEnv.state.showHiddenFiles = false
71         val parcel = Parcel.obtain()
72         mEnv.state.sortModel = SortModel.CREATOR.createFromParcel(parcel)
73 
74         mActivity = TestActivity.create(mEnv)
75         mActivity.activityManager = ActivityManagers.create(false)
76         mActivity.userManager = UserManagers.create()
77     }
78 
79     /**
80      * Creates a text, PNG, MP4 and MPG files named sample-000x, for x in 0 .. count - 1.
81      * Each file gets a matching extension. The 0th file is modified 1h, 1st 2 hours, .. etc., ago.
82      */
83     fun createDocuments(count: Int): Array<DocumentInfo> {
84         val extensionList = arrayOf("txt", "png", "mp4", "mpg")
85         val now = System.currentTimeMillis()
86         val flags = (DocumentsContract.Document.FLAG_SUPPORTS_WRITE
87                 or DocumentsContract.Document.FLAG_SUPPORTS_DELETE
88                 or DocumentsContract.Document.FLAG_SUPPORTS_RENAME)
89         return Array<DocumentInfo>(count) { i ->
90             val id = String.format(Locale.US, "%05d", i)
91             val name = "sample-$id.${extensionList[i % extensionList.size]}"
92             mEnv.model.createDocumentForUser(
93                 name,
94                 TestModel.guessMimeType(name),
95                 flags,
96                 now - 1.hours.inWholeMilliseconds * i,
97                 UserId.DEFAULT_USER
98             )
99         }
100     }
101 }
102