DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (31ec81b5d7bb)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#filter substitution
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package @ANDROID_PACKAGE_NAME@;

import java.util.LinkedList;
import android.os.SystemClock;

public class FennecMochitestAssert implements Assert {
    private LinkedList<testInfo> mTestList = new LinkedList<testInfo>();

    // Internal state variables to make logging match up with existing mochitests
    private int mLineNumber = 0;
    private int mPassed = 0;
    private int mFailed = 0;
    private int mTodo = 0;
    
    // Used to write the first line of the test file
    private boolean mLogStarted = false;

    // Used to write the test-start/test-end log lines
    private String mLogTestName = "";

    // Measure the time it takes to run test case
    private long mStartTime = 0;

    public FennecMochitestAssert() {
    }

    /** Write information to a logfile and logcat */
    public void dumpLog(String message) {
        FennecNativeDriver.log(FennecNativeDriver.LogLevel.INFO, message);
    }

    /** Write information to a logfile and logcat */
    public void dumpLog(String message, Throwable t) {
        FennecNativeDriver.log(FennecNativeDriver.LogLevel.INFO, message, t);
    }

    /** Set the filename used for dumpLog. */
    public void setLogFile(String filename) {
        FennecNativeDriver.setLogFile(filename);

        String message;
        if (!mLogStarted) {
            dumpLog(Integer.toString(mLineNumber++) + " INFO SimpleTest START");
            mLogStarted = true;
        }

        if (mLogTestName != "") {
            long diff = SystemClock.uptimeMillis() - mStartTime;
            message = Integer.toString(mLineNumber++) + " INFO TEST-END | " + mLogTestName;
            message += " | finished in " + diff + "ms";
            dumpLog(message);
            mLogTestName = "";
        }
    }

    public void setTestName(String testName) {
        String[] nameParts = testName.split("\\.");
        mLogTestName = nameParts[nameParts.length - 1];
        mStartTime = SystemClock.uptimeMillis();

        dumpLog(Integer.toString(mLineNumber++) + " INFO TEST-START | " + mLogTestName);
    }

    class testInfo {
        public boolean mResult;
        public String mName;
        public String mDiag;
        public boolean mTodo;
        public boolean mInfo;
        public testInfo(boolean r, String n, String d, boolean t, boolean i) {
            mResult = r;
            mName = n;
            mDiag = d;
            mTodo = t;
            mInfo = i;
        }

    }

    private void _logMochitestResult(testInfo test, String passString, String failString) {
        boolean isError = true;
        String resultString = failString;
        if (test.mResult || test.mTodo) {
            isError = false;
        }
        if (test.mResult)
        {
            resultString = passString;
        }
        String diag = test.mName;
        if (test.mDiag != null) diag += " - " + test.mDiag;

        String message = Integer.toString(mLineNumber++) + " INFO " + resultString + " | " + mLogTestName + " | " + diag;
        dumpLog(message);

        if (test.mInfo) {
            // do not count TEST-INFO messages
        } else if (test.mTodo) {
            mTodo++;
        } else if (isError) {
            mFailed++;
        } else {
            mPassed++;
        }
        if (isError) {
            junit.framework.Assert.fail(message);
        }
    }

    public void endTest() {
        String message;

        if (mLogTestName != "") {
            long diff = SystemClock.uptimeMillis() - mStartTime;
            message = Integer.toString(mLineNumber++) + " INFO TEST-END | " + mLogTestName;
            message += " | finished in " + diff + "ms";
            dumpLog(message);
            mLogTestName = "";
        }

        message = Integer.toString(mLineNumber++) + " INFO TEST-START | Shutdown";
        dumpLog(message);
        message = Integer.toString(mLineNumber++) + " INFO Passed: " + Integer.toString(mPassed);
        dumpLog(message);
        message = Integer.toString(mLineNumber++) + " INFO Failed: " + Integer.toString(mFailed);
        dumpLog(message);
        message = Integer.toString(mLineNumber++) + " INFO Todo: " + Integer.toString(mTodo);
        dumpLog(message);
        message = Integer.toString(mLineNumber++) + " INFO SimpleTest FINISHED";
        dumpLog(message);
    }

    public void ok(boolean condition, String name, String diag) {
        testInfo test = new testInfo(condition, name, diag, false, false);
        _logMochitestResult(test, "TEST-PASS", "TEST-UNEXPECTED-FAIL");
        mTestList.add(test);
    }

