1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2000
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsISupports.h"
39 : #include "nsRegressionTester.h"
40 :
41 : #include "nsXPIDLString.h"
42 : #include "nsReadableUtils.h"
43 : #include "nsIWindowWatcher.h"
44 : #include "nsVoidArray.h"
45 : #include "prmem.h"
46 : #include "nsIDocShellTreeItem.h"
47 : #include "nsIDocShellTreeNode.h"
48 : #include "nsPIDOMWindow.h"
49 : #include "nsIPresShell.h"
50 : #include "nsIDocument.h"
51 : #include "nsIDOMDocument.h"
52 : #include "nsIURI.h"
53 : #include "nsIDOMHTMLDocument.h"
54 : #include "nsISimpleEnumerator.h"
55 : #include "nsIDocShell.h"
56 : #include "nsIContentViewer.h"
57 : #include "nsIContentViewerFile.h"
58 : #include "nsIFrame.h"
59 : #include "nsStyleStruct.h"
60 : #include "nsIFrameUtil.h"
61 : #include "nsLayoutCID.h"
62 : #include "nsNetUtil.h"
63 : #include "nsILocalFile.h"
64 : #include "nsIViewManager.h"
65 : #include "nsIView.h"
66 :
67 :
68 :
69 : static NS_DEFINE_CID(kFrameUtilCID, NS_FRAME_UTIL_CID);
70 :
71 :
72 0 : nsRegressionTester::nsRegressionTester()
73 : {
74 0 : }
75 :
76 0 : nsRegressionTester::~nsRegressionTester()
77 : {
78 0 : }
79 :
80 0 : NS_IMPL_ISUPPORTS1(nsRegressionTester, nsILayoutRegressionTester)
81 :
82 : NS_IMETHODIMP
83 0 : nsRegressionTester::DumpFrameModel(nsIDOMWindow *aWindowToDump,
84 : nsILocalFile *aDestFile,
85 : PRUint32 aFlagsMask, PRInt32 *aResult)
86 : {
87 0 : NS_ENSURE_ARG(aWindowToDump);
88 0 : NS_ENSURE_ARG_POINTER(aResult);
89 :
90 0 : *aResult = DUMP_RESULT_ERROR;
91 :
92 : #ifndef DEBUG
93 : return NS_ERROR_NOT_AVAILABLE;
94 : #else
95 0 : nsresult rv = NS_ERROR_NOT_AVAILABLE;
96 : PRUint32 busyFlags;
97 : bool stillLoading;
98 :
99 0 : nsCOMPtr<nsIDocShell> docShell;
100 0 : rv = GetDocShellFromWindow(aWindowToDump, getter_AddRefs(docShell));
101 0 : if (NS_FAILED(rv)) return rv;
102 :
103 : // find out if the document is loaded
104 0 : docShell->GetBusyFlags(&busyFlags);
105 : stillLoading = busyFlags & (nsIDocShell::BUSY_FLAGS_BUSY |
106 0 : nsIDocShell::BUSY_FLAGS_PAGE_LOADING);
107 0 : if (stillLoading)
108 : {
109 0 : *aResult = DUMP_RESULT_LOADING;
110 0 : return NS_OK;
111 : }
112 :
113 0 : nsCOMPtr<nsIPresShell> presShell;
114 0 : docShell->GetPresShell(getter_AddRefs(presShell));
115 :
116 0 : nsIFrame* root = presShell->GetRootFrame();
117 :
118 0 : FILE* fp = stdout;
119 0 : if (aDestFile)
120 : {
121 0 : rv = aDestFile->OpenANSIFileDesc("w", &fp);
122 0 : if (NS_FAILED(rv)) return rv;
123 : }
124 0 : if (aFlagsMask & DUMP_FLAGS_MASK_PRINT_MODE) {
125 0 : nsCOMPtr <nsIContentViewer> viewer;
126 0 : docShell->GetContentViewer(getter_AddRefs(viewer));
127 0 : if (viewer){
128 0 : nsCOMPtr<nsIContentViewerFile> viewerFile = do_QueryInterface(viewer);
129 0 : if (viewerFile) {
130 0 : viewerFile->Print(true, fp, nsnull);
131 : }
132 : }
133 : }
134 : else {
135 0 : root->DumpRegressionData(presShell->GetPresContext(), fp, 0);
136 : }
137 0 : if (fp != stdout)
138 0 : fclose(fp);
139 0 : *aResult = DUMP_RESULT_COMPLETED;
140 0 : return NS_OK;
141 : #endif
142 : }
143 :
144 : NS_IMETHODIMP
145 0 : nsRegressionTester::CompareFrameModels(nsILocalFile *aBaseFile, nsILocalFile *aVerFile, PRUint32 aFlags, PRInt32 *aResult)
146 : {
147 0 : NS_ENSURE_ARG(aBaseFile);
148 0 : NS_ENSURE_ARG(aVerFile);
149 0 : NS_ENSURE_ARG_POINTER(aResult);
150 :
151 0 : *aResult = NS_OK;
152 :
153 : nsresult rv;
154 : FILE* baseFile;
155 0 : rv = aBaseFile->OpenANSIFileDesc("r", &baseFile);
156 0 : if (NS_FAILED(rv)) return rv;
157 :
158 : FILE* verFile;
159 0 : rv = aVerFile->OpenANSIFileDesc("r", &verFile);
160 0 : if (NS_FAILED(rv)) {
161 0 : fclose(baseFile);
162 0 : return rv;
163 : }
164 :
165 0 : nsCOMPtr<nsIFrameUtil> frameUtil = do_CreateInstance(kFrameUtilCID, &rv);
166 0 : if (NS_SUCCEEDED(rv))
167 : {
168 0 : PRInt32 outputLevel = (aFlags == COMPARE_FLAGS_VERBOSE) ? 0 : 1;
169 0 : rv = frameUtil->CompareRegressionData(baseFile, verFile, outputLevel);
170 : // CompareRegressionData closes |baseFile| and |verFile|.
171 : } else {
172 0 : fclose(verFile);
173 0 : fclose(baseFile);
174 : }
175 :
176 0 : *aResult = NS_FAILED(rv);
177 0 : return NS_OK;
178 : }
179 :
180 : nsresult
181 0 : nsRegressionTester::GetDocShellFromWindow(nsIDOMWindow* inWindow, nsIDocShell** outShell)
182 : {
183 0 : nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(inWindow));
184 0 : if (!window) return NS_ERROR_FAILURE;
185 :
186 0 : *outShell = window->GetDocShell();
187 0 : NS_IF_ADDREF(*outShell);
188 :
189 0 : return NS_OK;
190 : }
|