package de.nenick.espressomacchiato.tools;

import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.util.Log;

import java.io.File;

public class EspScreenshotTool {

    public static void takeWithName(String name) {
        File sddir = obtainScreenshotDirectory();
        if (!sddir.exists()) {
            throw new IllegalStateException("screenshot folder does not exist: " + sddir.getAbsolutePath());
        }
        String screenshotName = name + ".png";
        File screenShotFile = new File(sddir, screenshotName);

        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        Log.v("EspressoMacchiato", "Take screenshot to " + sddir);

        if (!device.takeScreenshot(screenShotFile)) {
            throw new IllegalStateException("take picture failed");
        }
    }

    private static File obtainScreenshotDirectory() {
        File externalCacheDir = InstrumentationRegistry.getTargetContext().getExternalCacheDir();
        if (externalCacheDir == null) {
            throw new IllegalStateException("could not find external cache dir to store screenshot");
        }
        return new File(externalCacheDir.getAbsolutePath(), "test-screenshots");
    }
}

@@ -1041,160 +1041,8 @@
 ();%0A
-        if (externalCacheDir == null) %7B%0A            throw new IllegalStateException(%22could not find external cache dir to store screenshot%22);%0A        %7D%0A
     
@@ -1077,34 +1077,16 @@
 CacheDir
-.getAbsolutePath()
 , %22test-

remove not necessary check