    public void is(Object a, Object b, String name) {
        boolean pass = checkObjectsEqual(a,b);
        ok(pass, name, getEqualString(a,b, pass));
    }
    
    public void isnot(Object a, Object b, String name) {
        boolean pass = checkObjectsNotEqual(a,b);
        ok(pass, name, getNotEqualString(a,b,pass));
    }

    public void ispixel(int actual, int r, int g, int b, String name) {
        int aAlpha = ((actual >> 24) & 0xFF);
        int aR = ((actual >> 16) & 0xFF);
        int aG = ((actual >> 8) & 0xFF);
        int aB = (actual & 0xFF);
        boolean pass = checkPixel(actual, r, g, b);
        ok(pass, name, "Color rgba(" + aR + "," + aG + "," + aB + "," + aAlpha + ")" + (pass ? " " : " not") + " close enough to expected rgb(" + r + "," + g + "," + b + ")");
    }

    public void isnotpixel(int actual, int r, int g, int b, String name) {
        int aAlpha = ((actual >> 24) & 0xFF);
        int aR = ((actual >> 16) & 0xFF);
        int aG = ((actual >> 8) & 0xFF);
        int aB = (actual & 0xFF);
        boolean pass = checkPixel(actual, r, g, b);
        ok(!pass, name, "Color rgba(" + aR + "," + aG + "," + aB + "," + aAlpha + ")" + (!pass ? " is" : " is not") + " different enough from rgb(" + r + "," + g + "," + b + ")");
    }

    private boolean checkPixel(int actual, int r, int g, int b) {
        // When we read GL pixels the GPU has already processed them and they
        // are usually off by a little bit. For example a CSS-color pixel of color #64FFF5
        // was turned into #63FFF7 when it came out of glReadPixels. So in order to compare
        // against the expected value, we use a little fuzz factor. For the alpha we just
        // make sure it is always 0xFF. There is also bug 691354 which crops up every so
        // often just to make our lives difficult. However the individual color components
        // should never be off by more than 8.
        int aAlpha = ((actual >> 24) & 0xFF);
        int aR = ((actual >> 16) & 0xFF);
        int aG = ((actual >> 8) & 0xFF);
        int aB = (actual & 0xFF);
        boolean pass = (aAlpha == 0xFF) /* alpha */
                           && (Math.abs(aR - r) <= 8) /* red */
                           && (Math.abs(aG - g) <= 8) /* green */
                           && (Math.abs(aB - b) <= 8); /* blue */
        if (pass) {
            return true;
        } else {
            return false;
        }
    }

    public void todo(boolean condition, String name, String diag) {
        testInfo test = new testInfo(condition, name, diag, true, false);
        _logMochitestResult(test, "TEST-UNEXPECTED-PASS", "TEST-KNOWN-FAIL");
        mTestList.add(test);
    }

    public void todo_is(Object a, Object b, String name) {
        boolean pass = checkObjectsEqual(a,b);
        todo(pass, name, getEqualString(a,b,pass));
    }

    public void todo_isnot(Object a, Object b, String name) {
        boolean pass = checkObjectsNotEqual(a,b);
        todo(pass, name, getNotEqualString(a,b,pass));
    }

    private boolean checkObjectsEqual(Object a, Object b) {
        if (a == null || b == null) {
            if (a == null && b == null) {
                return true;
            }
            return false;
        } else {
            return a.equals(b);
        }
    }

    private String getEqualString(Object a, Object b, boolean pass) {
        if (pass) {
            return a + " should equal " + b;
        }
        return "got " + a + ", expected " + b;
    }

    private boolean checkObjectsNotEqual(Object a, Object b) {
        if (a == null || b == null) {
            if ((a == null && b != null) || (a != null && b == null)) {
                return true;
            } else {
                return false;
            }
        } else {
            return !a.equals(b);
        }
    }

    private String getNotEqualString(Object a, Object b, boolean pass) {
        if(pass) {
            return a + " should not equal " + b;
        }
        return "didn't expect " + a + ", but got it";
    }

    public void info(String name, String message) {
        testInfo test = new testInfo(true, name, message, false, true);
        _logMochitestResult(test, "TEST-INFO", "INFO FAILED?");
    }
}