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