1 /*
2  * Copyright (C) 2023 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 android.scopedstorage.cts.device;
18 
19 import android.app.Instrumentation;
20 import android.content.pm.PackageManager;
21 import android.os.FileUtils;
22 
23 import androidx.test.platform.app.InstrumentationRegistry;
24 
25 import org.junit.Assume;
26 
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 
32 public class DeviceTestUtils {
33 
34     private static final Instrumentation sInstrumentation =
35             InstrumentationRegistry.getInstrumentation();
36 
createContentFromResource(int resourceId, File file)37     protected static void createContentFromResource(int resourceId, File file) throws IOException {
38         try (FileOutputStream fileOutputStream = new FileOutputStream(file);
39                 InputStream in = sInstrumentation.getContext()
40                         .getResources().openRawResource(resourceId)) {
41             // Dump the image we have to external storage
42             FileUtils.copy(in, fileOutputStream);
43             // Sync file to disk to ensure file is fully written to the lower fs attempting to
44             // open for redaction. Otherwise, the FUSE daemon might not accurately parse the
45             // EXIF tags and might misleadingly think there are not tags to redact
46             fileOutputStream.getFD().sync();
47         }
48 
49     }
50 
checkUISupported()51     protected static void checkUISupported() {
52         PackageManager pm = sInstrumentation.getContext().getPackageManager();
53 
54         // Do not run tests on Watches, TVs, Auto or devices without UI.
55         Assume.assumeTrue(!pm.hasSystemFeature(PackageManager.FEATURE_EMBEDDED)
56                 && !pm.hasSystemFeature(PackageManager.FEATURE_WATCH)
57                 && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
58                 && !pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
59     }
60 }
61