#filter substitution /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Firefox Mobile Test Framework. * * The Initial Developer of the Original Code is Mozilla. * Portions created by the Initial Developer are Copyright (C) 2011 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Trevor Fairey * David Burns * Joel Maher * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ package @ANDROID_PACKAGE_NAME@; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.nio.IntBuffer; import java.util.HashMap; import java.util.List; import java.lang.Class; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.InvocationHandler; import java.lang.Long; import android.app.Activity; import android.opengl.GLSurfaceView; import android.view.View; import android.util.Log; import org.json.*; import com.jayway.android.robotium.solo.Solo; public class FennecNativeDriver implements Driver { private static final int FRAME_TIME_THRESHOLD = 17; // allow 17ms per frame (~60fps) // Map of IDs to element names. private HashMap mLocators = null; private Activity mActivity; private Solo mSolo; private static String mLogFile = null; private static LogLevel mLogLevel = LogLevel.LOG_LEVEL_INFO; // Objects for reflexive access of fennec classes. private ClassLoader mClassLoader; private Class mGel; private Class mGe; private Class mGas; private Method mRegisterGEL; private Method mUnregisterGEL; private Method mSendGE; private Method _startFrameRecording; private Method _stopFrameRecording; private Method _startCheckerboardRecording; private Method _stopCheckerboardRecording; private Method _getPixels; public enum LogLevel { LOG_LEVEL_DEBUG(1), LOG_LEVEL_INFO(2), LOG_LEVEL_WARN(3), LOG_LEVEL_ERROR(4); private int mValue; LogLevel(int value) { mValue = value; } public boolean isEnabled(LogLevel configuredLevel) { return mValue >= configuredLevel.getValue(); } private int getValue() { return mValue; } } public FennecNativeDriver(Activity activity, Solo robocop) { mActivity = activity; mSolo = robocop; // Set up table of fennec_ids. mLocators = convertTextToTable(getFile("/mnt/sdcard/fennec_ids.txt")); // Set up reflexive access of java classes and methods. try { mClassLoader = activity.getClassLoader(); mGel = mClassLoader.loadClass("org.mozilla.gecko.GeckoEventListener"); mGe = mClassLoader.loadClass("org.mozilla.gecko.GeckoEvent"); mGas = mClassLoader.loadClass("org.mozilla.gecko.GeckoAppShell"); Class [] parameters = new Class[2]; parameters[0] = String.class; parameters[1] = mGel; mRegisterGEL = mGas.getMethod("registerGeckoEventListener", parameters); mUnregisterGEL = mGas.getMethod("unregisterGeckoEventListener", parameters); parameters = new Class[1]; parameters[0] = mGe; mSendGE = mGas.getMethod("sendEventToGecko", parameters); Class gfx = mClassLoader.loadClass("org.mozilla.gecko.gfx.PanningPerfAPI"); _startFrameRecording = gfx.getDeclaredMethod("startFrameTimeRecording"); _stopFrameRecording = gfx.getDeclaredMethod("stopFrameTimeRecording"); _startCheckerboardRecording = gfx.getDeclaredMethod("startCheckerboardRecording"); _stopCheckerboardRecording = gfx.getDeclaredMethod("stopCheckerboardRecording"); Class layerView = mClassLoader.loadClass("org.mozilla.gecko.gfx.LayerView"); _getPixels = layerView.getDeclaredMethod("getPixels"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } } //Information on the location of the Gecko Frame. private boolean mGeckoInfo = false; private int mGeckoTop = 100; private int mGeckoLeft = 0; private int mGeckoHeight= 700; private int mGeckoWidth = 1024; private void getGeckoInfo() { View geckoLayout = mActivity.findViewById(Integer.decode((String)mLocators.get("gecko_layout"))); if (geckoLayout != null) { int[] pos = new int[2]; geckoLayout.getLocationOnScreen(pos); mGeckoTop = pos[1]; mGeckoLeft = pos[0]; mGeckoWidth = geckoLayout.getWidth(); mGeckoHeight = geckoLayout.getHeight(); mGeckoInfo = true; } else { throw new RoboCopException("Unable to find view gecko_layout"); } } public int getGeckoTop() { if (!mGeckoInfo) { getGeckoInfo(); } return mGeckoTop; } public int getGeckoLeft() { if (!mGeckoInfo) { getGeckoInfo(); } return mGeckoLeft; } public int getGeckoHeight() { if (!mGeckoInfo) { getGeckoInfo(); } return mGeckoHeight; } public int getGeckoWidth() { if (!mGeckoInfo) { getGeckoInfo(); } return mGeckoWidth; } public Element findElement(Activity activity, String name) { if (name == null) { throw new IllegalArgumentException("Can not findElements when passed a null"); } if (mLocators.containsKey(name)) { return new FennecNativeElement(Integer.decode((String)mLocators.get(name)), activity, mSolo); } throw new RoboCopException("Element does not exist in the list"); } public void startFrameRecording() { try { Object [] params = null; _startFrameRecording.invoke(null, params); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public int stopFrameRecording() { Class [] parameters = new Class[1]; parameters[0] = null; try { Object [] params = null; List frames = (List)_stopFrameRecording.invoke(null, params); int numDelays = 0; for (int i = 1; i < frames.size(); i++) { if (frames.get(i) - frames.get(i-1) > FRAME_TIME_THRESHOLD) { numDelays++; } } return numDelays; } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return 0; } public void startCheckerboardRecording() { try { Object [] params = null; _startCheckerboardRecording.invoke(null, params); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public float stopCheckerboardRecording() { Class [] parameters = new Class[1]; parameters[0] = null; try { Object [] params = null; List checkerboard = (List)_stopCheckerboardRecording.invoke(null, params); float completeness = 0; for (float val : checkerboard) { completeness += (1.0f - val); } return completeness / (float)checkerboard.size(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return 0.0f; } private View getSurfaceView() { try { Class c = Class.forName("org.mozilla.gecko.gfx.LayerView"); for (View v : mSolo.getCurrentViews()) { if (c.isInstance(v)) { return v; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } public PaintedSurface getPaintedSurface() { View view = getSurfaceView(); if (view == null) { return null; } IntBuffer pixelBuffer; try { pixelBuffer = (IntBuffer)_getPixels.invoke(view); } catch (Exception e) { e.printStackTrace(); return null; } // now we need to (1) flip the image, because GL likes to do things up-side-down, // and (2) rearrange the bits from AGBR-8888 to ARGB-8888. int w = view.getWidth(); int h = view.getHeight(); pixelBuffer.position(0); String mapFile = "/mnt/sdcard/pixels.map"; FileOutputStream fos = null; BufferedOutputStream bos = null; DataOutputStream dos = null; try { fos = new FileOutputStream(mapFile); bos = new BufferedOutputStream(fos); dos = new DataOutputStream(bos); for (int y = h - 1; y >= 0; y--) { for (int x = 0; x < w; x++) { int agbr = pixelBuffer.get(); dos.writeInt((agbr & 0xFF00FF00) | ((agbr >> 16) & 0x000000FF) | ((agbr << 16) & 0x00FF0000)); } } } catch (IOException e) { throw new RoboCopException("exception with pixel writer on file: " + mapFile); } finally { try { if (dos != null) { dos.flush(); dos.close(); } // closing dos automatically closes bos if (fos != null) { fos.flush(); fos.close(); } } catch (IOException e) { e.printStackTrace(); throw new RoboCopException("exception closing pixel writer on file: " + mapFile); } } return new PaintedSurface(mapFile, w, h); } public int mHeight=0; public int mScrollHeight=0; public int mPageHeight=10; class scrollHandler implements InvocationHandler { public scrollHandler(){}; public Object invoke(Object proxy, Method method, Object[] args) { try { // Disect the JSON object into the appropriate variables JSONObject jo = ((JSONObject)args[1]); mScrollHeight = jo.getInt("y"); mHeight = jo.getInt("cheight"); // We don't want a height of 0. That means it's a bad response. if (mHeight > 0) { mPageHeight = jo.getInt("height"); } } catch( Throwable e) { FennecNativeDriver.log(FennecNativeDriver.LogLevel.LOG_LEVEL_WARN, "WARNING: ScrollReceived, but read wrong!"); } return null; } } public int getScrollHeight() { return mScrollHeight; } public int getPageHeight() { return mPageHeight; } public int getHeight() { return mHeight; } public void setupScrollHandling() { //Setup scrollHandler to catch "robocop:scroll" events. try { Class [] interfaces = new Class[1]; interfaces[0] = mGel; Object[] finalParams = new Object[2]; finalParams[0] = "robocop:scroll"; finalParams[1] = Proxy.newProxyInstance(mClassLoader, interfaces, new scrollHandler()); mRegisterGEL.invoke(null, finalParams); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } /** * Takes a filename, loads the file, and returns a string version of the entire file. */ public static String getFile(String filename) { StringBuilder text = new StringBuilder(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(filename)); String line; while ((line = br.readLine()) != null) { text.append(line); text.append('\n'); } } catch(IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { } } return text.toString(); } /** * Takes a string of "key=value" pairs split by \n and creates a hash table. */ public static HashMap convertTextToTable(String data) { HashMap retVal = new HashMap(); String[] lines = data.split("\n"); for (int i = 0; i < lines.length; i++) { String[] parts = lines[i].split("="); retVal.put(parts[0].trim(), parts[1].trim()); } return retVal; } /** * Set the filename used for logging. If the file already exists, delete it * as a safe-guard against accidentally appending to an old log file. */ public static void setLogFile(String filename) { mLogFile = filename; File file = new File(mLogFile); if (file.exists()) { file.delete(); } } public static void setLogLevel(LogLevel level) { mLogLevel = level; } public static void log(LogLevel level, String message) { log(level, message, null); } public static void log(LogLevel level, Throwable t) { log(level, null, t); } public static void log(LogLevel level, String message, Throwable t) { if (mLogFile == null) { assert(false); } if (level.isEnabled(mLogLevel)) { PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(mLogFile, true)); if (message != null) { pw.println(message); } if (t != null) { t.printStackTrace(pw); } } catch (IOException ioe) { Log.e("Robocop", "exception with file writer on: " + mLogFile); } finally { pw.close(); } // PrintWriter doesn't throw IOE but sets an error flag instead, // so check for that if (pw.checkError()) { Log.e("Robocop", "exception with file writer on: " + mLogFile); } } if (level == LogLevel.LOG_LEVEL_INFO) { Log.i("Robocop", message, t); } else if (level == LogLevel.LOG_LEVEL_DEBUG) { Log.d("Robocop", message, t); } else if (level == LogLevel.LOG_LEVEL_WARN) { Log.w("Robocop", message, t); } else if (level == LogLevel.LOG_LEVEL_ERROR) { Log.e("Robocop", message, t); } } }