1 /*
2  * Copyright 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 
17 package androidx.benchmark
18 
19 import android.os.Build
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SdkSuppress
22 import androidx.test.filters.SmallTest
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.After
25 import org.junit.Before
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.junit.runners.Parameterized
29 
30 @SmallTest
31 @RunWith(Parameterized::class)
32 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.S)
33 class VirtualFileTest(
34     private val file1: VirtualFile,
35     private val file2: VirtualFile,
36     private val expectedFile1Path: String,
37     private val expectedFileType: String,
38 ) {
39 
40     companion object {
41         @JvmStatic
42         @Parameterized.Parameters
datanull43         fun data(): Collection<Array<Any>> =
44             listOf(
45                 arrayOf(
46                     UserFile.inOutputsDir("test1.file"),
47                     UserFile.inOutputsDir("test2.file"),
48                     "/storage/emulated/0/Android/media/androidx.benchmark.test/test1.file",
49                     "UserFile",
50                 ),
51                 arrayOf(
52                     ShellFile.inTempDir("test1.file"),
53                     ShellFile.inTempDir("test2.file"),
54                     "/data/local/tmp/test1.file",
55                     "ShellFile",
56                 )
57             )
58     }
59 
60     @Before
61     fun setUp() {
62         file1.delete()
63         file2.delete()
64     }
65 
66     @After
tearDownnull67     fun tearDown() {
68         file1.delete()
69         file2.delete()
70     }
71 
72     @Test
absolutePathnull73     fun absolutePath() {
74         assertThat(file1.absolutePath).isEqualTo(expectedFile1Path)
75     }
76 
77     @Test
fileTypenull78     fun fileType() {
79         assertThat(file1.fileType).isEqualTo(expectedFileType)
80     }
81 
82     @Test
writeReadTextnull83     fun writeReadText() {
84         file1.writeText("test")
85         assertThat(file1.readText()).isEqualTo("test")
86     }
87 
88     @Test
writeReadBytesnull89     fun writeReadBytes() {
90         val bytes = ByteArray(3) { it.toByte() }
91         file1.writeBytes(bytes)
92         assertThat(file1.readBytes()).isEqualTo(bytes)
93     }
94 
95     @Test
existDeletenull96     fun existDelete() {
97         file1.writeText("text")
98         assertThat(file1.exists()).isTrue()
99         assertThat(file1.delete()).isTrue()
100         assertThat(file1.exists()).isFalse()
101     }
102 
103     @Test
copyFromnull104     fun copyFrom() {
105         file2.writeText("text")
106         file1.copyFrom(file2)
107         assertThat(file1.readText()).isEqualTo("text")
108     }
109 
110     @Test
copyTonull111     fun copyTo() {
112         file1.writeText("text")
113         file1.copyTo(file2)
114         assertThat(file2.readText()).isEqualTo("text")
115     }
116 
117     @Test
moveTonull118     fun moveTo() {
119         file1.writeText("text")
120         file1.moveTo(file2)
121         assertThat(file2.readText()).isEqualTo("text")
122         assertThat(file1.exists()).isFalse()
123     }
124 
125     @Test
md5sumnull126     fun md5sum() {
127         file1.writeText("text")
128         assertThat(file1.md5sum()).isEqualTo("1cb251ec0d568de6a929b520c4aed8d1")
129     }
130 
131     @Test
lsnull132     fun ls() {
133         file1.writeText("text")
134         assertThat(file1.ls().first()).isEqualTo(expectedFile1Path)
135     }
136 }
137 
138 @SmallTest
139 @RunWith(AndroidJUnit4::class)
140 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.S)
141 class VirtualFileIoTest {
142 
143     private lateinit var userFile: UserFile
144     private lateinit var shellFile: ShellFile
145 
146     @Before
setUpnull147     fun setUp() {
148         userFile = UserFile.inOutputsDir("user.file1").apply { delete() }
149         shellFile = ShellFile.inTempDir("shell.file1").apply { delete() }
150     }
151 
152     @Test
shellCopyToUsernull153     fun shellCopyToUser() {
154         shellFile.writeText("test")
155         shellFile.copyTo(userFile)
156         assertThat(userFile.readText()).isEqualTo("test")
157     }
158 
159     @Test
shellCopyFromUsernull160     fun shellCopyFromUser() {
161         userFile.writeText("test")
162         shellFile.copyFrom(userFile)
163         assertThat(shellFile.readText()).isEqualTo("test")
164     }
165 
166     @Test
userCopyToShellnull167     fun userCopyToShell() {
168         userFile.writeText("test")
169         userFile.copyTo(shellFile)
170         assertThat(shellFile.readText()).isEqualTo("test")
171     }
172 
173     @Test
userCopyFromShellnull174     fun userCopyFromShell() {
175         shellFile.writeText("test")
176         userFile.copyFrom(shellFile)
177         assertThat(userFile.readText()).isEqualTo("test")
178     }
179 }
180