Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011-2012 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 android.renderscript.cts;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.os.StrictMode;
     22 import android.renderscript.RenderScript.RSErrorHandler;
     23 import android.renderscript.RenderScript.RSMessageHandler;
     24 import android.renderscript.RSRuntimeException;
     25 import android.test.AndroidTestCase;
     26 import android.util.Log;
     27 
     28 /**
     29  * Base RenderScript test class. This class provides a message handler and a
     30  * convenient way to wait for compute scripts to complete their execution.
     31  */
     32 public class RSBase extends AndroidTestCase {
     33 
     34     Context mCtx;
     35     Resources mRes;
     36 
     37     private int result;
     38     // msgHandled is used to synchronize between waitForMessage() and the
     39     // RSMessageHandler thread.
     40     private volatile boolean msgHandled;
     41 
     42     protected static final int RS_MSG_TEST_PASSED = 100;
     43     protected static final int RS_MSG_TEST_FAILED = 101;
     44     protected static final int RS_MSG_TEST_FLUSH = 102;
     45 
     46     RSMessageHandler mRsMessage = new RSMessageHandler() {
     47         public void run() {
     48             if (result == 0) {
     49                 switch (mID) {
     50                     case RS_MSG_TEST_PASSED:
     51                     case RS_MSG_TEST_FAILED:
     52                         result = mID;
     53                         break;
     54                     case RS_MSG_TEST_FLUSH:
     55                         break;
     56                     default:
     57                         fail("Got unexpected RS message");
     58                         return;
     59                 }
     60             }
     61             msgHandled = true;
     62         }
     63     };
     64 
     65     protected void waitForMessage() {
     66         while (!msgHandled) {
     67             Thread.yield();
     68         }
     69     }
     70 
     71     protected boolean FoundError = false;
     72     protected RSErrorHandler mRsError = new RSErrorHandler() {
     73         public void run() {
     74             FoundError = true;
     75             Log.e("RenderscriptCTS", mErrorMessage);
     76             throw new RSRuntimeException("Received error " + mErrorNum +
     77                                          " message " + mErrorMessage);
     78         }
     79     };
     80 
     81     @Override
     82     protected void setUp() throws Exception {
     83         super.setUp();
     84 
     85         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
     86                                .detectLeakedClosableObjects()
     87                                .penaltyLog()
     88                                .build());
     89 
     90         result = 0;
     91         msgHandled = false;
     92         mCtx = getContext();
     93         mRes = mCtx.getResources();
     94     }
     95 
     96     /**
     97      * Verify that we didn't fail on the control or script side of things.
     98      */
     99     protected void checkForErrors() {
    100         assertFalse(FoundError);
    101         assertTrue(result != RS_MSG_TEST_FAILED);
    102     }
    103 }
    104