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