1 /*
2  * Copyright (C) 2022 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.view.textclassifier.cts;
18 
19 import android.app.Instrumentation;
20 import android.app.UiAutomation;
21 import android.content.Context;
22 import android.graphics.Bitmap;
23 import android.util.Log;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import com.android.compatibility.common.util.BitmapUtils;
28 
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 
33 /**
34  * Helper for common funcionalities.
35  */
36 public final class Helper {
37 
38     private static final String TAG = "Helper";
39 
40     public static final String LOCAL_TEST_FILES_DIR = "/sdcard/CtsTextClassifierTestCases";
41 
42 
43     /**
44      * Takes a screenshot and save it in the file system for analysis.
45      */
takeScreenshotAndSave(Context context, String testName, String targetFolder)46     public static void takeScreenshotAndSave(Context context, String testName,
47             String targetFolder) {
48         File file = null;
49         try {
50             file = createTestFile(testName, "sreenshot.png", targetFolder);
51             if (file != null) {
52                 Log.i(TAG, "Taking screenshot on " + file);
53                 final Bitmap screenshot = takeScreenshot();
54                 saveBitmapToFile(screenshot, file);
55             }
56         } catch (Exception e) {
57             Log.e(TAG, "Error taking screenshot and saving on " + file, e);
58         }
59     }
60 
61     /**
62      * Save dumpsys result in the file system for analysis.
63      */
dumpsysAndSave(String dumpsysString, String testName, String targetFolder)64     public static void dumpsysAndSave(String dumpsysString, String testName,
65             String targetFolder) {
66         File file = null;
67         try {
68             file = createTestFile(testName, "dumpsys.txt", targetFolder);
69             if (file != null) {
70                 Log.i(TAG, "dumpSys on" + file);
71                 FileWriter wr = new FileWriter(file);
72                 wr.write(dumpsysString);
73                 wr.flush();
74                 wr.close();
75             }
76         } catch (Exception e) {
77             Log.e(TAG, "Error taking screenshot and saving on " + file, e);
78         }
79     }
80 
saveBitmapToFile(Bitmap bitmap, File file)81     private static File saveBitmapToFile(Bitmap bitmap, File file) {
82         Log.i(TAG, "Saving bitmap at " + file);
83         BitmapUtils.saveBitmap(bitmap, file.getParent(), file.getName());
84         return file;
85     }
86 
takeScreenshot()87     private static Bitmap takeScreenshot() {
88         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
89         UiAutomation automan = instrumentation.getUiAutomation();
90         return automan.takeScreenshot();
91     }
92 
createTestFile(String testName, String name, String targetFolder)93     private static File createTestFile(String testName, String name, String targetFolder)
94             throws IOException {
95         final File dir = getLocalDirectory(targetFolder);
96         if (dir == null) return null;
97         final String prefix = testName.replaceAll("\\.|\\(|\\/", "_").replaceAll("\\)", "");
98         final String filename = prefix + "-" + name;
99 
100         return createFile(dir, filename);
101     }
102 
getLocalDirectory(String targetFolder)103     private static File getLocalDirectory(String targetFolder) {
104         final File dir = new File(targetFolder);
105         dir.mkdirs();
106         if (!dir.exists()) {
107             Log.e(TAG, "Could not create directory " + dir);
108             return null;
109         }
110         return dir;
111     }
112 
createFile(File dir, String filename)113     private static File createFile(File dir, String filename) throws IOException {
114         final File file = new File(dir, filename);
115         if (file.exists()) {
116             Log.v(TAG, "Deleting file " + file);
117             file.delete();
118         }
119         if (!file.createNewFile()) {
120             Log.e(TAG, "Could not create file " + file);
121             return null;
122         }
123         return file;
124     }
125 
126 }
127