Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.base.test; | |
| 6 | |
| 7 import android.support.test.InstrumentationRegistry; | |
| 8 import android.support.test.uiautomator.UiDevice; | |
| 9 | |
| 10 import org.junit.runners.model.Statement; | |
| 11 | |
| 12 import org.chromium.base.CommandLine; | |
| 13 import org.chromium.base.Log; | |
| 14 | |
| 15 import java.io.File; | |
| 16 | |
| 17 /** | |
| 18 * Statement that captures screenshots if |base| statement fails. | |
| 19 * | |
| 20 * If --screenshot-path commandline flag is given, this |Statement| | |
| 21 * will save a screenshot to the specified path in the case of a test failure. | |
| 22 */ | |
| 23 public class ScreenshotOnFailureStatement extends Statement { | |
| 24 private static final String TAG = "ScreenshotOnFail"; | |
| 25 | |
| 26 private final Statement mBase; | |
| 27 | |
| 28 public ScreenshotOnFailureStatement(final Statement base) { | |
| 29 mBase = base; | |
| 30 } | |
| 31 | |
| 32 @Override | |
| 33 public void evaluate() throws Throwable { | |
| 34 try { | |
| 35 mBase.evaluate(); | |
| 36 } catch (Throwable e) { | |
| 37 takeScreenshot(); | |
|
the real yoland
2017/05/09 15:23:28
we should support AssumptionViolatedException here
mikecase (-- gone --)
2017/05/10 02:55:10
Ack
| |
| 38 throw e; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 private void takeScreenshot() { | |
| 43 String screenshotFilePath = CommandLine.isInitialized() | |
|
the real yoland
2017/05/09 15:23:28
hmm, there is potentially a better way to do this,
mikecase (-- gone --)
2017/05/10 02:55:10
Done! I agree. I have another CL for render tests
| |
| 44 ? CommandLine.getInstance().getSwitchValue("screenshot-file") | |
| 45 : null; | |
| 46 if (screenshotFilePath == null) { | |
| 47 Log.d(TAG, | |
| 48 "Did not save screenshot of failure. Must specify --screensh ot-file " | |
| 49 + "command-line flag to enable this feature."); | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInst rumentation()); | |
| 54 File screenshotFile = new File(screenshotFilePath); | |
| 55 File screenshotDir = screenshotFile.getParentFile(); | |
| 56 if (screenshotDir == null) { | |
| 57 Log.d(TAG, | |
| 58 String.format( | |
| 59 "Failed to create parent directory for %s. Can't sav e screenshot.", | |
| 60 screenshotFile)); | |
| 61 return; | |
| 62 } | |
| 63 if (!screenshotDir.exists()) { | |
| 64 if (!screenshotDir.mkdirs()) { | |
| 65 Log.d(TAG, | |
| 66 String.format( | |
| 67 "Failed to create %s. Can't save screenshot.", s creenshotDir)); | |
| 68 return; | |
| 69 } | |
| 70 } | |
| 71 Log.d(TAG, String.format("Saving screenshot of test failure, %s", screen shotFile)); | |
| 72 uiDevice.takeScreenshot(screenshotFile); | |
| 73 } | |
| 74 } | |
| OLD | NEW |