1 /* 2 * Copyright (C) 2010 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 17 package com.android.rs.test; 18 import android.content.Context; 19 import android.renderscript.RenderScript.RSMessageHandler; 20 21 public class UnitTest extends Thread { 22 public String name; 23 public int result; 24 private ScriptField_ListAllocs_s.Item mItem; 25 private RSTestCore mRSTC; 26 private boolean msgHandled; 27 protected Context mCtx; 28 29 /* These constants must match those in shared.rsh */ 30 public static final int RS_MSG_TEST_PASSED = 100; 31 public static final int RS_MSG_TEST_FAILED = 101; 32 33 private static int numTests = 0; 34 public int testID; 35 UnitTest(RSTestCore rstc, String n, int initResult, Context ctx)36 protected UnitTest(RSTestCore rstc, String n, int initResult, Context ctx) { 37 super(); 38 mRSTC = rstc; 39 name = n; 40 msgHandled = false; 41 mCtx = ctx; 42 result = initResult; 43 testID = numTests++; 44 } 45 UnitTest(RSTestCore rstc, String n, Context ctx)46 protected UnitTest(RSTestCore rstc, String n, Context ctx) { 47 this(rstc, n, 0, ctx); 48 } 49 UnitTest(RSTestCore rstc, Context ctx)50 protected UnitTest(RSTestCore rstc, Context ctx) { 51 this (rstc, "<Unknown>", ctx); 52 } 53 UnitTest(Context ctx)54 protected UnitTest(Context ctx) { 55 this (null, ctx); 56 } 57 58 protected RSMessageHandler mRsMessage = new RSMessageHandler() { 59 public void run() { 60 if (result == 0) { 61 switch (mID) { 62 case RS_MSG_TEST_PASSED: 63 result = 1; 64 break; 65 case RS_MSG_TEST_FAILED: 66 result = -1; 67 break; 68 default: 69 RSTest.log("Unit test got unexpected message"); 70 return; 71 } 72 } 73 74 if (mItem != null) { 75 mItem.result = result; 76 msgHandled = true; 77 try { 78 mRSTC.refreshTestResults(); 79 } 80 catch (IllegalStateException e) { 81 /* Ignore the case where our message receiver has been 82 disconnected. This happens when we leave the application 83 before it finishes running all of the unit tests. */ 84 } 85 } 86 } 87 }; 88 waitForMessage()89 public void waitForMessage() { 90 while (!msgHandled) { 91 yield(); 92 } 93 } 94 setItem(ScriptField_ListAllocs_s.Item item)95 public void setItem(ScriptField_ListAllocs_s.Item item) { 96 mItem = item; 97 } 98 run()99 public void run() { 100 /* This method needs to be implemented for each subclass */ 101 if (mRSTC != null) { 102 mRSTC.refreshTestResults(); 103 } 104 } 105 } 106