1 /*
2  * Copyright (C) 2018 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.server.wm.flicker.monitor;
18 
19 import android.os.Environment;
20 
21 import com.google.common.io.BaseEncoding;
22 
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 
30 /** Collects test artifacts during a UI transition. */
31 public abstract class TransitionMonitor {
32     static final String TAG = "FLICKER";
33     public static final Path OUTPUT_DIR =
34             Paths.get(Environment.getExternalStorageDirectory().toString(), "flicker");
35     protected String mChecksum;
36     protected Path mOutputPath;
37 
38     /** Starts monitor. */
start()39     public abstract void start();
40 
41     /** Stops monitor. */
stop()42     public abstract void stop();
43 
44     /**
45      * Saves any monitor artifacts to file adding {@code testTag} and {@code iteration} to the file
46      * name.
47      *
48      * @param testTag suffix added to artifact name
49      * @param iteration suffix added to artifact name
50      * @return Path to saved artifact
51      */
save(String testTag, int iteration)52     public Path save(String testTag, int iteration) {
53         return save(testTag + "_" + iteration);
54     }
55 
56     /**
57      * Saves any monitor artifacts to file adding {@code testTag} to the file name.
58      *
59      * @param testTag suffix added to artifact name
60      * @return Path to saved artifact
61      */
62     /**
63      * Saves trace file to the external storage directory suffixing the name with the testtag and
64      * iteration.
65      *
66      * <p>Moves the trace file from the default location via a shell command since the test app does
67      * not have security privileges to access /data/misc/wmtrace.
68      *
69      * @param testTag suffix added to trace name used to identify trace
70      * @return Path to saved trace file and file checksum (SHA-256)
71      */
save(String testTag)72     public Path save(String testTag) {
73         mOutputPath.toFile().mkdirs();
74         Path savedTrace = saveTrace(testTag);
75         mChecksum = calculateChecksum(savedTrace);
76         return savedTrace;
77     }
78 
saveTrace(String testTag)79     protected Path saveTrace(String testTag) {
80         throw new UnsupportedOperationException("Save not implemented for this monitor");
81     }
82 
getChecksum()83     public String getChecksum() {
84         return mChecksum;
85     }
86 
calculateChecksum(Path traceFile)87     static String calculateChecksum(Path traceFile) {
88         try {
89             MessageDigest digest = MessageDigest.getInstance("SHA-256");
90             byte[] fileData = Files.readAllBytes(traceFile);
91             byte[] hash = digest.digest(fileData);
92             return BaseEncoding.base16().encode(hash).toLowerCase();
93         } catch (NoSuchAlgorithmException e) {
94             throw new IllegalArgumentException("Checksum algorithm SHA-256 not found", e);
95         } catch (IOException e) {
96             throw new IllegalArgumentException("File not found", e);
97         }
98     }
99 }
100