SkQP: Refactor Java method SkQP.runTests()

Since it was only called by SkQPActivity.run(), I simply moved the code
into that method.

CQ_INCLUDE_TRYBOTS=skia.primary:Build-Debian9-Clang-x86-devrel-Android_SKQP,Test-Debian9-Clang-NUC7i5BNK-CPU-Emulator-x86-devrel-All-Android_SKQP

Change-Id: I82ef407dd440fa94e4c01c0f21a76389793ece58
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225177
Reviewed-by: Derek Sollenberger <djsollen@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
Auto-Submit: Hal Canary <halcanary@google.com>
diff --git a/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQP.java b/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQP.java
index 2f1381b..127b608 100644
--- a/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQP.java
+++ b/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQP.java
@@ -7,12 +7,12 @@
 
 package org.skia.skqp;
 
-import android.content.Context;
 import android.content.res.AssetManager;
-import android.util.Log;
-import java.io.File;
-import java.io.IOException;
 
+/**
+  This class does the heavy lifting for SkQP and provides the JNI interface
+  used by both SkQPActivity (firebase interface) and SkQPRunner (JUnit interface).
+ */
 public class SkQP {
     protected native void nInit(AssetManager assetManager, String dataDir);
     protected native long nExecuteGM(int gm, int backend) throws SkQPException;
@@ -30,77 +30,5 @@
     static {
       System.loadLibrary("skqp_app");
     }
-
-    protected void runTests(Context context, String outputDirPath) {
-        Log.i(LOG_PREFIX, "Output Dir: " + outputDirPath);
-        File outputDir = new File(outputDirPath);
-        try {
-            ensureEmtpyDirectory(outputDir);
-        } catch (IOException e) {
-            Log.e(LOG_PREFIX, "ensureEmtpyDirectory:" + e.getMessage());
-        }
-
-        // Note: nInit will initialize the mGMs, mBackends and mUnitTests fields.
-        AssetManager assetManager = context.getResources().getAssets();
-        this.nInit(assetManager, outputDirPath);
-
-        for (int backend = 0; backend < mBackends.length; backend++) {
-          String classname = kSkiaGM + mBackends[backend];
-          for (int gm = 0; gm < mGMs.length; gm++) {
-              String testName = kSkiaGM + mBackends[backend] + "_" +mGMs[gm];
-              long value = java.lang.Long.MAX_VALUE;
-              String error = null;
-              Log.w(LOG_PREFIX, "Running: " + testName);
-              try {
-                  value = this.nExecuteGM(gm, backend);
-              } catch (SkQPException exept) {
-                  error = exept.getMessage();
-              }
-              if (error != null) {
-                // Record error message and carry on.
-              } else if (value != 0) {
-                // Record failure and carry on.
-                  // SkQPRunner.Fail(desc, notifier, String.format(
-                  //             "Image mismatch: max channel diff = %f", value));
-              } else {
-                // Record success for this test.
-              }
-          }
-        }
-        for (int unitTest = 0; unitTest < mUnitTests.length; unitTest++) {
-            String testName = kSkiaUnitTests + "_" + mUnitTests[unitTest];
-            Log.w(LOG_PREFIX, "Running: " + testName);
-            String[] errors = this.nExecuteUnitTest(unitTest);
-            if (errors != null && errors.length > 0) {
-                for (String error : errors) {
-                  Log.w(LOG_PREFIX, "Error running " + testName + ":" + error);
-                }
-            } else {
-              Log.i(LOG_PREFIX, "Test: " + testName + " finished successfully.");
-            }
-        }
-        Log.i(LOG_PREFIX, "Finished running all tests.");
-        nMakeReport();
-    }
-
-    protected static void ensureEmtpyDirectory(File f) throws IOException {
-      if (f.exists()) {
-        delete(f);
-      }
-      if (!f.mkdirs()) {
-        throw new IOException("Unable to create directory:" + f.getAbsolutePath());
-      }
-    }
-
-    protected static void delete(File f) throws IOException {
-      if (f.isDirectory()) {
-        for (File s : f.listFiles()) {
-          delete(s);
-        }
-      }
-      if (!f.delete()) {
-        throw new IOException("Unable to delete:" + f.getAbsolutePath());
-      }
-    }
 }
 
