• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.systemui.cts;
18 
19 import android.util.Log;
20 
21 import androidx.test.runner.screenshot.BasicScreenCaptureProcessor;
22 import androidx.test.runner.screenshot.ScreenCapture;
23 import androidx.test.runner.screenshot.Screenshot;
24 
25 import org.junit.rules.TestWatcher;
26 import org.junit.runner.Description;
27 import org.junit.runners.model.Statement;
28 
29 import java.io.IOException;
30 import java.nio.file.FileSystems;
31 
32 public class ScreenshotTestRule extends TestWatcher {
33     private String mBasePath;
34     private String mClassName;
35     private String mTestMethod;
36     private boolean mFinish;
37 
38     private static class ScreenCaptureProcessor extends BasicScreenCaptureProcessor {
ScreenCaptureProcessor(String basePath)39         ScreenCaptureProcessor(String basePath) {
40             super();
41             try {
42                 mDefaultScreenshotPath = FileSystems.getDefault()
43                         .getPath(basePath).toFile().getCanonicalFile();
44             } catch (IOException e) {
45                 Log.e("ScreenshotTestRule", "Can't create directory.", e);
46             }
47         }
48     }
49 
ScreenshotTestRule(String basePath)50     public ScreenshotTestRule(String basePath) {
51         super();
52         mBasePath = basePath;
53     }
54 
55     @Override
apply(Statement base, Description description)56     public Statement apply(Statement base, Description description) {
57         mClassName = description.getTestClass().getSimpleName();
58         mTestMethod = description.getMethodName();
59         mFinish = false;
60         return super.apply(base, description);
61     }
62 
capture(String fileName)63     private void capture(String fileName) {
64         ScreenCapture screenCapture = Screenshot.capture();
65         screenCapture.setName(fileName);
66         ScreenCaptureProcessor processors = new ScreenCaptureProcessor(mBasePath);
67 
68         try {
69             processors.process(screenCapture);
70         } catch (IOException e) {
71             Log.e("ScreenshotTestRule", "Can't screenshot.", e);
72         }
73     }
74 
capture(Description description, String result)75     private void capture(Description description, String result) {
76         String fileName = description.getTestClass().getSimpleName() + "#"
77                 + description.getMethodName();
78         capture(fileName + "_" + result);
79     }
80 
capture()81     public void capture() {
82         capture(mClassName + "#" + mTestMethod);
83     }
84 
85     @Override
finished(Description description)86     protected void finished(Description description) {
87         super.finished(description);
88         mFinish = true;
89     }
90 
91     @Override
failed(Throwable throwable, Description description)92     protected void failed(Throwable throwable, Description description) {
93         super.failed(throwable, description);
94         if (mFinish) {
95             return;
96         }
97         mFinish = true;
98 
99         capture(description, "failed");
100     }
101 }
102