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