1/* 2 * Copyright (C) 2016 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.rsov.test; 18 19import android.app.ListActivity; 20import android.renderscript.Allocation; 21import android.renderscript.RenderScript; 22import android.widget.ArrayAdapter; 23 24import java.util.ArrayList; 25import java.util.ListIterator; 26import java.util.Timer; 27import java.util.TimerTask; 28 29public class RSoVTestCore { 30 ListActivity mCtx; 31 32 public RSoVTestCore(ListActivity ctx) { 33 mCtx = ctx; 34 } 35 36 private RenderScript mRS; 37 38 private ArrayList<UnitTest> unitTests; 39 private ListIterator<UnitTest> test_iter; 40 private UnitTest activeTest; 41 private boolean stopTesting; 42 43 private ScriptField_ListAllocs_s mListAllocs; 44 45 private ArrayAdapter<UnitTest> testAdapter; 46 47 /* Periodic timer for ensuring future tests get scheduled */ 48 private Timer mTimer; 49 public static final int RS_TIMER_PERIOD = 100; 50 51 public void init(RenderScript rs) { 52 mRS = rs; 53 stopTesting = false; 54 55 unitTests = new ArrayList<UnitTest>(); 56 57 unitTests.add(new UT_invert(this, mCtx)); 58 unitTests.add(new UT_modulo(this, mCtx)); 59 unitTests.add(new UT_multi_kernel(this, mCtx)); 60 unitTests.add(new UT_multi_input(this, mCtx)); 61 unitTests.add(new UT_global_query(this, mCtx)); 62 63 UnitTest[] uta = new UnitTest[unitTests.size()]; 64 uta = unitTests.toArray(uta); 65 66 /* 67 mListAllocs = new ScriptField_ListAllocs_s(mRS, uta.length); 68 for (int i = 0; i < uta.length; i++) { 69 ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item(); 70 listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT); 71 listElem.result = uta[i].getResult(); 72 mListAllocs.set(listElem, i, false); 73 uta[i].setItem(listElem); 74 } 75 76 mListAllocs.copyAll(); 77 */ 78 testAdapter = new ArrayAdapter<UnitTest>(mCtx, android.R.layout.simple_list_item_1, unitTests); 79 mCtx.setListAdapter(testAdapter); 80 81 test_iter = unitTests.listIterator(); 82 refreshTestResults(); /* Kick off the first test */ 83 84 TimerTask pTask = new TimerTask() { 85 public void run() { 86 refreshTestResults(); 87 } 88 }; 89 90 mTimer = new Timer(); 91 mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD); 92 } 93 94 public void checkAndRunNextTest() { 95 mCtx.runOnUiThread(new Runnable() { 96 public void run() { 97 if (testAdapter != null) 98 testAdapter.notifyDataSetChanged(); 99 } 100 }); 101 102 if (activeTest != null) { 103 if (!activeTest.isAlive()) { 104 /* Properly clean up on our last test */ 105 try { 106 activeTest.join(); 107 } catch (InterruptedException e) { 108 } 109 activeTest = null; 110 } 111 } 112 113 if (!stopTesting && activeTest == null) { 114 if (test_iter.hasNext()) { 115 activeTest = test_iter.next(); 116 activeTest.start(); 117 /* This routine will only get called once when a new test 118 * should start running. The message handler in UnitTest.java 119 * ensures this. */ 120 } else { 121 if (mTimer != null) { 122 mTimer.cancel(); 123 mTimer.purge(); 124 mTimer = null; 125 } 126 } 127 } 128 } 129 130 public void refreshTestResults() { 131 checkAndRunNextTest(); 132 } 133 134 public void cleanup() { 135 stopTesting = true; 136 UnitTest t = activeTest; 137 138 /* Stop periodic refresh of testing */ 139 if (mTimer != null) { 140 mTimer.cancel(); 141 mTimer.purge(); 142 mTimer = null; 143 } 144 145 /* Wait to exit until we finish the current test */ 146 if (t != null) { 147 try { 148 t.join(); 149 } catch (InterruptedException e) { 150 } 151 t = null; 152 } 153 154 } 155 156} 157