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_v16;
     18 import android.content.Context;
     19 import android.util.Log;
     20 import android.renderscript.RenderScript.RSMessageHandler;
     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 void _RS_ASSERT(String message, boolean b) {
     60         if(b == false) {
     61             result = -1;
     62             Log.e(name, message + " FAILED");
     63         }
     64     }
     65 
     66     protected void updateUI() {
     67         if (mItem != null) {
     68             mItem.result = result;
     69             msgHandled = true;
     70             try {
     71                 mRSTC.refreshTestResults();
     72             }
     73             catch (IllegalStateException e) {
     74                 /* Ignore the case where our message receiver has been
     75                    disconnected. This happens when we leave the application
     76                    before it finishes running all of the unit tests. */
     77             }
     78         }
     79     }
     80 
     81     protected RSMessageHandler mRsMessage = new RSMessageHandler() {
     82         public void run() {
     83             if (result == 0) {
     84                 switch (mID) {
     85                     case RS_MSG_TEST_PASSED:
     86                         result = 1;
     87                         break;
     88                     case RS_MSG_TEST_FAILED:
     89                         result = -1;
     90                         break;
     91                     default:
     92                         RSTest_v16.log("Unit test got unexpected message");
     93                         return;
     94                 }
     95             }
     96 
     97             updateUI();
     98         }
     99     };
    100 
    101     public void waitForMessage() {
    102         while (!msgHandled) {
    103             yield();
    104         }
    105     }
    106 
    107     public void setItem(ScriptField_ListAllocs_s.Item item) {
    108         mItem = item;
    109     }
    110 
    111     public void run() {
    112         /* This method needs to be implemented for each subclass */
    113         if (mRSTC != null) {
    114             mRSTC.refreshTestResults();
    115         }
    116     }
    117 }
    118