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