1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program Tester Core 3 * ---------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief dEQP instrumentation 22 *//*--------------------------------------------------------------------*/ 23 24 package com.drawelements.deqp.testercore; 25 26 import android.app.Instrumentation; 27 import android.os.Bundle; 28 import java.io.File; 29 import java.lang.Thread; 30 31 public class DeqpInstrumentation extends Instrumentation { 32 private static final String LOG_TAG = "dEQP/Instrumentation"; 33 private static final long LAUNCH_TIMEOUT_MS = 10000; 34 private static final long NO_DATA_TIMEOUT_MS = 5000; 35 private static final long NO_ACTIVITY_SLEEP_MS = 100; 36 private static final long REMOTE_DEAD_SLEEP_MS = 100; 37 38 private String m_cmdLine; 39 private String m_logFileName; 40 private boolean m_logData; 41 42 @Override onCreate(Bundle arguments)43 public void onCreate(Bundle arguments) { 44 super.onCreate(arguments); 45 46 m_cmdLine = arguments.getString("deqpCmdLine"); 47 m_logFileName = arguments.getString("deqpLogFilename"); 48 49 if (m_cmdLine == null) 50 m_cmdLine = ""; 51 52 if (m_logFileName == null) 53 m_logFileName = "/sdcard/TestLog.qpa"; 54 55 if (arguments.getString("deqpLogData") != null) { 56 if (arguments.getString("deqpLogData") 57 .compareToIgnoreCase("true") == 0) 58 m_logData = true; 59 else 60 m_logData = false; 61 } else 62 m_logData = false; 63 64 start(); 65 } 66 67 @Override onStart()68 public void onStart() { 69 super.onStart(); 70 71 final RemoteAPI remoteApi = 72 new RemoteAPI(getTargetContext(), m_logFileName); 73 final TestLogParser parser = new TestLogParser(); 74 75 try { 76 Log.d(LOG_TAG, "onStart"); 77 78 final String testerName = ""; 79 final File logFile = new File(m_logFileName); 80 81 if (logFile.exists()) 82 logFile.delete(); // Remove log file left by previous session 83 84 remoteApi.start(testerName, m_cmdLine, null); 85 86 { 87 final long startTimeMs = System.currentTimeMillis(); 88 89 while (true) { 90 final long timeSinceStartMs = 91 System.currentTimeMillis() - startTimeMs; 92 93 if (logFile.exists()) 94 break; 95 else if (timeSinceStartMs > LAUNCH_TIMEOUT_MS) { 96 remoteApi.kill(); 97 throw new Exception( 98 "Timeout while waiting for log file"); 99 } else 100 Thread.sleep(NO_ACTIVITY_SLEEP_MS); 101 } 102 } 103 104 parser.init(this, m_logFileName, m_logData); 105 106 // parse until tester dies 107 { 108 while (true) { 109 if (!parser.parse()) { 110 Thread.sleep(NO_ACTIVITY_SLEEP_MS); 111 if (!remoteApi.isRunning()) 112 break; 113 } 114 } 115 } 116 117 // parse remaining messages 118 { 119 long lastDataMs = System.currentTimeMillis(); 120 121 while (true) { 122 if (parser.parse()) 123 lastDataMs = System.currentTimeMillis(); 124 else { 125 final long timeSinceLastDataMs = 126 System.currentTimeMillis() - lastDataMs; 127 128 if (timeSinceLastDataMs > NO_DATA_TIMEOUT_MS) 129 break; // Assume no data is available for reading 130 // any more 131 132 // Remote is dead, wait a bit until trying to read again 133 Thread.sleep(REMOTE_DEAD_SLEEP_MS); 134 } 135 } 136 } 137 138 finish(0, new Bundle()); 139 } catch (Exception e) { 140 Log.e(LOG_TAG, "Exception", e); 141 142 Bundle info = new Bundle(); 143 info.putString("Exception", e.getMessage()); 144 finish(1, info); 145 } finally { 146 try { 147 parser.deinit(); 148 } catch (Exception e) { 149 Log.w(LOG_TAG, "Got exception while closing log", e); 150 } 151 remoteApi.kill(); 152 } 153 } 154 testCaseResult(String code, String details)155 public void testCaseResult(String code, String details) { 156 Bundle info = new Bundle(); 157 158 info.putString("dEQP-EventType", "TestCaseResult"); 159 info.putString("dEQP-TestCaseResult-Code", code); 160 info.putString("dEQP-TestCaseResult-Details", details); 161 162 sendStatus(0, info); 163 } 164 beginTestCase(String testCase)165 public void beginTestCase(String testCase) { 166 Bundle info = new Bundle(); 167 168 info.putString("dEQP-EventType", "BeginTestCase"); 169 info.putString("dEQP-BeginTestCase-TestCasePath", testCase); 170 171 sendStatus(0, info); 172 } 173 endTestCase()174 public void endTestCase() { 175 Bundle info = new Bundle(); 176 177 info.putString("dEQP-EventType", "EndTestCase"); 178 sendStatus(0, info); 179 } 180 testLogData(String log)181 public void testLogData(String log) throws InterruptedException { 182 if (m_logData) { 183 final int chunkSize = 4 * 1024; 184 185 while (log != null) { 186 String message; 187 188 if (log.length() > chunkSize) { 189 message = log.substring(0, chunkSize); 190 log = log.substring(chunkSize); 191 } else { 192 message = log; 193 log = null; 194 } 195 196 Bundle info = new Bundle(); 197 198 info.putString("dEQP-EventType", "TestLogData"); 199 info.putString("dEQP-TestLogData-Log", message); 200 sendStatus(0, info); 201 202 if (log != null) { 203 Thread.sleep(1); // 1ms 204 } 205 } 206 } 207 } 208 beginSession()209 public void beginSession() { 210 Bundle info = new Bundle(); 211 212 info.putString("dEQP-EventType", "BeginSession"); 213 sendStatus(0, info); 214 } 215 endSession()216 public void endSession() { 217 Bundle info = new Bundle(); 218 219 info.putString("dEQP-EventType", "EndSession"); 220 sendStatus(0, info); 221 } 222 sessionInfo(String name, String value)223 public void sessionInfo(String name, String value) { 224 Bundle info = new Bundle(); 225 226 info.putString("dEQP-EventType", "SessionInfo"); 227 info.putString("dEQP-SessionInfo-Name", name); 228 info.putString("dEQP-SessionInfo-Value", value); 229 230 sendStatus(0, info); 231 } 232 terminateTestCase(String reason)233 public void terminateTestCase(String reason) { 234 Bundle info = new Bundle(); 235 236 info.putString("dEQP-EventType", "TerminateTestCase"); 237 info.putString("dEQP-TerminateTestCase-Reason", reason); 238 239 sendStatus(0, info); 240 } 241 242 @Override onDestroy()243 public void onDestroy() { 244 Log.e(LOG_TAG, "onDestroy"); 245 super.onDestroy(); 246 Log.e(LOG_TAG, "onDestroy"); 247 } 248 } 249