layout/tools/layout-debug/ui/content/layoutdebug.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 var gBrowser;
michael@0 6 var gProgressListener;
michael@0 7 var gDebugger;
michael@0 8 var gRTestIndexList;
michael@0 9 var gRTestURLList = null;
michael@0 10
michael@0 11 const nsILayoutDebuggingTools = Components.interfaces.nsILayoutDebuggingTools;
michael@0 12 const nsIDocShell = Components.interfaces.nsIDocShell;
michael@0 13 const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
michael@0 14
michael@0 15 const NS_LAYOUT_DEBUGGINGTOOLS_CONTRACTID = "@mozilla.org/layout-debug/layout-debuggingtools;1";
michael@0 16
michael@0 17
michael@0 18 function nsLDBBrowserContentListener()
michael@0 19 {
michael@0 20 this.init();
michael@0 21 }
michael@0 22
michael@0 23 nsLDBBrowserContentListener.prototype = {
michael@0 24
michael@0 25 init : function()
michael@0 26 {
michael@0 27 this.mStatusText = document.getElementById("status-text");
michael@0 28 this.mURLBar = document.getElementById("urlbar");
michael@0 29 this.mForwardButton = document.getElementById("forward-button");
michael@0 30 this.mBackButton = document.getElementById("back-button");
michael@0 31 this.mStopButton = document.getElementById("stop-button");
michael@0 32 },
michael@0 33
michael@0 34 QueryInterface : function(aIID)
michael@0 35 {
michael@0 36 if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
michael@0 37 aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
michael@0 38 aIID.equals(Components.interfaces.nsISupports))
michael@0 39 return this;
michael@0 40 throw Components.results.NS_NOINTERFACE;
michael@0 41 },
michael@0 42
michael@0 43 // nsIWebProgressListener implementation
michael@0 44 onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
michael@0 45 {
michael@0 46 if (!(aStateFlags & nsIWebProgressListener.STATE_IS_NETWORK) ||
michael@0 47 aWebProgress != gBrowser.webProgress)
michael@0 48 return;
michael@0 49
michael@0 50 if (aStateFlags & nsIWebProgressListener.STATE_START) {
michael@0 51 this.setButtonEnabled(this.mStopButton, true);
michael@0 52 this.setButtonEnabled(this.mForwardButton, gBrowser.canGoForward);
michael@0 53 this.setButtonEnabled(this.mBackButton, gBrowser.canGoBack);
michael@0 54 this.mStatusText.value = "loading...";
michael@0 55 this.mLoading = true;
michael@0 56
michael@0 57 } else if (aStateFlags & nsIWebProgressListener.STATE_STOP) {
michael@0 58 this.setButtonEnabled(this.mStopButton, false);
michael@0 59 this.mStatusText.value = this.mURLBar.value + " loaded";
michael@0 60
michael@0 61 if (gRTestURLList && this.mLoading) {
michael@0 62 // Let other things happen in the first 20ms, since this
michael@0 63 // doesn't really seem to be when the page is done loading.
michael@0 64 setTimeout("gRTestURLList.doneURL()", 20);
michael@0 65 }
michael@0 66 this.mLoading = false;
michael@0 67 }
michael@0 68 },
michael@0 69
michael@0 70 onProgressChange : function(aWebProgress, aRequest,
michael@0 71 aCurSelfProgress, aMaxSelfProgress,
michael@0 72 aCurTotalProgress, aMaxTotalProgress)
michael@0 73 {
michael@0 74 },
michael@0 75
michael@0 76 onLocationChange : function(aWebProgress, aRequest, aLocation, aFlags)
michael@0 77 {
michael@0 78 this.mURLBar.value = aLocation.spec;
michael@0 79 this.setButtonEnabled(this.mForwardButton, gBrowser.canGoForward);
michael@0 80 this.setButtonEnabled(this.mBackButton, gBrowser.canGoBack);
michael@0 81 },
michael@0 82
michael@0 83 onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
michael@0 84 {
michael@0 85 this.mStatusText.value = aMessage;
michael@0 86 },
michael@0 87
michael@0 88 onSecurityChange : function(aWebProgress, aRequest, aState)
michael@0 89 {
michael@0 90 },
michael@0 91
michael@0 92 // non-interface methods
michael@0 93 setButtonEnabled : function(aButtonElement, aEnabled)
michael@0 94 {
michael@0 95 if (aEnabled)
michael@0 96 aButtonElement.removeAttribute("disabled");
michael@0 97 else
michael@0 98 aButtonElement.setAttribute("disabled", "true");
michael@0 99 },
michael@0 100
michael@0 101 mStatusText : null,
michael@0 102 mURLBar : null,
michael@0 103 mForwardButton : null,
michael@0 104 mBackButton : null,
michael@0 105 mStopButton : null,
michael@0 106
michael@0 107 mLoading : false
michael@0 108
michael@0 109 }
michael@0 110
michael@0 111 function OnLDBLoad()
michael@0 112 {
michael@0 113 gBrowser = document.getElementById("browser");
michael@0 114
michael@0 115 gProgressListener = new nsLDBBrowserContentListener();
michael@0 116 gBrowser.addProgressListener(gProgressListener);
michael@0 117
michael@0 118 gDebugger = Components.classes[NS_LAYOUT_DEBUGGINGTOOLS_CONTRACTID].
michael@0 119 createInstance(nsILayoutDebuggingTools);
michael@0 120
michael@0 121 if (window.arguments && window.arguments[0])
michael@0 122 gBrowser.loadURI(window.arguments[0]);
michael@0 123 else
michael@0 124 gBrowser.goHome();
michael@0 125
michael@0 126 gDebugger.init(gBrowser.contentWindow);
michael@0 127
michael@0 128 checkPersistentMenus();
michael@0 129 gRTestIndexList = new RTestIndexList();
michael@0 130 }
michael@0 131
michael@0 132 function checkPersistentMenu(item)
michael@0 133 {
michael@0 134 var menuitem = document.getElementById("menu_" + item);
michael@0 135 menuitem.setAttribute("checked", gDebugger[item]);
michael@0 136 }
michael@0 137
michael@0 138 function checkPersistentMenus()
michael@0 139 {
michael@0 140 // Restore the toggles that are stored in prefs.
michael@0 141 checkPersistentMenu("paintFlashing");
michael@0 142 checkPersistentMenu("paintDumping");
michael@0 143 checkPersistentMenu("invalidateDumping");
michael@0 144 checkPersistentMenu("eventDumping");
michael@0 145 checkPersistentMenu("motionEventDumping");
michael@0 146 checkPersistentMenu("crossingEventDumping");
michael@0 147 checkPersistentMenu("reflowCounts");
michael@0 148 }
michael@0 149
michael@0 150
michael@0 151 function OnLDBUnload()
michael@0 152 {
michael@0 153 gBrowser.removeProgressListener(gProgressListener);
michael@0 154 }
michael@0 155
michael@0 156 function toggle(menuitem)
michael@0 157 {
michael@0 158 // trim the initial "menu_"
michael@0 159 var feature = menuitem.id.substring(5);
michael@0 160 gDebugger[feature] = menuitem.getAttribute("checked") == "true";
michael@0 161 }
michael@0 162
michael@0 163 function openFile()
michael@0 164 {
michael@0 165 var nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 166 var fp = Components.classes["@mozilla.org/filepicker;1"]
michael@0 167 .createInstance(nsIFilePicker);
michael@0 168 fp.init(window, "Select a File", nsIFilePicker.modeOpen);
michael@0 169 fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterAll);
michael@0 170 if (fp.show() == nsIFilePicker.returnOK && fp.fileURL.spec &&
michael@0 171 fp.fileURL.spec.length > 0) {
michael@0 172 gBrowser.loadURI(fp.fileURL.spec);
michael@0 173 }
michael@0 174 }
michael@0 175 const LDB_RDFNS = "http://mozilla.org/newlayout/LDB-rdf#";
michael@0 176 const NC_RDFNS = "http://home.netscape.com/NC-rdf#";
michael@0 177
michael@0 178 function RTestIndexList() {
michael@0 179 this.init();
michael@0 180 }
michael@0 181
michael@0 182 RTestIndexList.prototype = {
michael@0 183
michael@0 184 init : function()
michael@0 185 {
michael@0 186 const nsIPrefService = Components.interfaces.nsIPrefService;
michael@0 187 const PREF_SERVICE_CONTRACTID = "@mozilla.org/preferences-service;1";
michael@0 188 const PREF_BRANCH_NAME = "layout_debugger.rtest_url.";
michael@0 189 const nsIRDFService = Components.interfaces.nsIRDFService;
michael@0 190 const RDF_SERVICE_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
michael@0 191 const nsIRDFDataSource = Components.interfaces.nsIRDFDataSource;
michael@0 192 const RDF_DATASOURCE_CONTRACTID =
michael@0 193 "@mozilla.org/rdf/datasource;1?name=in-memory-datasource";
michael@0 194
michael@0 195 this.mPrefService = Components.classes[PREF_SERVICE_CONTRACTID].
michael@0 196 getService(nsIPrefService);
michael@0 197 this.mPrefBranch = this.mPrefService.getBranch(PREF_BRANCH_NAME);
michael@0 198
michael@0 199 this.mRDFService = Components.classes[RDF_SERVICE_CONTRACTID].
michael@0 200 getService(nsIRDFService);
michael@0 201 this.mDataSource = Components.classes[RDF_DATASOURCE_CONTRACTID].
michael@0 202 createInstance(nsIRDFDataSource);
michael@0 203
michael@0 204 this.mLDB_Root = this.mRDFService.GetResource(LDB_RDFNS + "Root");
michael@0 205 this.mNC_Name = this.mRDFService.GetResource(NC_RDFNS + "name");
michael@0 206 this.mNC_Child = this.mRDFService.GetResource(NC_RDFNS + "child");
michael@0 207
michael@0 208 this.load();
michael@0 209
michael@0 210 document.getElementById("menu_RTest_baseline").database.
michael@0 211 AddDataSource(this.mDataSource);
michael@0 212 document.getElementById("menu_RTest_verify").database.
michael@0 213 AddDataSource(this.mDataSource);
michael@0 214 document.getElementById("menu_RTest_remove").database.
michael@0 215 AddDataSource(this.mDataSource);
michael@0 216 },
michael@0 217
michael@0 218 save : function()
michael@0 219 {
michael@0 220 this.mPrefBranch.deleteBranch("");
michael@0 221
michael@0 222 const nsIRDFLiteral = Components.interfaces.nsIRDFLiteral;
michael@0 223 const nsIRDFResource = Components.interfaces.nsIRDFResource;
michael@0 224 var etor = this.mDataSource.GetTargets(this.mLDB_Root,
michael@0 225 this.mNC_Child, true);
michael@0 226 var i = 0;
michael@0 227 while (etor.hasMoreElements()) {
michael@0 228 var resource = etor.getNext().QueryInterface(nsIRDFResource);
michael@0 229 var literal = this.mDataSource.GetTarget(resource, this.mNC_Name, true);
michael@0 230 literal = literal.QueryInterface(nsIRDFLiteral);
michael@0 231 this.mPrefBranch.setCharPref(i.toString(), literal.Value);
michael@0 232 ++i;
michael@0 233 }
michael@0 234
michael@0 235 this.mPrefService.savePrefFile(null);
michael@0 236 },
michael@0 237
michael@0 238 load : function()
michael@0 239 {
michael@0 240 var prefList = this.mPrefBranch.getChildList("");
michael@0 241
michael@0 242 var i = 0;
michael@0 243 for (var pref in prefList) {
michael@0 244 var file = this.mPrefBranch.getCharPref(pref);
michael@0 245 var resource = this.mRDFService.GetResource(file);
michael@0 246 var literal = this.mRDFService.GetLiteral(file);
michael@0 247 this.mDataSource.Assert(this.mLDB_Root, this.mNC_Child, resource, true);
michael@0 248 this.mDataSource.Assert(resource, this.mNC_Name, literal, true);
michael@0 249 ++i;
michael@0 250 }
michael@0 251
michael@0 252 },
michael@0 253
michael@0 254 /* Add a new list of regression tests to the menus. */
michael@0 255 add : function()
michael@0 256 {
michael@0 257 const nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 258 const NS_FILEPICKER_CONTRACTID = "@mozilla.org/filepicker;1";
michael@0 259
michael@0 260 var fp = Components.classes[NS_FILEPICKER_CONTRACTID].
michael@0 261 createInstance(nsIFilePicker);
michael@0 262
michael@0 263 // XXX l10n (but this is just for 5 developers, so no problem)
michael@0 264 fp.init(window, "New Regression Test List", nsIFilePicker.modeOpen);
michael@0 265 fp.appendFilters(nsIFilePicker.filterAll);
michael@0 266 fp.defaultString = "rtest.lst";
michael@0 267 if (fp.show() != nsIFilePicker.returnOK)
michael@0 268 return;
michael@0 269
michael@0 270 var file = fp.file.persistentDescriptor;
michael@0 271 var resource = this.mRDFService.GetResource(file);
michael@0 272 var literal = this.mRDFService.GetLiteral(file);
michael@0 273 this.mDataSource.Assert(this.mLDB_Root, this.mNC_Child, resource, true);
michael@0 274 this.mDataSource.Assert(resource, this.mNC_Name, literal, true);
michael@0 275
michael@0 276 this.save();
michael@0 277
michael@0 278 },
michael@0 279
michael@0 280 remove : function(file)
michael@0 281 {
michael@0 282 var resource = this.mRDFService.GetResource(file);
michael@0 283 var literal = this.mRDFService.GetLiteral(file);
michael@0 284 this.mDataSource.Unassert(this.mLDB_Root, this.mNC_Child, resource);
michael@0 285 this.mDataSource.Unassert(resource, this.mNC_Name, literal);
michael@0 286
michael@0 287 this.save();
michael@0 288 },
michael@0 289
michael@0 290 mPrefBranch : null,
michael@0 291 mPrefService : null,
michael@0 292 mRDFService : null,
michael@0 293 mDataSource : null,
michael@0 294 mLDB_Root : null,
michael@0 295 mNC_Child : null,
michael@0 296 mNC_Name : null
michael@0 297 }
michael@0 298
michael@0 299 const nsIFileInputStream = Components.interfaces.nsIFileInputStream;
michael@0 300 const nsILineInputStream = Components.interfaces.nsILineInputStream;
michael@0 301 const nsIFile = Components.interfaces.nsIFile;
michael@0 302 const nsILocalFile = Components.interfaces.nsILocalFile;
michael@0 303 const nsIFileURL = Components.interfaces.nsIFileURL;
michael@0 304 const nsIIOService = Components.interfaces.nsIIOService;
michael@0 305 const nsILayoutRegressionTester = Components.interfaces.nsILayoutRegressionTester;
michael@0 306
michael@0 307 const NS_LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
michael@0 308 const IO_SERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
michael@0 309 const NS_LOCALFILEINPUTSTREAM_CONTRACTID =
michael@0 310 "@mozilla.org/network/file-input-stream;1";
michael@0 311
michael@0 312
michael@0 313 function RunRTest(aFilename, aIsBaseline, aIsPrinting)
michael@0 314 {
michael@0 315 if (gRTestURLList) {
michael@0 316 // XXX Does alert work?
michael@0 317 alert("Already running regression test.\n");
michael@0 318 return;
michael@0 319 }
michael@0 320 dump("Running " + (aIsBaseline?"baseline":"verify") +
michael@0 321 (aIsPrinting?" PrintMode":"") + " test for " + aFilename + ".\n");
michael@0 322
michael@0 323 var listFile = Components.classes[NS_LOCAL_FILE_CONTRACTID].
michael@0 324 createInstance(nsILocalFile);
michael@0 325 listFile.persistentDescriptor = aFilename;
michael@0 326 gRTestURLList = new RTestURLList(listFile, aIsBaseline, aIsPrinting);
michael@0 327 gRTestURLList.startURL();
michael@0 328 }
michael@0 329
michael@0 330 function RTestURLList(aLocalFile, aIsBaseline, aIsPrinting) {
michael@0 331 this.init(aLocalFile, aIsBaseline, aIsPrinting);
michael@0 332 }
michael@0 333
michael@0 334 RTestURLList.prototype = {
michael@0 335 init : function(aLocalFile, aIsBaseline, aIsPrinting)
michael@0 336 {
michael@0 337 this.mIsBaseline = aIsBaseline;
michael@0 338 this.mIsPrinting = aIsPrinting;
michael@0 339 this.mURLs = new Array();
michael@0 340 this.readFileList(aLocalFile);
michael@0 341 this.mRegressionTester =
michael@0 342 Components.classes["@mozilla.org/layout-debug/regressiontester;1"].
michael@0 343 createInstance(nsILayoutRegressionTester)
michael@0 344 },
michael@0 345
michael@0 346 readFileList : function(aLocalFile)
michael@0 347 {
michael@0 348 var ios = Components.classes[IO_SERVICE_CONTRACTID]
michael@0 349 .getService(nsIIOService);
michael@0 350 var dirURL = ios.newFileURI(aLocalFile.parent);
michael@0 351
michael@0 352 var fis = Components.classes[NS_LOCALFILEINPUTSTREAM_CONTRACTID].
michael@0 353 createInstance(nsIFileInputStream);
michael@0 354 fis.init(aLocalFile, -1, -1, false);
michael@0 355 var lis = fis.QueryInterface(nsILineInputStream);
michael@0 356
michael@0 357 var line = {value:null};
michael@0 358 do {
michael@0 359 var more = lis.readLine(line);
michael@0 360 var str = line.value;
michael@0 361 str = /^[^#]*/.exec(str); // strip everything after "#"
michael@0 362 str = /\S*/.exec(str); // take the first chunk of non-whitespace
michael@0 363 if (!str || str == "")
michael@0 364 continue;
michael@0 365
michael@0 366 var item = dirURL.resolve(str);
michael@0 367 if (item.match(/\/rtest.lst$/)) {
michael@0 368 var itemurl = ios.newURI(item, null, null);
michael@0 369 itemurl = itemurl.QueryInterface(nsIFileURL);
michael@0 370 this.readFileList(itemurl.file);
michael@0 371 } else {
michael@0 372 this.mURLs.push( {url:item, dir:aLocalFile.parent, relurl:str} );
michael@0 373 }
michael@0 374 } while (more);
michael@0 375 },
michael@0 376
michael@0 377 doneURL : function()
michael@0 378 {
michael@0 379 var basename =
michael@0 380 String(this.mCurrentURL.relurl).replace(/[:=&.\/?]/g, "_") + ".rgd";
michael@0 381
michael@0 382 var data = this.mCurrentURL.dir.clone();
michael@0 383 data.append( this.mIsBaseline ? "baseline" : "verify");
michael@0 384 if (!data.exists())
michael@0 385 data.create(nsIFile.DIRECTORY_TYPE, 0777)
michael@0 386 data.append(basename);
michael@0 387
michael@0 388 dump("Writing regression data to " +
michael@0 389 data.QueryInterface(nsILocalFile).persistentDescriptor + "\n");
michael@0 390 if (this.mIsPrinting) {
michael@0 391 this.mRegressionTester.dumpFrameModel(gBrowser.contentWindow, data,
michael@0 392 nsILayoutRegressionTester.DUMP_FLAGS_MASK_PRINT_MODE);
michael@0 393 }
michael@0 394 else {
michael@0 395 this.mRegressionTester.dumpFrameModel(gBrowser.contentWindow, data, 0);
michael@0 396 }
michael@0 397
michael@0 398
michael@0 399
michael@0 400 if (!this.mIsBaseline) {
michael@0 401 var base_data = this.mCurrentURL.dir.clone();
michael@0 402 base_data.append("baseline");
michael@0 403 base_data.append(basename);
michael@0 404 dump("Comparing to regression data from " +
michael@0 405 base_data.QueryInterface(nsILocalFile).persistentDescriptor + "\n");
michael@0 406 var filesDiffer =
michael@0 407 this.mRegressionTester.compareFrameModels(base_data, data,
michael@0 408 nsILayoutRegressionTester.COMPARE_FLAGS_BRIEF)
michael@0 409 dump("Comparison for " + this.mCurrentURL.url + " " +
michael@0 410 (filesDiffer ? "failed" : "passed") + ".\n");
michael@0 411 }
michael@0 412
michael@0 413 this.mCurrentURL = null;
michael@0 414
michael@0 415 this.startURL();
michael@0 416 },
michael@0 417
michael@0 418 startURL : function()
michael@0 419 {
michael@0 420 this.mCurrentURL = this.mURLs.shift();
michael@0 421 if (!this.mCurrentURL) {
michael@0 422 gRTestURLList = null;
michael@0 423 return;
michael@0 424 }
michael@0 425
michael@0 426 gBrowser.loadURI(this.mCurrentURL.url);
michael@0 427 },
michael@0 428
michael@0 429 mURLs : null,
michael@0 430 mCurrentURL : null, // url (string), dir (nsIFileURL), relurl (string)
michael@0 431 mIsBaseline : null,
michael@0 432 mRegressionTester : null,
michael@0 433 mIsPrinting : null
michael@0 434 }

mercurial