1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.rs.rsov.test; 18 19import android.content.Context; 20import android.renderscript.RenderScript.RSMessageHandler; 21import android.util.Log; 22 23public class UnitTest extends Thread { 24 public String name; 25 private int result; 26 private ScriptField_ListAllocs_s.Item mItem; 27 private RSoVTestCore mRSTC; 28 private boolean msgHandled; 29 protected Context mCtx; 30 31 /* These constants must match those in shared.rsh */ 32 public static final int RS_MSG_TEST_PASSED = 100; 33 public static final int RS_MSG_TEST_FAILED = 101; 34 public static final int TEST_PASSED = 1; 35 public static final int TEST_FAILED = -1; 36 37 private static int numTests = 0; 38 public int testID; 39 40 protected UnitTest(RSoVTestCore rstc, String n, int initResult, Context ctx) { 41 super(); 42 mRSTC = rstc; 43 name = n; 44 msgHandled = false; 45 mCtx = ctx; 46 result = initResult; 47 testID = numTests++; 48 } 49 50 protected UnitTest(RSoVTestCore rstc, String n, Context ctx) { 51 this(rstc, n, 0, ctx); 52 } 53 54 protected UnitTest(RSoVTestCore rstc, Context ctx) { 55 this(rstc, "<Unknown>", ctx); 56 } 57 58 protected UnitTest(Context ctx) { 59 this(null, ctx); 60 } 61 62 protected void _RS_ASSERT(String message, boolean b) { 63 if (b == false) { 64 Log.e(name, message + " FAILED"); 65 failTest(); 66 } 67 } 68 69 private void updateUI() { 70 msgHandled = true; 71 if (mItem != null) { 72 mItem.result = result; 73 if (mRSTC != null) { 74 // Add null check for mRSTC, for instrumentation tests. 75 try { 76 mRSTC.refreshTestResults(); 77 } catch (IllegalStateException e) { 78 /* Ignore the case where our message receiver has been 79 disconnected. This happens when we leave the application 80 before it finishes running all of the unit tests. */ 81 } 82 } 83 } 84 } 85 86 protected RSMessageHandler mRsMessage = new RSMessageHandler() { 87 public void run() { 88 if (result == 0) { 89 switch (mID) { 90 case RS_MSG_TEST_PASSED: 91 result = TEST_PASSED; 92 break; 93 case RS_MSG_TEST_FAILED: 94 result = TEST_FAILED; 95 break; 96 default: 97 RSoVTest.log("Unit test got unexpected message"); 98 return; 99 } 100 } 101 102 updateUI(); 103 } 104 }; 105 106 public void waitForMessage() { 107 while (!msgHandled) { 108 yield(); 109 } 110 } 111 112 public int getResult() { 113 return result; 114 } 115 116 public void failTest() { 117 result = TEST_FAILED; 118 updateUI(); 119 } 120 121 public void passTest() { 122 if (result != TEST_FAILED) { 123 result = TEST_PASSED; 124 } 125 updateUI(); 126 } 127 128 public String toString() { 129 String out = name; 130 if (result == TEST_PASSED) { 131 out += " - PASSED"; 132 } else if (result == TEST_FAILED) { 133 out += " - FAILED"; 134 } 135 return out; 136 } 137 138 public void setItem(ScriptField_ListAllocs_s.Item item) { 139 mItem = item; 140 } 141 142 public void run() { 143 /* This method needs to be implemented for each subclass */ 144 if (mRSTC != null) { 145 mRSTC.refreshTestResults(); 146 } 147 } 148} 149