Home | History | Annotate | Download | only in test
      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 
     17 package com.android.rs.test;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.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 
     28 
     29 public class RSTestCore {
     30     int mWidth;
     31     int mHeight;
     32     Context mCtx;
     33 
     34     public RSTestCore(Context ctx) {
     35         mCtx = ctx;
     36     }
     37 
     38     private Resources mRes;
     39     private RenderScriptGL mRS;
     40 
     41     private Font mFont;
     42     ScriptField_ListAllocs_s mListAllocs;
     43     int mLastX;
     44     int mLastY;
     45     private ScriptC_rslist mScript;
     46 
     47     private ArrayList<UnitTest> unitTests;
     48     private ListIterator<UnitTest> test_iter;
     49     private UnitTest activeTest;
     50     private boolean stopTesting;
     51 
     52     /* Periodic timer for ensuring future tests get scheduled */
     53     private Timer mTimer;
     54     public static final int RS_TIMER_PERIOD = 100;
     55 
     56     public void init(RenderScriptGL rs, Resources res, int width, int height) {
     57         mRS = rs;
     58         mRes = res;
     59         mWidth = width;
     60         mHeight = height;
     61         stopTesting = false;
     62 
     63         mScript = new ScriptC_rslist(mRS, mRes, R.raw.rslist);
     64 
     65         unitTests = new ArrayList<UnitTest>();
     66 
     67         unitTests.add(new UT_primitives(this, mRes, mCtx));
     68         unitTests.add(new UT_constant(this, mRes, mCtx));
     69         unitTests.add(new UT_vector(this, mRes, mCtx));
     70         unitTests.add(new UT_array_init(this, mRes, mCtx));
     71         unitTests.add(new UT_array_alloc(this, mRes, mCtx));
     72         unitTests.add(new UT_clamp(this, mRes, mCtx));
     73         unitTests.add(new UT_clamp_relaxed(this, mRes, mCtx));
     74         unitTests.add(new UT_convert(this, mRes, mCtx));
     75         unitTests.add(new UT_convert_relaxed(this, mRes, mCtx));
     76         unitTests.add(new UT_rsdebug(this, mRes, mCtx));
     77         unitTests.add(new UT_rstime(this, mRes, mCtx));
     78         unitTests.add(new UT_rstypes(this, mRes, mCtx));
     79         unitTests.add(new UT_alloc(this, mRes, mCtx));
     80         unitTests.add(new UT_refcount(this, mRes, mCtx));
     81         unitTests.add(new UT_foreach(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_element(this, mRes, mCtx));
     88         unitTests.add(new UT_sampler(this, mRes, mCtx));
     89         unitTests.add(new UT_program_store(this, mRes, mCtx));
     90         unitTests.add(new UT_program_raster(this, mRes, mCtx));
     91         unitTests.add(new UT_mesh(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             ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item();
    110             listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT);
    111             listElem.result = uta[i].getResult();
    112             mListAllocs.set(listElem, i, false);
    113             uta[i].setItem(listElem);
    114         }
    115 
    116         mListAllocs.copyAll();
    117 
    118         mScript.bind_gList(mListAllocs);
    119 
    120         mFont = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
    121         mScript.set_gFont(mFont);
    122 
    123         mRS.bindRootScript(mScript);
    124 
    125         test_iter = unitTests.listIterator();
    126         refreshTestResults(); /* Kick off the first test */
    127 
    128         TimerTask pTask = new TimerTask() {
    129             public void run() {
    130                 refreshTestResults();
    131             }
    132         };
    133 
    134         mTimer = new Timer();
    135         mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD);
    136     }
    137 
    138     public void checkAndRunNextTest() {
    139         if (activeTest != null) {
    140             if (!activeTest.isAlive()) {
    141                 /* Properly clean up on our last test */
    142                 try {
    143                     activeTest.join();
    144                 }
    145                 catch (InterruptedException e) {
    146                 }
    147                 activeTest = null;
    148             }
    149         }
    150 
    151         if (!stopTesting && activeTest == null) {
    152             if (test_iter.hasNext()) {
    153                 activeTest = test_iter.next();
    154                 activeTest.start();
    155                 /* This routine will only get called once when a new test
    156                  * should start running. The message handler in UnitTest.java
    157                  * ensures this. */
    158             }
    159             else {
    160                 if (mTimer != null) {
    161                     mTimer.cancel();
    162                     mTimer.purge();
    163                     mTimer = null;
    164                 }
    165             }
    166         }
    167     }
    168 
    169     public void refreshTestResults() {
    170         checkAndRunNextTest();
    171 
    172         if (mListAllocs != null && mScript != null && mRS != null) {
    173             mListAllocs.copyAll();
    174 
    175             mScript.bind_gList(mListAllocs);
    176             mRS.bindRootScript(mScript);
    177         }
    178     }
    179 
    180     public void cleanup() {
    181         stopTesting = true;
    182         UnitTest t = activeTest;
    183 
    184         /* Stop periodic refresh of testing */
    185         if (mTimer != null) {
    186             mTimer.cancel();
    187             mTimer.purge();
    188             mTimer = null;
    189         }
    190 
    191         /* Wait to exit until we finish the current test */
    192         if (t != null) {
    193             try {
    194                 t.join();
    195             }
    196             catch (InterruptedException e) {
    197             }
    198             t = null;
    199         }
    200 
    201     }
    202 
    203     public void newTouchPosition(float x, float y, float pressure, int id) {
    204     }
    205 
    206     public void onActionDown(int x, int y) {
    207         mScript.set_gDY(0.0f);
    208         mLastX = x;
    209         mLastY = y;
    210         refreshTestResults();
    211     }
    212 
    213     public void onActionMove(int x, int y) {
    214         int dx = mLastX - x;
    215         int dy = mLastY - y;
    216 
    217         if (Math.abs(dy) <= 2) {
    218             dy = 0;
    219         }
    220 
    221         mScript.set_gDY(dy);
    222 
    223         mLastX = x;
    224         mLastY = y;
    225         refreshTestResults();
    226     }
    227 }
    228