Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2008-2011 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_vector(this, mRes, mCtx));
     69         unitTests.add(new UT_rsdebug(this, mRes, mCtx));
     70         unitTests.add(new UT_rstime(this, mRes, mCtx));
     71         unitTests.add(new UT_rstypes(this, mRes, mCtx));
     72         unitTests.add(new UT_alloc(this, mRes, mCtx));
     73         unitTests.add(new UT_refcount(this, mRes, mCtx));
     74         unitTests.add(new UT_foreach(this, mRes, mCtx));
     75         unitTests.add(new UT_math(this, mRes, mCtx));
     76         unitTests.add(new UT_fp_mad(this, mRes, mCtx));
     77         /*
     78         unitTests.add(new UnitTest(null, "<Pass>", 1));
     79         unitTests.add(new UnitTest());
     80         unitTests.add(new UnitTest(null, "<Fail>", -1));
     81 
     82         for (int i = 0; i < 20; i++) {
     83             unitTests.add(new UnitTest(null, "<Pass>", 1));
     84         }
     85         */
     86 
     87         UnitTest [] uta = new UnitTest[unitTests.size()];
     88         uta = unitTests.toArray(uta);
     89 
     90         mListAllocs = new ScriptField_ListAllocs_s(mRS, uta.length);
     91         for (int i = 0; i < uta.length; i++) {
     92             ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item();
     93             listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT);
     94             listElem.result = uta[i].result;
     95             mListAllocs.set(listElem, i, false);
     96             uta[i].setItem(listElem);
     97         }
     98 
     99         mListAllocs.copyAll();
    100 
    101         mScript.bind_gList(mListAllocs);
    102 
    103         mFont = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
    104         mScript.set_gFont(mFont);
    105 
    106         mRS.bindRootScript(mScript);
    107 
    108         test_iter = unitTests.listIterator();
    109         refreshTestResults(); /* Kick off the first test */
    110 
    111         TimerTask pTask = new TimerTask() {
    112             public void run() {
    113                 refreshTestResults();
    114             }
    115         };
    116 
    117         mTimer = new Timer();
    118         mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD);
    119     }
    120 
    121     public void checkAndRunNextTest() {
    122         if (activeTest != null) {
    123             if (!activeTest.isAlive()) {
    124                 /* Properly clean up on our last test */
    125                 try {
    126                     activeTest.join();
    127                 }
    128                 catch (InterruptedException e) {
    129                 }
    130                 activeTest = null;
    131             }
    132         }
    133 
    134         if (!stopTesting && activeTest == null) {
    135             if (test_iter.hasNext()) {
    136                 activeTest = test_iter.next();
    137                 activeTest.start();
    138                 /* This routine will only get called once when a new test
    139                  * should start running. The message handler in UnitTest.java
    140                  * ensures this. */
    141             }
    142             else {
    143                 if (mTimer != null) {
    144                     mTimer.cancel();
    145                     mTimer.purge();
    146                     mTimer = null;
    147                 }
    148             }
    149         }
    150     }
    151 
    152     public void refreshTestResults() {
    153         checkAndRunNextTest();
    154 
    155         if (mListAllocs != null && mScript != null && mRS != null) {
    156             mListAllocs.copyAll();
    157 
    158             mScript.bind_gList(mListAllocs);
    159             mRS.bindRootScript(mScript);
    160         }
    161     }
    162 
    163     public void cleanup() {
    164         stopTesting = true;
    165         UnitTest t = activeTest;
    166 
    167         /* Stop periodic refresh of testing */
    168         if (mTimer != null) {
    169             mTimer.cancel();
    170             mTimer.purge();
    171             mTimer = null;
    172         }
    173 
    174         /* Wait to exit until we finish the current test */
    175         if (t != null) {
    176             try {
    177                 t.join();
    178             }
    179             catch (InterruptedException e) {
    180             }
    181             t = null;
    182         }
    183 
    184     }
    185 
    186     public void newTouchPosition(float x, float y, float pressure, int id) {
    187     }
    188 
    189     public void onActionDown(int x, int y) {
    190         mScript.set_gDY(0.0f);
    191         mLastX = x;
    192         mLastY = y;
    193         refreshTestResults();
    194     }
    195 
    196     public void onActionMove(int x, int y) {
    197         int dx = mLastX - x;
    198         int dy = mLastY - y;
    199 
    200         if (Math.abs(dy) <= 2) {
    201             dy = 0;
    202         }
    203 
    204         mScript.set_gDY(dy);
    205 
    206         mLastX = x;
    207         mLastY = y;
    208         refreshTestResults();
    209     }
    210 }
    211