13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Tester Core
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief TesterCore remote interface.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypackage com.drawelements.deqp.testercore;
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.app.ActivityManager;
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.content.Context;
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.content.ComponentName;
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.content.Intent;
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.content.pm.PackageManager;
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.os.Process;
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.os.Build;
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport android.text.TextUtils;
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport java.lang.System;
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryimport java.util.List;
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic class RemoteAPI {
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private static final String			LOG_TAG				= "dEQP/RemoteAPI";
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private Context						m_context;
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private String						m_processName;
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private String						m_logFileName;
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	public RemoteAPI (Context context, String logFileName) {
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_context			= context;
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_processName		= m_context.getPackageName() + ":testercore";
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_logFileName		= logFileName;
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private ComponentName getDefaultTesterComponent () {
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return new ComponentName(m_context.getPackageName(), "android.app.NativeActivity");
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private ComponentName getTesterComponent (String testerName) {
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (testerName != null && !testerName.equals("")) {
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ComponentName component = ComponentName.unflattenFromString(testerName);
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (component == null) {
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				Log.e(LOG_TAG, "Invalid component name supplied (" + testerName + "), using default");
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				component = getDefaultTesterComponent();
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return component;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		} else {
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return getDefaultTesterComponent();
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	public boolean start (String testerName, String cmdLine, String caseList) {
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Choose component
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ComponentName component = getTesterComponent(testerName);
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Intent testIntent = new Intent();
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		testIntent.setComponent(component);
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		testIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Add all data to cmdLine
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		cmdLine = testerName + " " + cmdLine + " --deqp-log-filename=" + m_logFileName;
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (caseList != null)
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			cmdLine = cmdLine + " --deqp-caselist=" + caseList;
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		cmdLine = cmdLine.replaceAll("  ", " ");
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		testIntent.putExtra("cmdLine", cmdLine);
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Try to resolve intent.
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		boolean	isActivity	= m_context.getPackageManager().resolveActivity(testIntent, 0) != null;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		boolean isService	= m_context.getPackageManager().resolveService(testIntent, 0) != null;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (!isActivity && !isService) {
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			Log.e(LOG_TAG, "Can't resolve component as activity or service (" + component.flattenToString() + "), using default");
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			component = getDefaultTesterComponent();
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		Log.d(LOG_TAG, "Starting activity " + component.flattenToString());
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		try {
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (isService)
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				m_context.startService(testIntent);
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			else
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				m_context.startActivity(testIntent);
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		} catch (Exception e) {
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			Log.e(LOG_TAG, "Failed to start tester", e);
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return false;
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return true;
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	public boolean kill () {
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ActivityManager.RunningAppProcessInfo processInfo = findProcess(m_processName);
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (processInfo != null) {
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			Log.d(LOG_TAG, "Killing " + m_processName);
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			Process.killProcess(processInfo.pid);
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return true;
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		} else {
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return false;
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// \todo [2010-11-01 pyry] Block until tester process is not running?
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	public boolean isRunning () {
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return isProcessRunning(m_processName);
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	public String getLogFileName () {
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return m_logFileName;
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private ActivityManager.RunningAppProcessInfo findProcess (String name) {
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ActivityManager activityMgr = (ActivityManager)m_context.getSystemService(Context.ACTIVITY_SERVICE);
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		List<ActivityManager.RunningAppProcessInfo> processes = activityMgr.getRunningAppProcesses();
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (ActivityManager.RunningAppProcessInfo info : processes) {
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			// Log.v(LOG_TAG, "Found proc : " + info.processName + " " + info.pid);
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (info.processName.equals(name))
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				return info;
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return null;
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	private boolean isProcessRunning (String processName) {
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Log.d(LOG_TAG, "isProcessRunning(): " + processName);
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return (findProcess(processName) != null);
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
151