michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.io.BufferedOutputStream; michael@0: import java.io.BufferedReader; michael@0: import java.io.DataOutputStream; michael@0: import java.io.File; michael@0: import java.io.FileOutputStream; michael@0: import java.io.FileReader; michael@0: import java.io.FileWriter; michael@0: import java.io.IOException; michael@0: import java.io.PrintWriter; michael@0: import java.nio.IntBuffer; michael@0: import java.util.HashMap; michael@0: import java.util.List; michael@0: import java.util.Map; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.gfx.LayerView; michael@0: import org.mozilla.gecko.gfx.PanningPerfAPI; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: michael@0: import android.app.Activity; michael@0: import android.util.Log; michael@0: import android.view.View; michael@0: michael@0: import com.jayway.android.robotium.solo.Solo; michael@0: michael@0: public class FennecNativeDriver implements Driver { michael@0: private static final int FRAME_TIME_THRESHOLD = 25; // allow 25ms per frame (40fps) michael@0: michael@0: private Activity mActivity; michael@0: private Solo mSolo; michael@0: private String mRootPath; michael@0: michael@0: private static String mLogFile = null; michael@0: private static LogLevel mLogLevel = LogLevel.INFO; michael@0: michael@0: public enum LogLevel { michael@0: DEBUG(1), michael@0: INFO(2), michael@0: WARN(3), michael@0: ERROR(4); michael@0: michael@0: private int mValue; michael@0: LogLevel(int value) { michael@0: mValue = value; michael@0: } michael@0: public boolean isEnabled(LogLevel configuredLevel) { michael@0: return mValue >= configuredLevel.getValue(); michael@0: } michael@0: private int getValue() { michael@0: return mValue; michael@0: } michael@0: } michael@0: michael@0: public FennecNativeDriver(Activity activity, Solo robocop, String rootPath) { michael@0: mActivity = activity; michael@0: mSolo = robocop; michael@0: mRootPath = rootPath; michael@0: } michael@0: michael@0: //Information on the location of the Gecko Frame. michael@0: private boolean mGeckoInfo = false; michael@0: private int mGeckoTop = 100; michael@0: private int mGeckoLeft = 0; michael@0: private int mGeckoHeight= 700; michael@0: private int mGeckoWidth = 1024; michael@0: michael@0: private void getGeckoInfo() { michael@0: View geckoLayout = mActivity.findViewById(R.id.gecko_layout); michael@0: if (geckoLayout != null) { michael@0: int[] pos = new int[2]; michael@0: geckoLayout.getLocationOnScreen(pos); michael@0: mGeckoTop = pos[1]; michael@0: mGeckoLeft = pos[0]; michael@0: mGeckoWidth = geckoLayout.getWidth(); michael@0: mGeckoHeight = geckoLayout.getHeight(); michael@0: mGeckoInfo = true; michael@0: } else { michael@0: throw new RoboCopException("Unable to find view gecko_layout"); michael@0: } michael@0: } michael@0: michael@0: public int getGeckoTop() { michael@0: if (!mGeckoInfo) { michael@0: getGeckoInfo(); michael@0: } michael@0: return mGeckoTop; michael@0: } michael@0: michael@0: public int getGeckoLeft() { michael@0: if (!mGeckoInfo) { michael@0: getGeckoInfo(); michael@0: } michael@0: return mGeckoLeft; michael@0: } michael@0: michael@0: public int getGeckoHeight() { michael@0: if (!mGeckoInfo) { michael@0: getGeckoInfo(); michael@0: } michael@0: return mGeckoHeight; michael@0: } michael@0: michael@0: public int getGeckoWidth() { michael@0: if (!mGeckoInfo) { michael@0: getGeckoInfo(); michael@0: } michael@0: return mGeckoWidth; michael@0: } michael@0: michael@0: /** Find the element with given id. michael@0: * michael@0: * @return An Element representing the view, or null if the view is not found. michael@0: */ michael@0: public Element findElement(Activity activity, int id) { michael@0: return new FennecNativeElement(id, activity); michael@0: } michael@0: michael@0: public void startFrameRecording() { michael@0: PanningPerfAPI.startFrameTimeRecording(); michael@0: } michael@0: michael@0: public int stopFrameRecording() { michael@0: final List frames = PanningPerfAPI.stopFrameTimeRecording(); michael@0: int badness = 0; michael@0: for (int i = 1; i < frames.size(); i++) { michael@0: long frameTime = frames.get(i) - frames.get(i - 1); michael@0: int delay = (int)(frameTime - FRAME_TIME_THRESHOLD); michael@0: // for each frame we miss, add the square of the delay. This michael@0: // makes large delays much worse than small delays. michael@0: if (delay > 0) { michael@0: badness += delay * delay; michael@0: } michael@0: } michael@0: michael@0: // Don't do any averaging of the numbers because really we want to michael@0: // know how bad the jank was at its worst michael@0: return badness; michael@0: } michael@0: michael@0: public void startCheckerboardRecording() { michael@0: PanningPerfAPI.startCheckerboardRecording(); michael@0: } michael@0: michael@0: public float stopCheckerboardRecording() { michael@0: final List checkerboard = PanningPerfAPI.stopCheckerboardRecording(); michael@0: float total = 0; michael@0: for (float val : checkerboard) { michael@0: total += val; michael@0: } michael@0: return total * 100.0f; michael@0: } michael@0: michael@0: private LayerView getSurfaceView() { michael@0: final LayerView layerView = mSolo.getView(LayerView.class, 0); michael@0: michael@0: if (layerView == null) { michael@0: log(LogLevel.WARN, "getSurfaceView could not find LayerView"); michael@0: for (final View v : mSolo.getViews()) { michael@0: log(LogLevel.WARN, " View: " + v); michael@0: } michael@0: } michael@0: return layerView; michael@0: } michael@0: michael@0: public PaintedSurface getPaintedSurface() { michael@0: final LayerView view = getSurfaceView(); michael@0: if (view == null) { michael@0: return null; michael@0: } michael@0: michael@0: final IntBuffer pixelBuffer = view.getPixels(); michael@0: michael@0: // now we need to (1) flip the image, because GL likes to do things up-side-down, michael@0: // and (2) rearrange the bits from AGBR-8888 to ARGB-8888. michael@0: int w = view.getWidth(); michael@0: int h = view.getHeight(); michael@0: pixelBuffer.position(0); michael@0: String mapFile = mRootPath + "/pixels.map"; michael@0: michael@0: FileOutputStream fos = null; michael@0: BufferedOutputStream bos = null; michael@0: DataOutputStream dos = null; michael@0: try { michael@0: fos = new FileOutputStream(mapFile); michael@0: bos = new BufferedOutputStream(fos); michael@0: dos = new DataOutputStream(bos); michael@0: michael@0: for (int y = h - 1; y >= 0; y--) { michael@0: for (int x = 0; x < w; x++) { michael@0: int agbr = pixelBuffer.get(); michael@0: dos.writeInt((agbr & 0xFF00FF00) | ((agbr >> 16) & 0x000000FF) | ((agbr << 16) & 0x00FF0000)); michael@0: } michael@0: } michael@0: } catch (IOException e) { michael@0: throw new RoboCopException("exception with pixel writer on file: " + mapFile); michael@0: } finally { michael@0: try { michael@0: if (dos != null) { michael@0: dos.flush(); michael@0: dos.close(); michael@0: } michael@0: // closing dos automatically closes bos michael@0: if (fos != null) { michael@0: fos.flush(); michael@0: fos.close(); michael@0: } michael@0: } catch (IOException e) { michael@0: log(LogLevel.ERROR, e); michael@0: throw new RoboCopException("exception closing pixel writer on file: " + mapFile); michael@0: } michael@0: } michael@0: return new PaintedSurface(mapFile, w, h); michael@0: } michael@0: michael@0: public int mHeight=0; michael@0: public int mScrollHeight=0; michael@0: public int mPageHeight=10; michael@0: michael@0: public int getScrollHeight() { michael@0: return mScrollHeight; michael@0: } michael@0: public int getPageHeight() { michael@0: return mPageHeight; michael@0: } michael@0: public int getHeight() { michael@0: return mHeight; michael@0: } michael@0: michael@0: public void setupScrollHandling() { michael@0: GeckoAppShell.registerEventListener("robocop:scroll", new GeckoEventListener() { michael@0: @Override michael@0: public void handleMessage(final String event, final JSONObject message) { michael@0: try { michael@0: mScrollHeight = message.getInt("y"); michael@0: mHeight = message.getInt("cheight"); michael@0: // We don't want a height of 0. That means it's a bad response. michael@0: if (mHeight > 0) { michael@0: mPageHeight = message.getInt("height"); michael@0: } michael@0: } catch (JSONException e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN, michael@0: "WARNING: ScrollReceived, but message does not contain " + michael@0: "expected fields: " + e); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Takes a filename, loads the file, and returns a string version of the entire file. michael@0: */ michael@0: public static String getFile(String filename) michael@0: { michael@0: StringBuilder text = new StringBuilder(); michael@0: michael@0: BufferedReader br = null; michael@0: try { michael@0: br = new BufferedReader(new FileReader(filename)); michael@0: String line; michael@0: michael@0: while ((line = br.readLine()) != null) { michael@0: text.append(line); michael@0: text.append('\n'); michael@0: } michael@0: } catch (IOException e) { michael@0: log(LogLevel.ERROR, e); michael@0: } finally { michael@0: try { michael@0: br.close(); michael@0: } catch (IOException e) { michael@0: } michael@0: } michael@0: return text.toString(); michael@0: } michael@0: michael@0: /** michael@0: * Takes a string of "key=value" pairs split by \n and creates a hash table. michael@0: */ michael@0: public static Map convertTextToTable(String data) michael@0: { michael@0: HashMap retVal = new HashMap(); michael@0: michael@0: String[] lines = data.split("\n"); michael@0: for (int i = 0; i < lines.length; i++) { michael@0: String[] parts = lines[i].split("=", 2); michael@0: retVal.put(parts[0].trim(), parts[1].trim()); michael@0: } michael@0: return retVal; michael@0: } michael@0: michael@0: public static void logAllStackTraces(LogLevel level) { michael@0: StringBuffer sb = new StringBuffer(); michael@0: sb.append("Dumping ALL the threads!\n"); michael@0: Map allStacks = Thread.getAllStackTraces(); michael@0: for (Thread t : allStacks.keySet()) { michael@0: sb.append(t.toString()).append('\n'); michael@0: for (StackTraceElement ste : allStacks.get(t)) { michael@0: sb.append(ste.toString()).append('\n'); michael@0: } michael@0: sb.append('\n'); michael@0: } michael@0: log(level, sb.toString()); michael@0: } michael@0: michael@0: /** michael@0: * Set the filename used for logging. If the file already exists, delete it michael@0: * as a safe-guard against accidentally appending to an old log file. michael@0: */ michael@0: public static void setLogFile(String filename) { michael@0: mLogFile = filename; michael@0: File file = new File(mLogFile); michael@0: if (file.exists()) { michael@0: file.delete(); michael@0: } michael@0: } michael@0: michael@0: public static void setLogLevel(LogLevel level) { michael@0: mLogLevel = level; michael@0: } michael@0: michael@0: public static void log(LogLevel level, String message) { michael@0: log(level, message, null); michael@0: } michael@0: michael@0: public static void log(LogLevel level, Throwable t) { michael@0: log(level, null, t); michael@0: } michael@0: michael@0: public static void log(LogLevel level, String message, Throwable t) { michael@0: if (mLogFile == null) { michael@0: assert(false); michael@0: } michael@0: michael@0: if (level.isEnabled(mLogLevel)) { michael@0: PrintWriter pw = null; michael@0: try { michael@0: pw = new PrintWriter(new FileWriter(mLogFile, true)); michael@0: if (message != null) { michael@0: pw.println(message); michael@0: } michael@0: if (t != null) { michael@0: t.printStackTrace(pw); michael@0: } michael@0: } catch (IOException ioe) { michael@0: Log.e("Robocop", "exception with file writer on: " + mLogFile); michael@0: } finally { michael@0: pw.close(); michael@0: } michael@0: // PrintWriter doesn't throw IOE but sets an error flag instead, michael@0: // so check for that michael@0: if (pw.checkError()) { michael@0: Log.e("Robocop", "exception with file writer on: " + mLogFile); michael@0: } michael@0: } michael@0: michael@0: if (level == LogLevel.INFO) { michael@0: Log.i("Robocop", message, t); michael@0: } else if (level == LogLevel.DEBUG) { michael@0: Log.d("Robocop", message, t); michael@0: } else if (level == LogLevel.WARN) { michael@0: Log.w("Robocop", message, t); michael@0: } else if (level == LogLevel.ERROR) { michael@0: Log.e("Robocop", message, t); michael@0: } michael@0: } michael@0: }