1/*
2 * Copyright (C) 2008-2012 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;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.renderscript.*;
22import android.util.Log;
23import java.util.ArrayList;
24import java.util.ListIterator;
25import java.util.Timer;
26import java.util.TimerTask;
27import android.app.ListActivity;
28import android.widget.ArrayAdapter;
29
30public class RSTestCore {
31    ListActivity mCtx;
32
33    public RSTestCore(ListActivity ctx) {
34        mCtx = ctx;
35    }
36
37    private Resources mRes;
38    private RenderScript mRS;
39
40    private ArrayList<UnitTest> unitTests;
41    private ListIterator<UnitTest> test_iter;
42    private UnitTest activeTest;
43    private boolean stopTesting;
44
45    private ScriptField_ListAllocs_s mListAllocs;
46
47    private ArrayAdapter<UnitTest> testAdapter;
48
49    /* Periodic timer for ensuring future tests get scheduled */
50    private Timer mTimer;
51    public static final int RS_TIMER_PERIOD = 100;
52
53    public void init(RenderScript rs, Resources res) {
54        mRS = rs;
55        mRes = res;
56        stopTesting = false;
57
58        unitTests = new ArrayList<UnitTest>();
59
60        unitTests.add(new UT_primitives(this, mRes, mCtx));
61        unitTests.add(new UT_constant(this, mRes, mCtx));
62        unitTests.add(new UT_vector(this, mRes, mCtx));
63        unitTests.add(new UT_unsigned(this, mRes, mCtx));
64        unitTests.add(new UT_array_init(this, mRes, mCtx));
65        unitTests.add(new UT_array_alloc(this, mRes, mCtx));
66        unitTests.add(new UT_kernel(this, mRes, mCtx));
67        unitTests.add(new UT_kernel_struct(this, mRes, mCtx));
68        unitTests.add(new UT_kernel2d(this, mRes, mCtx));
69        unitTests.add(new UT_kernel3d(this, mRes, mCtx));
70        unitTests.add(new UT_kernel2d_oldstyle(this, mRes, mCtx));
71        unitTests.add(new UT_ctxt_default(this, mRes, mCtx));
72        unitTests.add(new UT_bug_char(this, mRes, mCtx));
73        unitTests.add(new UT_clamp(this, mRes, mCtx));
74        unitTests.add(new UT_clamp_relaxed(this, mRes, mCtx));
75        unitTests.add(new UT_convert(this, mRes, mCtx));
76        unitTests.add(new UT_convert_relaxed(this, mRes, mCtx));
77        unitTests.add(new UT_copy_test(this, mRes, mCtx));
78        unitTests.add(new UT_rsdebug(this, mRes, mCtx));
79        unitTests.add(new UT_rstime(this, mRes, mCtx));
80        unitTests.add(new UT_rstypes(this, mRes, mCtx));
81        unitTests.add(new UT_alloc(this, mRes, mCtx));
82        unitTests.add(new UT_check_dims(this, mRes, mCtx));
83        unitTests.add(new UT_static_globals(this, mRes, mCtx));
84        unitTests.add(new UT_refcount(this, mRes, mCtx));
85        unitTests.add(new UT_foreach(this, mRes, mCtx));
86        unitTests.add(new UT_foreach_bounds(this, mRes, mCtx));
87        unitTests.add(new UT_noroot(this, mRes, mCtx));
88        unitTests.add(new UT_single_source_alloc(this, mRes, mCtx));
89        unitTests.add(new UT_single_source_script(this, mRes, mCtx));
90        unitTests.add(new UT_single_source_ref_count(this, mRes, mCtx));
91        unitTests.add(new UT_script_group2_pointwise(this, mRes, mCtx));
92        unitTests.add(new UT_script_group2_gatherscatter(this, mRes, mCtx));
93        unitTests.add(new UT_script_group2_nochain(this, mRes, mCtx));
94        unitTests.add(new UT_script_group2_float(this, mRes, mCtx));
95        unitTests.add(new UT_atomic(this, mRes, mCtx));
96        unitTests.add(new UT_struct(this, mRes, mCtx));
97        unitTests.add(new UT_math(this, mRes, mCtx));
98        unitTests.add(new UT_math_conformance(this, mRes, mCtx));
99        unitTests.add(new UT_math_agree(this, mRes, mCtx));
100        unitTests.add(new UT_fp16(this, mRes, mCtx));
101        unitTests.add(new UT_math_fp16(this, mRes, mCtx));
102        unitTests.add(new UT_fp16_globals(this, mRes, mCtx));
103        unitTests.add(new UT_min(this, mRes, mCtx));
104        unitTests.add(new UT_int4(this, mRes, mCtx));
105        unitTests.add(new UT_element(this, mRes, mCtx));
106        unitTests.add(new UT_sampler(this, mRes, mCtx));
107        /*unitTests.add(new UT_program_store(this, mRes, mCtx));
108        unitTests.add(new UT_program_raster(this, mRes, mCtx));
109        unitTests.add(new UT_mesh(this, mRes, mCtx));*/
110        unitTests.add(new UT_foreach_multi(this, mRes, mCtx));
111        unitTests.add(new UT_fp_mad(this, mRes, mCtx));
112        unitTests.add(new UT_reduce(this, mRes, mCtx));
113        unitTests.add(new UT_reduce_backward(this, mRes, mCtx));
114        unitTests.add(new UT_small_struct(this, mRes, mCtx));
115
116        /*
117        unitTests.add(new UnitTest(null, "<Pass>", 1));
118        unitTests.add(new UnitTest());
119        unitTests.add(new UnitTest(null, "<Fail>", -1));
120
121        for (int i = 0; i < 20; i++) {
122            unitTests.add(new UnitTest(null, "<Pass>", 1));
123        }
124        */
125
126        UnitTest [] uta = new UnitTest[unitTests.size()];
127        uta = unitTests.toArray(uta);
128
129        mListAllocs = new ScriptField_ListAllocs_s(mRS, uta.length);
130        for (int i = 0; i < uta.length; i++) {
131            ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item();
132            listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT);
133            listElem.result = uta[i].getResult();
134            mListAllocs.set(listElem, i, false);
135            uta[i].setItem(listElem);
136        }
137
138        mListAllocs.copyAll();
139
140        testAdapter = new ArrayAdapter<UnitTest>(mCtx, android.R.layout.simple_list_item_1, unitTests);
141        mCtx.setListAdapter(testAdapter);
142
143        test_iter = unitTests.listIterator();
144        refreshTestResults(); /* Kick off the first test */
145
146        TimerTask pTask = new TimerTask() {
147            public void run() {
148                refreshTestResults();
149            }
150        };
151
152        mTimer = new Timer();
153        mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD);
154    }
155
156    public void checkAndRunNextTest() {
157        mCtx.runOnUiThread(new Runnable() {
158                public void run() {
159                    if (testAdapter != null)
160                        testAdapter.notifyDataSetChanged();
161                }
162            });
163
164        if (activeTest != null) {
165            if (!activeTest.isAlive()) {
166                /* Properly clean up on our last test */
167                try {
168                    activeTest.join();
169                }
170                catch (InterruptedException e) {
171                }
172                activeTest = null;
173            }
174        }
175
176        if (!stopTesting && activeTest == null) {
177            if (test_iter.hasNext()) {
178                activeTest = test_iter.next();
179                activeTest.start();
180                /* This routine will only get called once when a new test
181                 * should start running. The message handler in UnitTest.java
182                 * ensures this. */
183            }
184            else {
185                if (mTimer != null) {
186                    mTimer.cancel();
187                    mTimer.purge();
188                    mTimer = null;
189                }
190            }
191        }
192    }
193
194    public void refreshTestResults() {
195        checkAndRunNextTest();
196    }
197
198    public void cleanup() {
199        stopTesting = true;
200        UnitTest t = activeTest;
201
202        /* Stop periodic refresh of testing */
203        if (mTimer != null) {
204            mTimer.cancel();
205            mTimer.purge();
206            mTimer = null;
207        }
208
209        /* Wait to exit until we finish the current test */
210        if (t != null) {
211            try {
212                t.join();
213            }
214            catch (InterruptedException e) {
215            }
216            t = null;
217        }
218
219    }
220
221}
222