UnitTest.java revision 031ec58cfc7a20927302a5300eba3f5fc1709b50
1ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines/*
2ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Copyright (C) 2010 The Android Open Source Project
3ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines *
4ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Licensed under the Apache License, Version 2.0 (the "License");
5ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * you may not use this file except in compliance with the License.
6ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * You may obtain a copy of the License at
7ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines *
8ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines *      http://www.apache.org/licenses/LICENSE-2.0
9ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines *
10ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * Unless required by applicable law or agreed to in writing, software
11ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * distributed under the License is distributed on an "AS IS" BASIS,
12ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * See the License for the specific language governing permissions and
14ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines * limitations under the License.
15ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines */
16ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
17ff4d70872aa48675a06fc4673c732860c1619758Stephen Hinespackage com.android.rs.test;
18ff4d70872aa48675a06fc4673c732860c1619758Stephen Hinesimport android.renderscript.RenderScript.RSMessage;
19ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hinesimport android.util.Log;
20ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
21ff4d70872aa48675a06fc4673c732860c1619758Stephen Hinespublic class UnitTest extends Thread {
22ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public String name;
23ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public int result;
24bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    private ScriptField_ListAllocs_s.Item mItem;
25bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    private RSTestCore mRSTC;
2601f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines    private boolean msgHandled;
27ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
28ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    /* These constants must match those in shared.rsh */
29ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public static final int RS_MSG_TEST_PASSED = 100;
30ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public static final int RS_MSG_TEST_FAILED = 101;
31ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
32ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines    private static int numTests = 0;
33ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines    public int testID;
34ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines
35bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    protected UnitTest(RSTestCore rstc, String n, int initResult) {
36ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        super();
37bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        mRSTC = rstc;
38ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        name = n;
3901f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        msgHandled = false;
40ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        result = initResult;
41ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        testID = numTests++;
42ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
43ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
44bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    protected UnitTest(RSTestCore rstc, String n) {
45bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        this(rstc, n, 0);
46bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    }
47bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
48bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    protected UnitTest(RSTestCore rstc) {
49bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        this (rstc, "<Unknown>");
50ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
51ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
52ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    protected UnitTest() {
53bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        this (null);
54ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
55ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
56ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    protected RSMessage mRsMessage = new RSMessage() {
57ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        public void run() {
58031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines            if (result == 0) {
59031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                switch (mID) {
60031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    case RS_MSG_TEST_PASSED:
61031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        result = 1;
62031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        break;
63031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    case RS_MSG_TEST_FAILED:
64031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        result = -1;
65031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        break;
66031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    default:
67031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        android.util.Log.v("RenderScript", "Unit test got unexpected message");
68031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        return;
69031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                }
70ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines            }
71bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
72bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines            if (mItem != null) {
73bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines                mItem.result = result;
7401f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines                msgHandled = true;
75ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                try {
76ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                    mRSTC.refreshTestResults();
77ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                }
78ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                catch (IllegalStateException e) {
79ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                    /* Ignore the case where our message receiver has been
80ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                       disconnected. This happens when we leave the application
81ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                       before it finishes running all of the unit tests. */
82ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                }
83bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines            }
84ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        }
85ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    };
86ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
8701f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines    public void waitForMessage() {
8801f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        while (!msgHandled) {
8901f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines            yield();
9001f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        }
9101f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines    }
9201f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines
93bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    public void setItem(ScriptField_ListAllocs_s.Item item) {
94bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        mItem = item;
95bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    }
96bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
97ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public void run() {
98ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        /* This method needs to be implemented for each subclass */
99ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        if (mRSTC != null) {
100ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines            mRSTC.refreshTestResults();
101ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        }
102ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
103ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines}
104ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
105