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 17 package com.android.launcher3.util.rule 18 19 import androidx.test.platform.app.InstrumentationRegistry 20 import java.io.File 21 import org.junit.rules.TestRule 22 import org.junit.runner.Description 23 import org.junit.runners.model.Statement 24 25 /** Copy a file from the tests assets folder to the phone. */ 26 class TestToPhoneFileCopier( 27 val src: String, 28 dest: String, 29 private val removeOnFinish: Boolean = false 30 ) : TestRule { 31 32 private val dstFile = 33 File(InstrumentationRegistry.getInstrumentation().targetContext.dataDir, dest) 34 getDstnull35 fun getDst() = dstFile.absolutePath 36 37 fun before() = 38 dstFile.writeBytes( 39 InstrumentationRegistry.getInstrumentation().context.assets.open(src).readBytes() 40 ) 41 42 fun after() { 43 if (removeOnFinish) { 44 dstFile.delete() 45 } 46 } 47 applynull48 override fun apply(base: Statement, description: Description): Statement = 49 object : Statement() { 50 override fun evaluate() { 51 before() 52 try { 53 base.evaluate() 54 } finally { 55 after() 56 } 57 } 58 } 59 } 60