1/*
2 * Copyright (C) 2013 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
17package com.android.rs.test_compatlegacy;
18import android.content.Context;
19import android.util.Log;
20import android.support.v8.renderscript.RenderScript.RSMessageHandler;
21
22public class UnitTest extends Thread {
23    public String name;
24    private int result;
25    private ScriptField_ListAllocs_s.Item mItem;
26    private RSTestCore mRSTC;
27    private boolean msgHandled;
28    protected Context mCtx;
29
30    /* These constants must match those in shared.rsh */
31    public static final int RS_MSG_TEST_PASSED = 100;
32    public static final int RS_MSG_TEST_FAILED = 101;
33
34    private static int numTests = 0;
35    public int testID;
36
37    protected UnitTest(RSTestCore rstc, String n, int initResult, Context ctx) {
38        super();
39        mRSTC = rstc;
40        name = n;
41        msgHandled = false;
42        mCtx = ctx;
43        result = initResult;
44        testID = numTests++;
45    }
46
47    protected UnitTest(RSTestCore rstc, String n, Context ctx) {
48        this(rstc, n, 0, ctx);
49    }
50
51    protected UnitTest(RSTestCore rstc, Context ctx) {
52        this (rstc, "<Unknown>", ctx);
53    }
54
55    protected UnitTest(Context ctx) {
56        this (null, ctx);
57    }
58
59    protected void _RS_ASSERT(String message, boolean b) {
60        if(b == false) {
61            Log.e(name, message + " FAILED");
62            failTest();
63        }
64    }
65
66    private void updateUI() {
67        if (mItem != null) {
68            mItem.result = result;
69            msgHandled = true;
70            try {
71                mRSTC.refreshTestResults();
72            }
73            catch (IllegalStateException e) {
74                /* Ignore the case where our message receiver has been
75                   disconnected. This happens when we leave the application
76                   before it finishes running all of the unit tests. */
77            }
78        }
79    }
80
81    protected RSMessageHandler mRsMessage = new RSMessageHandler() {
82        public void run() {
83            if (result == 0) {
84                switch (mID) {
85                    case RS_MSG_TEST_PASSED:
86                        result = 1;
87                        break;
88                    case RS_MSG_TEST_FAILED:
89                        result = -1;
90                        break;
91                    default:
92                        RSTest.log("Unit test got unexpected message");
93                        return;
94                }
95            }
96
97            updateUI();
98        }
99    };
100
101    public void waitForMessage() {
102        while (!msgHandled) {
103            yield();
104        }
105    }
106
107    public int getResult() {
108        return result;
109    }
110
111    public void failTest() {
112        result = -1;
113        updateUI();
114    }
115
116    public void passTest() {
117        if (result != -1) {
118            result = 1;
119        }
120        updateUI();
121    }
122
123    public String toString() {
124        String out = name;
125        if (result == 1) {
126            out += " - PASSED";
127        }
128        else if (result == -1) {
129            out += " - FAILED";
130        }
131        return out;
132    }
133
134    public void setItem(ScriptField_ListAllocs_s.Item item) {
135        mItem = item;
136    }
137
138    public void run() {
139        /* This method needs to be implemented for each subclass */
140        if (mRSTC != null) {
141            mRSTC.refreshTestResults();
142        }
143    }
144}
145