Back to index

lightning-sunbird  0.9+nobinonly
nsRegressionTester.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsISupports.h"
00039 #include "nsRegressionTester.h"
00040 
00041 #include "nsXPIDLString.h"
00042 #include "nsReadableUtils.h"
00043 #include "nsIWindowWatcher.h"
00044 #include "nsVoidArray.h"
00045 #include "prmem.h"
00046 #include "nsIDocShellTreeItem.h"
00047 #include "nsIDocShellTreeNode.h"
00048 #include "nsIDOMWindowInternal.h"
00049 #include "nsIPresShell.h"
00050 #include "nsIDocument.h"
00051 #include "nsIDOMDocument.h"
00052 #include "nsIURI.h"
00053 #include "nsIDOMHTMLDocument.h"
00054 #include "nsISimpleEnumerator.h"
00055 #include "nsIScriptGlobalObject.h"
00056 #include "nsIDocShell.h"
00057 #include "nsIFrameDebug.h"
00058 #include "nsIFrame.h"
00059 #include "nsStyleStruct.h"
00060 #include "nsIFrameUtil.h"
00061 #include "nsLayoutCID.h"
00062 #include "nsNetUtil.h"
00063 #include "nsILocalFile.h"
00064 #include "nsIPrefService.h"
00065 #include "nsIViewManager.h"
00066 #include "nsIView.h"
00067 
00068 
00069 
00070 static NS_DEFINE_CID(kFrameUtilCID, NS_FRAME_UTIL_CID);
00071 
00072 
00073 nsRegressionTester::nsRegressionTester()
00074 {
00075 }
00076 
00077 nsRegressionTester::~nsRegressionTester() 
00078 {
00079 }
00080 
00081 NS_IMPL_ISUPPORTS1(nsRegressionTester, nsILayoutRegressionTester)
00082 
00083 NS_IMETHODIMP
00084 nsRegressionTester::DumpFrameModel(nsIDOMWindow *aWindowToDump, nsILocalFile *aDestFile, PRUint32 aFlagsMask, PRInt32 *aResult) 
00085 {
00086   NS_ENSURE_ARG(aWindowToDump);
00087   NS_ENSURE_ARG_POINTER(aResult);
00088 
00089   nsresult    rv = NS_ERROR_NOT_AVAILABLE;
00090   PRUint32    busyFlags;
00091   PRBool      stillLoading;
00092 
00093   *aResult = DUMP_RESULT_ERROR;
00094   
00095   nsCOMPtr<nsIDocShell> docShell;
00096   rv = GetDocShellFromWindow(aWindowToDump, getter_AddRefs(docShell));
00097   if (NS_FAILED(rv)) return rv;
00098 
00099   // find out if the document is loaded
00100   docShell->GetBusyFlags(&busyFlags);
00101   stillLoading = busyFlags && (nsIDocShell::BUSY_FLAGS_BUSY | nsIDocShell::BUSY_FLAGS_PAGE_LOADING);
00102   if (stillLoading)
00103   {
00104     *aResult = DUMP_RESULT_LOADING;
00105     return NS_OK;
00106   }
00107   
00108   nsCOMPtr<nsIPresShell> presShell;
00109   docShell->GetPresShell(getter_AddRefs(presShell));
00110 
00111   nsIFrame* root = presShell->GetRootFrame();
00112 
00113   nsIFrameDebug*  fdbg;
00114   rv = CallQueryInterface(root, &fdbg);
00115   if (NS_FAILED(rv)) return rv;
00116 
00117   PRBool  dumpStyle = (aFlagsMask & DUMP_FLAGS_MASK_DUMP_STYLE) != 0;
00118 
00119   FILE* fp = stdout;
00120   if (aDestFile)
00121   {
00122     rv = aDestFile->OpenANSIFileDesc("w", &fp);
00123     if (NS_FAILED(rv)) return rv;
00124   }
00125   
00126   fdbg->DumpRegressionData(presShell->GetPresContext(), fp, 0, dumpStyle);
00127   if (fp != stdout)
00128     fclose(fp);
00129 
00130   *aResult = DUMP_RESULT_COMPLETED;
00131   return NS_OK;
00132 }
00133 
00134 NS_IMETHODIMP
00135 nsRegressionTester::CompareFrameModels(nsILocalFile *aBaseFile, nsILocalFile *aVerFile, PRUint32 aFlags, PRInt32 *aResult) 
00136 {
00137   NS_ENSURE_ARG(aBaseFile);
00138   NS_ENSURE_ARG(aVerFile);
00139   NS_ENSURE_ARG_POINTER(aResult);
00140   
00141   *aResult = NS_OK;
00142   
00143   nsresult rv;
00144   FILE* baseFile;
00145   rv = aBaseFile->OpenANSIFileDesc("r", &baseFile);
00146   if (NS_FAILED(rv)) return rv;
00147 
00148   FILE* verFile;
00149   rv = aVerFile->OpenANSIFileDesc("r", &verFile);
00150   if (NS_FAILED(rv)) {
00151     fclose(baseFile);
00152     return rv;
00153   }
00154 
00155   nsCOMPtr<nsIFrameUtil> frameUtil = do_CreateInstance(kFrameUtilCID, &rv);
00156   if (NS_SUCCEEDED(rv))
00157   {
00158     PRInt32 outputLevel = (aFlags == COMPARE_FLAGS_VERBOSE) ? 0 : 1;
00159     rv = frameUtil->CompareRegressionData(baseFile, verFile, outputLevel);
00160     // CompareRegressionData closes |baseFile| and |verFile|.
00161   } else {
00162     fclose(verFile);          
00163     fclose(baseFile);
00164   }
00165 
00166   *aResult = NS_FAILED(rv);
00167   return NS_OK;
00168 }
00169 
00170 nsresult
00171 nsRegressionTester::GetDocShellFromWindow(nsIDOMWindow* inWindow, nsIDocShell** outShell)
00172 {
00173   nsCOMPtr<nsIScriptGlobalObject> scriptObj(do_QueryInterface(inWindow));
00174   if (!scriptObj) return NS_ERROR_FAILURE;
00175 
00176   *outShell = scriptObj->GetDocShell();
00177   NS_IF_ADDREF(*outShell);
00178 
00179   return NS_OK;
00180 }