package de.nenick.espressomacchiato.tools;

import android.content.Context;
import android.support.test.InstrumentationRegistry;

import java.io.File;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * Tool for application data "file storage/cache, shared preferences, database".
 * <p>
 * Best situation is to clear all data is when activity is not started.
 * <p>
 * You can delay activity start with:<br>
 * new ActivityTestRule(Activity.class, false, false)<br>
 * clearApplicationData()<br>
 * ActivityTestRule.lunchActivity()
 */
public class EspAppDataTool {

    /**
     * Execute all existing clear operations.
     */
    public static void clearApplicationData() {
        clearStorage();
        clearCache();
        clearDatabase();
        clearSharedPreferences();
    }

    /**
     * Clear all shared preferences.
     */
    public static void clearSharedPreferences() {
        String[] preferenceFiles = new File(InstrumentationRegistry.getTargetContext().getFilesDir().getParentFile(), "shared_prefs").list();
        for (String preferenceFile : preferenceFiles) {
            InstrumentationRegistry.getTargetContext().getSharedPreferences(preferenceFile.replace(".xml", ""), Context.MODE_PRIVATE).edit().clear().commit();
        }
    }

    /**
     * only works if all database connections are closed. does not produce error if connection still open.
     */
    public static void clearDatabase() {
        String[] databaseList = InstrumentationRegistry.getTargetContext().databaseList();
        for (String database : databaseList) {
            InstrumentationRegistry.getContext().deleteDatabase(database);
            assertThat(InstrumentationRegistry.getContext().getDatabasePath(database).exists(), is(false));
        }
    }

    public static void clearCache() {
        File cacheDir = InstrumentationRegistry.getTargetContext().getCacheDir();
        assertThat(deleteRecursive(cacheDir), is(true));
    }

    public static void clearStorage() {
        File filesDir = InstrumentationRegistry.getTargetContext().getFilesDir();
        assertThat(deleteRecursive(filesDir), is(true));
    }

    private static boolean deleteRecursive(File directory) {
        if (directory.isDirectory()) {
            String[] directoryContent = directory.list();
            for (String content : directoryContent) {
                assertThat(deleteRecursive(new File(directory, content)), is(true));
            }
        }
        return directory.delete();
    }
}

@@ -143,16 +143,41 @@
 io.File;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -645,45 +645,90 @@
   * 
-Execute all existing clear operations
+Clear all application data except screenshots taken with %7B@link EspScreenshotTool%7D
 .%0A  
@@ -801,16 +801,33 @@
 rStorage
+ExceptScreenshots
 ();%0A    
@@ -2109,16 +2109,35 @@
 Storage(
+String ... excludes
 ) %7B%0A    
@@ -2226,124 +2226,545 @@
     
-assertThat(deleteRecursive(filesDir), is(true));%0A    %7D%0A%0A    private static boolean deleteRecursive(File directory) %7B
+String%5B%5D directoryContent = filesDir.list();%0A        for (String content : directoryContent) %7B%0A            assertThat(deleteRecursive(new File(filesDir, content), excludes), is(true));%0A        %7D%0A%0A    %7D%0A%0A    public static void clearStorageExceptScreenshots() %7B%0A        clearStorage(EspScreenshotTool.screenshotFolderName);%0A    %7D%0A%0A    private static boolean deleteRecursive(File directory, String ... excludes) %7B%0A        if(excludes.length %3E 0 && Arrays.asList(excludes).contains(directory.getName())) %7B%0A            return true;%0A        %7D%0A
 %0A   
@@ -2982,16 +2982,26 @@
 content)
+, excludes
 ), is(tr

exclude mechanism to save screenshots