1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 package org.skia.skqp; 9 10 import android.content.Context; 11 import android.content.res.AssetManager; 12 import android.util.Log; 13 import java.io.File; 14 import java.io.IOException; 15 16 public class SkQP { 17 protected native void nInit(AssetManager assetManager, String dataDir, boolean experimentalMode); 18 protected native float nExecuteGM(int gm, int backend) throws SkQPException; 19 protected native String[] nExecuteUnitTest(int test); 20 protected native void nMakeReport(); 21 22 protected String[] mGMs; 23 protected String[] mBackends; 24 protected String[] mUnitTests; 25 26 protected static final String kSkiaGM = "skqp_"; 27 protected static final String kSkiaUnitTests = "skqp_unitTest"; 28 protected static final String LOG_PREFIX = "org.skia.skqp"; 29 30 static { 31 System.loadLibrary("skqp_app"); 32 } 33 34 protected void runTests(Context context, String outputDirPath) { 35 Log.i(LOG_PREFIX, "Output Dir: " + outputDirPath); 36 File outputDir = new File(outputDirPath); 37 try { 38 ensureEmtpyDirectory(outputDir); 39 } catch (IOException e) { 40 Log.e(LOG_PREFIX, "ensureEmtpyDirectory:" + e.getMessage()); 41 } 42 43 // Note: nInit will initialize the mGMs, mBackends and mUnitTests fields. 44 AssetManager assetManager = context.getResources().getAssets(); 45 this.nInit(assetManager, outputDirPath, true); 46 47 for (int backend = 0; backend < mBackends.length; backend++) { 48 String classname = kSkiaGM + mBackends[backend]; 49 for (int gm = 0; gm < mGMs.length; gm++) { 50 String testName = kSkiaGM + mBackends[backend] + "_" +mGMs[gm]; 51 float value = java.lang.Float.MAX_VALUE; 52 String error = null; 53 Log.w(LOG_PREFIX, "Running: " + testName); 54 try { 55 value = this.nExecuteGM(gm, backend); 56 } catch (SkQPException exept) { 57 error = exept.getMessage(); 58 } 59 if (error != null) { 60 // Record error message and carry on. 61 } else if (value != 0) { 62 // Record failure and carry on. 63 // SkQPRunner.Fail(desc, notifier, String.format( 64 // "Image mismatch: max channel diff = %f", value)); 65 } else { 66 // Record success for this test. 67 } 68 } 69 } 70 for (int unitTest = 0; unitTest < mUnitTests.length; unitTest++) { 71 String testName = kSkiaUnitTests + "_" + mUnitTests[unitTest]; 72 Log.w(LOG_PREFIX, "Running: " + testName); 73 String[] errors = this.nExecuteUnitTest(unitTest); 74 if (errors != null && errors.length > 0) { 75 for (String error : errors) { 76 Log.w(LOG_PREFIX, "Error running " + testName + ":" + error); 77 } 78 } else { 79 Log.i(LOG_PREFIX, "Test: " + testName + " finished successfully."); 80 } 81 } 82 Log.i(LOG_PREFIX, "Finished running all tests."); 83 nMakeReport(); 84 } 85 86 protected static void ensureEmtpyDirectory(File f) throws IOException { 87 if (f.exists()) { 88 delete(f); 89 } 90 if (!f.mkdirs()) { 91 throw new IOException("Unable to create directory:" + f.getAbsolutePath()); 92 } 93 } 94 95 protected static void delete(File f) throws IOException { 96 if (f.isDirectory()) { 97 for (File s : f.listFiles()) { 98 delete(s); 99 } 100 } 101 if (!f.delete()) { 102 throw new IOException("Unable to delete:" + f.getAbsolutePath()); 103 } 104 } 105 } 106 107