michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsISupports.h" michael@0: #include "nsRegressionTester.h" michael@0: michael@0: #include "nsXPIDLString.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsIWindowWatcher.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsIURI.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsIContentViewerFile.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsStyleStruct.h" michael@0: #include "nsIFrameUtil.h" michael@0: #include "nsLayoutCID.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIFile.h" michael@0: #include "nsViewManager.h" michael@0: #include "nsView.h" michael@0: michael@0: michael@0: michael@0: static NS_DEFINE_CID(kFrameUtilCID, NS_FRAME_UTIL_CID); michael@0: michael@0: michael@0: nsRegressionTester::nsRegressionTester() michael@0: { michael@0: } michael@0: michael@0: nsRegressionTester::~nsRegressionTester() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsRegressionTester, nsILayoutRegressionTester) michael@0: michael@0: NS_IMETHODIMP michael@0: nsRegressionTester::DumpFrameModel(nsIDOMWindow *aWindowToDump, michael@0: nsIFile *aDestFile, michael@0: uint32_t aFlagsMask, int32_t *aResult) michael@0: { michael@0: NS_ENSURE_ARG(aWindowToDump); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: *aResult = DUMP_RESULT_ERROR; michael@0: michael@0: #ifndef DEBUG michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: #else michael@0: nsresult rv = NS_ERROR_NOT_AVAILABLE; michael@0: uint32_t busyFlags; michael@0: bool stillLoading; michael@0: michael@0: nsCOMPtr docShell; michael@0: rv = GetDocShellFromWindow(aWindowToDump, getter_AddRefs(docShell)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: // find out if the document is loaded michael@0: docShell->GetBusyFlags(&busyFlags); michael@0: stillLoading = busyFlags & (nsIDocShell::BUSY_FLAGS_BUSY | michael@0: nsIDocShell::BUSY_FLAGS_PAGE_LOADING); michael@0: if (stillLoading) michael@0: { michael@0: *aResult = DUMP_RESULT_LOADING; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsCOMPtr presShell = docShell->GetPresShell(); michael@0: michael@0: nsIFrame* root = presShell->GetRootFrame(); michael@0: michael@0: FILE* fp = stdout; michael@0: if (aDestFile) michael@0: { michael@0: rv = aDestFile->OpenANSIFileDesc("w", &fp); michael@0: if (NS_FAILED(rv)) return rv; michael@0: } michael@0: if (aFlagsMask & DUMP_FLAGS_MASK_PRINT_MODE) { michael@0: nsCOMPtr viewer; michael@0: docShell->GetContentViewer(getter_AddRefs(viewer)); michael@0: if (viewer){ michael@0: nsCOMPtr viewerFile = do_QueryInterface(viewer); michael@0: if (viewerFile) { michael@0: viewerFile->Print(true, fp, nullptr); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: root->DumpRegressionData(presShell->GetPresContext(), fp, 0); michael@0: } michael@0: if (fp != stdout) michael@0: fclose(fp); michael@0: *aResult = DUMP_RESULT_COMPLETED; michael@0: return NS_OK; michael@0: #endif michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsRegressionTester::CompareFrameModels(nsIFile *aBaseFile, nsIFile *aVerFile, michael@0: uint32_t aFlags, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG(aBaseFile); michael@0: NS_ENSURE_ARG(aVerFile); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: michael@0: *aResult = false; michael@0: michael@0: nsresult rv; michael@0: FILE* baseFile; michael@0: rv = aBaseFile->OpenANSIFileDesc("r", &baseFile); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: FILE* verFile; michael@0: rv = aVerFile->OpenANSIFileDesc("r", &verFile); michael@0: if (NS_FAILED(rv)) { michael@0: fclose(baseFile); michael@0: return rv; michael@0: } michael@0: michael@0: nsCOMPtr frameUtil = do_CreateInstance(kFrameUtilCID, &rv); michael@0: if (NS_SUCCEEDED(rv)) michael@0: { michael@0: int32_t outputLevel = (aFlags == COMPARE_FLAGS_VERBOSE) ? 0 : 1; michael@0: rv = frameUtil->CompareRegressionData(baseFile, verFile, outputLevel); michael@0: // CompareRegressionData closes |baseFile| and |verFile|. michael@0: } else { michael@0: fclose(verFile); michael@0: fclose(baseFile); michael@0: } michael@0: michael@0: *aResult = NS_FAILED(rv); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsRegressionTester::GetDocShellFromWindow(nsIDOMWindow* inWindow, nsIDocShell** outShell) michael@0: { michael@0: nsCOMPtr window(do_QueryInterface(inWindow)); michael@0: if (!window) return NS_ERROR_FAILURE; michael@0: michael@0: *outShell = window->GetDocShell(); michael@0: NS_IF_ADDREF(*outShell); michael@0: michael@0: return NS_OK; michael@0: }