diff --git a/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQPActivity.java b/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQPActivity.java
index ab7ce16..cc085ac 100644
--- a/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQPActivity.java
+++ b/platform_tools/android/apps/skqp/src/main/java/org/skia/skqp/SkQPActivity.java
@@ -1,14 +1,15 @@
 package org.skia.skqp;
 
 import android.content.Context;
+import android.content.res.AssetManager;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.util.Log;
+import java.io.File;
+import java.io.IOException;
 
 public class SkQPActivity extends AppCompatActivity implements Runnable {
-    private SkQP testRunner = new SkQP();
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -28,8 +29,78 @@
 
         Context context = getApplicationContext();
         String outputDirPath = "/sdcard/Android/data/" + context.getPackageName();
-        testRunner.runTests(context, outputDirPath);
+        SkQP impl = new SkQP();
+
+        Log.i(SkQP.LOG_PREFIX, "Output Dir: " + outputDirPath);
+        File outputDir = new File(outputDirPath);
+        try {
+            SkQPActivity.ensureEmtpyDirectory(outputDir);
+        } catch (IOException e) {
+            Log.e(SkQP.LOG_PREFIX, "ensureEmtpyDirectory:" + e.getMessage());
+        }
+
+        // Note: nInit will initialize the mGMs, mBackends and mUnitTests fields.
+        AssetManager assetManager = context.getResources().getAssets();
+        impl.nInit(assetManager, outputDirPath);
+
+        for (int backend = 0; backend < impl.mBackends.length; backend++) {
+          String classname = SkQP.kSkiaGM + impl.mBackends[backend];
+          for (int gm = 0; gm < impl.mGMs.length; gm++) {
+              String testName = SkQP.kSkiaGM + impl.mBackends[backend] + "_" + impl.mGMs[gm];
+              long value = java.lang.Long.MAX_VALUE;
+              String error = null;
+              Log.i(SkQP.LOG_PREFIX, "Running: " + testName);
+              try {
+                  value = impl.nExecuteGM(gm, backend);
+              } catch (SkQPException exept) {
+                  error = exept.getMessage();
+              }
+              if (error != null) {
+                  Log.w(SkQP.LOG_PREFIX, "Error: " + testName + " " + error);
+              } else if (value != 0) {
+                  Log.w(SkQP.LOG_PREFIX, String.format("Fail: %s %f", testName, value));
+              } else {
+                  Log.i(SkQP.LOG_PREFIX, "Pass: " + testName);
+              }
+          }
+        }
+        for (int unitTest = 0; unitTest < impl.mUnitTests.length; unitTest++) {
+            String testName = SkQP.kSkiaUnitTests + "_" + impl.mUnitTests[unitTest];
+            Log.w(SkQP.LOG_PREFIX, "Running: " + testName);
+            String[] errors = impl.nExecuteUnitTest(unitTest);
+            if (errors != null && errors.length > 0) {
+                for (String error : errors) {
+                  Log.w(SkQP.LOG_PREFIX, "Error running " + testName + ":" + error);
+                }
+            } else {
+              Log.i(SkQP.LOG_PREFIX, "Test: " + testName + " finished successfully.");
+            }
+        }
+        Log.i(SkQP.LOG_PREFIX, "Finished running all tests.");
+        impl.nMakeReport();
+
+
         finish();
     }
+
+    private static void ensureEmtpyDirectory(File f) throws IOException {
+        if (f.exists()) {
+            SkQPActivity.delete(f);
+        }
+        if (!f.mkdirs()) {
+            throw new IOException("Unable to create directory:" + f.getAbsolutePath());
+        }
+    }
+
+    private static void delete(File f) throws IOException {
+        if (f.isDirectory()) {
+            for (File s : f.listFiles()) {
+                SkQPActivity.delete(s);
+            }
+        }
+        if (!f.delete()) {
+            throw new IOException("Unable to delete:" + f.getAbsolutePath());
+        }
+    }
 }