1 /*
2  * Copyright (C) 2021 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 static androidx.test.InstrumentationRegistry.getInstrumentation;
20 
21 import android.app.Instrumentation;
22 import android.app.UiAutomation;
23 import android.os.ParcelFileDescriptor;
24 import android.util.Log;
25 
26 import androidx.test.uiautomator.UiDevice;
27 
28 import org.junit.rules.TestRule;
29 import org.junit.runner.Description;
30 import org.junit.runners.model.Statement;
31 
32 import java.io.File;
33 import java.lang.annotation.ElementType;
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.lang.annotation.Target;
37 
38 /**
39  * Rule which captures a screen record for a test.
40  * After adding this rule to the test class, apply the annotation @ScreenRecord to individual tests
41  */
42 public class ScreenRecordRule implements TestRule {
43 
44     private static final String TAG = "ScreenRecordRule";
45 
46     @Override
apply(Statement base, Description description)47     public Statement apply(Statement base, Description description) {
48         if (description.getAnnotation(ScreenRecord.class) == null) {
49             return base;
50         }
51 
52         return new Statement() {
53             @Override
54             public void evaluate() throws Throwable {
55                 Instrumentation inst = getInstrumentation();
56                 UiAutomation automation = inst.getUiAutomation();
57                 UiDevice device = UiDevice.getInstance(inst);
58 
59                 File outputFile = new File(inst.getTargetContext().getFilesDir(),
60                         "screenrecord-" + description.getMethodName() + ".mp4");
61                 device.executeShellCommand("killall screenrecord");
62                 ParcelFileDescriptor output =
63                         automation.executeShellCommand("screenrecord " + outputFile);
64                 String screenRecordPid = device.executeShellCommand("pidof screenrecord");
65                 boolean success = false;
66                 try {
67                     base.evaluate();
68                     success = true;
69                 } finally {
70                     device.executeShellCommand("kill -INT " + screenRecordPid);
71                     Log.e(TAG, "Screenrecord captured at: " + outputFile);
72                     output.close();
73                     if (success) {
74                         automation.executeShellCommand("rm " + outputFile);
75                     }
76                 }
77             }
78         };
79     }
80 
81     /**
82      * Interface to indicate that the test should capture screenrecord
83      */
84     @Retention(RetentionPolicy.RUNTIME)
85     @Target(ElementType.METHOD)
86     public @interface ScreenRecord {
87     }
88 }
89