Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2008-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 
     17 package com.android.rs.test_compat;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.support.v8.renderscript.*;
     22 import android.util.Log;
     23 import java.util.ArrayList;
     24 import java.util.ListIterator;
     25 import java.util.Timer;
     26 import java.util.TimerTask;
     27 import android.app.ListActivity;
     28 import android.widget.ArrayAdapter;
     29 
     30 public 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_instance(this, mRes, mCtx));
     62         unitTests.add(new UT_constant(this, mRes, mCtx));
     63         unitTests.add(new UT_vector(this, mRes, mCtx));
     64         unitTests.add(new UT_unsigned(this, mRes, mCtx));
     65         unitTests.add(new UT_array_init(this, mRes, mCtx));
     66         unitTests.add(new UT_array_alloc(this, mRes, mCtx));
     67         unitTests.add(new UT_kernel(this, mRes, mCtx));
     68         unitTests.add(new UT_kernel_struct(this, mRes, mCtx));
     69         unitTests.add(new UT_bug_char(this, mRes, mCtx));
     70         unitTests.add(new UT_clamp(this, mRes, mCtx));
     71         unitTests.add(new UT_clamp_relaxed(this, mRes, mCtx));
     72         unitTests.add(new UT_convert(this, mRes, mCtx));
     73         unitTests.add(new UT_convert_relaxed(this, mRes, mCtx));
     74         unitTests.add(new UT_copy_test(this, mRes, mCtx));
     75         unitTests.add(new UT_rsdebug(this, mRes, mCtx));
     76         unitTests.add(new UT_rstime(this, mRes, mCtx));
     77         unitTests.add(new UT_rstypes(this, mRes, mCtx));
     78         unitTests.add(new UT_alloc(this, mRes, mCtx));
     79         unitTests.add(new UT_refcount(this, mRes, mCtx));
     80         unitTests.add(new UT_foreach(this, mRes, mCtx));
     81         unitTests.add(new UT_foreach_bounds(this, mRes, mCtx));
     82         unitTests.add(new UT_noroot(this, mRes, mCtx));
     83         unitTests.add(new UT_atomic(this, mRes, mCtx));
     84         unitTests.add(new UT_struct(this, mRes, mCtx));
     85         unitTests.add(new UT_math(this, mRes, mCtx));
     86         unitTests.add(new UT_math_conformance(this, mRes, mCtx));
     87         unitTests.add(new UT_math_agree(this, mRes, mCtx));
     88         unitTests.add(new UT_min(this, mRes, mCtx));
     89         unitTests.add(new UT_int4(this, mRes, mCtx));
     90         unitTests.add(new UT_element(this, mRes, mCtx));
     91         unitTests.add(new UT_sampler(this, mRes, mCtx));
     92         unitTests.add(new UT_fp_mad(this, mRes, mCtx));
     93 
     94         /*
     95         unitTests.add(new UnitTest(null, "<Pass>", 1));
     96         unitTests.add(new UnitTest());
     97         unitTests.add(new UnitTest(null, "<Fail>", -1));
     98 
     99         for (int i = 0; i < 20; i++) {
    100             unitTests.add(new UnitTest(null, "<Pass>", 1));
    101         }
    102         */
    103 
    104         UnitTest [] uta = new UnitTest[unitTests.size()];
    105         uta = unitTests.toArray(uta);
    106 
    107         mListAllocs = new ScriptField_ListAllocs_s(mRS, uta.length);
    108         for (int i = 0; i < uta.length; i++) {
    109 
    110             ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item();
    111             listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT);
    112             listElem.result = uta[i].getResult();
    113             mListAllocs.set(listElem, i, false);
    114             uta[i].setItem(listElem);
    115         }
    116 
    117         mListAllocs.copyAll();
    118 
    119         testAdapter = new ArrayAdapter<UnitTest>(mCtx, android.R.layout.simple_list_item_1, unitTests);
    120         mCtx.setListAdapter(testAdapter);
    121 
    122         test_iter = unitTests.listIterator();
    123         refreshTestResults(); /* Kick off the first test */
    124 
    125         TimerTask pTask = new TimerTask() {
    126             public void run() {
    127                 refreshTestResults();
    128             }
    129         };
    130 
    131         mTimer = new Timer();
    132         mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD);
    133     }
    134 
    135     public void checkAndRunNextTest() {
    136         mCtx.runOnUiThread(new Runnable() {
    137                 public void run() {
    138                     if (testAdapter != null)
    139                         testAdapter.notifyDataSetChanged();
    140                 }
    141             });
    142 
    143         if (activeTest != null) {
    144             if (!activeTest.isAlive()) {
    145                 /* Properly clean up on our last test */
    146                 try {
    147                     activeTest.join();
    148                 }
    149                 catch (InterruptedException e) {
    150                 }
    151                 activeTest = null;
    152             }
    153         }
    154 
    155         if (!stopTesting && activeTest == null) {
    156             if (test_iter.hasNext()) {
    157                 activeTest = test_iter.next();
    158                 activeTest.start();
    159                 /* This routine will only get called once when a new test
    160                  * should start running. The message handler in UnitTest.java
    161                  * ensures this. */
    162             }
    163             else {
    164                 if (mTimer != null) {
    165                     mTimer.cancel();
    166                     mTimer.purge();
    167                     mTimer = null;
    168                 }
    169             }
    170         }
    171     }
    172 
    173     public void refreshTestResults() {
    174         checkAndRunNextTest();
    175     }
    176 
    177     public void cleanup() {
    178         stopTesting = true;
    179         UnitTest t = activeTest;
    180 
    181         /* Stop periodic refresh of testing */
    182         if (mTimer != null) {
    183             mTimer.cancel();
    184             mTimer.purge();
    185             mTimer = null;
    186         }
    187 
    188         /* Wait to exit until we finish the current test */
    189         if (t != null) {
    190             try {
    191                 t.join();
    192             }
    193             catch (InterruptedException e) {
    194             }
    195             t = null;
    196         }
    197 
    198     }
    199 
    200 }
    201