UnitTest.java revision 3d782666d73c3ce0ffb77e71db7309bc3297ecb8
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;
186b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liaoimport android.content.Context;
19bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Samsimport android.renderscript.RenderScript.RSMessageHandler;
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;
276b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao    protected Context mCtx;
28ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
29ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    /* These constants must match those in shared.rsh */
30ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public static final int RS_MSG_TEST_PASSED = 100;
31ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public static final int RS_MSG_TEST_FAILED = 101;
32ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
33ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines    private static int numTests = 0;
34ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines    public int testID;
35ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines
366b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao    protected UnitTest(RSTestCore rstc, String n, int initResult, Context ctx) {
37ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        super();
38bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        mRSTC = rstc;
39ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        name = n;
4001f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        msgHandled = false;
416b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao        mCtx = ctx;
42ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        result = initResult;
43ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        testID = numTests++;
44ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
45ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
466b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao    protected UnitTest(RSTestCore rstc, String n, Context ctx) {
476b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao        this(rstc, n, 0, ctx);
48bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    }
49bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
506b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao    protected UnitTest(RSTestCore rstc, Context ctx) {
516b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao        this (rstc, "<Unknown>", ctx);
52ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
53ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
546b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao    protected UnitTest(Context ctx) {
556b32fab1dbfd8bc1cc176557fe0a7b2ebd4966bdShih-wei Liao        this (null, ctx);
56ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
57ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
58bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    protected RSMessageHandler mRsMessage = new RSMessageHandler() {
59ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        public void run() {
60031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines            if (result == 0) {
61031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                switch (mID) {
62031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    case RS_MSG_TEST_PASSED:
63031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        result = 1;
64031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        break;
65031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    case RS_MSG_TEST_FAILED:
66031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        result = -1;
67031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        break;
68031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                    default:
693d782666d73c3ce0ffb77e71db7309bc3297ecb8Stephen Hines                        RSTest.log("Unit test got unexpected message");
70031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                        return;
71031ec58cfc7a20927302a5300eba3f5fc1709b50Stephen Hines                }
72ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines            }
73bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
74bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines            if (mItem != null) {
75bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines                mItem.result = result;
7601f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines                msgHandled = true;
77ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                try {
78ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                    mRSTC.refreshTestResults();
79ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                }
80ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                catch (IllegalStateException e) {
81ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                    /* Ignore the case where our message receiver has been
82ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                       disconnected. This happens when we leave the application
83ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                       before it finishes running all of the unit tests. */
84ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines                }
85bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines            }
86ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        }
87ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    };
88ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines
8901f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines    public void waitForMessage() {
9001f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        while (!msgHandled) {
9101f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines            yield();
9201f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines        }
9301f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines    }
9401f0ad7c13b8878c2167bff10ea875d7509edca5Stephen Hines
95bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    public void setItem(ScriptField_ListAllocs_s.Item item) {
96bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines        mItem = item;
97bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines    }
98bbc529244d7ec0ade499afebe0582be6735a4416Stephen Hines
99ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    public void run() {
100ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines        /* This method needs to be implemented for each subclass */
101ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        if (mRSTC != null) {
102ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines            mRSTC.refreshTestResults();
103ab98bb6e8b95bef7415c1ad239be71f93322fbadStephen Hines        }
104ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines    }
105ff4d70872aa48675a06fc4673c732860c1619758Stephen Hines}
106