package com.tomtom.espresso.test.actions;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;

import com.squareup.spoon.Spoon;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;

import java.io.File;

public final class SpoonScreenshotAction implements ViewAction {
    private final String tag;
    private final String testClass;
    private final String testMethod;
    private static File lastScreenshot;

    /**
     * Initialize with information required to take a screenshot
     * @param tag Name of the screenshot to include in the file name
     * @param testClass Name of the class taking the screenshot (required by Spoon library)
     * @param testMethod Name of the method taking the screenshot
     */
    public SpoonScreenshotAction(final String tag, final String testClass, final String testMethod) {
        this.tag = tag;
        this.testClass = testClass;
        this.testMethod = testMethod;
    }

    @Override
    public Matcher<View> getConstraints() {
        return Matchers.anything();
    }

    @Override
    public String getDescription() {
        return "Taking a screenshot using spoon.";
    }

    @Override
    public void perform(final UiController uiController, final View view) {
        lastScreenshot = Spoon.screenshot(getActivity(view), tag, testClass, testMethod);
    }

    public static File getLastScreenshot() {
        return lastScreenshot;
    }

    /**
     * Get the activity from the context of the view
     * @param view View from which the activity will be inferred
     * @return Activity that contains the given view
     */
    private static Activity getActivity(final View view) {
        Context context = view.getContext();
        while (!(context instanceof Activity)) {
            if (context instanceof ContextWrapper) {
                context = ((ContextWrapper) context).getBaseContext();
            } else {
                throw new IllegalStateException("Got a context of class " + context.getClass() + " and I don't know how to get the Activity from it");
            }
        }
        return (Activity) context;
    }

    /**
     * Espresso action to be take a screenshot of the current activity
     * This must be called directly from the test method
     * @param tag Name of the screenshot to include in the file name
     */
    public static void perform(final String tag) {
        final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        final String testClass = trace[3].getClassName();
        final String testMethod = trace[3].getMethodName();
        onView(isRoot()).perform(new SpoonScreenshotAction(tag, testClass, testMethod));
    }
}

@@ -514,16 +514,176 @@
 .File;%0A%0A
+/**%0A * Class to take screenshots using Spoon library from an Espresso test%0A * Original code from Gist: https://gist.github.com/edenman/7fdd32a4d59ccc01185b%0A */%0A
 public f
@@ -764,17 +764,18 @@
  String 
-t
+mT
 ag;%0A    
@@ -787,33 +787,34 @@
 te final String 
-t
+mT
 estClass;%0A    pr
@@ -824,33 +824,34 @@
 te final String 
-t
+mT
 estMethod;%0A    p
@@ -869,17 +869,242 @@
 ic File 
-l
+sLastScreenshot;%0A%0A    /**%0A     * Get the last captured screenshot file%0A     * @return Last screenshot file handler or null if there was no screenshot taken%0A     */%0A    public static File getLastScreenshot() %7B%0A        return sL
 astScree
@@ -1101,32 +1101,38 @@
 sLastScreenshot;
+%0A    %7D
 %0A%0A    /**%0A     *
@@ -1535,22 +1535,18 @@
         
-this.t
+mT
 ag = tag
@@ -1555,22 +1555,18 @@
         
-this.t
+mT
 estClass
@@ -1591,14 +1591,10 @@
     
-this.t
+mT
 estM
@@ -1928,17 +1928,18 @@
         
-l
+sL
 astScree
@@ -1982,22 +1982,24 @@
 (view), 
-t
+mT
 ag, 
-t
+mT
 estClass
@@ -1992,33 +1992,34 @@
 ag, mTestClass, 
-t
+mT
 estMethod);%0A    
@@ -2025,91 +2025,8 @@
  %7D%0A%0A
-    public static File getLastScreenshot() %7B%0A        return lastScreenshot;%0A    %7D%0A%0A
     

Fix screenshot taking action class