Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.LinkedList; |
michael@0 | 8 | |
michael@0 | 9 | import android.os.SystemClock; |
michael@0 | 10 | |
michael@0 | 11 | public class FennecMochitestAssert implements Assert { |
michael@0 | 12 | private LinkedList<testInfo> mTestList = new LinkedList<testInfo>(); |
michael@0 | 13 | |
michael@0 | 14 | // Internal state variables to make logging match up with existing mochitests |
michael@0 | 15 | private int mLineNumber = 0; |
michael@0 | 16 | private int mPassed = 0; |
michael@0 | 17 | private int mFailed = 0; |
michael@0 | 18 | private int mTodo = 0; |
michael@0 | 19 | |
michael@0 | 20 | // Used to write the first line of the test file |
michael@0 | 21 | private boolean mLogStarted = false; |
michael@0 | 22 | |
michael@0 | 23 | // Used to write the test-start/test-end log lines |
michael@0 | 24 | private String mLogTestName = ""; |
michael@0 | 25 | |
michael@0 | 26 | // Measure the time it takes to run test case |
michael@0 | 27 | private long mStartTime = 0; |
michael@0 | 28 | |
michael@0 | 29 | public FennecMochitestAssert() { |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | /** Write information to a logfile and logcat */ |
michael@0 | 33 | public void dumpLog(String message) { |
michael@0 | 34 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.INFO, message); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /** Write information to a logfile and logcat */ |
michael@0 | 38 | public void dumpLog(String message, Throwable t) { |
michael@0 | 39 | FennecNativeDriver.log(FennecNativeDriver.LogLevel.INFO, message, t); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | /** Set the filename used for dumpLog. */ |
michael@0 | 43 | public void setLogFile(String filename) { |
michael@0 | 44 | FennecNativeDriver.setLogFile(filename); |
michael@0 | 45 | |
michael@0 | 46 | String message; |
michael@0 | 47 | if (!mLogStarted) { |
michael@0 | 48 | dumpLog(Integer.toString(mLineNumber++) + " INFO SimpleTest START"); |
michael@0 | 49 | mLogStarted = true; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | if (mLogTestName != "") { |
michael@0 | 53 | long diff = SystemClock.uptimeMillis() - mStartTime; |
michael@0 | 54 | message = Integer.toString(mLineNumber++) + " INFO TEST-END | " + mLogTestName; |
michael@0 | 55 | message += " | finished in " + diff + "ms"; |
michael@0 | 56 | dumpLog(message); |
michael@0 | 57 | mLogTestName = ""; |
michael@0 | 58 | } |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | public void setTestName(String testName) { |
michael@0 | 62 | String[] nameParts = testName.split("\\."); |
michael@0 | 63 | mLogTestName = nameParts[nameParts.length - 1]; |
michael@0 | 64 | mStartTime = SystemClock.uptimeMillis(); |
michael@0 | 65 | |
michael@0 | 66 | dumpLog(Integer.toString(mLineNumber++) + " INFO TEST-START | " + mLogTestName); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | class testInfo { |
michael@0 | 70 | public boolean mResult; |
michael@0 | 71 | public String mName; |
michael@0 | 72 | public String mDiag; |
michael@0 | 73 | public boolean mTodo; |
michael@0 | 74 | public boolean mInfo; |
michael@0 | 75 | public testInfo(boolean r, String n, String d, boolean t, boolean i) { |
michael@0 | 76 | mResult = r; |
michael@0 | 77 | mName = n; |
michael@0 | 78 | mDiag = d; |
michael@0 | 79 | mTodo = t; |
michael@0 | 80 | mInfo = i; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | private void _logMochitestResult(testInfo test, String passString, String failString) { |
michael@0 | 86 | boolean isError = true; |
michael@0 | 87 | String resultString = failString; |
michael@0 | 88 | if (test.mResult || test.mTodo) { |
michael@0 | 89 | isError = false; |
michael@0 | 90 | } |
michael@0 | 91 | if (test.mResult) |
michael@0 | 92 | { |
michael@0 | 93 | resultString = passString; |
michael@0 | 94 | } |
michael@0 | 95 | String diag = test.mName; |
michael@0 | 96 | if (test.mDiag != null) diag += " - " + test.mDiag; |
michael@0 | 97 | |
michael@0 | 98 | String message = Integer.toString(mLineNumber++) + " INFO " + resultString + " | " + mLogTestName + " | " + diag; |
michael@0 | 99 | dumpLog(message); |
michael@0 | 100 | |
michael@0 | 101 | if (test.mInfo) { |
michael@0 | 102 | // do not count TEST-INFO messages |
michael@0 | 103 | } else if (test.mTodo) { |
michael@0 | 104 | mTodo++; |
michael@0 | 105 | } else if (isError) { |
michael@0 | 106 | mFailed++; |
michael@0 | 107 | } else { |
michael@0 | 108 | mPassed++; |
michael@0 | 109 | } |
michael@0 | 110 | if (isError) { |
michael@0 | 111 | junit.framework.Assert.fail(message); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | public void endTest() { |
michael@0 | 116 | String message; |
michael@0 | 117 | |
michael@0 | 118 | if (mLogTestName != "") { |
michael@0 | 119 | long diff = SystemClock.uptimeMillis() - mStartTime; |
michael@0 | 120 | message = Integer.toString(mLineNumber++) + " INFO TEST-END | " + mLogTestName; |
michael@0 | 121 | message += " | finished in " + diff + "ms"; |
michael@0 | 122 | dumpLog(message); |
michael@0 | 123 | mLogTestName = ""; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | message = Integer.toString(mLineNumber++) + " INFO TEST-START | Shutdown"; |
michael@0 | 127 | dumpLog(message); |
michael@0 | 128 | message = Integer.toString(mLineNumber++) + " INFO Passed: " + Integer.toString(mPassed); |
michael@0 | 129 | dumpLog(message); |
michael@0 | 130 | message = Integer.toString(mLineNumber++) + " INFO Failed: " + Integer.toString(mFailed); |
michael@0 | 131 | dumpLog(message); |
michael@0 | 132 | message = Integer.toString(mLineNumber++) + " INFO Todo: " + Integer.toString(mTodo); |
michael@0 | 133 | dumpLog(message); |
michael@0 | 134 | message = Integer.toString(mLineNumber++) + " INFO SimpleTest FINISHED"; |
michael@0 | 135 | dumpLog(message); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | public void ok(boolean condition, String name, String diag) { |
michael@0 | 139 | testInfo test = new testInfo(condition, name, diag, false, false); |
michael@0 | 140 | _logMochitestResult(test, "TEST-PASS", "TEST-UNEXPECTED-FAIL"); |
michael@0 | 141 | mTestList.add(test); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | public void is(Object actual, Object expected, String name) { |
michael@0 | 145 | boolean pass = checkObjectsEqual(actual, expected); |
michael@0 | 146 | ok(pass, name, getEqualString(actual, expected, pass)); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | public void isnot(Object actual, Object notExpected, String name) { |
michael@0 | 150 | boolean pass = checkObjectsNotEqual(actual, notExpected); |
michael@0 | 151 | ok(pass, name, getNotEqualString(actual, notExpected, pass)); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | public void ispixel(int actual, int r, int g, int b, String name) { |
michael@0 | 155 | int aAlpha = ((actual >> 24) & 0xFF); |
michael@0 | 156 | int aR = ((actual >> 16) & 0xFF); |
michael@0 | 157 | int aG = ((actual >> 8) & 0xFF); |
michael@0 | 158 | int aB = (actual & 0xFF); |
michael@0 | 159 | boolean pass = checkPixel(actual, r, g, b); |
michael@0 | 160 | ok(pass, name, "Color rgba(" + aR + "," + aG + "," + aB + "," + aAlpha + ")" + (pass ? " " : " not") + " close enough to expected rgb(" + r + "," + g + "," + b + ")"); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | public void isnotpixel(int actual, int r, int g, int b, String name) { |
michael@0 | 164 | int aAlpha = ((actual >> 24) & 0xFF); |
michael@0 | 165 | int aR = ((actual >> 16) & 0xFF); |
michael@0 | 166 | int aG = ((actual >> 8) & 0xFF); |
michael@0 | 167 | int aB = (actual & 0xFF); |
michael@0 | 168 | boolean pass = checkPixel(actual, r, g, b); |
michael@0 | 169 | ok(!pass, name, "Color rgba(" + aR + "," + aG + "," + aB + "," + aAlpha + ")" + (!pass ? " is" : " is not") + " different enough from rgb(" + r + "," + g + "," + b + ")"); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | private boolean checkPixel(int actual, int r, int g, int b) { |
michael@0 | 173 | // When we read GL pixels the GPU has already processed them and they |
michael@0 | 174 | // are usually off by a little bit. For example a CSS-color pixel of color #64FFF5 |
michael@0 | 175 | // was turned into #63FFF7 when it came out of glReadPixels. So in order to compare |
michael@0 | 176 | // against the expected value, we use a little fuzz factor. For the alpha we just |
michael@0 | 177 | // make sure it is always 0xFF. There is also bug 691354 which crops up every so |
michael@0 | 178 | // often just to make our lives difficult. However the individual color components |
michael@0 | 179 | // should never be off by more than 8. |
michael@0 | 180 | int aAlpha = ((actual >> 24) & 0xFF); |
michael@0 | 181 | int aR = ((actual >> 16) & 0xFF); |
michael@0 | 182 | int aG = ((actual >> 8) & 0xFF); |
michael@0 | 183 | int aB = (actual & 0xFF); |
michael@0 | 184 | boolean pass = (aAlpha == 0xFF) /* alpha */ |
michael@0 | 185 | && (Math.abs(aR - r) <= 8) /* red */ |
michael@0 | 186 | && (Math.abs(aG - g) <= 8) /* green */ |
michael@0 | 187 | && (Math.abs(aB - b) <= 8); /* blue */ |
michael@0 | 188 | if (pass) { |
michael@0 | 189 | return true; |
michael@0 | 190 | } else { |
michael@0 | 191 | return false; |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | public void todo(boolean condition, String name, String diag) { |
michael@0 | 196 | testInfo test = new testInfo(condition, name, diag, true, false); |
michael@0 | 197 | _logMochitestResult(test, "TEST-UNEXPECTED-PASS", "TEST-KNOWN-FAIL"); |
michael@0 | 198 | mTestList.add(test); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | public void todo_is(Object actual, Object expected, String name) { |
michael@0 | 202 | boolean pass = checkObjectsEqual(actual, expected); |
michael@0 | 203 | todo(pass, name, getEqualString(actual, expected, pass)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | public void todo_isnot(Object actual, Object notExpected, String name) { |
michael@0 | 207 | boolean pass = checkObjectsNotEqual(actual, notExpected); |
michael@0 | 208 | todo(pass, name, getNotEqualString(actual, notExpected, pass)); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | private boolean checkObjectsEqual(Object a, Object b) { |
michael@0 | 212 | if (a == null || b == null) { |
michael@0 | 213 | if (a == null && b == null) { |
michael@0 | 214 | return true; |
michael@0 | 215 | } |
michael@0 | 216 | return false; |
michael@0 | 217 | } else { |
michael@0 | 218 | return a.equals(b); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | private String getEqualString(Object a, Object b, boolean pass) { |
michael@0 | 223 | if (pass) { |
michael@0 | 224 | return a + " should equal " + b; |
michael@0 | 225 | } |
michael@0 | 226 | return "got " + a + ", expected " + b; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | private boolean checkObjectsNotEqual(Object a, Object b) { |
michael@0 | 230 | if (a == null || b == null) { |
michael@0 | 231 | if ((a == null && b != null) || (a != null && b == null)) { |
michael@0 | 232 | return true; |
michael@0 | 233 | } else { |
michael@0 | 234 | return false; |
michael@0 | 235 | } |
michael@0 | 236 | } else { |
michael@0 | 237 | return !a.equals(b); |
michael@0 | 238 | } |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | private String getNotEqualString(Object a, Object b, boolean pass) { |
michael@0 | 242 | if(pass) { |
michael@0 | 243 | return a + " should not equal " + b; |
michael@0 | 244 | } |
michael@0 | 245 | return "didn't expect " + a + ", but got it"; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | public void info(String name, String message) { |
michael@0 | 249 | testInfo test = new testInfo(true, name, message, false, true); |
michael@0 | 250 | _logMochitestResult(test, "TEST-INFO", "INFO FAILED?"); |
michael@0 | 251 | } |
michael@0 | 252 | } |