UnitTest.java revision 6b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bd
1ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines/* 2ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Copyright (C) 2010 The Android Open Source Project 3ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * 4ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Licensed under the Apache License, Version 2.0 (the "License"); 5ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * you may not use this file except in compliance with the License. 6ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * You may obtain a copy of the License at 7ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * 8ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * http://www.apache.org/licenses/LICENSE-2.0 9ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * 10ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Unless required by applicable law or agreed to in writing, software 11ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * distributed under the License is distributed on an "AS IS" BASIS, 12ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * See the License for the specific language governing permissions and 14ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * limitations under the License. 15ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines */ 16ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 17ff4d70872aa48675a06fc4673c732860c1619758Stephen Hinespackage com.android.rs.test; 186b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liaoimport android.content.Context; 19bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Samsimport android.renderscript.RenderScript.RSMessageHandler; 20ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hinesimport android.util.Log; 21ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 22ff4d70872aa48675a06fc4673c732860c1619758Stephen Hinespublic class UnitTest extends Thread { 23ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public String name; 24ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public int result; 25bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines private ScriptField_ListAllocs_s.Item mItem; 26bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines private RSTestCore mRSTC; 2701f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines private boolean msgHandled; 286b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao protected Context mCtx; 29ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 30ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines /* These constants must match those in shared.rsh */ 31ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public static final int RS_MSG_TEST_PASSED = 100; 32ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public static final int RS_MSG_TEST_FAILED = 101; 33ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 34ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines private static int numTests = 0; 35ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines public int testID; 36ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines 376b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao protected UnitTest(RSTestCore rstc, String n, int initResult, Context ctx) { 38ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines super(); 39bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines mRSTC = rstc; 40ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines name = n; 4101f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines msgHandled = false; 426b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao mCtx = ctx; 43ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines result = initResult; 44ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines testID = numTests++; 45ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 46ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 476b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao protected UnitTest(RSTestCore rstc, String n, Context ctx) { 486b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao this(rstc, n, 0, ctx); 49bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines } 50bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines 516b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao protected UnitTest(RSTestCore rstc, Context ctx) { 526b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao this (rstc, "<Unknown>", ctx); 53ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 54ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 556b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao protected UnitTest(Context ctx) { 566b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao this (null, ctx); 57ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 58ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 59bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams protected RSMessageHandler mRsMessage = new RSMessageHandler() { 60ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public void run() { 61031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines if (result == 0) { 62031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines switch (mID) { 63031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines case RS_MSG_TEST_PASSED: 64031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines result = 1; 65031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines break; 66031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines case RS_MSG_TEST_FAILED: 67031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines result = -1; 68031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines break; 69031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines default: 70031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines android.util.Log.v("RenderScript", "Unit test got unexpected message"); 71031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines return; 72031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines } 73ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 74bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines 75bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines if (mItem != null) { 76bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines mItem.result = result; 7701f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines msgHandled = true; 78ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines try { 79ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines mRSTC.refreshTestResults(); 80ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines } 81ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines catch (IllegalStateException e) { 82ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines /* Ignore the case where our message receiver has been 83ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines disconnected. This happens when we leave the application 84ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines before it finishes running all of the unit tests. */ 85ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines } 86bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines } 87ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 88ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines }; 89ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines 9001f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines public void waitForMessage() { 9101f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines while (!msgHandled) { 9201f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines yield(); 9301f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines } 9401f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines } 9501f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines 96bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines public void setItem(ScriptField_ListAllocs_s.Item item) { 97bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines mItem = item; 98bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines } 99bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines 100ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines public void run() { 101ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines /* This method needs to be implemented for each subclass */ 102ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines if (mRSTC != null) { 103ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines mRSTC.refreshTestResults(); 104ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines } 105ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines } 106ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines} 107