• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.cts.rsblas;
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     // msgHandled is used to synchronize between waitForMessage() and the
38     // RSMessageHandler thread.
39     private volatile boolean msgHandled;
40 
41     protected static final int RS_MSG_TEST_PASSED = 100;
42     protected static final int RS_MSG_TEST_FAILED = 101;
43     protected static final int RS_MSG_TEST_FLUSH = 102;
44 
45     RSMessageHandler mRsMessage = new RSMessageHandler() {
46         public void run() {
47             if (result == 0) {
48                 switch (mID) {
49                     case RS_MSG_TEST_PASSED:
50                     case RS_MSG_TEST_FAILED:
51                         result = mID;
52                         break;
53                     case RS_MSG_TEST_FLUSH:
54                         break;
55                     default:
56                         fail("Got unexpected RS message");
57                         return;
58                 }
59             }
60             msgHandled = true;
61         }
62     };
63 
waitForMessage()64     protected void waitForMessage() {
65         while (!msgHandled) {
66             Thread.yield();
67         }
68     }
69 
70     protected boolean FoundError = false;
71     protected RSErrorHandler mRsError = new RSErrorHandler() {
72         public void run() {
73             FoundError = true;
74             Log.e("RenderscriptCTS", mErrorMessage);
75             throw new RSRuntimeException("Received error " + mErrorNum +
76                                          " message " + mErrorMessage);
77         }
78     };
79 
80     @Override
setUp()81     protected void setUp() throws Exception {
82         super.setUp();
83         result = 0;
84         msgHandled = false;
85         mCtx = getContext();
86         mRes = mCtx.getResources();
87     }
88 
89     /**
90      * Verify that we didn't fail on the control or script side of things.
91      */
checkForErrors()92     protected void checkForErrors() {
93         assertFalse(FoundError);
94         assertTrue(result != RS_MSG_TEST_FAILED);
95     }
96 }
97